Language: C++

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
ll a[100005],b[100005];
int n;
int main(){
	cin >> n;
	for(int i=0;i<n;i++) scanf("%lld",&a[i]);
	for(int i=0;i<n;i++) scanf("%lld",&b[i]);
	ll M = 0,Mval = 0; ll sidelazy = 0;
	set<pair<ll,ll>,greater<pair<ll,ll> > >S; ll lazy = 0;
	for(int i=0;i<n;i++){
	
		ll newM,newMval;
		if(M<=0){
			if(M != 0){
				S.insert(mp(M-sidelazy,-lazy)); 
			}
			sidelazy += b[i]-a[i]; M=b[i]-a[i]; lazy--;
		}
		else{
			ll cand = M,MIN = Mval+M,pre = M,val = Mval+M;
			for(set<pair<ll,ll>,greater<pair<ll,ll> > >::iterator it=S.begin();it!=S.end();it++){
				if(sidelazy+(*it).fi>0){
					if((*it).sc+lazy+1<0){
						if(MIN >= val){
							MIN = val;
							cand = min(cand,pre);
						}
					}
					else{
						if(MIN >= val- ((*it).sc+lazy+1) * (pre-(*it).fi-sidelazy)){
							MIN = val- ((*it).sc+lazy+1) * (pre-(*it).fi-sidelazy);
							cand = min(cand,(*it).fi+sidelazy);
						}
					}
					val -= ((*it).sc+lazy+1) * (pre-(*it).fi-sidelazy);
					pre = (*it).fi+sidelazy;
				}
				else{
					if((*it).sc+lazy+1<0){
						if(MIN >= val){
							MIN = val;
							cand = min(cand,pre);
						}
					}
					else{
						if(MIN >= val- ((*it).sc+lazy+1) * pre){
							MIN = val- ((*it).sc+lazy+1) * (pre);
							cand = min(cand,0);
						}
					}
					if((*it).sc+lazy-1<0){
						if(MIN >= val- ((*it).sc+lazy+1) * pre){
							MIN = val- ((*it).sc+lazy+1) * (pre);
							cand = min(cand,0);
						}
					}
					else{
						if(MIN >= val- ((*it).sc+lazy+1) * pre - ((*it).sc+lazy-1) * (-(*it).fi-sidelazy)){
							MIN = val- ((*it).sc+lazy+1) * pre - ((*it).sc+lazy-1) * (-(*it).fi-sidelazy);
							cand = min(cand,(*it).fi+sidelazy);
						}
					}
					break;
				}
			}
			M = cand;
			Mval = MIN; lazy--; vector<P>vec;
			for(set<pair<ll,ll>,greater<pair<ll,ll> > >::iterator it=S.begin();it!=S.end();){
				if(sidelazy+(*it).fi>0){
					ll a = (*it).fi, x = (*it).sc;
					S.erase(it++);
					if(a+sidelazy>=M);
					else{
						vec.pb(mp(a,x+2));
					}
				}
				else{
					ll a = (*it).fi, x = (*it).sc;
					S.erase(it);
					if(a+sidelazy>=M);
					else{
						if(0<M) S.insert(mp(-sidelazy,x+2));
						if(a+sidelazy<M) S.insert(mp(a,x));
					}
					break;
				}
			}
			sidelazy += b[i]-a[i]; M += b[i]-a[i]; for(int i=0;i<vec.size();i++) S.insert(vec[i]);
		
		}
		
	}
	if(M<=0){
		cout << Mval << endl;
	}
	else{
		ll pre = M;
		for(set<pair<ll,ll>,greater<pair<ll,ll> > >::iterator it=S.begin();it!=S.end();it++){
			if(sidelazy+(*it).fi>0){
				Mval -= ((*it).sc+lazy) * (pre-(*it).fi-sidelazy);
				pre = (*it).fi+sidelazy;
			}
			else{
				Mval -= ((*it).sc+lazy) * (pre);
				break;
			}
		}
		cout << Mval << endl;
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:80:67: error: no matching function for call to 'min(ll&, int)'
   80 |                                                         cand = min(cand,0);
      |                                                                ~~~^~~~~~~~
In file included from /usr/include/c++/14/bits/specfun.h:43,
                 from /usr/include/c++/14/cmath:3906,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:80:67: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   80 |                                                         cand = min(cand,0);
      |                                                                ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:80:67: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   80 |                                                         cand = min(cand,0);
      |                                                                ~~~^~~~~~~~
a.cc:86:67: error: no matching function for call to 'min(ll&, int)'
   86 |                                                         cand = min(cand,0);
      |                                                                ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:86:67: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   86 |                                                         cand = min(cand,0);
      |                                                                ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:86:67: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   86 |                                                         cand = min(cand,0);
      |                                                                ~~~^~~~~~~~