-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE.cpp
71 lines (68 loc) · 1.73 KB
/
E.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
#include <bits/stdc++.h>
#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 2e5 + 5;
std::multiset<P> big,small;
int now = 0;LL sm1 = 0,sm2 = 0;int smb = 0;
int main(){
// freopen("A.in","r",stdin);
// freopen("A.out","w",stdout);
int n;scanf("%d",&n);
FOR(c,1,n){
int tp,d;scanf("%d%d",&tp,&d);
if(d > 0){
now += tp;
if(small.empty() || d >= small.rbegin()->fi){
big.insert(MP(d,tp));sm1 += d;smb += tp;
}
else{
small.insert(MP(d,tp));sm2 += d;
}
}
else{
now -= tp;d = -d;
if(big.count(MP(d,tp))) big.erase(big.find(MP(d,tp))),sm1 -= d,smb -= tp;
else small.erase(small.find(MP(d,tp))),sm2 -= d;
}
while(big.size() > now){
small.insert(*big.begin());
sm1 -= big.begin()->fi;
sm2 += big.begin()->fi;
smb -= big.begin()->se;
big.erase(big.begin());
}
while(big.size() < now){
big.insert(*small.rbegin());
sm1 += small.rbegin()->fi;
sm2 -= small.rbegin()->fi;
smb += small.rbegin()->se;
small.erase((--small.end()));
}
if(!big.empty() && !small.empty()){
assert(big.begin()->fi >= small.rbegin()->fi);
}
if(smb != now || now == 0){
printf("%lld\n",sm1*2+sm2);
}
else{
if(small.empty()){
printf("%lld\n",sm1*2+sm2-big.begin()->fi);
}
else{
printf("%lld\n",sm1*2+sm2-big.begin()->fi+small.rbegin()->fi);
}
}
}
return 0;
}