#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int M=1e9+7;
struct buben{
int dt;
int dl;
int i;
int tp;
buben(int _a,int _b,int _c,int _t) : dt(_a),dl(_b),i(_c),tp(_t){}
const bool operator <(const buben &o) const{
return make_tuple(1ll*dt*o.dl,dl,i,tp)<make_tuple(1ll*dl*o.dt,o.dl,o.i,o.tp);
}
};
set<buben> st;
signed main(){
fast_resp;
int n,m;
cin>>n>>m;
vec<int>a(n);
vec<int>b(m);
for(auto &z : a) cin>>z;
for(auto &z : b) cin>>z;
vec<int> la(n),lb(m);
for(int i=0;i+1<n;i++) la[i]=i+1,st.insert(buben(a[i+1]-a[i],1,i,0));
for(int i=0;i+1<m;i++) lb[i]=i+1,st.insert(buben(b[i+1]-b[i],1,i,1));
ll ans=0;
while(sz(st)){
int i=st.rbegin()->i;
int dl=st.rbegin()->dl;
int typ=st.rbegin()->tp;
st.erase(--st.end());
// cout<<"W "<<dl<<' '<<i<<endl;
if(typ==0){
if(la[i]==sz(a)-1){
while(dl--){
ans+=b.back();
a.pop_back();
}
}
else{
int j=la[i];
st.erase(buben(a[la[j]]-a[j],la[j]-j,j,0));
la[i]=la[j];
st.insert(buben(a[la[i]]-a[i],la[i]-i,i,0));
}
}
else{
if(lb[i]==sz(b)-1){
while(dl--){
ans+=a.back();
b.pop_back();
}
}
else{
int j=lb[i];
st.erase(buben(b[lb[j]]-b[j],lb[j]-j,j,1));
lb[i]=lb[j];
// cout<<"WT "<<i<<' '<<lb[i]<<' '<<lb[i]-i<<endl;
st.insert(buben(b[lb[i]]-b[i],lb[i]-i,i,1));
}
}
}
cout<<ans;
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 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 |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
452 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
456 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
0 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 |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
96 ms |
6372 KB |
Output is correct |
5 |
Correct |
51 ms |
5816 KB |
Output is correct |
6 |
Correct |
36 ms |
2740 KB |
Output is correct |
7 |
Correct |
378 ms |
15060 KB |
Output is correct |
8 |
Correct |
385 ms |
15232 KB |
Output is correct |
9 |
Correct |
411 ms |
15132 KB |
Output is correct |
10 |
Correct |
382 ms |
14964 KB |
Output is correct |
11 |
Correct |
84 ms |
14944 KB |
Output is correct |
12 |
Correct |
382 ms |
15052 KB |
Output is correct |
13 |
Correct |
410 ms |
15152 KB |
Output is correct |
14 |
Correct |
135 ms |
15160 KB |
Output is correct |
15 |
Correct |
67 ms |
15156 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 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 |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
452 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
456 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
0 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 |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
96 ms |
6372 KB |
Output is correct |
33 |
Correct |
51 ms |
5816 KB |
Output is correct |
34 |
Correct |
36 ms |
2740 KB |
Output is correct |
35 |
Correct |
378 ms |
15060 KB |
Output is correct |
36 |
Correct |
385 ms |
15232 KB |
Output is correct |
37 |
Correct |
411 ms |
15132 KB |
Output is correct |
38 |
Correct |
382 ms |
14964 KB |
Output is correct |
39 |
Correct |
84 ms |
14944 KB |
Output is correct |
40 |
Correct |
382 ms |
15052 KB |
Output is correct |
41 |
Correct |
410 ms |
15152 KB |
Output is correct |
42 |
Correct |
135 ms |
15160 KB |
Output is correct |
43 |
Correct |
67 ms |
15156 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 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 |
111 ms |
7296 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
7 ms |
948 KB |
Output is correct |
58 |
Correct |
25 ms |
2388 KB |
Output is correct |
59 |
Correct |
415 ms |
16344 KB |
Output is correct |
60 |
Correct |
412 ms |
14716 KB |
Output is correct |
61 |
Correct |
420 ms |
15056 KB |
Output is correct |
62 |
Correct |
402 ms |
14460 KB |
Output is correct |
63 |
Correct |
78 ms |
15564 KB |
Output is correct |
64 |
Correct |
385 ms |
15372 KB |
Output is correct |
65 |
Correct |
413 ms |
16368 KB |
Output is correct |
66 |
Correct |
111 ms |
16196 KB |
Output is correct |
67 |
Correct |
76 ms |
16572 KB |
Output is correct |