Time limit : 2sec / Memory limit : 256MB

Score : 900 points

Problem Statement

Joisino has a formula consisting of N terms: A1 op1 A2  opN−1 AN. Here, Ai is an integer, and opi is an binary operator either + or -. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.

Constraints

  • 1≦N≦105
  • 1≦Ai≦109
  • opi is either + or -.

Input

The input is given from Standard Input in the following format:

N
A1 op1 A2 opN−1 AN

Output

Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.


Sample Input 1

Copy
3
5 - 1 - 3

Sample Output 1

Copy
7

The maximum possible value is: 5−(1−3)=7.


Sample Input 2

Copy
5
1 - 2 + 3 - 4 + 5

Sample Output 2

Copy
5

The maximum possible value is: 1−(2+3−4)+5=5.


Sample Input 3

Copy
5
1 - 20 - 13 + 14 - 5

Sample Output 3

Copy
13

The maximum possible value is: 1−(20−(13+14)−5)=13.

题意:

给你一个只含有数字和加减号的字符串表达式,你可以加无数个括号,来改变运算顺序。

让你求出这个表达式能表达的最大的结果值。

思路:

首先我们要知道这个结论,

即一个表达式最多有效 括号最多是两层,因为只有在负号后面加括号才有作用,那么一个括号对括号里面的数值的影响是取负号一次,两层是取一部分是负,一部分是正(两层里的),如果有三层,那么第三层提取到最外边一层不会改变数值。

那么我们即可定义dp的状态

dp[i][j] 表示到第i个数时,第i个数后面有j个括号时,表示式的最大值。

那么我们来分析一下转移:

我们从i-1转移到i

当是‘+’时

我们可以在 dp[i-1][j] 后面直接加上 a[i] 即可转移到 dp[i][0]

在d[i-1][1] 括号里 加入a[i] ,贡献值为 - a[i] ,可以转移到dp[i][1]

dp[i-1][2] 最里层括号加入a[i] ,贡献值 为+ a[i] 可以转移到dp[i][2]

当是'-' 时

我们默认减号后面必须加括号,即使只包括自己  即这样的  - ( a[i] )

那么我们把负号后面的dp[i][0] = -inf ,即无穷小,不影响我们其他的运算。

我们可以在 dp[i-1][0/1 ]后面加上 -(a[i]) 即转移到 dp[i-1][1]

在dp[i-1][1]的括号里面加上 - (a[i] ),或者dp[i-1][2] 的外层括号里加上 - (a[i]) 

转移到dp[i][2] ,数值贡献时 +a[i]

这样就把全部的转移表示出来了,注意开ll。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
// const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
char op[maxn];
int a[maxn];
ll dp[maxn/][];
const ll inf=(ll)(1e18); int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n;
cin>>a[];
repd(i,,n)
{
cin>>op[i]>>a[i];
}
dp[][]=a[];
dp[][]=dp[][]=-inf;
repd(i,,n)
{
if(op[i]=='+')
{
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]))+a[i];
dp[i][]=dp[i-][]-a[i];
dp[i][]=dp[i-][]+a[i];
}else
{
dp[i][]=-inf;
dp[i][]=max(dp[i-][],dp[i-][])-a[i];
dp[i][]=max(dp[i-][],dp[i-][])+a[i];
}
}
cout<<max(dp[n][],max(dp[n][],dp[n][]))<<endl; return ; } inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

最新文章

  1. Python之路 day2 文件基础操作
  2. cmd输出的日志里有中文乱码的解决办法
  3. Spring MVC 前后端 Json 方式交互和处理
  4. javascript中的splice方法介绍&amp;示例
  5. msmms (一) sms与mms区别
  6. Azure Deploy
  7. css+div网页设计(二)--布局与定位
  8. 第二百九十九天 how can I 坚持
  9. H.数7(模拟)
  10. SQL语言(一)
  11. 2019年度SAP项目实践计划
  12. Scratch 简单的小游戏 --- 碰碰球
  13. Eureka服务注册与发现
  14. maven编译错误maven-assembly-plugin:2.2-beta-5:assembly (default-cli) on project
  15. docker默认ip查询
  16. C#调用C++ DLL的方式
  17. ExpressRoute 合作伙伴和对等位置
  18. java学习助手
  19. Python3 小工具-ARP欺骗
  20. 【动态规划】【线段树】 Codeforces Round #426 (Div. 1) B. The Bakery

热门文章

  1. .net sqlite 内存溢出 问题的分析与解决。
  2. [NLP] nlp-lstm-cos -&gt; sin
  3. VASP计算参考
  4. Delphi XE2 之 FireMonkey 入门(34) - 控件基础: TFmxObject: 克隆对象
  5. 获取当前操作的IFrame 对象的方法
  6. Eclipse 添加 UML Model插件
  7. 关于mysql中修改某个字段类型,以及备份表中数据到新建的表中,从新建的表中移除数据到修改过的表中
  8. Java基础/时间日期格式
  9. Powershell 脚本输出前十条消耗内存的进程到excel
  10. if——while表达式详解