#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct choice{
int a, posl, posr;
choice(int a, int posl, int posr): a(a), posl(posl), posr(posr) {}
friend bool operator<(const choice &lhs, const choice &rhs){
ll l = 1LL * lhs.a * (rhs.posr - rhs.posl), r = 1LL * rhs.a * (lhs.posr - lhs.posl);
if (l != r) return l < r;
return lhs.posl < rhs.posl;
}
};
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<int> a(n), b(m);
for (int i=0; i<n; i++) cin >> a[i];
for (int j=0; j<m; j++) cin >> b[j];
const int MAXV = 1000000000;
set<choice> s;
vector<int> nxta(n), nxtb(m);
for (int i=1; i<n; i++){
nxta[i-1] = i;
s.emplace(a[i-1] - a[i], i-1, i);
}
nxta.back() = -1;
for (int i=1; i<m; i++){
nxtb[i-1] = i;
s.emplace(b[i-1] - b[i], i-1 + MAXV, i + MAXV);
}
nxtb.back() = -1;
ll result = 0;
int lasta = n-1, lastb = m-1;
while (s.size()){
choice c = *s.begin(); s.erase(s.begin());
if (c.posl < MAXV){
int nxtpos = nxta[c.posl] = nxta[c.posr];
nxta[c.posr] = -1;
if (nxtpos == -1){
result += b[lastb] * (c.posr - c.posl);
// cout << "B: " << lastb << " " << b[lastb] << "\n";
// cout << s.size() << "\n";
lasta = c.posl;
}
else{
// cout << "SUS: " << s.size();
s.erase(s.find(choice(a[c.posr] - a[nxtpos], c.posr, nxtpos)));
s.emplace(a[c.posl] - a[nxtpos], c.posl, nxtpos);
// cout << " " << s.size() << "\n";
}
}
else{
c.posl -= MAXV, c.posr -= MAXV;
int nxtpos = nxtb[c.posl] = nxtb[c.posr];
nxtb[c.posr] = -1;
if (nxtpos == -1){
result += a[lasta] * (c.posr - c.posl);
// cout << "A: " << lasta << " " << a[lasta] << "\n";
// cout << s.size() << "\n";
lastb = c.posl;
}
else{
// cout << "SUS: " << s.size();
s.erase(s.find(choice(b[c.posr] - b[nxtpos], c.posr + MAXV, nxtpos + MAXV)));
s.emplace(b[c.posl] - b[nxtpos], c.posl + MAXV, nxtpos + MAXV);
// cout << " " << s.size() << "\n";
}
}
}
cout << result << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
70 ms |
6524 KB |
Output is correct |
5 |
Correct |
39 ms |
5976 KB |
Output is correct |
6 |
Correct |
23 ms |
2908 KB |
Output is correct |
7 |
Correct |
329 ms |
15084 KB |
Output is correct |
8 |
Correct |
268 ms |
15304 KB |
Output is correct |
9 |
Correct |
327 ms |
15444 KB |
Output is correct |
10 |
Correct |
255 ms |
15444 KB |
Output is correct |
11 |
Correct |
78 ms |
15244 KB |
Output is correct |
12 |
Correct |
278 ms |
15320 KB |
Output is correct |
13 |
Correct |
262 ms |
15180 KB |
Output is correct |
14 |
Correct |
126 ms |
15296 KB |
Output is correct |
15 |
Correct |
98 ms |
15280 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
456 KB |
Output is correct |
25 |
Correct |
1 ms |
432 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |