#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;
#define maxn 100005
int h,w;
ll a[maxn],b[maxn];
priority_queue<iii,vector<iii>,greater<iii>> pqh,pqv;
set<int> s1,s2;
int main(){
sf("%d%d",&h,&w);
for(int i=1;i<=h;++i){
sf("%lld",&a[i]);
s1.insert(i);
if(i>1)pqh.push({a[i-1]-a[i],{i-1,i}});
}
for(int i=1;i<=w;++i){
sf("%lld",&b[i]);
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);
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:21:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | sf("%d%d",&h,&w);
| ^
kyoto.cpp:23:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | sf("%lld",&a[i]);
| ^
kyoto.cpp:28:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | sf("%lld",&b[i]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
2 ms |
448 KB |
Output is correct |
10 |
Correct |
2 ms |
420 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
424 KB |
Output is correct |
17 |
Correct |
1 ms |
432 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 |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
308 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 |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
113 ms |
6304 KB |
Output is correct |
5 |
Correct |
62 ms |
6208 KB |
Output is correct |
6 |
Correct |
39 ms |
3276 KB |
Output is correct |
7 |
Correct |
371 ms |
15156 KB |
Output is correct |
8 |
Correct |
375 ms |
15128 KB |
Output is correct |
9 |
Correct |
329 ms |
15132 KB |
Output is correct |
10 |
Correct |
361 ms |
15044 KB |
Output is correct |
11 |
Correct |
134 ms |
15148 KB |
Output is correct |
12 |
Correct |
372 ms |
15160 KB |
Output is correct |
13 |
Correct |
312 ms |
15216 KB |
Output is correct |
14 |
Correct |
258 ms |
15112 KB |
Output is correct |
15 |
Correct |
126 ms |
15324 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
308 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
2 ms |
448 KB |
Output is correct |
10 |
Correct |
2 ms |
420 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
424 KB |
Output is correct |
17 |
Correct |
1 ms |
432 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 |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
308 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 |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
113 ms |
6304 KB |
Output is correct |
33 |
Correct |
62 ms |
6208 KB |
Output is correct |
34 |
Correct |
39 ms |
3276 KB |
Output is correct |
35 |
Correct |
371 ms |
15156 KB |
Output is correct |
36 |
Correct |
375 ms |
15128 KB |
Output is correct |
37 |
Correct |
329 ms |
15132 KB |
Output is correct |
38 |
Correct |
361 ms |
15044 KB |
Output is correct |
39 |
Correct |
134 ms |
15148 KB |
Output is correct |
40 |
Correct |
372 ms |
15160 KB |
Output is correct |
41 |
Correct |
312 ms |
15216 KB |
Output is correct |
42 |
Correct |
258 ms |
15112 KB |
Output is correct |
43 |
Correct |
126 ms |
15324 KB |
Output is correct |
44 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
308 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
304 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
308 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
132 ms |
7256 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
9 ms |
1108 KB |
Output is correct |
58 |
Correct |
28 ms |
2380 KB |
Output is correct |
59 |
Correct |
426 ms |
16308 KB |
Output is correct |
60 |
Correct |
342 ms |
15956 KB |
Output is correct |
61 |
Correct |
354 ms |
15912 KB |
Output is correct |
62 |
Correct |
369 ms |
16084 KB |
Output is correct |
63 |
Correct |
150 ms |
16000 KB |
Output is correct |
64 |
Correct |
331 ms |
16144 KB |
Output is correct |
65 |
Correct |
423 ms |
16184 KB |
Output is correct |
66 |
Correct |
172 ms |
16308 KB |
Output is correct |
67 |
Correct |
126 ms |
15492 KB |
Output is correct |