Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
int main() {
  ll n, lim;
  cin >> n >> lim;
  vl v(n), dp(n + 1, 0);
  rep(i, n) {
    cin >> v[i];
    if (i)
      dp[i] += v[i] + v[i - 1];
  }
  ll ans = 0;
  rep(i, n) {
    if (!i)
      continue;
    if (v[i] + v[i - 1] > lim) {
      ll cnt = 0, base = v[i] + v[i - 1];
      ans += base - lim;
      for (; 0 <= v[i] && (v[i] + v[i - 1]) != lim;)
        --v[i];
      if (i != cnt)
        v[i - 1] -= cnt - i;
    }
  }
  // rep(i,n)cout<<v[i]<<" ";
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
int main() {
  ll n, lim;
  cin >> n >> lim;
  vl v(n), dp(n + 1, 0);
  rep(i, n) {
    cin >> v[i];
    if (i)
      dp[i] += v[i] + v[i - 1];
  }
  ll ans = 0;
  rep(i, n) {
    if (!i)
      continue;
    if (v[i] + v[i - 1] > lim) {
      ll cnt = 0, base = v[i] + v[i - 1];
      ans += base - lim;
      if (v[i] > base - lim)
        v[i] -= base - lim;
      else {
        v[i - 1] -= base - lim - v[i];
        v[i] = 0;
      }
    }
  }
  // rep(i,n)cout<<v[i]<<" ";
  cout << ans << endl;
}