stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h> 

using namespace std;



typedef long long ll;

#define pa pair<int, int> 

#define pal pair<long long, long long>

#define pad pair<double, double>

#define pb push_back

#define mp make_pair

#define VIN(v) for(int64_t i=0;i<(v).size();i++){cin >> (v).at(i);}



typedef vector<bool> bvec;

typedef vector<int> ivec;

typedef vector<long long> lvec;

typedef vector<double> dvec;

typedef vector<pa> pvec;



typedef vector< vector<bool> > bmat;

typedef vector< vector<int> > imat;

typedef vector<string> svec;

typedef vector< vector<string> > smat;

const ll infll = (1LL << 60) - 1;

const int inf = (1 << 30) - 1;



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int  h, n;

    cin >> h >> n;

    ivec a(n), b(n);

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

    ivec dp(h+1, inf);

    dp[0] = 0;

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

        for (int j = 0; j < n; j++) dp[i] = fmin(dp[i], dp[fmax(0, i-a[j])] + b[j]);

    }

    cout << dp[h] << endl;



}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h> 

using namespace std;



typedef long long ll;

#define pa pair<int, int> 

#define pal pair<long long, long long>

#define pad pair<double, double>

#define pb push_back

#define mp make_pair

#define VIN(v) for(int64_t i=0;i<(v).size();i++){cin >> (v).at(i);}



typedef vector<bool> bvec;

typedef vector<int> ivec;

typedef vector<long long> lvec;

typedef vector<double> dvec;

typedef vector<pa> pvec;



typedef vector< vector<bool> > bmat;

typedef vector< vector<int> > imat;

typedef vector<string> svec;

typedef vector< vector<string> > smat;

const ll infll = (1LL << 60) - 1;

const int inf = (1 << 30) - 1;



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int  h, n;

    cin >> h >> n;

    ivec a(n), b(n);

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

    ivec dp(h+1, inf);

    dp[0] = 0;

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

        for (int j = 0; j < n; j++) dp[i] = min(dp[i], dp[max(0, i-a[j])] + b[j]);

    }

    cout << dp[h] << endl;



}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

 

void chmin(int& a, int b){

    a = min(a, b);

}

 

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(100000, 1e9);

    dp[0] = 0;

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

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

    }

    cout << dp[H] << endl;

    return 0;}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h> 

using namespace std;



typedef long long ll;

#define pa pair<int, int> 

#define pal pair<long long, long long>

#define pad pair<double, double>

#define pb push_back

#define mp make_pair

#define VIN(v) for(int64_t i=0;i<(v).size();i++){cin >> (v).at(i);}



typedef vector<bool> bvec;

typedef vector<int> ivec;

typedef vector<long long> lvec;

typedef vector<double> dvec;

typedef vector<pa> pvec;



typedef vector< vector<bool> > bmat;

typedef vector< vector<int> > imat;

typedef vector<string> svec;

typedef vector< vector<string> > smat;

const ll infll = (1LL << 60) - 1;

const int inf = (1 << 30) - 1;



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int  h, n;

    cin >> h >> n;

    ivec a(n), b(n);

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

    ivec dp(h+1, inf);

    dp[0] = 0;

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

        for (int j = 0; j < n; j++) dp[i] = min(dp[i], dp[max(0, i-a[j])] + b[j]);

    }

    cout << dp[h] << endl;



}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h> 



using namespace std;

using ll=long long;

using ull=unsigned long long;

using pii=pair<int,int>;



#define INF LONG_MAX

#define MOD 1000000007

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

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

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



int main(){

  ios::sync_with_stdio(false);

  cin.tie(0);



  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<ll>>dp(N+1,vector<ll>(H+10001,INT_MAX));

  dp[0][0]=0;

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

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

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

      if(j-A[i-1]>=0)dp[i][j]=min(dp[i-1][j],dp[i][j-A[i-1]]+B[i-1]);

    }

  }

  ll ans=LONG_MAX;

  for(int i=H;i<=H+10000;i++){

    ans=min(dp[N][i],ans);

  }

  cout<<ans<<endl;

  return 0;

}




// Optimized solution to the same problem (faster):
#include <bits/stdc++.h> 



using namespace std;

using ll=long long;

using ull=unsigned long long;

using pii=pair<int,int>;



#define INF LONG_MAX

#define MOD 1000000007

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

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

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



int main(){

  ios::sync_with_stdio(false);

  cin.tie(0);



  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<ll>>dp(N+1,vector<ll>(H+1,LONG_MAX/2));

  dp[0][0]=0;

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

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

      dp[i+1][j]=min(dp[i][j],dp[i+1][max(j-A[i],0)]+B[i]);

    }

  }



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

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



const int INF = 1000000000;



int main() {

	int h, n; cin >> h >> n;

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

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

		cin >> a.at(i) >> b.at(i);

	}

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

	dp[0][0] = 0;

	for (int j = 1; j <= h; j++) {

		dp[0][j] = INF;

	}

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

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

			dp.at(i).at(j) = min( dp.at(i - 1).at(j), dp.at(i).at( max(j - a.at(i - 1), 0 ) )+ b.at(i - 1) );

            dp[i][j] = min( dp[i - 1][j], dp[i][max(j - a[i - 1],0)]+ b[i - 1] );

		}

	}

	cout << dp.at(n).at(h) << endl;

}


// Optimized solution to the same problem (faster):
/*

 * 3.cpp

 * Copyright (C) 2020 itachi <itachi@itachi-Z510>

 *

 * Distributed under terms of the MIT license.

 */



#include <bits/stdc++.h>



using namespace std;



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));

   /* for (auto v:dp) { */

   /*     for (auto val:v) cout << val << ' ';cout << endl; */

   /* } */

   for (int j=1;j<=h;j++) {

       dp[0][j] = INT_MAX;

   }

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

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

           dp[i][j] = min(dp[i-1][j], dp[i][max(0,j-a[i-1])] + b[i-1]);

       }

   }

   cout << dp[n][h] << endl;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<algorithm>

#include<cmath>

#include<cstdio>

#include<functional>

#include<iomanip>

#include<iostream>

#include<map>

#include<numeric>

#include<queue>

#include<set>

#include<string>

#include<utility>

#include<vector>



using namespace std;

typedef long long ll;

typedef unsigned long long ull;

const ll MOD = 1000000007;

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

#define repl(i,s,e) for(int i=s;i<e;i++)

#define reple(i,s,e) for(int i=s;i<=e;i++)

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

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



int main()

{	

	int H, N;

	cin >> H >> N;



	vector<int> A(N);

	vector<int> B(N);



	rep(i, N) cin >> A[i] >> B[i];



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



	reple(i, 0, N) dp[i][0] = 0;



	rep(i, N)

	{

		reple(j, 0, H)

		{

			dp[i + 1][j] = min(dp[i][j], dp[i + 1][max(j - A[i], 0)] + B[i]);

		}

	}



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



	return 0;

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cmath>

#include<cstdio>

#include<functional>

#include<iomanip>

#include<iostream>

#include<map>

#include<numeric>

#include<queue>

#include<set>

#include<string>

#include<utility>

#include<vector>



using namespace std;

typedef long long ll;

typedef unsigned long long ull;

const ll MOD = 1000000007;

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

#define repl(i,s,e) for(int i=s;i<e;i++)

#define reple(i,s,e) for(int i=s;i<=e;i++)

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

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



template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

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);

	rep(i, N) cin >> A[i] >> B[i];



	//dp[i] := 体力をi以下にするために必要な魔力の最小値

	vector<ll> dp(H + 1, 1000000000000);

	dp[0] = 0;



	reple(i, 1, H)

	{

		rep(j, N)

		{

			int h = max(i - A[j], 0);



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

		}

	}



	cout << dp[H] << endl;



	return 0;

}	

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ALL(v) (v).begin(),(v).end()

#define REP(i,p,n) for(int i=p;i<(int)(n);++i)

#define rep(i,n) REP(i,0,n)

#define SZ(x) ((int)(x).size())

#define debug(x) cerr << #x << ": " << x << '\n'



typedef long long int Int;

typedef pair<int,int> P;

using ll = long long;

using VI = vector<int>;

template<class T>bool chmax(T &a, const T b) { if (a<b) { a=b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T b) { if (b<a) { a=b; return 1; } return 0; }

const long long INF = 1LL<<60;



int main(){

    ll H,N;

    cin >> H >> N;

    vector<ll> A(N);

    vector<ll> B(N);

    for(int i=0;i<N;i++) cin >> A[i] >> B[i];

    vector<vector<ll>> dp(N+1,vector<ll>(H+1,INF));

    dp[0][0] = 0;

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

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

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

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

        }

    }

    ll ans = INF;

    /*for(ll i=0;i<=N;i++){

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

    }*/

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

}

// Optimized solution to the same problem (faster):


#include<bits/stdc++.h>

using namespace std;

#define ALL(v) (v).begin(),(v).end()

#define REP(i,p,n) for(int i=p;i<(int)(n);++i)

#define rep(i,n) REP(i,0,n)

#define SZ(x) ((int)(x).size())

#define debug(x) cerr << #x << ": " << x << '\n'



typedef long long int Int;

typedef pair<int,int> P;

using ll = long long;

using VI = vector<int>;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const long long INF = 1LL<<60;



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<ll> dp(H+1,INF);

    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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}




// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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: */


// Optimized solution to the same problem (faster):
#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: */


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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);

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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);

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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);

}

// Fastest known solution for this problem: