Problem:
Farm Village
There is a village along a road. This village has $N$ houses numbered $1$ to $N$ in order along the road. Each house has a field that can make up to two units of the crop and needs just one unit of the crop. The total cost to distribute one unit of the crop to each house is the summation of carrying costs and growing costs.
The carrying cost: The cost to carry one unit of the crop between the $i$-th house and the ($i+1$)-th house is $d_i$. It takes the same cost in either direction to carry.
The growing cost: The cost to grow one unit of the crop in the $i$-th house's field is $g_i$.
Your task is to calculate the minimum total cost to supply one unit of the crop to each house.
Input
The input consists of a single test case formatted as follows.
$N$
$d_1$ ... $d_{N-1}$
$g_1$ ... $g_{N}$
The first line consists of an integer $N$ ($2 \leq 200,000$), which is the number of the houses. The second line consists of $N-1$ integers separated by spaces. The $i$-th integer $d_i$ ($1 \leq d_i \leq 10^9$, $1 \leq i \leq N-1$)represents the carrying cost between the $i$-th and the ($i+1$)-th houses. The third line consists of $N$ integers separated by spaces. The $i$-th integer $g_i$ ($1 \leq g_i \leq 10^9$, $1 \leq i \leq N$) represents the growing cost of the $i$-th house's field.
Output
Print the minimum cost to supply one unit of the crop to each house.
Sample Input 1
2
3
1 5
Output for Sample Input 1
5
Sample Input 2
3
100 100
1 2 3
Output for Sample Input 2
6
Sample Input 3
4
1 2 3
1 1 100 100
Output for Sample Input 3
12

Submitted solutions:

code_snippet: #include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define re register
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define MAXN 4000007
#define ll long long
#define mo 19930726
using namespace std;
typedef unsigned long long ull;
priority_queue<ll,vector<ll>,greater<ll> > pl,pr;
ll d[2000001],g[200001];
ll ans,n;
int main()
{
	cin>>n;
	for(re int i=2;i<=n;i++){
		cin>>d[i];
		d[i]+=d[i-1];
	}
	for(re int i=1;i<=n;i++){
		cin>>g[i];
	}
	ll tp;
	for(re int i=1;i<=n;i++){
		int package=2;
		while(package--){
			if(!pl.empty()&&pl.top()+d[i]+g[i]<0){
				tp=pl.top();
				pl.pop();
				ans+=tp+d[i]+g[i];
				pl.push(-(d[i]+g[i]));
				pr.push(-(tp+d[i]+d[i]));
			} else pr.push(g[i]-d[i]);
		}
		tp=pr.top();
		pr.pop();
		ans+=tp+d[i];
		pl.push(-(tp+d[i]+d[i]));
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 11388
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
#include <queue>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)
using ll = long long;
template<class E> using V = vector<E>;

template<class Val, class Cmp>
struct OffsetPriQue {
    priority_queue<Val, vector<Val>, Cmp> que;
    Val offset = 0;
    void push(Val val){
        que.push(val - offset);
    }
    void add(Val val){
        offset += val;
    }
    Val top(){
        return offset + que.top();
    }
    Val pop(){
        auto res = top();
        que.pop();
        return res;
    }
};

void testcase(){
    OffsetPriQue<ll, less<ll>> queL;
    OffsetPriQue<ll, greater<ll>> queR;
    ll N; cin >> N;
    V<ll> W(N-1); REP(i,N-1) cin >> W[i];
    V<ll> G(N); REP(i,N) cin >> G[i];

    ll ans = G[0];
    queL.push(G[0]);
    queR.push(G[0]);

    for(int i=1; i<N; i++){
        queL.add(-W[i-1]);
        queR.add(W[i-1]);
        if(queL.top() <= G[i]){
            queR.push(G[i]);
            queR.push(G[i]);
            ans += queR.top();
            queL.push(queR.pop());
        } else {
            queL.push(G[i]);
            queL.push(G[i]);
            ans += G[i] * 2;
            ans += -queL.top();
            queR.push(queL.pop());
        }
    }
    cout << ans << "\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    testcase();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 9876
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005;
const ll INF=15LL<<58;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N;cin>>N;
    ll ad=0;
    priority_queue<ll> L;
    priority_queue<ll,vector<ll>,greater<ll>> R;
    ll ans=0;
    
    vector<ll> A(N),B(N);
    for(int i=1;i<N;i++) cin>>A[i];
    for(int i=0;i<N;i++){
        cin>>B[i];
        ans+=B[i];
    }
    
    ad=0;
    L.push(B[0]);
    L.push(-INF);
    R.push(B[0]);
    R.push(INF);
    
    for(int i=1;i<N;i++){
        ad+=A[i];
        
        {
            ll a=L.top()-ad,b=R.top()+ad;
            ans+=min({-a+B[i],b-B[i],0LL});
        }
        
        {
            ll a=L.top()-ad,b=R.top()+ad,c=B[i];
            if(c<a){
                L.pop();
                L.push(c+ad);
                R.push(a-ad);
            }else{
                R.push(c-ad);
            }
        }
        
        {
            ll a=L.top()-ad,b=R.top()+ad,c=B[i];
            
            if(b<c){
                R.pop();
                R.push(c-ad);
                L.push(b+ad);
            }else{
                L.push(c+ad);
            }
        }
    }
    
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 10512
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int main(){
    int n;
    cin >> n;
    vl d(n-1), g(n);
    ll ans = 0;
	rep(i,n-1){
        cin >> d[i];
    }
    rep(i,n){
        cin >> g[i];
        ans += g[i];
    }
	priority_queue<ll> l;
	priority_queue<ll,vector<ll>,greater<ll> > r;
    l.push(g[0]),r.push(g[0]);
    ll lsum = 0, rsum = 0;
	rep(i,n-1){
		lsum += d[i], rsum += d[i];
		if(g[i+1] < l.top()-lsum){
			l.push(g[i+1]+lsum), l.push(g[i+1]+lsum);
			ll val = l.top() - lsum;
			ans -= val - g[i+1];
			l.pop();
			r.push(val-rsum);
		}else if(r.top()+rsum < g[i+1]){
			r.push(g[i+1]-rsum), r.push(g[i+1]-rsum);
			ll val = r.top() + rsum;
            ans -= g[i+1] - val;
			r.pop();
			l.push(val+lsum);
		}else{
			l.push(g[i+1]+lsum);
			r.push(g[i+1]-rsum);
		}
	}
	cout << ans << "\n";
}
accuracy: 1
time_ms: 120
memory_kb: 9496
final_rank: 3