// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC target ("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(c i) \
{
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef XOX
~debug()
{
cerr << endl;
}
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
ris << "" << d.first << " --> " << d.second << "";
}
sim dor(rge<c> d)
{
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &)
{
ris;
}
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#ifdef XOX
#warning Times may differ!!!
#endif
#define endl '\n'
#define pb emplace_back
#define vt vector
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int nax = 1000 * 1007, mod = 1e9 + 7;
struct wek {
int p, q, i;
bool operator<(const wek &he) const
{
if ((ll)p * he.q != (ll)q * he.p) return (ll)p * he.q < (ll)q * he.p;
if (q != he.q) return q < he.q;
return i < he.i;
}
};
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int h, w;
cin >> h >> w;
vt<int> a(h), b(w);
for (auto &i : a) cin >> i;
for (auto &i : b) cin >> i;
vt<int> na(h), nb(w);
for (int i = 0; i < h; i++) na[i] = i + 1;
for (int i = 0; i < w; i++) nb[i] = i + 1;
multiset<wek> poz, pio;
for (int i = 0; i < h - 1; i++) poz.insert({a[i + 1] - a[i], 1, i});
for (int i = 0; i < w - 1; i++) pio.insert({b[i + 1] - b[i], 1, i});
poz.insert({(int)-2e9, 1, 1});
pio.insert({(int)-2e9, 1, 1});
ll ans = 0;
while (sz(pio) > 1 || sz(poz) > 1) {
if (*pio.rbegin() < *poz.rbegin()) {
auto [p, q, i] = *poz.rbegin();
poz.erase({p, q, i});
int j = na[i];
if (j == (int)a.size() - 1) {
while (q--) {
ans += b.back(); a.pop_back();
}
}
else {
int k = na[j];
poz.erase({a[k] - a[j], k - j, j}); poz.insert({a[k] - a[i], k - i, i});
na[i] = k;
}
}
else {
auto [p, q, i] = *pio.rbegin();
pio.erase({p, q, i});
int j = nb[i];
if (j == (int)b.size() - 1) {
while (q--) {
ans += a.back(); b.pop_back();
}
}
else {
int k = nb[j];
pio.erase({b[k] - b[j], k - j, j}); pio.insert({b[k] - b[i], k - i, i});
nb[i] = k;
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 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 |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 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 |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
476 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
320 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 |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
96 ms |
6396 KB |
Output is correct |
5 |
Correct |
48 ms |
5864 KB |
Output is correct |
6 |
Correct |
30 ms |
2768 KB |
Output is correct |
7 |
Correct |
407 ms |
15176 KB |
Output is correct |
8 |
Correct |
368 ms |
15252 KB |
Output is correct |
9 |
Correct |
417 ms |
15100 KB |
Output is correct |
10 |
Correct |
376 ms |
15196 KB |
Output is correct |
11 |
Correct |
100 ms |
15052 KB |
Output is correct |
12 |
Correct |
342 ms |
15192 KB |
Output is correct |
13 |
Correct |
356 ms |
15260 KB |
Output is correct |
14 |
Correct |
179 ms |
15052 KB |
Output is correct |
15 |
Correct |
86 ms |
15268 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 |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
320 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 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 |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 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 |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
476 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
320 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 |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
316 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
96 ms |
6396 KB |
Output is correct |
33 |
Correct |
48 ms |
5864 KB |
Output is correct |
34 |
Correct |
30 ms |
2768 KB |
Output is correct |
35 |
Correct |
407 ms |
15176 KB |
Output is correct |
36 |
Correct |
368 ms |
15252 KB |
Output is correct |
37 |
Correct |
417 ms |
15100 KB |
Output is correct |
38 |
Correct |
376 ms |
15196 KB |
Output is correct |
39 |
Correct |
100 ms |
15052 KB |
Output is correct |
40 |
Correct |
342 ms |
15192 KB |
Output is correct |
41 |
Correct |
356 ms |
15260 KB |
Output is correct |
42 |
Correct |
179 ms |
15052 KB |
Output is correct |
43 |
Correct |
86 ms |
15268 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 |
316 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
320 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
96 ms |
7300 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
6 ms |
1036 KB |
Output is correct |
58 |
Correct |
21 ms |
2356 KB |
Output is correct |
59 |
Correct |
337 ms |
16472 KB |
Output is correct |
60 |
Correct |
328 ms |
16464 KB |
Output is correct |
61 |
Correct |
354 ms |
16384 KB |
Output is correct |
62 |
Correct |
330 ms |
16360 KB |
Output is correct |
63 |
Correct |
91 ms |
16196 KB |
Output is correct |
64 |
Correct |
344 ms |
16288 KB |
Output is correct |
65 |
Correct |
320 ms |
16456 KB |
Output is correct |
66 |
Correct |
127 ms |
16332 KB |
Output is correct |
67 |
Correct |
98 ms |
16568 KB |
Output is correct |