// 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;
set<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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
324 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
0 ms |
320 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
312 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
93 ms |
6272 KB |
Output is correct |
5 |
Correct |
47 ms |
5916 KB |
Output is correct |
6 |
Correct |
31 ms |
2720 KB |
Output is correct |
7 |
Correct |
362 ms |
15088 KB |
Output is correct |
8 |
Correct |
358 ms |
15156 KB |
Output is correct |
9 |
Correct |
362 ms |
15344 KB |
Output is correct |
10 |
Correct |
381 ms |
15176 KB |
Output is correct |
11 |
Correct |
108 ms |
15108 KB |
Output is correct |
12 |
Correct |
366 ms |
15180 KB |
Output is correct |
13 |
Correct |
369 ms |
15180 KB |
Output is correct |
14 |
Correct |
195 ms |
15052 KB |
Output is correct |
15 |
Correct |
89 ms |
15396 KB |
Output is correct |
16 |
Correct |
1 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 |
0 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 |
324 KB |
Output is correct |
23 |
Correct |
0 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
388 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
324 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
0 ms |
320 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
312 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
316 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
93 ms |
6272 KB |
Output is correct |
33 |
Correct |
47 ms |
5916 KB |
Output is correct |
34 |
Correct |
31 ms |
2720 KB |
Output is correct |
35 |
Correct |
362 ms |
15088 KB |
Output is correct |
36 |
Correct |
358 ms |
15156 KB |
Output is correct |
37 |
Correct |
362 ms |
15344 KB |
Output is correct |
38 |
Correct |
381 ms |
15176 KB |
Output is correct |
39 |
Correct |
108 ms |
15108 KB |
Output is correct |
40 |
Correct |
366 ms |
15180 KB |
Output is correct |
41 |
Correct |
369 ms |
15180 KB |
Output is correct |
42 |
Correct |
195 ms |
15052 KB |
Output is correct |
43 |
Correct |
89 ms |
15396 KB |
Output is correct |
44 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
324 KB |
Output is correct |
51 |
Correct |
0 ms |
328 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
388 KB |
Output is correct |
54 |
Correct |
1 ms |
320 KB |
Output is correct |
55 |
Correct |
95 ms |
7372 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
7 ms |
980 KB |
Output is correct |
58 |
Correct |
22 ms |
2448 KB |
Output is correct |
59 |
Correct |
345 ms |
16340 KB |
Output is correct |
60 |
Correct |
367 ms |
16340 KB |
Output is correct |
61 |
Correct |
351 ms |
16332 KB |
Output is correct |
62 |
Correct |
405 ms |
16356 KB |
Output is correct |
63 |
Correct |
96 ms |
16204 KB |
Output is correct |
64 |
Correct |
375 ms |
16360 KB |
Output is correct |
65 |
Correct |
343 ms |
16460 KB |
Output is correct |
66 |
Correct |
129 ms |
16332 KB |
Output is correct |
67 |
Correct |
100 ms |
16560 KB |
Output is correct |