// Code by Parsa Eslami
#include <bits/stdc++.h>
#define pii pair<int,int>
#define bit(i,j) ((j>>i)&1)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define S second
#define F first
#define pb push_back
#define SZ(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define err(x) cout<<#x<<": "<<x<<'\n';
using namespace std;
const int N=2e5+4;
const int lg=20;
pii a[N];
pii b[N];
int mx[lg][N],n,k;
int pw[lg];
long long ans;
struct fenwick{
int fen[N];
void add(int k){
while(k<N){
fen[k]++;
k+=(k&(-k));
}
}
int get(int k){
int rt=0;
while(k>0){
rt+=fen[k];
k-=(k&(-k));
}
return rt;
}
};fenwick fen;
bool cmp(pii a,pii b){
return (max(a.F,a.S)<max(b.F,b.S));
}
int MX(int l,int r){
int LG=31-__builtin_clz(r-l+1);
return max(mx[LG][l],mx[LG][r-pw[LG]+1]);
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
pw[0]=1; FOR(i,1,lg-1) pw[i]=pw[i-1]*2;
cin>>n>>k;
FOR(i,1,n) cin>>a[i].F>>a[i].S;
sort(a+1,a+1+n,cmp);
FOR(i,1,k){ cin>>b[i].F; b[i].S=i;}
sort(b+1,b+1+k);
FOR(i,1,k) mx[0][i]=b[i].S;
FOR(j,1,lg-1) FOR(i,1,k-pw[j]+1){
mx[j][i]=max(mx[j-1][i],mx[j-1][i+pw[j-1]]);
}
int p=k;
FORR(i,n,1){
if(a[i].F==a[i].S){
ans+=a[i].F;
continue;
}
int mx=max(a[i].F,a[i].S);
int mn=a[i].F+a[i].S-mx;
while(p>0&&b[p].F>=mx){
fen.add(b[p].S);
p--;
}
int l=0,r=k+1;
int R,L;
while(r-l>1){
int mid=(r+l)/2;
if(b[mid].F<mx) l=mid;
else r=mid;
}
R=l;
l=0,r=k+1;
while(r-l>1){
int mid=(r+l)/2;
if(b[mid].F>=mn) r=mid;
else l=mid;
}
L=r;
int x;
if(L>R) x=-1;
else x=MX(L,R);
if(x==-1){
if(fen.get(k)&1) ans+=a[i].S;
else ans+=a[i].F;
}else{
if((fen.get(k)-fen.get(x))&1) ans+=mn;
else ans+=mx;
}
}
cout<<ans<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
372 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
372 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
8 ms |
1364 KB |
Output is correct |
15 |
Correct |
14 ms |
2388 KB |
Output is correct |
16 |
Correct |
24 ms |
3488 KB |
Output is correct |
17 |
Correct |
33 ms |
4620 KB |
Output is correct |
18 |
Correct |
33 ms |
4620 KB |
Output is correct |
19 |
Correct |
28 ms |
4520 KB |
Output is correct |
20 |
Correct |
30 ms |
4572 KB |
Output is correct |
21 |
Correct |
29 ms |
4604 KB |
Output is correct |
22 |
Correct |
25 ms |
4196 KB |
Output is correct |
23 |
Correct |
25 ms |
4052 KB |
Output is correct |
24 |
Correct |
23 ms |
4140 KB |
Output is correct |
25 |
Correct |
25 ms |
4056 KB |
Output is correct |
26 |
Correct |
28 ms |
4452 KB |
Output is correct |
27 |
Correct |
36 ms |
4640 KB |
Output is correct |
28 |
Correct |
27 ms |
4632 KB |
Output is correct |
29 |
Correct |
28 ms |
4552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
372 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
8 ms |
1364 KB |
Output is correct |
15 |
Correct |
14 ms |
2388 KB |
Output is correct |
16 |
Correct |
24 ms |
3488 KB |
Output is correct |
17 |
Correct |
33 ms |
4620 KB |
Output is correct |
18 |
Correct |
33 ms |
4620 KB |
Output is correct |
19 |
Correct |
28 ms |
4520 KB |
Output is correct |
20 |
Correct |
30 ms |
4572 KB |
Output is correct |
21 |
Correct |
29 ms |
4604 KB |
Output is correct |
22 |
Correct |
25 ms |
4196 KB |
Output is correct |
23 |
Correct |
25 ms |
4052 KB |
Output is correct |
24 |
Correct |
23 ms |
4140 KB |
Output is correct |
25 |
Correct |
25 ms |
4056 KB |
Output is correct |
26 |
Correct |
28 ms |
4452 KB |
Output is correct |
27 |
Correct |
36 ms |
4640 KB |
Output is correct |
28 |
Correct |
27 ms |
4632 KB |
Output is correct |
29 |
Correct |
28 ms |
4552 KB |
Output is correct |
30 |
Correct |
56 ms |
17936 KB |
Output is correct |
31 |
Correct |
92 ms |
19032 KB |
Output is correct |
32 |
Correct |
147 ms |
20404 KB |
Output is correct |
33 |
Correct |
166 ms |
23280 KB |
Output is correct |
34 |
Correct |
47 ms |
17740 KB |
Output is correct |
35 |
Correct |
189 ms |
23052 KB |
Output is correct |
36 |
Correct |
175 ms |
23072 KB |
Output is correct |
37 |
Correct |
163 ms |
23044 KB |
Output is correct |
38 |
Correct |
154 ms |
23112 KB |
Output is correct |
39 |
Correct |
182 ms |
23220 KB |
Output is correct |
40 |
Correct |
144 ms |
22976 KB |
Output is correct |
41 |
Correct |
166 ms |
23076 KB |
Output is correct |
42 |
Correct |
181 ms |
23172 KB |
Output is correct |
43 |
Correct |
127 ms |
22092 KB |
Output is correct |
44 |
Correct |
126 ms |
22092 KB |
Output is correct |
45 |
Correct |
143 ms |
22252 KB |
Output is correct |
46 |
Correct |
129 ms |
21136 KB |
Output is correct |
47 |
Correct |
124 ms |
21052 KB |
Output is correct |
48 |
Correct |
149 ms |
23064 KB |
Output is correct |
49 |
Correct |
169 ms |
23092 KB |
Output is correct |