#pragma GCC optimize("Ofast")
#pragma GCC optimization (unroll-loops)
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC optimize("O3")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#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);
}
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int rd()
{
int s=0;
char ch=getchar(),last;
while(ch<'0'||ch>'9') last=ch,ch=getchar();
while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
return last=='-'?-s:s;
}
int num[100];
inline void rt(int x)
{
if(x<0) putchar('-'),x=-x;;
int len=0;
do num[len++]=x%10;while(x/=10);
while(len--) putchar(num[len]+'0');
}
void solve(){
int n=rd(),m=rd();
for(int i=1;i<=n;i++) A[i]=rd();
for(int i=1;i<=m;i++) B[i]=rd();
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;
}
}
rt(ans);
}
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:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization (unroll-loops)
|
kyoto.cpp: In function 'int main()':
kyoto.cpp:127:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp:128:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp: In function 'void solve()':
kyoto.cpp:84:18: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | return last=='-'?-s:s;
| ~~~~~~~~~^~~~~
kyoto.cpp:81:20: note: 'last' was declared here
81 | char ch=getchar(),last;
| ^~~~
kyoto.cpp:84:18: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | return last=='-'?-s:s;
| ~~~~~~~~~^~~~~
kyoto.cpp:81:20: note: 'last' was declared here
81 | char ch=getchar(),last;
| ^~~~
kyoto.cpp:84:18: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | return last=='-'?-s:s;
| ~~~~~~~~~^~~~~
kyoto.cpp:81:20: note: 'last' was declared here
81 | char ch=getchar(),last;
| ^~~~
kyoto.cpp:84:18: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | return last=='-'?-s:s;
| ~~~~~~~~~^~~~~
kyoto.cpp:81:20: note: 'last' was declared here
81 | char ch=getchar(),last;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
388 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
340 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 |
0 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
368 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 |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2644 KB |
Output is correct |
9 |
Correct |
5 ms |
2644 KB |
Output is correct |
10 |
Correct |
4 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
2644 KB |
Output is correct |
13 |
Correct |
4 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
4 ms |
4560 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
388 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
340 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 |
0 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
368 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 |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
1236 KB |
Output is correct |
33 |
Correct |
1 ms |
1236 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
5 ms |
2680 KB |
Output is correct |
36 |
Correct |
4 ms |
2644 KB |
Output is correct |
37 |
Correct |
5 ms |
2644 KB |
Output is correct |
38 |
Correct |
4 ms |
2644 KB |
Output is correct |
39 |
Correct |
3 ms |
2644 KB |
Output is correct |
40 |
Correct |
4 ms |
2644 KB |
Output is correct |
41 |
Correct |
4 ms |
2644 KB |
Output is correct |
42 |
Correct |
3 ms |
2644 KB |
Output is correct |
43 |
Correct |
4 ms |
4560 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
4 ms |
1876 KB |
Output is correct |
56 |
Correct |
0 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
1 ms |
724 KB |
Output is correct |
59 |
Correct |
6 ms |
3796 KB |
Output is correct |
60 |
Correct |
7 ms |
3920 KB |
Output is correct |
61 |
Correct |
6 ms |
3796 KB |
Output is correct |
62 |
Correct |
6 ms |
3796 KB |
Output is correct |
63 |
Correct |
6 ms |
5076 KB |
Output is correct |
64 |
Correct |
6 ms |
3796 KB |
Output is correct |
65 |
Correct |
6 ms |
3796 KB |
Output is correct |
66 |
Correct |
5 ms |
3796 KB |
Output is correct |
67 |
Correct |
5 ms |
5536 KB |
Output is correct |