Skip to content

Instantly share code, notes, and snippets.

@enriquefynn
Last active May 8, 2017 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enriquefynn/e07cd68a52181baba6b3ed645d1a32be to your computer and use it in GitHub Desktop.
Save enriquefynn/e07cd68a52181baba6b3ed645d1a32be to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main() {
long long n, x, v[1000000];
unsigned long long cnt = 0;
cin >> n >> x;
for (int i = 0; i < n; ++i)
cin >> v[i];
for (int i = 0; i < n-1; ++i) {
long long rem = v[i] + v[i+1] - x;
if (rem < 0)
continue;
if (rem > v[i+1]) {
v[i] = v[i] - (v[i+1] - rem);
v[i+1] = 0;
cnt += rem;
}
else {
v[i+1] -= rem;
cnt += rem;
}
}
cout << cnt << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment