E. Fafa and Ancient Mathematics

Codeforces
IDCF935E
Time2000ms
Memory256MB
Difficulty
dfs and similardptrees
English · Original
Chinese · Translation
Formal · Original
Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as _Ahmes arithmetic expression_. An _Ahmes arithmetic expression_ can be defined as: * "_d_" is an Ahmes arithmetic expression, where _d_ is a one-digit positive integer; * "(_E_1 _op_ _E_2)" is an Ahmes arithmetic expression, where _E_1 and _E_2 are valid Ahmes arithmetic expressions (without spaces) and _op_ is either plus ( + ) or minus ( - ). For example _5_, _(1-1)_ and _((1+(2-3))-5)_ are valid Ahmes arithmetic expressions.On his trip to Egypt, Fafa found a piece of papyrus paper having one of these Ahmes arithmetic expressions written on it. Being very ancient, the papyrus piece was very worn out. As a result, all the operators were erased, keeping only the numbers and the brackets. Since Fafa loves mathematics, he decided to challenge himself with the following task: Given the number of plus and minus operators in the original expression, find out the maximum possible value for the expression on the papyrus paper after putting the plus and minus operators in the place of the original erased operators. ## Input The first line contains a string _E_ (1 ≤ |_E_| ≤ 104) — a valid Ahmes arithmetic expression. All operators are erased and replaced with '_?_'. The second line contains two space-separated integers _P_ and _M_ (0 ≤ _min_(_P_, _M_) ≤ 100) — the number of plus and minus operators, respectively. It is guaranteed that _P_ + _M_ =  the number of erased operators. ## Output Print one line containing the answer to the problem. [samples] ## Note * The first sample will be (1 + 1)  =  2. * The second sample will be (2 + (1 - 2))  =  1. * The third sample will be ((1 - (5 - 7)) + ((6 + 2) + 7))  =  18. * The fourth sample will be ((1 + (5 + 7)) - ((6 - 2) - 7))  =  16.
古埃及人以理解复杂的数学概念而闻名。古埃及数学家阿姆斯喜欢在纸莎草纸上书写一种他称为 _阿姆斯算术表达式_ 的算术表达式。 在一次埃及之旅中,法法发现了一张写有这种阿姆斯算术表达式的纸莎草纸。由于年代久远,纸张已严重磨损,所有运算符都被擦除,仅保留了数字和括号。由于法法热爱数学,他决定挑战自己完成以下任务: 给定原始表达式中加号和减号运算符的数量,求出在将加号和减号运算符填入被擦除位置后,纸莎草纸上表达式可能取得的最大值。 第一行包含一个字符串 #cf_span[E] #cf_span[(1 ≤ |E| ≤ 104)] —— 一个有效的阿姆斯算术表达式,所有运算符均被擦除并替换为 '_?_'。 第二行包含两个空格分隔的整数 #cf_span[P] 和 #cf_span[M] (#cf_span[0 ≤ min(P, M) ≤ 100]) —— 分别表示加号和减号运算符的数量。 保证 #cf_span[P + M = ] 被擦除的运算符数量。 请输出一行,包含该问题的答案。 ## Input 第一行包含一个字符串 #cf_span[E] #cf_span[(1 ≤ |E| ≤ 104)] —— 一个有效的阿姆斯算术表达式,所有运算符均被擦除并替换为 '_?_'。第二行包含两个空格分隔的整数 #cf_span[P] 和 #cf_span[M] (#cf_span[0 ≤ min(P, M) ≤ 100]) —— 分别表示加号和减号运算符的数量。保证 #cf_span[P + M = ] 被擦除的运算符数量。 ## Output 请输出一行,包含该问题的答案。 [samples] ## Note 第一个样例为 #cf_span[(1 + 1)  =  2]。第二个样例为 #cf_span[(2 + (1 - 2))  =  1]。第三个样例为 #cf_span[((1 - (5 - 7)) + ((6 + 2) + 7))  =  18]。第四个样例为 #cf_span[((1 + (5 + 7)) - ((6 - 2) - 7))  =  16]。
**Definitions** Let $ E $ be a string representing a valid Ahmes arithmetic expression with numbers and brackets, where all operators are replaced by placeholders `?`. Let $ P, M \in \mathbb{Z}_{\geq 0} $ be the number of plus and minus operators to be inserted, respectively, with $ P + M = \text{number of } ? \text{ in } E $. **Constraints** 1. $ 1 \leq |E| \leq 10^4 $ 2. $ 0 \leq \min(P, M) \leq 100 $ 3. $ P + M = $ number of `?` in $ E $ 4. The expression $ E $ is syntactically valid with numbers and balanced brackets. **Objective** Assign exactly $ P $ plus signs and $ M $ minus signs to the $ P + M $ placeholder positions in $ E $ such that the resulting arithmetic expression evaluates to the **maximum possible value**. Compute and output this maximum value.
Samples
Input #1
(1?1)
1 0
Output #1
2
Input #2
(2?(1?2))
1 1
Output #2
1
Input #3
((1?(5?7))?((6?2)?7))
3 2
Output #3
18
Input #4
((1?(5?7))?((6?2)?7))
2 3
Output #4
16
API Response (JSON)
{
  "problem": {
    "name": "E. Fafa and Ancient Mathematics",
    "description": {
      "content": "Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he cal",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF935E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he cal...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "古埃及人以理解复杂的数学概念而闻名。古埃及数学家阿姆斯喜欢在纸莎草纸上书写一种他称为 _阿姆斯算术表达式_ 的算术表达式。\n\n在一次埃及之旅中,法法发现了一张写有这种阿姆斯算术表达式的纸莎草纸。由于年代久远,纸张已严重磨损,所有运算符都被擦除,仅保留了数字和括号。由于法法热爱数学,他决定挑战自己完成以下任务:\n\n给定原始表达式中加号和减号运算符的数量,求出在将加号和减号运算符填入被擦除位置后,纸莎...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ E $ be a string representing a valid Ahmes arithmetic expression with numbers and brackets, where all operators are replaced by placeholders `?`.  \nLet $ P, M \\in \\mathbb{Z}_{\\...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments