CF39A.C*++ Calculations

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:64MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

C*++ language is quite similar to C++. The similarity manifests itself in the fact that the programs written in C*++ sometimes behave unpredictably and lead to absolutely unexpected effects. For example, let's imagine an arithmetic expression in C*++ that looks like this (expression is the main term):

  • expression ::= summand | expression + summand | expression - summand
  • summand ::= increment | coefficient*increment
  • increment ::= a++ | ++a
  • coefficient ::= 0|1|2|...|1000

For example, "5*a++-3*a+a" is a valid expression in C*++.

Thus, we have a sum consisting of several summands divided by signs "+" or "-". Every summand is an expression "a++" or "++a" multiplied by some integer coefficient. If the coefficient is omitted, it is suggested being equal to 1.

The calculation of such sum in C*++ goes the following way. First all the summands are calculated one after another, then they are summed by the usual arithmetic rules. If the summand contains "a++", then during the calculation first the value of the "a" variable is multiplied by the coefficient, then value of "a" is increased by 1. If the summand contains "++a", then the actions on it are performed in the reverse order: first "a" is increased by 1, then — multiplied by the coefficient.

The summands may be calculated in any order, that's why sometimes the result of the calculation is completely unpredictable! Your task is to find its largest possible value.

C*++ 语言与 C++ 非常相似。这种相似性体现在:用 C*++ 编写的程序有时行为不可预测,会产生完全出人意料的效果。例如,考虑如下 C*++ 中的算术表达式(其中 expression 是主项):

  • expression ::= summand | expression + summand | expression - summand
  • summand ::= increment | coefficient*increment
  • increment ::= a++ | ++a
  • coefficient ::= 0|1|2|...|1000

例如,5*a++-3*++a+a++ 是一个合法的 C*++ 表达式。

因此,我们得到一个由若干项(summand)通过 + 或 - 号连接而成的和式。每一项均为形如 a++ 或 ++a 的增量表达式,乘以某个整数系数。若系数被省略,则默认其值为 1。

该和式在 C*++ 中的计算过程如下:首先依次计算所有各项,再按通常的算术规则将它们相加。若某一项包含 a++,则在计算该项时,先将变量 a 的当前值乘以该系数,再将 a 的值增加 1;若某一项包含 ++a,则执行顺序相反:先将 a 的值增加 1,再乘以该系数。

由于各项可以以任意顺序计算,因此有时计算结果完全不可预测!你的任务是求出该表达式可能取得的最大值。

输入格式

The first input line contains an integer a ( - 1000 ≤ a ≤ 1000) — the initial value of the variable "a". The next line contains an expression in C*++ language of the described type. The number of the summands in the expression does not exceed 1000. It is guaranteed that the line describing the expression contains no spaces and tabulation.

第一行输入包含一个整数 aa(−1000≤a≤1000-1000 \leq a \leq 1000)——变量“a”的初始值。
下一行包含一个符合上述描述的 C*++ 语言表达式。表达式中加项的个数不超过 1000。保证描述该表达式的行中不含空格和制表符。

输出格式

Output a single number — the maximal possible value of the expression.

输出一个数字——该表达式的最大可能值。

输入输出样例

  • 输入#1

    1
    5*a++-3*++a+a++

    输出#1

    11
  • 输入#2

    3
    a+++++a

    输出#2

    8

说明/提示

Consider the second example. Initially a = 3. Suppose that at first the first summand is calculated, and then the second one is. The first summand gets equal to 3, and the value of a is increased by 1. At the calculation of the second summand a is increased once more (gets equal to 5). The value of the second summand is 5, and together they give 8. If we calculate the second summand first and the first summand later, then the both summands equals to 4, and the result is 8, too.

考虑第二个例子。初始时 a=3a = 3。假设首先计算第一个加数,然后计算第二个加数。第一个加数的值为 3,同时 aa 的值增加 1。在计算第二个加数时,aa 再次增加(变为 5)。第二个加数的值为 5,两者之和为 8。如果我们先计算第二个加数、后计算第一个加数,则两个加数均为 4,结果同样为 8。

输入解题思路,AI测评打分。不知道怎么写?

首页