-
Notifications
You must be signed in to change notification settings - Fork 3
/
A.cpp
146 lines (128 loc) · 2.58 KB
/
A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <bits/stdc++.h>
using namespace std;
// #define __ll128_t i128
const double eps = 1e-6;
#define ll long long
typedef long double ld;
#define pb push_back // for vector
#define pi pair
// #define mp make_pair
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ff first
#define ss second
#define vt vector
#define vi vt<ll>
#define ub upper_bound
#define lb lower_bound
#define repr(i, n, a) for (ll i = n; i >= a; i--)
#define dq deque
#define inset(a, st) st.find(a) != st.end()
#define issub(a, b) b.find(a) != string::npos // check if a is substr of b
#define len(a) (ll)a.size()
const ld PI = 2 * acos(0.0);
const ll mod = 1e9 + 7;
const ll nax = 2e5 + 5;
void ff(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
bool cmp(pi<int,int>&a, pi<int,int>&b){
if(a.second == b.second){
return a.first < b.first;
}
else {
if(a.first > b.first){
if(a.second == 0){
return 0;
}
else {
return 0;
}
}
else {
if(b.second == 0){
return 1;
}
else {
return 1;
}
}
}
}
class Pair {
public :
ll coeff;
ll val;
ll res;
Pair (ll coeff, ll val, ll res){
this->coeff = coeff; this->val = val; this->res = res;
}
Pair (){}
};
bool cmp3(Pair & a, Pair & b){
if(a.res == b.res){
return a.val > b.val;
}
return a.res < b.res ;
}
void solve(){
int a;
cin>>a;
string s; cin>>s;
vt<string>v;
int i = 0 , j = 0;
while(j < (int)s.size()){
if(s.substr(j, 3) == "++a" || s.substr(j , 3) == "a++"){
v.push_back(s.substr(i , j+2-i+1));
j += 3;
i = j;
continue;
}
j++;
}
ll sol = 0;
vt<pi<int , int>>vals;
rep(i , 0 , v.size()){
string num = "";
int j = 0;
while( v[i][j] != '*' && v[i][j] != 'a'){
num = num + v[i][j];
j++;
}
if(num == "+" || num == "" || num == "+++"|| num == "++"){
num = "1";
}
else if(num =="-" || num == "-++"){
num ="-1";
}
if(v[i].substr(v[i].size() - 3 , 3 ) == "++a"){
vals.push_back({stoi(num) , 0});
}
else {
vals.push_back({stoi(num), 1});
}
}
sort(all(vals), cmp);
rep( i , 0 , vals.size()){
// cout << vals[i].first <<" "<<vals[i].second<<" ";
if(vals[i].second == 0){
a++;
sol += vals[i].first * a;
}
else {
sol += vals[i].first*a;
a++;
}
}
cout << sol<<"\n";
}
int main(){
// ff();
// ll t;
// cin>>t;
// while(t--){
solve();
// }
return 0;
}