// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;
int h, w;
int a[MAXN], b[MAXN];
ll ans;
struct node {
ll num, denom;
int id;
bool isrow;
bool operator<(const node &o) const {
if (num * o.denom == o.num * denom) {
return ii{id, isrow} < ii{o.id, o.isrow};
}
return num * o.denom > o.num * denom;
}
};
set<node> st;
set<ii> rst, cst;
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> h >> w;
REP (i, 0, h) {
cin >> a[i];
}
REP (i, 0, w) {
cin >> b[i];
}
REP (i, 0, h) {
rst.insert({i, a[i]});
}
REP (i, 0, w) {
cst.insert({i, b[i]});
}
REP (i, 1, h) {
st.insert({a[i] - a[i - 1], 1, i, 1});
}
REP (i, 1, w) {
st.insert({b[i] - b[i - 1], 1, i, 0});
}
while (h > 1 && w > 1) {
auto [num, denom, id, isrow] = *st.begin(); st.erase(st.begin());
cerr << num << ' ' << denom << ' ' << id << ' ' << isrow << '\n';
cerr << h << ' ' << w << '\n';
if (isrow) {
auto ptr = rst.erase(rst.find({id, a[id]}));
if (id == h - 1) {
assert(ptr == rst.end());
int nh = prev(rst.end()) -> FI + 1;
ans += (ll) b[w - 1] * (h - nh);
h = nh;
} else {
assert(ptr != rst.end() && ptr != rst.begin());
st.erase({ptr -> SE - a[id],
ptr -> FI - id,
ptr -> FI, 1});
st.insert({ptr -> SE - prev(ptr) -> SE,
ptr -> FI - prev(ptr) -> FI,
ptr -> FI, 1});
}
} else {
auto ptr = cst.erase(cst.find({id, b[id]}));
if (id == w - 1) {
assert(ptr == cst.end());
int nw = prev(cst.end()) -> FI + 1;
ans += (ll) a[h - 1] * (w - nw);
w = nw;
} else {
assert(ptr != cst.end() && ptr != cst.begin());
st.erase({ptr -> SE - b[id],
ptr -> FI - id,
ptr -> FI, 0});
st.insert({ptr -> SE - prev(ptr) -> SE,
ptr -> FI - prev(ptr) -> FI,
ptr -> FI, 0});
}
}
}
while (h > 1) {
ans += b[0];
h--;
}
while (w > 1) {
ans += a[0];
w--;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 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 |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
480 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
154 ms |
9904 KB |
Output is correct |
5 |
Correct |
73 ms |
9172 KB |
Output is correct |
6 |
Correct |
50 ms |
4152 KB |
Output is correct |
7 |
Correct |
610 ms |
23704 KB |
Output is correct |
8 |
Correct |
572 ms |
23756 KB |
Output is correct |
9 |
Correct |
578 ms |
23768 KB |
Output is correct |
10 |
Correct |
595 ms |
23684 KB |
Output is correct |
11 |
Correct |
131 ms |
23616 KB |
Output is correct |
12 |
Correct |
554 ms |
23768 KB |
Output is correct |
13 |
Correct |
612 ms |
23772 KB |
Output is correct |
14 |
Correct |
360 ms |
23740 KB |
Output is correct |
15 |
Correct |
149 ms |
24016 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
284 KB |
Output is correct |
24 |
Correct |
1 ms |
328 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 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 |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
480 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
154 ms |
9904 KB |
Output is correct |
33 |
Correct |
73 ms |
9172 KB |
Output is correct |
34 |
Correct |
50 ms |
4152 KB |
Output is correct |
35 |
Correct |
610 ms |
23704 KB |
Output is correct |
36 |
Correct |
572 ms |
23756 KB |
Output is correct |
37 |
Correct |
578 ms |
23768 KB |
Output is correct |
38 |
Correct |
595 ms |
23684 KB |
Output is correct |
39 |
Correct |
131 ms |
23616 KB |
Output is correct |
40 |
Correct |
554 ms |
23768 KB |
Output is correct |
41 |
Correct |
612 ms |
23772 KB |
Output is correct |
42 |
Correct |
360 ms |
23740 KB |
Output is correct |
43 |
Correct |
149 ms |
24016 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
328 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
284 KB |
Output is correct |
52 |
Correct |
1 ms |
328 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
153 ms |
11120 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
10 ms |
1432 KB |
Output is correct |
58 |
Correct |
32 ms |
3572 KB |
Output is correct |
59 |
Correct |
602 ms |
24880 KB |
Output is correct |
60 |
Correct |
570 ms |
24652 KB |
Output is correct |
61 |
Correct |
582 ms |
24572 KB |
Output is correct |
62 |
Correct |
609 ms |
24708 KB |
Output is correct |
63 |
Correct |
133 ms |
24636 KB |
Output is correct |
64 |
Correct |
627 ms |
24764 KB |
Output is correct |
65 |
Correct |
612 ms |
24780 KB |
Output is correct |
66 |
Correct |
165 ms |
25008 KB |
Output is correct |
67 |
Correct |
165 ms |
24140 KB |
Output is correct |