Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 2e9;
- const int N = 2e5 + 10;
- vector <int> val;
- stack <int> st;
- int main(){
- int n;
- scanf("%d", &n);
- int num;
- scanf("%d", &num);
- st.push(num);
- for(int i=2;i<=n;i++){
- char opr; scanf(" %c", &opr);
- scanf("%d", &num);
- num *= (opr == '-' ? -1: 1);
- if(num < 0){
- if(!st.empty()){
- val.push_back(st.top());
- st.pop();
- }
- val.push_back(num);
- }
- else{
- int tp = 0;
- if(!st.empty()){
- tp = st.top();
- st.pop();
- }
- st.push(tp + num);
- }
- }
- if(!st.empty()){
- val.push_back(st.top());
- st.pop();
- }
- int sz = val.size();
- vector <int> qs_abs(sz + 10, 0);
- for(int i=sz-1;i>=0;i--)
- qs_abs[i] = qs_abs[i+1] + abs(val[i]);
- int sum = 0, ans = 0;
- for(int i=0;i<sz;i++){
- sum += val[i];
- ans = max(ans, sum);
- if(val[i] < 0 and i + 2 < sz)
- ans = max(ans, sum - val[i+1] + qs_abs[i+2]);
- }
- printf("%d", ans);
- return 0;
- }
Add Comment
Please, Sign In to add comment