#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array
/* observation:
with a shit ton of inequalities:
we only go up in the middle if
(Ab-Ac)/(b-c)<(Bx-By)/(x-y)<(Aa-Ab)/(a-b)
*/
//ca compares a's
const int N=1e6+6000;
int A[N],B[N],aa[N],bb[N];
bool ca(int x,int y,int z) {
return (A[x]-A[y])*(x-z)<(A[x]-A[z])*(x-y);
}
bool cb(int x,int y,int z) {
return (B[x]-B[y])*(x-z)<(B[x]-B[z])*(x-y);
}
bool cab(int x,int y,int z,int w) {
return (A[x]-A[y])*(w-z)<(B[z]-B[w])*(y-x);
}
void solve(){
int n,m; cin>>n>>m;
for(int i=1;i<=n;i++) cin>>A[i];
for(int i=1;i<=m;i++) cin>>B[i];
int ta=0,tb=0;
aa[++ta]=1; bb[++tb]=1;
for(int i=2;i<=n;i++){
while(ta>1 && ca(i,aa[ta],aa[ta-1])) ta--;
aa[++ta]=i;
}
for(int i=2;i<=m;i++){
while(tb>1 && cb(i,bb[tb],bb[tb-1])) tb--;
bb[++tb]=i;
}
int x=1,y=1,ans=0;
while(x<ta || y<tb){
//if already taken all a's force take y
if(x==ta || (y<tb && cab(aa[x],aa[x+1],bb[y],bb[y+1]))){
int cost=(bb[y+1]-bb[y])*A[aa[x]]; y++;
ans+=cost;
}
else{
int cost=(aa[x+1]-aa[x])*B[bb[y]]; x++;
ans+=cost;
}
}
cout<<ans<<'\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t tStart = clock();
int local=0,multi=0,debug=1,tt=1;
if(local){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
if(multi) cin>>tt;
for(int i=1;i<=tt;i++){
if(debug && multi && local) cout<<"样例 "<<i<<'\n';
solve();
}
fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:63:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp:64:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
368 KB |
Output is correct |
4 |
Correct |
5 ms |
1236 KB |
Output is correct |
5 |
Correct |
5 ms |
1144 KB |
Output is correct |
6 |
Correct |
3 ms |
780 KB |
Output is correct |
7 |
Correct |
12 ms |
2644 KB |
Output is correct |
8 |
Correct |
12 ms |
2644 KB |
Output is correct |
9 |
Correct |
12 ms |
2644 KB |
Output is correct |
10 |
Correct |
12 ms |
2644 KB |
Output is correct |
11 |
Correct |
11 ms |
2644 KB |
Output is correct |
12 |
Correct |
12 ms |
2644 KB |
Output is correct |
13 |
Correct |
19 ms |
2712 KB |
Output is correct |
14 |
Correct |
12 ms |
2644 KB |
Output is correct |
15 |
Correct |
12 ms |
4436 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
368 KB |
Output is correct |
32 |
Correct |
5 ms |
1236 KB |
Output is correct |
33 |
Correct |
5 ms |
1144 KB |
Output is correct |
34 |
Correct |
3 ms |
780 KB |
Output is correct |
35 |
Correct |
12 ms |
2644 KB |
Output is correct |
36 |
Correct |
12 ms |
2644 KB |
Output is correct |
37 |
Correct |
12 ms |
2644 KB |
Output is correct |
38 |
Correct |
12 ms |
2644 KB |
Output is correct |
39 |
Correct |
11 ms |
2644 KB |
Output is correct |
40 |
Correct |
12 ms |
2644 KB |
Output is correct |
41 |
Correct |
19 ms |
2712 KB |
Output is correct |
42 |
Correct |
12 ms |
2644 KB |
Output is correct |
43 |
Correct |
12 ms |
4436 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
8 ms |
1876 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
3 ms |
724 KB |
Output is correct |
59 |
Correct |
18 ms |
3792 KB |
Output is correct |
60 |
Correct |
19 ms |
3796 KB |
Output is correct |
61 |
Correct |
18 ms |
3824 KB |
Output is correct |
62 |
Correct |
18 ms |
3860 KB |
Output is correct |
63 |
Correct |
17 ms |
5068 KB |
Output is correct |
64 |
Correct |
18 ms |
3788 KB |
Output is correct |
65 |
Correct |
18 ms |
3788 KB |
Output is correct |
66 |
Correct |
17 ms |
3820 KB |
Output is correct |
67 |
Correct |
18 ms |
5580 KB |
Output is correct |