stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.8159146508344008x):
///in the name of ALLAH

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()

{

  //  ll p=100;

    //while(p--){

   ll n,x,c=0;

   cin>>n>>x;

   ll a[n];

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

    cin>>a[i];

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

   {

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

       {

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

           c+=p;

           if(p>a[i])

           {

               a[i]=0;

               a[i-1]-=(p-a[i]);

           }

           else{

            a[i]-=p;

           }

       }

   }

   cout<<c<<endl;

}
// Slower C++ solution:
#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;

}

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

using namespace std;

#define ll long long



int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



    int n,x;

    ll ans = 0;

    cin>>n>>x;

    int ara[n];

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



    if(ara[0] > x){

        ans += ara[0]-x;

        ara[0] = x;

    }



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

        int pre_sum = ara[i] + ara[i-1];

        int temp = max(0,pre_sum-x);

        ara[i] -= temp;

        ans += temp;

    }



    cout<<ans<<endl;

    

    return 0;

}
// Slower C++ solution:
#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;

}

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

using namespace std;

int main()

{

    long int n, x;

    cin>> n >> x;



    long int input;

    vector<long int> vec;



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

    {

        cin>> input;

        vec.push_back(input);

    }



    //long int temp = 0;



    long long int ans = 0;

    for(long int i = 1; i < vec.size(); i+= 2)

    {

        if(vec[i] + vec[i-1] >= x)

        {

            long int temp = vec[i] + vec[i-1] - x;

            ans += temp;

            if(temp >= vec[i])

            {

                vec[i] = 0;

                temp -= vec[i];



                vec[i-1] = vec[i-1] - temp;

            }

            else

                vec[i] = vec[i] - temp;



        }



        if((vec[i] + vec[i+1] >= x)&&(i+1 < n))

        {

            long int temp = vec[i+1] + vec[i] - x;



            ans += temp;



            if(temp >= vec[i+1])

            {

                vec[i+1] = 0;

                temp -= vec[i+1];

                vec[i] = vec[i] - temp;

            }

            else

                vec[i+1] = vec[i+1] - temp;

        }



    }



    cout<< ans;



    return 0;



}
// Slower C++ solution:
#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;

}

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

using namespace std;



int main()

{

    long long int n, x, input, cnt = 0, a, b;

    vector<long long int>candys;

    cin >> n >> x;

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

        cin >> input;

        candys.push_back(input);

    }



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

        long long int c = 0;

        if(i == 1)

            a = candys[0];

        else

            a = b;

        b = candys[i];

        if(a+b > x)

        {

            c = (a+b) - x;

            if(c >= b)

                b = 0;

            else

                b = b - c;

        }

        cnt += c;

    }

    cout << cnt;



    return 0;

}
// Slower C++ solution:
#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;

}

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

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/pb_ds/assoc_container.hpp>



#define            ll              long long

#define            pb              push_back

#define            mp              make_pair

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

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

#define            min3(a,b,c)     min(a,min(b,c))

#define            max3(a,b,c)     max(a,max(b,c))

#define            min4(a,b,c,d)   min(a,min(b,min(c,d)))

#define            max4(a,b,c,d)   max(a,max(b,max(c,d)))

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

#define            pii             pair<int,int>

#define            pll             pair<ll,ll>

#define            pli             pair<ll,int>

#define            pil             pair<int,ll>

#define            FF              first

#define            SS              second

#define            PI              acos(-1.0)

#define            bug             cout<<"ok"<<endl;



using namespace std;

using namespace __gnu_pbds;



typedef tree<int , null_type,less<int>,

       rb_tree_tag, tree_order_statistics_node_update> anik;



const int MOD = 1e9 + 7;

const ll MX= 1e9;

const ll INF = 1e9;



main()

{

   int n,x;

    cin>>n>>x;

    int a[n+9];



    loop(i,1,n)

    cin>>a[i];

    ll ans=0;

    a[0]=0;

    a[n+1]=0;

    loop(i,1,n)

    {

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

        {

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

            if(kor<=a[i+1])

            {

                a[i+1]-=kor;

                ans+=kor;

            }

            else

            {

                ans+=kor;

                a[i+1]=0;

                kor-=a[i+1];

                a[i]-=kor;

            }

        }

    }



    cout<<ans<<endl;

}
// Slower C++ solution:
#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;

}

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

#define int long long

using namespace std;

main(){

int t;

//cin>>t;

//while(t--){

int n , m;

cin>>n>>m;

int A[n];

int sum = 0;

int sum1 = 0;

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

    cin>>A[i];

    sum += A[i];

}



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

   int tmp = A[i] + A[i+1];

   if(tmp<=m) continue;

   if(A[i]>m){

        A[i] = m;

        A[i+1] = 0;

   }

   else if(A[i]<=m){

    A[i+1] = m - A[i];

   }

}



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

    //cout<<A[i]<<" ";

   sum1 += A[i];

}

//cout<<endl;

cout<<sum - sum1<<endl;

//}

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.8373252317502025x):
#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);

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 9.285791282401265x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.8091483675046942x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.2312795208682505x):


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

}
// Slower C++ solution:


 //### 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 ;

}

// Faster C++ solution for the same problem (measured speedup: 5.228192648937542x):


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

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.314693920605713x):


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

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 5.296609275559359x):


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

}
// Slower C++ solution:
#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;

}

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

#define n 500000

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

using namespace std;

typedef long long lli;

typedef double llf;

typedef pair<int, int> P;

void solve();

int main()

{

	ios_base::sync_with_stdio(0);

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

	int times = 1;

//	cin >> times;

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

	{

		solve();

		cout << '\n';

	}

	return 0;

}



lli a, b, c, d, e, f, g, h;

lli arr[n + 50], arr1[n + 50], arr2[n + 50];



//type here



void solve()

{

	cin >> a >> b;

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

		cin >> arr[i];

	lli ans = 0;

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

	{

		if(arr[i - 1] + arr[i] > b)

		{

			lli tmp = min(arr[i] + arr[i - 1] - b, arr[i]);

			arr[i] -= tmp;

			ans += tmp;

		}

		if(arr[i - 1] + arr[i] > b)

		{

			lli tmp = min(arr[i] + arr[i - 1] - b, arr[i - 1]);

			arr[i - 1] -= tmp;

			ans += tmp;

		}

	}

	cout << ans << '\n';

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.797890117683562x):
#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';

}