This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Super Idol的笑容
//    都没你的甜
//  八月正午的阳光
//    都没你耀眼
//  热爱105°C的你
// 滴滴清纯的蒸馏水
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define iii pair<double,ii>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m;
int arr[100005];
int brr[100005];
set<int> s1,s2;
priority_queue<iii,vector<iii>,greater<iii> > pq1,pq2;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	cin>>n>>m;
	rep(x,1,n+1) cin>>arr[x];
	rep(x,1,m+1) cin>>brr[x];
	
	rep(x,1,n+1) s1.insert(x);
	rep(x,1,m+1) s2.insert(x);
	
	rep(x,1,n) pq1.push({arr[x]-arr[x+1],ii(x,x+1)});
	rep(x,1,m) pq2.push({brr[x]-brr[x+1],ii(x,x+1)});
	
	pq1.push({1e18,ii(-1,-1)});
	pq2.push({1e18,ii(-1,-1)});
	
	int ans=0;
	while (sz(pq1)+sz(pq2)>2){
		if (pq1.top()<pq2.top()){
			ii p=pq1.top().se; pq1.pop();
			if (!s1.count(p.fi) || !s1.count(p.se)) continue;
			
			int l=p.fi;
			int r=-1;
			auto it=s1.find(p.se);
			if (it!=--s1.end()) r=*next(it);
			s1.erase(it);
			
			if (r!=-1) pq1.push({(double)(arr[l]-arr[r])/(r-l),ii(l,r)});
			else ans+=brr[*s2.rbegin()]*(p.se-p.fi);
		}
		else{
			ii p=pq2.top().se; pq2.pop();
			if (!s2.count(p.fi) || !s2.count(p.se)) continue;
			
			int l=p.fi;
			int r=-1;
			auto it=s2.find(p.se);
			if (it!=--s2.end()) r=*next(it);
			s2.erase(it);
			
			if (r!=-1) pq2.push({(double)(brr[l]-brr[r])/(r-l),ii(l,r)});
			else ans+=arr[*s1.rbegin()]*(p.se-p.fi);
		}
	}
	
	cout<<ans<<endl;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |