#P8683. [蓝桥杯 2019 省 B] 后缀表达式

[蓝桥杯 2019 省 B] 后缀表达式

Description

Given NN plus signs, MM minus signs, and N+M+1N+M+1 integers A1,A2,,AN+M+1A_1,A_2,\cdots,A_{N+M+1}, Xiaoming wants to know which legal postfix expression formed by these NN plus signs, MM minus signs, and N+M+1N+M+1 integers has the maximum value.

Please output this maximum value.

For example, using 1 2 3 + -, the postfix expression 2 3 + 1 - has value 44, which is the maximum.

Input Format

The first line contains two integers NN and MM.

The second line contains N+M+1N+M+1 integers A1,A2,,AN+M+1A_1,A_2,\cdots,A_{N+M+1}.

Output Format

Output one integer, which is the answer.

1 1
1 2 3
4

Hint

For all test cases, 0N,M1050 \le N,M \le 10^5, 109Ai109-10^9 \le A_i \le 10^9.

Lanqiao Cup 2019 Provincial Contest Group B, Problem I.

Translated by ChatGPT 5