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

#include<cstring>

#include<cstdio>

#define LL long long

using namespace std;

int n,m;

const int N=100010,mod=1e9+7;

int a[N];

struct ju

{

	LL v[3][3];

	ju(){memset(v,0,sizeof(v));}

	friend ju operator *(const ju &a,const ju &b)

	{

		ju c;

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

			for(int j=0;j<=2;++j)

				for(int k=0;k<=2;++k)

					(c.v[i][j]+=a.v[i][k]*b.v[k][j])%=mod;

		return c;

	}

}A,B,Ans;

ju ksm(ju a,LL b)

{

	ju res;res.v[0][0]=res.v[1][1]=res.v[2][2]=1;

	for(;b;b>>=1,a=a*a)

		if(b&1)res=res*a;

	return res;

}

signed main()

{

	cin>>n>>m;

	for(int i=1;i<=m;++i)scanf("%d",&a[i]);

	A.v[0][0]=A.v[1][0]=A.v[1][2]=2;

	A.v[0][1]=A.v[0][2]=A.v[1][1]=A.v[2][0]=A.v[2][2]=1;

	

	B.v[0][0]=B.v[0][1]=B.v[0][2]=B.v[1][1]=B.v[2][2]=1;

	B.v[1][2]=2;

	

	Ans.v[0][0]=1;

	for(int i=1;i<=m;++i)Ans=Ans*ksm(A,a[i]-a[i-1]-1)*B;

	Ans=Ans*ksm(A,n-a[m]);

	cout<<Ans.v[0][2];

	return 0;

}

/*

3 1 

2



13



5 2

2 3



66



10 9

1 2 3 4 5 6 7 8 9



100

*/

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

#include <assert.h>



typedef long long LL;



const int MAXM = 1e5, MOD = 1e9 + 7;

int n, m;



inline int add ( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }

inline int mul ( LL a, const int b ) { return ( a *= b ) < MOD ? a : a % MOD; }



inline int rint () {

	int x = 0; char s = getchar ();

	for ( ; s < '0' || '9' < s; s = getchar () );

	for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );

	return x;

}



struct Matrix {

	int n, m, mat[3][3];

	Matrix (): n ( 0 ), m ( 0 ), mat {} {}

	Matrix ( const int tn, const int tm ): n ( tn ), m ( tm ), mat {} {}

	inline int* operator [] ( const int key ) { return mat[key]; }

	inline Matrix operator * ( Matrix t ) {

		assert ( m == t.n );

		Matrix ret ( n, t.m );

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

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

				for ( int j = 0; j < t.m; ++ j ) {

					ret[i][j] = add ( ret[i][j], mul ( mat[i][k], t[k][j] ) );

				}

			}

		}

		return ret;

	}

};



inline Matrix qkpow ( Matrix a, int b ) {

	Matrix ret ( a.n, a.m );

	for ( int i = 0; i < ret.n; ++ i ) ret[i][i] = 1;

	for ( ; b; a = a * a, b >>= 1 ) if ( b & 1 ) ret = ret * a;

	return ret;

}



Matrix I ( 3, 1 ), A ( 3, 3 ), B ( 3, 3 );



inline void init () {

	I[0][0] = 1;

	A[0][0] = 1;

	A[1][0] = 2, A[1][1] = 1;

	A[2][0] = A[2][1] = A[2][2] = 1;

	B = A, ++ B[0][0], ++ B[0][1], ++ B[0][2];

}



int main () {

	init ();

	n = rint (), m = rint ();

	int las = 0;

	for ( int i = 1, x; i <= m; ++ i ) {

		x = rint ();

		I = A * qkpow ( B, x - las - 1 ) * I;

		las = x;

	}

	

	I = A * qkpow ( B, n - las - 1 ) * I;

	printf ( "%d\n", I[2][0] );

	return 0;

}

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

using namespace std;



//#define int long long

#define DBG 0

#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}

#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}

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

#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)

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

static const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;

static const int MOD = (int)(1e9 + 7);



//????????????

class Dice {

private:

	map<char, string> mp;

public:

	//string d = "UFRLBD";

	string d = "RENSWL";

	vector<int> v; //UFRLBD:123456

	Dice() {

		init();

		v.resize(6);

	};

	Dice(vector<int> v) :v(v) {

		init();

	};

	void init(){

		mp[d[0]] = "2354";

		mp[d[1]] = "1463";

		mp[d[2]] = "1265";

		mp[d[3]] = "1562";

		mp[d[4]] = "1364";

		mp[d[5]] = "2453";

	}

	void input() { rep(i, 0, 6) { cin >> v[i]; } }

	void roll(char c) { roll(mp[c]); }

	void roll(string s) {

		int tmp = v[s[0] - '1'];

		rep(i, 1, s.size())v[s[i - 1] - '1'] = v[s[i] - '1'];

		v[s[s.size() - 1] - '1'] = tmp;

	}

	bool operator==(const Dice &D)const { return v == D.v; }

};



signed main() {

	int n; cin >> n;

	vector<Dice> D(n); rep(i, 0, n) { D[i].input(); }

	rep(i, 0, n) {

		rep(j, i + 1, n) {

			rep(k, 0, 1000) {

				if (D[i] == D[j]) {

					cout << "No" << endl;

					return 0;

				}

				D[j].roll( D[j].d[rand() % 6] );

			}

		}

	}

	cout << "Yes" << endl;

	return 0;

}

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

using namespace std;



//#define int long long

#define DBG 0

#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}

#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}

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

#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)

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

static const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;

static const int MOD = (int)(1e9 + 7);



//????????????

class Dice {

private:

	map<char, string> mp;

public:

	//string d = "UFRLBD";

	string d = "RENSWL";

	vector<int> v; //UFRLBD:123456

	Dice() {

		init();

		v.resize(6);

	};

	Dice(vector<int> v) :v(v) {

		init();

	};

	void init(){

		mp[d[0]] = "2354";

		mp[d[1]] = "1463";

		mp[d[2]] = "1265";

		mp[d[3]] = "1562";

		mp[d[4]] = "1364";

		mp[d[5]] = "2453";

	}

	void input() { rep(i, 0, 6) { cin >> v[i]; } }

	void roll(char c) { roll(mp[c]); }

	void roll(string s) {

		int tmp = v[s[0] - '1'];

		rep(i, 1, s.size())v[s[i - 1] - '1'] = v[s[i] - '1'];

		v[s[s.size() - 1] - '1'] = tmp;

	}

	bool operator==(const Dice &D)const { return v == D.v; }

};



signed main() {

	int n; cin >> n;

	vector<Dice> D(n); rep(i, 0, n) { D[i].input(); }

	rep(i, 0, n) {

		rep(j, i + 1, n) {

			rep(k, 0, 30) {

				if (D[i] == D[j]) {

					cout << "No" << endl;

					return 0;

				}

				D[j].roll( D[j].d[rand() % 6] );

			}

		}

	}

	cout << "Yes" << endl;

	return 0;

}

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

#include <vector>

#include <limits>

using namespace std;



inline int f(int n)

{

  return max(0, min(255, n));

}



int main()

{

  int N, M;

  while (cin >> N >> M && N != 0) {

    vector<int> cs(M);

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

      cin >> cs[i];

    }

    vector<int> xs(N);

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

      cin >> xs[i];

    }



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

    dp[0][128] = 0;

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

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

        for (int k = 0; k < M; k++) {

          const int y = f(j+cs[k]);

          dp[i+1][y] = min(dp[i+1][y], dp[i][j] + (xs[i]-y)*(xs[i]-y));

        }

      }

    }

    int ans = numeric_limits<int>::max();

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

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

    }

    cout << ans << endl;

  }

  return 0;

}

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

#include <vector>

#include <algorithm>

using namespace std;



int sqr(int x) { return x*x; }



int main()

{

  int N, M;

  while (scanf("%d %d", &N, &M) != EOF && N != 0) {

    vector<int> cs(M);

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

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

    }



    static long long dp[20000+1][256];

    static const long long INF = 1LL<<50;

    fill_n(dp[0], 256, INF);

    dp[0][128] = 0;

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

      int x;

      scanf("%d", &x);

      fill_n(dp[i], 256, INF);

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

        for (int k = 0; k < M; k++) {

          int y = j + cs[k];

          if (y < 0) {

            y = 0;

          } else if (y > 255) {

            y = 255;

          }

          dp[i][y] = min(dp[i][y], dp[i-1][j] + sqr(y - x));

        }

      }

    }

    printf("%lld\n", *min_element(dp[N], dp[N]+256));

  }

  return 0;

}

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

#include <cmath>

#include <algorithm>

#include <bits/stdc++.h>

const int maxn = 1e5 + 10;

using namespace std;

int main()

{

    int n, x;

    long long a[maxn] = {0};

    cin >> n >> x;

    long long  count = 0;

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

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

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

    {

        if (a[i] > x)

        {

            count += (a[i] - x);

            a[i] = x;

        }

        if (a[i] + a[i+1] > x)

        {

            count += (a[i+1] + a[i] - x);

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

        }

    }

    cout << count << endl;

    return 0;

}

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

#define maxn 100086



using namespace std;



int n, m, x, y;

long long ans;



int main(){

	scanf("%d%d", &n, &m);

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

		scanf("%d", &x);

		if(i == 1){

			y = x;

			continue;

		} 

		int j = max(0, x + y - m);

		ans += j, y = max(0, x - j); 

	}	

	printf("%lld", ans);

}

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

#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define fr first

#define sc second

#define N 300009

#define inf 2e16

#define pi pair<ll,ll>

#define pb push_back

using namespace std;

typedef long long ll;

void slv()

{

}

vector<int>adj[N];

bool vis[N];

int dis[N];

void vfs(int s)

{

    queue<int>q;

    q.push(s);

    dis[s]=0;

    vis[s]=1;

    while (!q.empty())

    {

        int u=q.front();

        q.pop();

        for(auto v : adj[u])

        {

            if(!vis[v])

            {

                vis[v]=1;

                dis[v]=dis[u]+1;

                q.push(v);

            }

        }

    }

    

}

int main()

{

    ll n,x;

    cin>>n>>x;

    vector<ll>a(n+1,0);

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

    ll ans=0LL;

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

    {

        ll s=a[i]+a[i-1];

        if(s>x)

        {

            ans += s-x;

            a[i] = min(a[i],a[i]-(s-x));

        }

    }

    cout<<ans<<endl;

    return 0;

}


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



using namespace std;



#define ll long long

#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define endl '\n'

#define Endl '\n'

#define cont continue

#define gcd(a,b) __gcd(a,b)

#define lcm(a,b) a*b/__gcd(a,b)

#define pb push_back

#define mp make_pair

#define mem(x) memset(x, 0, sizeof(x))



const ll inf=1e18;

const int mod=1e9+7;

inline int bigmod(int B,int P){int R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;}

const int mxn = 2e5+10;



int main(){

    fast;



    int tt = 1;

    //cin>>tt;

    for(int tc = 1; tc <= tt; tc++) {

        ll n,x,ans = 0,s;

        cin>>n>>x;

        int ip[n];

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

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

			s = ip[i] + ip[i-1];

			if(s > x) {

				if(ip[i] >= (s-x)) {

					ans += (s-x);

					ip[i] -= (s-x);

				}

				else {

					ans += (s-x);

					s -= ip[i];

					ip[i] = 0;

					ip[i-1] -= (s-x);

				}

			}

		}

		cout<<ans<<endl;

        

        

    }







return 0;

}


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

#include <algorithm>

#define ll long long

using namespace std;



int main(){



  int n;

  ll x, ans = 0;

  ll a[100001] = {};



  cin >> n >> x;

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

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

    ll tmp = a[i] + a[i+1];

    if(tmp >= x){

      ans += tmp - x;

      a[i+1] -= tmp - x;

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

    }

  }

  cout << ans << endl;

  return 0;

}


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

#define ll long long

using namespace std;

  

int main(){

  int n;

  ll x;

  cin >> n >> x;

  ll a[n], sum = 0;

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

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

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

      sum += a[i+1] + a[i] - x;

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

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

    }

  }

  cout << sum << endl;

  return 0;

}

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





using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;



const int MAXN = 100 * 1000 + 123;











int n, x, a[MAXN];

ll ans;





int main() {

	cin >> n >> x;

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

		cin >> a[i];

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

		while (a[i] + a[i - 1] > x) {

			if (a[i]) {

				ans += min(a[i], a[i] + a[i - 1] - x);

				a[i] -= min(a[i], a[i] + a[i - 1] - x);

			}

			else {

				ans += min(a[i - 1], a[i] + a[i - 1] - x);

				a[i - 1] -= min(a[i - 1], a[i] + a[i - 1] - x);

			}

		}

	cout << ans;

	return 0;

}

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


#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

    int n,i,x;

    scanf("%d %d",&n,&x);

    long long ans=0,sum,a,b;

    scanf("%lld",&a); //第一个 盒子中的糖果个数

    if(a>x)   // x 是最后每个盒子要剩余的个数，  如果第一个盒子的糖果个数a大于x

    {

       ans+=a-x;  //  ans 是多出的个数

       a=x; 

    }

 

    for( i=1; i<n; i++) //剩下的 n-1 个盒子

    {

        scanf("%lld",&b);  

        sum=a+b;

        if(sum>x)

        {

            ans+=sum-x;

            a=b-sum+x;   

        }

        else a=b;       

    }                   

    printf("%lld",ans); // 

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


 //### In The Name Of GOD ###



 //## Ya Hossien ##

 

#include <bits/stdc++.h>



using namespace std;



typedef long long ll ;



const int mod = 1000000007;

const int inf = 2000000000;

const int maxn = 500*1000+500*1000+15;

const int MLOG = 18;



#define int long long

#define pb push_back

#define pp pop_back

#define X first

#define Y second

#define IO ios_base::sync_with_stdio(false);

#define sz(a) (int)(a.size())



int32_t main(){

	int n, x, ans = 0;

	cin >> n >> x;

	const int m = n;

	int a[m];

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

		cin >> a[i];

	}

	if(a[0] > x){

		ans += a[0]-x;

		a[0] = x;

	}

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

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

			int dif = a[i + 1] + a[i] - x;

			ans += dif;

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

		}

	}

	cout << ans ;

}

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


#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

    int n,i,x;

    scanf("%d %d",&n,&x);

    long long ans=0,sum,a,b;

    scanf("%lld",&a); //第一个 盒子中的糖果个数

    if(a>x)   // x 是最后每个盒子要剩余的个数，  如果第一个盒子的糖果个数a大于x

    {

       ans+=a-x;  //  ans 是多出的个数

       a=x; 

    }

 

    for( i=1; i<n; i++) //剩下的 n-1 个盒子

    {

        scanf("%lld",&b);  

        sum=a+b;

        if(sum>x)

        {

            ans+=sum-x;

            a=b-sum+x;   

        }

        else a=b;       

    }                   

    printf("%lld",ans); // 

    return 0;

}

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



using namespace std;

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

#define REP(i,a,b) REPP(i,a,b,1)

#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)

#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) REP(i,0,(int)a-1)

#define FORD(i,a) REV(i,(int)a-1,0)

#define PB push_back

#define POB pop_back

#define MP make_pair

#define FI first

#define SE second

typedef long long ll;

typedef unsigned long long ull;

typedef pair < int , int > pii;

typedef vector < int > vi;

typedef vector < pii > vii;

typedef vector < ll > vl;



const double EPS = 1e-9;

const double PI = acos(-1);

const int INF = 1e9;

const ll MOD = 1e9 + 7;



ll addMod(ll a, ll b){

	return (a+b) % MOD;

}



ll mulMod(ll a, ll b){

	return (a*b) % MOD;

}



/////////////////////////////////////////////////////////



const int N = 1e5;



int n,m,x;

ll arr[N + 5];

ll solve(){

	ll ret = 0;

	REP(i,1,n-1){

		ll tmp = min(arr[i], arr[i]+arr[i-1]-m);

		if(tmp > 0){

			ret += tmp;

			arr[i] -= tmp;

		}

		if(arr[i] + arr[i-1] - m > 0){

			if(arr[i-1] > 0){

				ret += arr[i-1] - m;

			}

			else{

				ret += arr[i] - m;

				arr[i] -= (arr[i] - m);

			}

		}

	}

	return ret;

}





int main(){

	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

	

	cin >> n >> m;

	FOR(i,n){

		cin >> arr[i];

	}

	cout << solve() << endl;

	

	return 0;

}

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


#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

    int n,i,x;

    scanf("%d %d",&n,&x);

    long long ans=0,sum,a,b;

    scanf("%lld",&a); //第一个 盒子中的糖果个数

    if(a>x)   // x 是最后每个盒子要剩余的个数，  如果第一个盒子的糖果个数a大于x

    {

       ans+=a-x;  //  ans 是多出的个数

       a=x; 

    }

 

    for( i=1; i<n; i++) //剩下的 n-1 个盒子

    {

        scanf("%lld",&b);  

        sum=a+b;

        if(sum>x)

        {

            ans+=sum-x;

            a=b-sum+x;   

        }

        else a=b;       

    }                   

    printf("%lld",ans); // 

    return 0;

}

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

#include <vector>

using namespace std;



int main()

{

    int n,x;

    while(cin>>n>>x)

    {

        vector <int> s;

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

        {

            int num;

            cin>>num;

            s.push_back(num);

        }

        long long cnt=0;

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

        {

            if(i==0)

            {

                if(s[i]>x)

                {

                    cnt+=s[i]-x;

                    s[i]=x;

                }

                continue;

            }

            if(i==n-1)

            {

                if(s[i]+s[i-1]>x)

                {

                    cnt+=(s[i]+s[i-1])-x;

                }

                continue;

            }

            if(s[i]+s[i-1]>x)

            {

                cnt+=(s[i]+s[i-1])-x;

                s[i]-=(s[i]+s[i-1])-x;

            }

            /*if(s[i]+s[i+1]>x)

            {

                cnt+=(s[i]+s[i+1])-x;

                s[i]-=(s[i]+s[i+1])-x;

            }*/

        }

        cout<<cnt<<endl;

    }

    return 0;

}


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


#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

    int n,i,x;

    scanf("%d %d",&n,&x);

    long long ans=0,sum,a,b;

    scanf("%lld",&a); //第一个 盒子中的糖果个数

    if(a>x)   // x 是最后每个盒子要剩余的个数，  如果第一个盒子的糖果个数a大于x

    {

       ans+=a-x;  //  ans 是多出的个数

       a=x; 

    }

 

    for( i=1; i<n; i++) //剩下的 n-1 个盒子

    {

        scanf("%lld",&b);  

        sum=a+b;

        if(sum>x)

        {

            ans+=sum-x;

            a=b-sum+x;   

        }

        else a=b;       

    }                   

    printf("%lld",ans); // 

    return 0;

}

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

#include <cmath>

#include <algorithm>

#include <bits/stdc++.h>

const int maxn = 1e5 + 10;

using namespace std;

int main()

{

    int n, x;

    long long a[maxn] = {0};

    cin >> n >> x;

    long long  count = 0;

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

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

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

    {

        if (a[i] > x)

        {

            count += (a[i] - x);

            a[i] = x;

        }

        if (a[i] + a[i+1] > x)

        {

            count += (a[i+1] + a[i] - x);

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

        }

    }

    cout << count << endl;

    return 0;

}

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

using namespace std;

typedef long long ll;

int main(){

	int n,k,w;

	cin>>n>>k;

	vector<ll> ac;

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

		cin>>w;

		ac.push_back(w);

	}

	long long  ans=0;

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

		if(ac[s]+ac[s-1]>k){

			int h=ac[s]+ac[s-1]-k;

			if(ac[s]-h>=0){

				ac[s]-=h;

			}

			else {

				// ac[s-1]-=(h-ac[s]);

				ac[s]=0;

			}

			ans+=h;

		}	

	}

	cout<<ans<<'\n';

}

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

#include <algorithm>



typedef long long ll;



using namespace std;



int main() {

    ll n, x, cnt = 0, prev = 0, now;

    cin >> n >> x;

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

        cin >> now;

        prev = now - max(prev + now - x, 0LL);

        cnt += now - prev;

    }

    cout << cnt << endl;

    return 0;

}


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

#include <algorithm>



typedef long long ll;



using namespace std;



int main() {

    ll n, x, cnt = 0, prev = 0, now;

    scanf("%lld%lld", &n, &x);

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

        scanf("%lld", &now);

        prev = now - max(prev + now - x, 0LL);

        cnt += now - prev;

    }

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

    return 0;

}


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



using namespace std;



typedef long long int64;





int main()

{

  int64 N, x, A[100001] = {};



  cin >> N >> x;

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

    cin >> A[i];

  }



  int64 ret = 0;

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

    ret += min(A[i], max(0LL, A[i - 1] + A[i] - x));

    A[i] -= min(A[i], max(0LL, A[i - 1] + A[i] - x));

    ret += max(0LL, A[i - 1] + A[i] - x);

  }



  cout << ret << endl;

}

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



using namespace std;



using int64 = long long;

const int mod = 1e9 + 7;

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

const int64 infll = (1LL << 61) - 1;



struct IoSetup {

  IoSetup() {

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    cout << fixed << setprecision(10);

    cerr << fixed << setprecision(10);

  }

} iosetup;



template< typename T >

ostream &operator<<(ostream &os, const vector< T > &v) {

  for(int i = 0; i < (int) v.size(); i++) {

    os << v[i] << (i + 1 != v.size() ? " " : "");

  }

  return os;

}



template< typename T >

istream &operator>>(istream &is, vector< T > &v) {

  for(T &in : v) is >> in;

  return is;

}



template< typename T1, typename T2 >

inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }



template< typename T1, typename T2 >

inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }



template< typename T = int64 >

vector< T > make_v(size_t a) {

  return vector< T >(a);

}



template< typename T, typename... Ts >

auto make_v(size_t a, Ts... ts) {

  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));

}



template< typename T, typename V >

typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {

  t = v;

}



template< typename T, typename V >

typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {

  for(auto &e : t) fill_v(e, v);

}



int main() {

  int N, X;

  cin >> N >> X;

  vector< int > A(N);

  cin >> A;



  int64 ret = 0;

  for(int i = 0; i < A.size(); i++) {

    int more = max(0, A[i] - X);

    ret += more;

    A[i] -= more;

  }



  for(int i = 1; i < A.size(); i++) {

    int more = max(0, A[i] + A[i - 1] - X);

    ret += more;

    A[i] -= more;

  }



  cout << ret << endl;

}


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



using namespace std;



int main() {

    string s;

    cin >> s;

    int cnt[26] = {0};

    for (int i = 0; i < s.size(); i++) {

        cnt[s[i] - 'a']++;

    }

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

        if (cnt[i] & 1) {

            puts("No");

            return 0;

        }

    }

    puts("Yes");

    return 0;

}


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

#include<string.h>

int main()

{

    int j,n,flag,count;

    char w[100]={0},i;

    scanf("%s",w);

    flag=0;

    n=strlen(w);

    for(i=97;i<='z';i++)

    {

        count=0;

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

            if(w[j]==i)

                count++;

        if(count%2!=0)

        {

            flag=1;

            break;

        }

    }

        if(flag==1)

        printf("No\n");

        else

    printf("Yes\n");

    return 0;

}


// Fastest known solution for this problem: