#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=1e5+9,inf=2e9;
int n,m,ans,a[N],b[N],dp[N][N];
bool cmp(pair<int,int> a,pair<int,int> b){
return a.F*b.S<b.F*a.S;
}
int solve(vector<int> A,vector<int> B){
int n=A.size(),m=B.size(),ida=0,idb=0,res=0;
f_(i,n-2,0) minm(A[i],A[i+1]);
f_(i,m-2,0) minm(B[i],B[i+1]);
while(ida+1<n || idb+1<m){
pair<int,int> mn={inf,1};
int b=0;
f(i,ida+1,n) if(cmp({A[i]-A[ida],i-ida},mn)) mn={A[i]-A[ida],i-ida};
f(i,idb+1,m) if(cmp({B[i]-B[idb],i-idb},mn)) b=1;
if(b==0){
ida++;
res+=B[idb];
}
else{
idb++;
res+=A[ida];
}
}
return res;
}
int32_t main(){
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>m;
int ida=1,idb=1;
f(i,1,n+1){
cin>>a[i];
if(a[i]<a[ida]) ida=i;
}
f(i,1,m+1){
cin>>b[i];
if(b[i]<b[idb]) idb=i;
}
vector<int> A,B;
f_(i,ida,1) A.pb(a[i]);
f_(i,idb,1) B.pb(b[i]);
ans+=solve(A,B);
A.clear();
B.clear();
f(i,ida,n+1) A.pb(a[i]);
f(i,idb,m+1) B.pb(b[i]);
ans+=solve(A,B);
cout<<ans;
}
Compilation message
/tmp/ccL4Heyn.o: in function `main':
kyoto.cpp:(.text.startup+0x32): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccL4Heyn.o
kyoto.cpp:(.text.startup+0x39): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
kyoto.cpp:(.text.startup+0x40): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
kyoto.cpp:(.text.startup+0x4b): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
kyoto.cpp:(.text.startup+0x5b): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccL4Heyn.o
kyoto.cpp:(.text.startup+0x6a): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccL4Heyn.o
kyoto.cpp:(.text.startup+0x78): relocation truncated to fit: R_X86_64_PC32 against symbol `a' defined in .bss section in /tmp/ccL4Heyn.o
kyoto.cpp:(.text.startup+0x8e): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
kyoto.cpp:(.text.startup+0xc6): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccL4Heyn.o
kyoto.cpp:(.text.startup+0xcf): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccL4Heyn.o
kyoto.cpp:(.text.startup+0xdd): additional relocation overflows omitted from the output
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status