//Sylwia Sapkowska
#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef DEBUG
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
const int oo = 1e9+7, K = 30;
const int mod = 998244353;
struct T{
int first, second, idx;
bool vertical;
T(int _f, int _s, int _i, int _v): first(_f), second(_s), idx(_i), vertical(_v) {}
bool operator < (const T &y) const {return first * y.second == second * y.first ? (idx == y.idx ? vertical < y.vertical : idx < y.idx) : first * y.second < second * y.first;};
};
void __print(T a){
cerr << "{" << a.first << ", " << a.second << "}, " << a.idx << ", " << a.vertical;
}
void solve(){
int n, m; cin >> n >> m;
vector<int>a(n+2, -oo), b(m+2, -oo);
for (int i = 1; i<=n; i++) cin >> a[i];
for (int i = 1; i<=m; i++) cin >> b[i];
multiset<T>s;
set<int>aliveA, aliveB;
aliveA.insert(oo); aliveB.insert(oo);
aliveA.insert(-oo); aliveB.insert(-oo);
aliveA.insert(1); aliveB.insert(1);
for (int i = 1; i<n; i++) {
aliveA.insert(i+1);
s.insert({a[i+1]-a[i], 1LL, i+1, 0});
}
for (int i = 1; i<m; i++) {
aliveB.insert(i+1);
s.insert({b[i+1]-b[i], 1LL, i+1, 1});
}
// debug(s);
pair<int, int> now = {n, m};
int ans = 0;
while (s.size()){
auto it = *s.rbegin();
if (!it.vertical){
a.swap(b);
aliveA.swap(aliveB);
}
//B
auto it2 = aliveB.lower_bound(it.idx);
auto R = *(++it2);
--it2;
auto L = *(--it2);
if (L != -oo && R != oo) s.insert({b[R] - b[L], R-L, R, it.vertical});
if (R != oo) s.erase(s.find({b[R] - b[it.idx], R-it.idx, R, it.vertical}));
aliveB.erase(it.idx);
//
if (!it.vertical){
a.swap(b);
aliveA.swap(aliveB);
}
if (!it.vertical && now.first == it.idx){
ans += b[now.second] * it.second;
now.first -= it.second;
}
if (it.vertical && now.second == it.idx) {
ans += a[now.first] * it.second;
now.second -= it.second;
}
// debug(it, now);
s.erase(s.find(it));
}
ans += (now.first-1)*b[1];
ans += (now.second-1)*a[1];
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
468 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 |
316 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 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 |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
229 ms |
11500 KB |
Output is correct |
5 |
Correct |
82 ms |
10700 KB |
Output is correct |
6 |
Correct |
50 ms |
4684 KB |
Output is correct |
7 |
Correct |
694 ms |
27660 KB |
Output is correct |
8 |
Correct |
640 ms |
27696 KB |
Output is correct |
9 |
Correct |
689 ms |
27780 KB |
Output is correct |
10 |
Correct |
657 ms |
27708 KB |
Output is correct |
11 |
Correct |
171 ms |
27600 KB |
Output is correct |
12 |
Correct |
675 ms |
27728 KB |
Output is correct |
13 |
Correct |
671 ms |
27836 KB |
Output is correct |
14 |
Correct |
326 ms |
27696 KB |
Output is correct |
15 |
Correct |
166 ms |
27920 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
0 ms |
320 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
316 KB |
Output is correct |
23 |
Correct |
1 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 |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
468 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 |
316 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 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 |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
316 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
229 ms |
11500 KB |
Output is correct |
33 |
Correct |
82 ms |
10700 KB |
Output is correct |
34 |
Correct |
50 ms |
4684 KB |
Output is correct |
35 |
Correct |
694 ms |
27660 KB |
Output is correct |
36 |
Correct |
640 ms |
27696 KB |
Output is correct |
37 |
Correct |
689 ms |
27780 KB |
Output is correct |
38 |
Correct |
657 ms |
27708 KB |
Output is correct |
39 |
Correct |
171 ms |
27600 KB |
Output is correct |
40 |
Correct |
675 ms |
27728 KB |
Output is correct |
41 |
Correct |
671 ms |
27836 KB |
Output is correct |
42 |
Correct |
326 ms |
27696 KB |
Output is correct |
43 |
Correct |
166 ms |
27920 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
324 KB |
Output is correct |
48 |
Correct |
0 ms |
320 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
316 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
324 KB |
Output is correct |
55 |
Correct |
200 ms |
12812 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
12 ms |
1492 KB |
Output is correct |
58 |
Correct |
39 ms |
4084 KB |
Output is correct |
59 |
Correct |
758 ms |
28872 KB |
Output is correct |
60 |
Correct |
746 ms |
28876 KB |
Output is correct |
61 |
Correct |
732 ms |
28892 KB |
Output is correct |
62 |
Correct |
810 ms |
28888 KB |
Output is correct |
63 |
Correct |
160 ms |
28744 KB |
Output is correct |
64 |
Correct |
797 ms |
28892 KB |
Output is correct |
65 |
Correct |
707 ms |
28892 KB |
Output is correct |
66 |
Correct |
238 ms |
28996 KB |
Output is correct |
67 |
Correct |
170 ms |
29152 KB |
Output is correct |