#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Fraction{
int x, y, i;
Fraction (int _x=0, int _y=1, int _i=0){
x=_x, y=_y, i=_i;
if (y<0) y*=-1, x*=-1;
}
bool operator<(const Fraction &b) const {
return make_pair(x*b.y, i)>make_pair(b.x*y, b.i);
}
};
const int N=1e5+10;
int h, w, a[N], b[N], da[N], db[N];
Fraction ca[N], cb[N];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
for (int i=1; i<=h; ++i) cin >> a[i];
for (int i=1; i<=w; ++i) cin >> b[i];
set<Fraction> st;
set<int> sta, stb;
for (int i=1; i<=h; ++i) sta.insert(i);
for (int i=1; i<=w; ++i) stb.insert(i);
for (int i=2; i<=h; ++i) ca[i]=Fraction(a[i]-a[i-1], 1, i), st.insert(ca[i]);
for (int i=2; i<=w; ++i) cb[i]=Fraction(b[i]-b[i-1], 1, -i), st.insert(cb[i]);
int x=h, y=w;
int ans=0;
while ((x!=1 || y!=1) && st.size()){
auto p=*st.begin(); st.erase(st.begin());
if (p.i<0){
p.i=-p.i;
db[p.i]=1;
stb.erase(p.i);
auto it=stb.lower_bound(p.i);
if (it!=stb.end()){
st.erase(cb[*it]);
cb[*it]=Fraction(b[*it]-b[*prev(it)], (*it)-(*prev(it)), -*it), st.insert(cb[*it]);
}
if (da[x] && db[y]){
while (da[x]) --x, ans+=b[y];
}
}else{
da[p.i]=1;
sta.erase(p.i);
auto it=sta.lower_bound(p.i);
if (it!=sta.end()){
st.erase(ca[*it]);
ca[*it]=Fraction(a[*it]-a[*prev(it)], (*it)-(*prev(it)), *it), st.insert(ca[*it]);
}
if (da[x] && db[y]){
while (db[y]) --y, ans+=a[x];
}
}
}
while (x!=1) --x, ans+=b[y];
while (y!=1) --y, ans+=a[x];
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
7004 KB |
Output is correct |
10 |
Correct |
3 ms |
7004 KB |
Output is correct |
11 |
Correct |
3 ms |
7004 KB |
Output is correct |
12 |
Correct |
3 ms |
6924 KB |
Output is correct |
13 |
Correct |
2 ms |
7004 KB |
Output is correct |
14 |
Correct |
3 ms |
7000 KB |
Output is correct |
15 |
Correct |
3 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
124 ms |
16624 KB |
Output is correct |
5 |
Correct |
59 ms |
15708 KB |
Output is correct |
6 |
Correct |
39 ms |
10584 KB |
Output is correct |
7 |
Correct |
507 ms |
30944 KB |
Output is correct |
8 |
Correct |
497 ms |
31056 KB |
Output is correct |
9 |
Correct |
466 ms |
30768 KB |
Output is correct |
10 |
Correct |
477 ms |
31060 KB |
Output is correct |
11 |
Correct |
114 ms |
30856 KB |
Output is correct |
12 |
Correct |
447 ms |
30960 KB |
Output is correct |
13 |
Correct |
463 ms |
30804 KB |
Output is correct |
14 |
Correct |
260 ms |
30948 KB |
Output is correct |
15 |
Correct |
114 ms |
30972 KB |
Output is correct |
16 |
Correct |
3 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
7000 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
1 ms |
6796 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
7004 KB |
Output is correct |
10 |
Correct |
3 ms |
7004 KB |
Output is correct |
11 |
Correct |
3 ms |
7004 KB |
Output is correct |
12 |
Correct |
3 ms |
6924 KB |
Output is correct |
13 |
Correct |
2 ms |
7004 KB |
Output is correct |
14 |
Correct |
3 ms |
7000 KB |
Output is correct |
15 |
Correct |
3 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6744 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
1 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
124 ms |
16624 KB |
Output is correct |
33 |
Correct |
59 ms |
15708 KB |
Output is correct |
34 |
Correct |
39 ms |
10584 KB |
Output is correct |
35 |
Correct |
507 ms |
30944 KB |
Output is correct |
36 |
Correct |
497 ms |
31056 KB |
Output is correct |
37 |
Correct |
466 ms |
30768 KB |
Output is correct |
38 |
Correct |
477 ms |
31060 KB |
Output is correct |
39 |
Correct |
114 ms |
30856 KB |
Output is correct |
40 |
Correct |
447 ms |
30960 KB |
Output is correct |
41 |
Correct |
463 ms |
30804 KB |
Output is correct |
42 |
Correct |
260 ms |
30948 KB |
Output is correct |
43 |
Correct |
114 ms |
30972 KB |
Output is correct |
44 |
Correct |
3 ms |
6744 KB |
Output is correct |
45 |
Correct |
1 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6748 KB |
Output is correct |
48 |
Correct |
1 ms |
7000 KB |
Output is correct |
49 |
Correct |
1 ms |
6744 KB |
Output is correct |
50 |
Correct |
1 ms |
6796 KB |
Output is correct |
51 |
Correct |
1 ms |
6748 KB |
Output is correct |
52 |
Correct |
2 ms |
6748 KB |
Output is correct |
53 |
Correct |
1 ms |
6748 KB |
Output is correct |
54 |
Correct |
1 ms |
6744 KB |
Output is correct |
55 |
Correct |
163 ms |
17996 KB |
Output is correct |
56 |
Correct |
3 ms |
6748 KB |
Output is correct |
57 |
Correct |
10 ms |
7772 KB |
Output is correct |
58 |
Correct |
31 ms |
10036 KB |
Output is correct |
59 |
Correct |
540 ms |
32116 KB |
Output is correct |
60 |
Correct |
532 ms |
32112 KB |
Output is correct |
61 |
Correct |
540 ms |
32124 KB |
Output is correct |
62 |
Correct |
482 ms |
32084 KB |
Output is correct |
63 |
Correct |
105 ms |
31924 KB |
Output is correct |
64 |
Correct |
464 ms |
32080 KB |
Output is correct |
65 |
Correct |
480 ms |
32140 KB |
Output is correct |
66 |
Correct |
152 ms |
31920 KB |
Output is correct |
67 |
Correct |
118 ms |
32344 KB |
Output is correct |