#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
#define MP make_pair
const ll MAXN = 1e5;
ll n[2];
ll a[2][MAXN+100];
struct frac{
ll x,y;
bool operator < (const frac &p)const{
return (p.y * y < 0) ^ (x*p.y<p.x*y);
}
};
set <pair <frac,ll> > seg[2];
set <ll> s[2];
pair <frac,ll> cv(bool o,ll x1,ll x2){
return MP(frac({a[o][x2]-a[o][x1],x2-x1}),x2);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
cin>>n[0]>>n[1];
for (ll j = 0;j < 2;j ++){
for (ll i = 1;i <= n[j];i ++)cin>>a[j][i];
}
for (ll j = 0;j < 2;j ++){
for (ll i = 1;i <= n[j];i ++){
s[j].insert(i);
if (i>1)seg[j].insert(MP(frac({a[j][i]-a[j][i-1],1}),i));
}
}
ll ans = 0;
while (sz(s[0]) > 1 && sz(s[1]) > 1){
bool o = (*seg[0].rbegin()).fi < (*seg[1].rbegin()).fi;
ll x = (*seg[o].rbegin()).se;
// cout<<o<<' '<<x<<' '<<a[o][x]<<endl;
// seg[o].erase(*seg[o].rbegin());
// assert(s[o].find(x)!=s[o].end());
if (x==*s[o].rbegin()){
auto tmp = s[o].find(x);
seg[o].erase(cv(o,*prev(tmp),x));
ans += (x-*prev(tmp)) * a[!o][*s[!o].rbegin()];
}
else{
if (x==*s[o].begin()){
auto tmp = s[o].find(x);
seg[o].erase(cv(o,x,*next(tmp)));
}
else{
auto tmp = s[o].find(x);
seg[o].erase(cv(o,*prev(tmp),x));
seg[o].erase(cv(o,x,*next(tmp)));
ll x1 = *prev(tmp),x2 = *next(tmp);
seg[o].insert(cv(o,x1,x2));
}
}
s[o].erase(x);
}
if (sz(s[0])==1)ans += (*s[1].rbegin() - 1) * a[0][1];
else ans += (*s[0].rbegin() - 1) * a[1][1];
cout<<ans;
}
/*
6 8
3 5 1 9 4 8
3 6 7 1 8 2 5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
520 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
464 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
129 ms |
10204 KB |
Output is correct |
5 |
Correct |
70 ms |
9688 KB |
Output is correct |
6 |
Correct |
43 ms |
4444 KB |
Output is correct |
7 |
Correct |
414 ms |
24660 KB |
Output is correct |
8 |
Correct |
433 ms |
24720 KB |
Output is correct |
9 |
Correct |
477 ms |
24560 KB |
Output is correct |
10 |
Correct |
415 ms |
24536 KB |
Output is correct |
11 |
Correct |
149 ms |
24404 KB |
Output is correct |
12 |
Correct |
443 ms |
24700 KB |
Output is correct |
13 |
Correct |
414 ms |
24608 KB |
Output is correct |
14 |
Correct |
210 ms |
24656 KB |
Output is correct |
15 |
Correct |
88 ms |
24660 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
520 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
464 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
129 ms |
10204 KB |
Output is correct |
33 |
Correct |
70 ms |
9688 KB |
Output is correct |
34 |
Correct |
43 ms |
4444 KB |
Output is correct |
35 |
Correct |
414 ms |
24660 KB |
Output is correct |
36 |
Correct |
433 ms |
24720 KB |
Output is correct |
37 |
Correct |
477 ms |
24560 KB |
Output is correct |
38 |
Correct |
415 ms |
24536 KB |
Output is correct |
39 |
Correct |
149 ms |
24404 KB |
Output is correct |
40 |
Correct |
443 ms |
24700 KB |
Output is correct |
41 |
Correct |
414 ms |
24608 KB |
Output is correct |
42 |
Correct |
210 ms |
24656 KB |
Output is correct |
43 |
Correct |
88 ms |
24660 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
344 KB |
Output is correct |
50 |
Correct |
1 ms |
344 KB |
Output is correct |
51 |
Correct |
0 ms |
456 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
151 ms |
11664 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
10 ms |
1368 KB |
Output is correct |
58 |
Correct |
32 ms |
3868 KB |
Output is correct |
59 |
Correct |
456 ms |
25684 KB |
Output is correct |
60 |
Correct |
430 ms |
26108 KB |
Output is correct |
61 |
Correct |
478 ms |
25680 KB |
Output is correct |
62 |
Correct |
490 ms |
25876 KB |
Output is correct |
63 |
Correct |
110 ms |
25784 KB |
Output is correct |
64 |
Correct |
447 ms |
26092 KB |
Output is correct |
65 |
Correct |
392 ms |
25712 KB |
Output is correct |
66 |
Correct |
152 ms |
25856 KB |
Output is correct |
67 |
Correct |
96 ms |
26028 KB |
Output is correct |