stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define rep(i,n) for(int i=0;i<(n);i++)

#define repa(i,a,n) for(int i=(a);i<(n);i++)

#define rrep(i,n) for(int i=n;i>=0;i--)

#define rrepa(i,a,n) for(int i=n;i>=(a);i--)

#define all(a) (a).begin(),(a).end()

#define MOD 1000000007

#define cmax(a,b) a=max(a,b)

#define cmin(a,b) a=min(a,b)

#define vc vector



typedef long long ll;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<ll> vl;

typedef vector<vl> vvl;

typedef vector<double> vd;

typedef vector<vd> vvd;

typedef pair<int,int> pi;



int main(){



    int h,n;

    cin>>h>>n;

    vi a(n),b(n);

    rep(i,n)cin>>a[i]>>b[i];



    vvi dp(n,vi(h+1,INT_MAX));



    rep(i,n){

        rep(j,h+1){

            if(i)dp[i][j]=dp[i-1][j];

            if(j-a[i]>=0){

                dp[i][j]=cmin(dp[i][j],dp[i][j-a[i]]+b[i]);

            }else{

                dp[i][j]=cmin(dp[i][j],((j+a[i]-1)/a[i])*b[i]);

            }

            // cerr<<"dp["<<i<<""<<"]["<<j<<"] = "<<dp[i][j]<<"\n";

        }

    }



    cout<<dp[n-1][h]<<"\n";



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.466802599844484x):
#include <bits/stdc++.h>



using namespace std;



#define rep(i,n) for(int i=0;i<(n);i++)

#define repa(i,a,n) for(int i=(a);i<(n);i++)

#define rrep(i,n) for(int i=n;i>=0;i--)

#define rrepa(i,a,n) for(int i=n;i>=(a);i--)

#define all(a) (a).begin(),(a).end()

#define MOD 1000000007

#define cmax(a,b) a=max(a,b)

#define cmin(a,b) a=min(a,b)

#define vc vector



typedef long long ll;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<ll> vl;

typedef vector<vl> vvl;

typedef vector<double> vd;

typedef vector<vd> vvd;

typedef pair<int,int> pi;



int main(){



    int h,n;

    cin>>h>>n;

    vi a(n),b(n);

    rep(i,n)cin>>a[i]>>b[i];



    vi dp(h+1,INT_MAX);

    dp[0]=0;



    rep(i,n){

        rep(j,h+1){

            if(j-a[i]>=0){

                dp[j]=cmin(dp[j],dp[j-a[i]]+b[i]);

            }else{

                dp[j]=cmin(dp[j],b[i]);

            }

            // cerr<<"dp["<<j<<"] = "<<dp[j]<<"\n";

        }

    }



    cout<<dp[h]<<"\n";



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define rep(i, n) for(int i = 0; i < (int)n; ++i)

#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)

#define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i)



typedef long long ll;

typedef long double ld;



const ll INF = 1e18;

const int Inf = 1e9;

const double EPS = 1e-9;

const ll MOD = 1e9 + 7;



int main() {

    cin.tie(nullptr);

    ios::sync_with_stdio(0);

    int h, n;

    cin >> h >> n;

    vector<vector<int> > dp(n + 1, vector<int>(h + 1, Inf));

    dp[0][0] = 0;

    rep (i, n) {

        int a, b;

        cin >> a >> b;

        rep (j, h + 1) {

            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);

            int t = min(h, j + a);

            dp[i + 1][t] = min(dp[i + 1][t], dp[i + 1][j] + b);

        }

    }

    int res = Inf;

    rep (i, n + 1) res = min(res, dp[i][h]);

    cout << res << endl;

    

    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.6234825792973173x):
#include <bits/stdc++.h>



using namespace std;



#define rep(i, n) for(int i = 0; i < (int)n; ++i)

#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)

#define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i)



using ll = long long;



const int Inf = 1e9;



int main() {

    int h, n;

    cin >> h >> n;

    vector<int> a(n), b(n);

    rep (i, n) cin >> a[i] >> b[i];

    vector<int> dp(h + 10, Inf);

    dp[0] = 0;

    rep (i, h + 1) {

        if (dp[i] == Inf) continue;

        rep (j, n) {

            int t = i + a[j];

            if (t >= h) t = h;

            dp[t] = min(dp[t], dp[i] + b[j]);

        } 

    }

    cout << dp[h] << endl;



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



template <class T> inline bool chmin(T &a, T b) { /* {{{ */

  if (a > b) {

    a = b;

    return true;

  }

  return false;

} /* }}} */



int main() {

  int H, N;

  cin >> H >> N;

  vector<int> A(N), B(N);

  for (int i = 0; i < N; i++) {

    cin >> A[i] >> B[i];

  }

  vector<vector<int>> dp(N + 1, vector<int>(H + 1, 1e9));

  dp[0][0] = 0;

  for (int j = 1; j <= A[0]; j++) {

    if (j <= H) {

      dp[0][j] = B[0];

    }

  }

  for (int i = 0; i < N; i++) {

    for (int j = 0; j <= H; j++) {

      chmin(dp[i + 1][j], dp[i][j]);

      int v = dp[i][j] + B[i];

      if (j + A[i] < H) {

        chmin(dp[i][j + A[i]], v);

        chmin(dp[i + 1][j + A[i]], v);

      } else {

        chmin(dp[i][H], v);

        chmin(dp[i + 1][H], v);

      }

    }

  }

  cout << dp[N][H] << endl;



  return 0;

}

/* vim:set fdm=marker: */


// Faster C++ solution for the same problem (measured speedup: 1.679223734651852x):
#include <bits/stdc++.h>



using namespace std;



template <class T> inline bool chmin(T &a, T b) { /* {{{ */

  if (a > b) {

    a = b;

    return true;

  }

  return false;

} /* }}} */



int main() {

  int H, N;

  cin >> H >> N;

  vector<int> A(N), B(N);

  for (int i = 0; i < N; i++) {

    cin >> A[i] >> B[i];

  }

  vector<int> dp(H + 1, 1e9);

  dp[0] = 0;

  for (int i = 0; i < N; i++) {

    for (int j = 0; j <= H; j++) {

      chmin(dp[min(j + A[i], H)], dp[j] + B[i]);

    }

  }

  cout << dp[H] << endl;



  return 0;

}

/* vim:set fdm=marker: */
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int N = 300000;

#define debug(a) cout << #a << " = " << a << endl;

#define pii pair<int,int>



int A[1000000];

int B[1000000];





int main(int argc, char const *argv[])

{

	int n;

	scanf("%d",&n);

	long long  ans = 0;

	for(int i = 0; i <= n;i++)scanf("%d",&A[i]);

	for(int i = 0; i < n;i++)scanf("%d",&B[i]);

	for(int i = 0; i < n;i++){

		int hala = min(A[i],B[i]);

		ans += hala;

		B[i] -= hala;

		A[i] -= hala;



		hala = min(A[i+1],B[i]);

		ans += hala;

		B[i] -= hala;

		A[i+1] -= hala;

	}

	printf("%lld\n",ans );

	

	

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.148878870113999x):
#include <cstdio>

#include <algorithm>

using namespace std;

int N;

int a[100005];

int b[100005];

int main() {

    scanf("%d",&N);

    for (int i = 0; i < N+1; i++) {

        scanf("%d",&a[i]);

    }

    for (int i = 0; i < N; i++) {

        scanf("%d",&b[i]);

    }

    long long ans = 0;

    for (int i = 0; i < N+1; i++) {

        if (i == N) {

            if (a[i] <= b[N-1]) {

                ans += a[i];

            } else {

                ans += b[N-1];

            }

            break;

        }



        if (i != 0 && b[i-1] > 0) {

            if (a[i] <= b[i-1]) {

                ans += a[i];

                a[i] = 0;

                b[i-1] -= a[i];



            } else {

                ans += b[i-1];

                a[i] -= b[i-1];

                b[i-1] = 0;

            }

        }

        if (a[i] <= b[i]) {

            b[i] -= a[i];

            ans += a[i];

            a[i] = 0;

        } else {

            a[i] -= b[i];

            ans += b[i];

            b[i] = 0;

        }

    }

    printf("%lld\n", ans);

    return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <vector>

using namespace std;



using i64 = long long;



#define REP(i, stop) for (int i = 0, i##_len = stop; i < i##_len; ++i)



signed main() {

	i64 n;

	scanf("%lld", &n);

	vector<i64> A(n + 1), B(n);

	REP(i, n + 1) {

		scanf("%lld", &A[i]);

	}

	REP(i, n) {

		scanf("%lld", &B[i]);

	}

	i64 s = 0;

	REP(i, n) {

		i64 m = A[i] < B[i] ? A[i] : B[i];

		s += m;

		B[i] -= m;

		m = A[i + 1] < B[i] ? A[i + 1] : B[i];

		s += m;

		A[i + 1] -= m;

	}

	printf("%lld", s);

}


// Faster C++ solution for the same problem (measured speedup: 4.845460282470355x):
#include <cstdio>

using namespace std;



using i64 = long long;



#define REP(i, stop) for (int i = 0, i##_len = stop; i < i##_len; ++i)



int A[100001];

signed main() {

	int n;

	scanf("%d", &n);

	REP(i, n + 1) {

		scanf("%d", &A[i]);

	}

	i64 s = 0;

	REP(i, n) {

		int b;

		scanf("%d", &b);

		int m = A[i] < b ? A[i] : b;

		s += m;

		b -= m;

		m = A[i + 1] < b ? A[i + 1] : b;

		s += m;

		A[i + 1] -= m;

	}

	printf("%lld", s);

}
// Slower C++ solution:
#include <cstdio>

#include <vector>

using namespace std;



using i64 = long long;



#define REP(i, stop) for (int i = 0, i##_len = stop; i < i##_len; ++i)



signed main() {

	i64 n;

	scanf("%lld", &n);

	vector<i64> A(n + 1);

	REP(i, n + 1) {

		scanf("%lld", &A[i]);

	}

	i64 s = 0;

	REP(i, n) {

		i64 b;

		scanf("%lld", &b);

		i64 m = A[i] < b ? A[i] : b;

		s += m;

		b -= m;

		m = A[i + 1] < b ? A[i + 1] : b;

		s += m;

		A[i + 1] -= m;

	}

	printf("%lld", s);

}


// Faster C++ solution for the same problem (measured speedup: 4.843584487477568x):
#include <cstdio>

using namespace std;



using i64 = long long;



#define REP(i, stop) for (int i = 0, i##_len = stop; i < i##_len; ++i)



int A[100001];

signed main() {

	int n;

	scanf("%d", &n);

	REP(i, n + 1) {

		scanf("%d", &A[i]);

	}

	i64 s = 0;

	REP(i, n) {

		int b;

		scanf("%d", &b);

		int m = A[i] < b ? A[i] : b;

		s += m;

		b -= m;

		m = A[i + 1] < b ? A[i + 1] : b;

		s += m;

		A[i + 1] -= m;

	}

	printf("%lld", s);

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

using namespace std;

int arr[1000000],arr1[1000000];

long long v = 0;

int main(){

    int a,b,c,i,n;

    //cin>>n>>a;

    cin>>n;

    for(i = 0;i <= n;i++){

        cin>>arr[i];

    }//城市

    for(i = 0;i < n;i++){

        cin>>arr1[i];

    }//英雄



    for(i = 0;i < n;i++){

        if(arr[i]>=arr1[i]){

            v = v+arr1[i];

            //cout<<v<<" 11111"<<endl;

        }

        else{

            v = v+arr[i];

           // cout<<v<<" 2222"<<endl;

            arr1[i] = arr1[i]-arr[i];

            if(arr1[i] >= arr[i+1]){

                v = v+arr[i+1];

                arr[i+1] = 0;

                //cout<<v<<" 3333"<<endl;

            }

            else{

                v = v+arr1[i];

                arr[i+1] = arr[i+1]-arr1[i];

                //cout<<v<<" 4444"<<endl;

            }

        }

    }

    cout<<v;

}


// Faster C++ solution for the same problem (measured speedup: 5.215186508295837x):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

typedef long long ll;

ll cun[1000000];

ll hero[1000000];

int main()

{

	int n;

	ll sum=0;

	scanf("%d",&n);

	for(int i=0;i<n+1;i++)

	{

		scanf("%lld",&cun[i]);

	}

	for(int i=0;i<n;i++)

	{

		scanf("%lld",&hero[i]);

	}

	

	for(int i=0;i<n;i++)

	{

		if(hero[i]>cun[i])

		{

			sum+=cun[i];

			hero[i]-=cun[i];

			cun[i]=0;

			//printf("heroi1=%d\n",hero[i]);

			

			if(cun[i+1]>hero[i])

			{

				sum+=hero[i];

			//	printf("heroi2=%d\n",hero[i]);

				cun[i+1]-=hero[i];

				hero[i]=0;

			}

			else

			{

				sum+=cun[i+1];

				hero[i]-=cun[i+1];

				cun[i+1]=0;

			}

		}

		else

		{

			sum+=hero[i];

			cun[i]-=hero[i];

		}

	//	printf("test%d %d\n",sum,hero[i]);

	}

	printf("%lld",sum);

}
// Slower C++ solution:
#include        <map>

#include      <cmath>

#include      <queue>

#include     <string>

#include     <cstdio>

#include     <vector>

#include    <cstring>

#include   <iostream>

#include  <algorithm>

using namespace std;

#define ll long long

const ll inf = 0x3f3f3f3f;

const int maxn = 1e5+5;

const ll mod = 1e9+7;



int a[maxn];

int b[maxn];



int main(){

    int n;

    scanf("%d",&n);

    for(int i=0;i<=n;i++){

        scanf("%d",&a[i]);

    }

    for(int i=0;i<n;i++){

        scanf("%d",&b[i]);

    }

    ll ans = 0;

    for(int i=0;i<n;i++){

        if(b[i]>a[i]){

            ans += a[i];

            b[i] -= a[i];

            if(b[i]>a[i+1]){

                ans += a[i+1];

                a[i+1] = 0;

                b[i] -= a[i+1];

            }else {

                ans += b[i];

                a[i+1] -= b[i];

                b[i] = 0;

            }

        }else {

            ans += b[i];

        }

    }

    printf("%lld\n",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.160876827720781x):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

typedef long long ll;

ll cun[1000000];

ll hero[1000000];

int main()

{

	int n;

	ll sum=0;

	scanf("%d",&n);

	for(int i=0;i<n+1;i++)

	{

		scanf("%lld",&cun[i]);

	}

	for(int i=0;i<n;i++)

	{

		scanf("%lld",&hero[i]);

	}

	

	for(int i=0;i<n;i++)

	{

		if(hero[i]>cun[i])

		{

			sum+=cun[i];

			hero[i]-=cun[i];

			cun[i]=0;

			//printf("heroi1=%d\n",hero[i]);

			

			if(cun[i+1]>hero[i])

			{

				sum+=hero[i];

			//	printf("heroi2=%d\n",hero[i]);

				cun[i+1]-=hero[i];

				hero[i]=0;

			}

			else

			{

				sum+=cun[i+1];

				hero[i]-=cun[i+1];

				cun[i+1]=0;

			}

		}

		else

		{

			sum+=hero[i];

			cun[i]-=hero[i];

		}

	//	printf("test%d %d\n",sum,hero[i]);

	}

	printf("%lld",sum);

}
// Slower C++ solution:
#include <cstdio>

#include <vector>

#include <iostream>

#include <cmath>

#define INF 500000+500

#define Max 0x3f3f3f3f

#define Mod 1000000007

typedef long long int ll;

using namespace std;

ll arr[INF],brr[INF];

ll sum1[INF];

int main() 

{

	ll n;

	cin >> n;

	ll t = n+1;

	for(int i=1; i<=t; i++) {

		cin >> arr[i];

	}

	ll cnt=0;

	for(int i=1; i<=n; i++) {

		cin >> brr[i];

		if(brr[i]<=arr[i])	cnt+=brr[i];

		else if(brr[i]<=arr[i]+arr[i+1]) {

			cnt += brr[i];

			arr[i+1] -= brr[i]-arr[i];

		}else{

			cnt += arr[i]+arr[i+1];

			arr[i+1] = 0;

		}

	}

	cout << cnt << endl;

	

	

	

	

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.221581328170306x):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

typedef long long ll;

ll cun[1000000];

ll hero[1000000];

int main()

{

	int n;

	ll sum=0;

	scanf("%d",&n);

	for(int i=0;i<n+1;i++)

	{

		scanf("%lld",&cun[i]);

	}

	for(int i=0;i<n;i++)

	{

		scanf("%lld",&hero[i]);

	}

	

	for(int i=0;i<n;i++)

	{

		if(hero[i]>cun[i])

		{

			sum+=cun[i];

			hero[i]-=cun[i];

			cun[i]=0;

			//printf("heroi1=%d\n",hero[i]);

			

			if(cun[i+1]>hero[i])

			{

				sum+=hero[i];

			//	printf("heroi2=%d\n",hero[i]);

				cun[i+1]-=hero[i];

				hero[i]=0;

			}

			else

			{

				sum+=cun[i+1];

				hero[i]-=cun[i+1];

				cun[i+1]=0;

			}

		}

		else

		{

			sum+=hero[i];

			cun[i]-=hero[i];

		}

	//	printf("test%d %d\n",sum,hero[i]);

	}

	printf("%lld",sum);

}
// Slower C++ solution:
#include<bits/stdc++.h>

#include<algorithm>

using namespace std;

int a[100002][2];

int main()

{



    int i,j,n,m;

    cin>>n;

    for(i=0; i<n+1; i++)

        cin>>a[i][0];

    for(i=0; i<n; i++)

        cin>>a[i][1];

    long long int sum=0;

    for(i=0; i<n; i++)

    {

        if(a[i][1]<=a[i][0])

            sum+=a[i][1];

        else

        {

            a[i][1]-=a[i][0];

            sum+=a[i][0];

            if(a[i+1][0]>=a[i][1])

            {

                a[i+1][0]-=a[i][1];

                sum+=a[i][1];

            }

            else

            {

                sum+=a[i+1][0];

                a[i+1][0]=0;

            }

        }

    }

    cout<<sum;

    }


// Faster C++ solution for the same problem (measured speedup: 5.220018507312212x):
#include <cstdio>



typedef long long int ll;



const int MAXN = 1e5 + 5;



int n;

ll ans;

ll A[MAXN], B[MAXN];



int main() {

    scanf("%d", &n);

    for (int i = 1; i <= n + 1; ++i) {

        scanf("%lld", &A[i]);

    }

    for (int i = 1; i <= n; ++i) {

        scanf("%lld", &B[i]);

        if (B[i] <= A[i])

            ans += B[i];

        else if (B[i] <= A[i] + A[i + 1]) {

            ans += B[i];

            A[i + 1] -= (B[i] - A[i]);

        }

        else {

            ans += (A[i] + A[i + 1]);

            A[i + 1] = 0;

        }

    }

    printf("%lld", ans);

    return 0;

}
// Slower C++ solution:
#include <iostream> 

using namespace std;

#include <cstdio>

#include <string>

#include <vector>

#include <queue>

#include <algorithm>

const int N = 1e5+10;

typedef long long ll;

ll a[N];

ll b[N];



int main()

{

	ll n;

	cin >> n;

	

	ll sum = 0;

	

	for (ll i = 0; i < n+1 ; i ++ ) cin >> a[i];

	

	for (ll i = 0; i < n; i ++ ) cin >> b[i];

	

	for (ll i = 0; i < n; i ++ )

	{

		if(a[i] < b[i])

		{

			sum += a[i];

			ll t = b[i] - a[i];

			if(t >= a[i+1])

			{

				sum += a[i+1];

				a[i+1] = 0;

			}

			else

			{

				sum += t;

				a[i+1] -= t;

			}

		}

		else

		{

			a[i] -= b[i];

			sum += b[i];

		}

	}

	

	cout << sum << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.226128138179309x):
#include <cstdio>



typedef long long int ll;



const int MAXN = 1e5 + 5;



int n;

ll ans;

ll A[MAXN], B[MAXN];



int main() {

    scanf("%d", &n);

    for (int i = 1; i <= n + 1; ++i) {

        scanf("%lld", &A[i]);

    }

    for (int i = 1; i <= n; ++i) {

        scanf("%lld", &B[i]);

        if (B[i] <= A[i])

            ans += B[i];

        else if (B[i] <= A[i] + A[i + 1]) {

            ans += B[i];

            A[i + 1] -= (B[i] - A[i]);

        }

        else {

            ans += (A[i] + A[i + 1]);

            A[i + 1] = 0;

        }

    }

    printf("%lld", ans);

    return 0;

}
// Slower C++ solution:
#include <cstdio>

#include<bits/stdc++.h>



using namespace std;



typedef long long int ll;

const int MAXN = 100005;



int n;

ll ans;

ll a[MAXN], B[MAXN];



int main() {

    scanf("%d", &n);

    for (int i = 1; i <= n + 1; ++i) 

	{

        scanf("%lld", &a[i]);

    }

    for (int i = 1; i < n + 1; ++i)

	{

        scanf("%lld", &B[i]);

        if (B[i] <= a[i])

            ans += B[i];

        else if (B[i] <= a[i] + a[i + 1]) 

		{

            ans += B[i];

            a[i + 1] -= (B[i] - a[i]);

        }

        else 

		{

            ans += (a[i] + a[i + 1]);

            a[i + 1] = 0;

        }

    }

    printf("%lld\n", ans);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.165028539622015x):
#include <cstdio>



typedef long long int ll;



const int MAXN = 1e5 + 5;



int n;

ll ans;

ll A[MAXN], B[MAXN];



int main() {

    scanf("%d", &n);

    for (int i = 1; i <= n + 1; ++i) {

        scanf("%lld", &A[i]);

    }

    for (int i = 1; i <= n; ++i) {

        scanf("%lld", &B[i]);

        if (B[i] <= A[i])

            ans += B[i];

        else if (B[i] <= A[i] + A[i + 1]) {

            ans += B[i];

            A[i + 1] -= (B[i] - A[i]);

        }

        else {

            ans += (A[i] + A[i + 1]);

            A[i + 1] = 0;

        }

    }

    printf("%lld", ans);

    return 0;

}
// Slower C++ solution:
/*zxj*/

#include<iostream> 

#include<string>

#include<cstring>

#include<algorithm>

#include<cstdio>

#include<cmath>

#include<set>

#include<map>

#include<vector>

#include<queue>

#include<sstream>

#define sf scanf

#define pf printf

#define pfn printf("\n");

#define pfk printf(" ");

#define pf0 printf("0");

#define pf1 printf("1");

#define ll long long

#define sfd(n) scanf("%d",&n);

#define sfdd(n,m) scanf("%d%d",&n,&m);

#define sfld(n) scanf("%lld",&n);

#define sfldd(n,m) scanf("%lld%lld",&n,&m);

#define sflf(n) scanf("%lf",&n);

#define sflff(n,m) scanf("%lf%lf",&n,&m);

#define sfc(n) scanf("%c",n);

#define sfcc(n,m) scanf("%c%c",n,m);

#define sfs(n) scanf("%s",n);

#define sfss(n,m) scanf("%s%s",n,m);

#define pfd(n) printf("%d",n);

#define pfld(n) printf("%lld",n);

#define pflf(n) printf("%lf",n);

#define pfc(n) printf("%c",n);

#define pfs(n) printf("%s",n);

using namespace std;

#define csh(a,n) memset(a,n,sizeof(a));

ll arr[1000000];

int main(){

	ll sum=0;

	ll n;

	cin>>n;

	for(ll i=0;i<n+1;i++){

		cin>>arr[i];

	}

	for(ll i=0;i<n;i++){

		ll a;

		cin>>a;

		if(a>arr[i]){

			a-=arr[i];

			sum+=arr[i];

			if(a>arr[i+1]){

				sum+=arr[i+1]; 

				arr[i+1]=0;

			}

			else{

				arr[i+1]-=a;

				sum+=a;

			}

		}

		else{

			sum+=a;

		}

	}

	cout<<sum<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 5.227330931962117x):
#include <cstdio>



typedef long long int ll;



const int MAXN = 1e5 + 5;



int n;

ll ans;

ll A[MAXN], B[MAXN];



int main() {

    scanf("%d", &n);

    for (int i = 1; i <= n + 1; ++i) {

        scanf("%lld", &A[i]);

    }

    for (int i = 1; i <= n; ++i) {

        scanf("%lld", &B[i]);

        if (B[i] <= A[i])

            ans += B[i];

        else if (B[i] <= A[i] + A[i + 1]) {

            ans += B[i];

            A[i + 1] -= (B[i] - A[i]);

        }

        else {

            ans += (A[i] + A[i + 1]);

            A[i + 1] = 0;

        }

    }

    printf("%lld", ans);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<algorithm>

#include<string.h>

using namespace std;

#define ll long long 

int main()

{

	ll n;

	cin>>n;

	ll a[111111];

	for(int i=0;i<n+1;i++)

	{

		cin>>a[i];

	}

	ll sum=0;

	ll b;

	for(int i=0;i<n;i++)

	{

		cin>>b;

		if(b>=a[i])

		{

			sum+=a[i];

			b-=a[i];

			if(b>=a[i+1])

			{

				sum+=a[i+1];

				a[i+1]=0;

			}

			else

			{

				sum+=b;

				a[i+1]-=b;

			}

		}

		else

		{

			sum+=b;

		}

	}

	cout<<sum<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.347521540034357x):
#include <cstdio>



typedef long long int ll;



const int MAXN = 1e5 + 5;



int n;

ll ans;

ll A[MAXN], B[MAXN];



int main() {

    scanf("%d", &n);

    for (int i = 1; i <= n + 1; ++i) {

        scanf("%lld", &A[i]);

    }

    for (int i = 1; i <= n; ++i) {

        scanf("%lld", &B[i]);

        if (B[i] <= A[i])

            ans += B[i];

        else if (B[i] <= A[i] + A[i + 1]) {

            ans += B[i];

            A[i + 1] -= (B[i] - A[i]);

        }

        else {

            ans += (A[i] + A[i + 1]);

            A[i + 1] = 0;

        }

    }

    printf("%lld", ans);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#include <string.h>

using namespace std;



#define pb push_back

#define int long long

const int N = 1e5+5;

int n, m, u, v;

int color[N];

bool visited[N], congthu = 1;

vector<int> adj[N];



void dfs(int u, int c)

{

    for (auto v: adj[u])

    {

        if(color[v] != -1 && color[v] != c) {

            congthu = 0;

        }

        color[v] = c;

    }

    for (auto v: adj[u])

    {

        if(!visited[v]) {

            visited[v] = 1;

            dfs(v, 1 - c);

        }

    }

}



signed main()

{

    cin >> n >> m;

    for (int i = 0; i < m; i++)

    {

        cin >> u >> v;

        adj[u].pb(v);

        adj[v].pb(u);

    }

    memset(color, -1, sizeof color);

    color[1] = 1;

    visited[1] = 1;

    dfs(1, 0);

    if (!congthu)

        cout << n*(n-1)/2 - m;

    else

    {

        int black = 0, white = 0;

        for (int i = 1; i <= n; i++)

        {

            if (color[i] == 1) black++;

            else if (color[i] == 0) white++;

        }

        cout << black*white-m;

    }

}

/*

6 5

1 2

2 3

3 4

4 5

5 6

*/


// Faster C++ solution for the same problem (measured speedup: 1.1454744999138218x):
#include <bits/stdc++.h>

using namespace std;



const int N = 110000;



vector<int> con[N];

int vis[N];



bool dfs(int u, int col) {

	vis[u] = col;

	for (int v : con[u]) {

		if (vis[v] && 3 - col != vis[v]) return false;

		if (!vis[v] && !dfs(v, 3 - col)) return false;

	}

	return true;

}



int main() {

	

	ios::sync_with_stdio(0);

	cin.tie(0), cout.tie(0);

	

	int n, m; cin >> n >> m;

	for (int i = 0; i < m; i++) {

		int u, v; cin >> u >> v;

		con[u].push_back(v), con[v].push_back(u);

	}

	

	if (!dfs(1, 1)) cout << (long long)n * (n - 1) / 2 - m << "\n";

	else {

		int cnt = 0;

		for (int i = 1; i <= n; i++) cnt += vis[i] == 1;

		cout << (long long)cnt * (n - cnt) - m << "\n";

	}

	

	return 0;

}