#include <bits/stdc++.h>
#include <math.h>
//in the name of god,aka allah
//**gray sety orz**
using namespace std;
#define pi pair<int , int>
#define pii pair<long long , pair<long long , long long>>
const int maxm = 3e5 + 5;
const long long mod = 1e9 + 7 ;
typedef long long ll;
ll n,m;
bool isval(int mid){
//cout << mid <<" " << mid*mid-mid <<endl;
if (((mid-1)*mid)/2 < m) return 0;
return 1;
}
int darage[maxm];
ll ss;
bool mm;
pi pedaret[maxm*2];
int rp[maxm*2];
pi w[maxm];
//ll rw[maxm][maxm];
bool cmp(pi x , pi y){
return max(x.first,x.second)>max(y.first,y.second);
}
void build(int v=1 , int l=1 , int r=m+1){
if(r-l==1){ rp[v] = 0, pedaret[v]={darage[l],l}; return; }
int mid = (l+r)/2;
build(v*2,l,mid) , build(v*2+1,mid,r);
rp[v]=rp[v*2]+rp[v*2+1];
pedaret[v]=max(pedaret[v*2],pedaret[v*2+1]);
}
void shift(int id , int v=1 , int l=1 , int r=m+1){
if(r-l==1){ rp[v] = 1 , pedaret[v] = {-1 , l}; return; }
int mid=(l+r)/2;
if(id<mid) shift(id , v*2 , l , mid);
else shift(id , v*2+1 , mid , r);
rp[v] = rp[v*2] + rp[v*2+1];
pedaret[v] = max(pedaret[v*2] , pedaret[v*2+1]);
}
int upd(int x , int v=1 , int l=1 , int r=m+1){
if(r-l<=1) return l;
int mid = (l+r)/2;
if(pedaret[v*2+1].first>=x) return upd(x,v*2+1,mid,r);
else return upd(x,v*2,l,mid);
}
int get(int lx , int rx , int v=1 , int l=1 , int r=m+1){
if(r<=lx||rx<=l) return 0;
if(lx<=l&&r<=rx) return rp[v];
int mid = (l+r)/2;
return get(lx,rx,v*2,l,mid)+get(lx,rx,v*2+1,mid,r);
}
map<ll,ll> mp;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >>n>>m;
for (int i=1; i<=n; i++) cin>>w[i].first>>w[i].second;
sort(w+1,w+n+1,cmp);
for (int i=1; i<=m; i++) cin>>darage[i];
build();
//cout<<55;
for (int i=1; i<=n; i++){
mm = 0;
if (w[i].second>w[i].first) swap(w[i].first,w[i].second) , mm = 1;
while (pedaret[1].first>=w[i].first) shift(pedaret[1].second);
//cout<<<69;
if (pedaret[1].first>=w[i].second){//cout<<44<<endl;
//cout<<get(upd(w[i].second),m+1)<<endl;
if (get(upd(w[i].second),m+1)%2) ss+=w[i].second;
else ss+=w[i].first;
}
//cout<<73;
else ss+=((mm)?((rp[1]%2)?w[i].first:w[i].second):(((rp[1]%2)?w[i].second:w[i].first)));
//cout<<i<<endl;
}
cout<<ss;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
7 ms |
844 KB |
Output is correct |
15 |
Correct |
15 ms |
1252 KB |
Output is correct |
16 |
Correct |
25 ms |
1460 KB |
Output is correct |
17 |
Correct |
33 ms |
2244 KB |
Output is correct |
18 |
Correct |
33 ms |
2464 KB |
Output is correct |
19 |
Correct |
30 ms |
2260 KB |
Output is correct |
20 |
Correct |
32 ms |
2260 KB |
Output is correct |
21 |
Correct |
24 ms |
2300 KB |
Output is correct |
22 |
Correct |
22 ms |
2356 KB |
Output is correct |
23 |
Correct |
25 ms |
2240 KB |
Output is correct |
24 |
Correct |
24 ms |
2340 KB |
Output is correct |
25 |
Correct |
21 ms |
2360 KB |
Output is correct |
26 |
Correct |
27 ms |
2284 KB |
Output is correct |
27 |
Correct |
30 ms |
2356 KB |
Output is correct |
28 |
Correct |
30 ms |
2344 KB |
Output is correct |
29 |
Correct |
30 ms |
2348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
7 ms |
844 KB |
Output is correct |
15 |
Correct |
15 ms |
1252 KB |
Output is correct |
16 |
Correct |
25 ms |
1460 KB |
Output is correct |
17 |
Correct |
33 ms |
2244 KB |
Output is correct |
18 |
Correct |
33 ms |
2464 KB |
Output is correct |
19 |
Correct |
30 ms |
2260 KB |
Output is correct |
20 |
Correct |
32 ms |
2260 KB |
Output is correct |
21 |
Correct |
24 ms |
2300 KB |
Output is correct |
22 |
Correct |
22 ms |
2356 KB |
Output is correct |
23 |
Correct |
25 ms |
2240 KB |
Output is correct |
24 |
Correct |
24 ms |
2340 KB |
Output is correct |
25 |
Correct |
21 ms |
2360 KB |
Output is correct |
26 |
Correct |
27 ms |
2284 KB |
Output is correct |
27 |
Correct |
30 ms |
2356 KB |
Output is correct |
28 |
Correct |
30 ms |
2344 KB |
Output is correct |
29 |
Correct |
30 ms |
2348 KB |
Output is correct |
30 |
Correct |
87 ms |
7332 KB |
Output is correct |
31 |
Correct |
111 ms |
10564 KB |
Output is correct |
32 |
Correct |
139 ms |
11824 KB |
Output is correct |
33 |
Correct |
184 ms |
14664 KB |
Output is correct |
34 |
Correct |
34 ms |
9132 KB |
Output is correct |
35 |
Correct |
183 ms |
14636 KB |
Output is correct |
36 |
Correct |
177 ms |
14520 KB |
Output is correct |
37 |
Correct |
203 ms |
14608 KB |
Output is correct |
38 |
Correct |
171 ms |
14600 KB |
Output is correct |
39 |
Correct |
201 ms |
14588 KB |
Output is correct |
40 |
Correct |
128 ms |
14552 KB |
Output is correct |
41 |
Correct |
176 ms |
14664 KB |
Output is correct |
42 |
Correct |
177 ms |
14620 KB |
Output is correct |
43 |
Correct |
105 ms |
13880 KB |
Output is correct |
44 |
Correct |
108 ms |
13952 KB |
Output is correct |
45 |
Correct |
110 ms |
13900 KB |
Output is correct |
46 |
Correct |
135 ms |
12756 KB |
Output is correct |
47 |
Correct |
147 ms |
12512 KB |
Output is correct |
48 |
Correct |
178 ms |
14788 KB |
Output is correct |
49 |
Correct |
174 ms |
14668 KB |
Output is correct |