#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define fi first
#define se second
#define LINF 1023456789123456789
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<double,ii> iii;
int h,w;
ll a[100005],b[100005];
priority_queue<iii,vector<iii>,greater<iii>> pqh,pqv;
set<int> s1,s2;
inline ll rd(){
ll x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar_unlocked();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
int main(){
h=(int)rd(),w=(int)rd();
for(int i=1;i<=h;++i){
a[i]=rd();
s1.insert(i);
if(i>1)pqh.push({a[i-1]-a[i],{i-1,i}});
}
for(int i=1;i<=w;++i){
b[i]=rd();
s2.insert(i);
if(i>1)pqv.push({b[i-1]-b[i],{i-1,i}});
}
pqh.push({LINF,{-1,-1}});
pqv.push({LINF,{-1,-1}});
ll ans=0;
while(pqh.size()+pqv.size()>2){
if(pqh.top().fi<pqv.top().fi){
ii p=pqh.top().se;pqh.pop();
if(!s1.count(p.fi)||!s1.count(p.se))continue;//already removed adjacent thing
int l=p.fi,r=-1;
auto it=s1.find(p.se);
if(it!=--s1.end())r=*next(it);
s1.erase(it);
if(r!=-1){
pqh.push({(double)(a[l]-a[r])/(r-l),{l,r}});
}
else ans+=b[*(--s2.end())]*(p.se-p.fi);
}
else{
ii p=pqv.top().se;pqv.pop();
if(!s2.count(p.fi)||!s2.count(p.se))continue;//already removed adjacent thing
int l=p.fi,r=-1;
auto it=s2.find(p.se);
if(it!=--s2.end())r=*next(it);
s2.erase(it);
if(r!=-1){
pqv.push({(double)(b[l]-b[r])/(r-l),{l,r}});
}
else ans+=a[*(--s1.end())]*(p.se-p.fi);
}
}
pf("%lld\n",ans);
}
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
0 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 |
212 KB |
Output is correct |
28 |
Correct |
1 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 |
340 KB |
Output is correct |
4 |
Correct |
98 ms |
6056 KB |
Output is correct |
5 |
Correct |
55 ms |
5960 KB |
Output is correct |
6 |
Correct |
34 ms |
3020 KB |
Output is correct |
7 |
Correct |
299 ms |
14496 KB |
Output is correct |
8 |
Correct |
304 ms |
14320 KB |
Output is correct |
9 |
Correct |
306 ms |
14388 KB |
Output is correct |
10 |
Correct |
314 ms |
14356 KB |
Output is correct |
11 |
Correct |
116 ms |
14280 KB |
Output is correct |
12 |
Correct |
303 ms |
14388 KB |
Output is correct |
13 |
Correct |
294 ms |
14348 KB |
Output is correct |
14 |
Correct |
211 ms |
14384 KB |
Output is correct |
15 |
Correct |
104 ms |
14412 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
0 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 |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
98 ms |
6056 KB |
Output is correct |
33 |
Correct |
55 ms |
5960 KB |
Output is correct |
34 |
Correct |
34 ms |
3020 KB |
Output is correct |
35 |
Correct |
299 ms |
14496 KB |
Output is correct |
36 |
Correct |
304 ms |
14320 KB |
Output is correct |
37 |
Correct |
306 ms |
14388 KB |
Output is correct |
38 |
Correct |
314 ms |
14356 KB |
Output is correct |
39 |
Correct |
116 ms |
14280 KB |
Output is correct |
40 |
Correct |
303 ms |
14388 KB |
Output is correct |
41 |
Correct |
294 ms |
14348 KB |
Output is correct |
42 |
Correct |
211 ms |
14384 KB |
Output is correct |
43 |
Correct |
104 ms |
14412 KB |
Output is correct |
44 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
114 ms |
6472 KB |
Output is correct |
56 |
Correct |
1 ms |
420 KB |
Output is correct |
57 |
Correct |
8 ms |
980 KB |
Output is correct |
58 |
Correct |
25 ms |
2124 KB |
Output is correct |
59 |
Correct |
305 ms |
14368 KB |
Output is correct |
60 |
Correct |
319 ms |
14384 KB |
Output is correct |
61 |
Correct |
307 ms |
14384 KB |
Output is correct |
62 |
Correct |
307 ms |
14264 KB |
Output is correct |
63 |
Correct |
123 ms |
14436 KB |
Output is correct |
64 |
Correct |
310 ms |
14264 KB |
Output is correct |
65 |
Correct |
308 ms |
14496 KB |
Output is correct |
66 |
Correct |
153 ms |
14384 KB |
Output is correct |
67 |
Correct |
104 ms |
14332 KB |
Output is correct |