#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define pb push_back
struct uwu {
int l,r,sz,v;
bool operator<(const uwu &oth) const {
// compare v/sz
// v/sz > oth.v/oth.sz
// v * oth.sz > oth.v * sz
if(v*oth.sz != oth.v*sz) {
return v*oth.sz>oth.v*sz;
}
// this makes high values first so i want l values increasing
return l>oth.l;
}
};
void db(uwu x) {
cout<<x.l<<' '<<x.r<<' '<<x.sz<<' '<<x.v<<endl;
}
int32_t main() {
int h,w; cin>>h>>w;
vi a(h+1); vi b(w+1);
for(int i=1; i<=h; i++) {
cin>>a[i];
}
for(int i=1; i<=w; i++) {
cin>>b[i];
}
set<uwu> A, B;
for(int i=1; i<h; i++) {
A.insert({i, i+1, 1, a[i+1]-a[i]});
}
for(int i=1; i<w; i++) {
B.insert({i, i+1, 1, b[i+1]-b[i]});
}
int ret=0;
vi toA(h+1), toB(w+1);
for(int i=0; i<=h; i++) {
toA[i]=i+1;
}
for(int i=0; i<=w; i++) {
toB[i]=i+1;
}
A.insert({0, 1, 1, (int)-1e9});
B.insert({0, 1, 1, (int)-1e9});
while(A.size()>1 || B.size()>1) {
// cout<<"A, B: ";
// db(*(A.begin())); db(*(B.begin()));
if(*A.begin() < *B.begin()) {
// cout<<"A bigger"<<endl;
uwu rm = *A.begin(); A.erase(A.begin());
if(toA[rm.l]==(int)a.size()-1) {
for(int _=0; _<rm.sz; _++) {
ret+=*(--b.end()); a.pop_back();
}
continue;
}
int nxt1=toA[rm.l]; int nxt2=toA[nxt1];
A.erase({nxt1, nxt2, nxt2-nxt1, a[nxt2]-a[nxt1]});
toA[rm.l]=nxt2;
// cout<<"resetA "<<rm.l<<' '<<nxt2<<endl;
if(rm.l<=nxt2) A.insert({rm.l, nxt2, nxt2- rm.l, a[nxt2]-a[rm.l]});
// cout<<"insert "<<rm.l<<' '<<nxt2<<' '<<nxt2-rm.l<<' '<<a[nxt2]-a[rm.l]<<endl;
} else {
// cout<<"B bigger"<<endl;
// remove from B
uwu rm = *B.begin(); B.erase(B.begin());
if(toB[rm.l]==(int)b.size()-1) {
// add to answer since we're forced (?)
for(int _=0; _<rm.sz; _++) {
ret+=*(--a.end()); b.pop_back();
}
continue;
}
int nxt1=toB[rm.l]; int nxt2=toB[nxt1];
B.erase({nxt1, nxt2, nxt2-nxt1, b[nxt2]-b[nxt1]}); // erase the next segment
toB[rm.l]=nxt2; // update range
// cout<<"resetB "<<rm.l<<' '<<nxt2<<endl;
if(rm.l<=nxt2) B.insert({rm.l, nxt2, nxt2-rm.l, b[nxt2]-b[rm.l]}); // add the new segment
// cout<<"insert "<<rm.l<<' '<<nxt2<<' '<<nxt2-rm.l<<' '<<b[nxt2]-b[rm.l]<<endl;
}
}
cout<<ret<<endl;
}
/*
1 2 1 10
4 5 1 43
popb 55
1 2 1 10
2 3 1 32
resetB 2 4
insert 2 4 2 -29
1 2 1 10
2 4 2 -29
resetA 1 3
insert 1 3 2 -65
1 3 2 -65
2 4 2 -29
popb 12popb 73
1 3 2 -65
1 2 1 -42
popa 7popa 820 1 1 -1000000000
1 2 1 -42
popb 41
175
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
500 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
484 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
856 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
432 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
84 ms |
8432 KB |
Output is correct |
5 |
Correct |
43 ms |
7764 KB |
Output is correct |
6 |
Correct |
28 ms |
3420 KB |
Output is correct |
7 |
Correct |
313 ms |
19832 KB |
Output is correct |
8 |
Correct |
328 ms |
20188 KB |
Output is correct |
9 |
Correct |
304 ms |
19796 KB |
Output is correct |
10 |
Correct |
308 ms |
20212 KB |
Output is correct |
11 |
Correct |
95 ms |
19784 KB |
Output is correct |
12 |
Correct |
325 ms |
20044 KB |
Output is correct |
13 |
Correct |
332 ms |
19996 KB |
Output is correct |
14 |
Correct |
142 ms |
19796 KB |
Output is correct |
15 |
Correct |
78 ms |
20168 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
436 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
432 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
500 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
484 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
856 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
432 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
84 ms |
8432 KB |
Output is correct |
33 |
Correct |
43 ms |
7764 KB |
Output is correct |
34 |
Correct |
28 ms |
3420 KB |
Output is correct |
35 |
Correct |
313 ms |
19832 KB |
Output is correct |
36 |
Correct |
328 ms |
20188 KB |
Output is correct |
37 |
Correct |
304 ms |
19796 KB |
Output is correct |
38 |
Correct |
308 ms |
20212 KB |
Output is correct |
39 |
Correct |
95 ms |
19784 KB |
Output is correct |
40 |
Correct |
325 ms |
20044 KB |
Output is correct |
41 |
Correct |
332 ms |
19996 KB |
Output is correct |
42 |
Correct |
142 ms |
19796 KB |
Output is correct |
43 |
Correct |
78 ms |
20168 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
436 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
432 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
93 ms |
9420 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
7 ms |
1116 KB |
Output is correct |
58 |
Correct |
28 ms |
3076 KB |
Output is correct |
59 |
Correct |
324 ms |
21320 KB |
Output is correct |
60 |
Correct |
341 ms |
21156 KB |
Output is correct |
61 |
Correct |
308 ms |
21076 KB |
Output is correct |
62 |
Correct |
346 ms |
21176 KB |
Output is correct |
63 |
Correct |
108 ms |
21056 KB |
Output is correct |
64 |
Correct |
296 ms |
21136 KB |
Output is correct |
65 |
Correct |
321 ms |
21076 KB |
Output is correct |
66 |
Correct |
141 ms |
21084 KB |
Output is correct |
67 |
Correct |
101 ms |
21332 KB |
Output is correct |