Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <map>
#include <vector>

static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;

template <typename T> std::vector<T> vectors(std::size_t n, T val) {
  return std::vector<T>(n, val);
}

template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
  return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}

template <class T> inline bool chmin(T &a, const T &b) {
  return (a > b) ? a = b, true : false;
}

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

template <class T> inline void chadd(T &a, const T &b) { a += b, a %= MOD; }

int main() {
  // Input
  int numChild;
  int numCandy;
  std::cin >> numChild >> numCandy;
  assert(1 <= numChild and numChild <= 100);
  assert(0 <= numCandy and numCandy <= 1.0e+5);
  std::vector<long long> upper(1 + numChild, -1);
  for (int i = 1; i <= numChild; ++i) {
    std::cin >> upper[i];
    assert(0 <= upper[i] and upper[i] <= numCandy);
  }

  // Initialization
  std::vector<std::vector<long long>> dp(1 + numChild);
  for (int i = 1; i <= numChild; ++i)
    dp[i].assign(1 + numCandy, 0LL);

  // Initial condition
  for (int k = 0; k <= upper[1]; ++k)
    dp[1][k] = 1;

  // DP main
  for (int i = 1; i < numChild; ++i) {
    long long currentSum = 0LL;
    std::deque<long long> dque;
    for (int k = 0; k <= numCandy; ++k) {
      if (k <= upper[i + 1]) {
        dque.push_back(dp[i][k]);
        chadd(currentSum, dque.back());
        dp[i + 1][k] = currentSum;
      } else {
        assert(dque.size() == (size_t)upper[i + 1] + 1);
        dque.push_back(dp[i][k]);
        chadd(currentSum, dque.back());
        chadd(currentSum, -dque.front());
        assert(currentSum >= 0);
        dque.pop_front();
        dp[i + 1][k] = currentSum;
      }
    }
  }

  // Output
  // long long res = 0LL;
  // for (int k = 0; k <= upper[numChild]; ++k) {
  //   chadd(res, dp[numChild][k]);
  // }
  // std::cout << res << std::endl;
  std::cout << dp[numChild][numCandy] << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <map>
#include <vector>

static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;

template <typename T> std::vector<T> vectors(std::size_t n, T val) {
  return std::vector<T>(n, val);
}

template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
  return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}

template <class T> inline bool chmin(T &a, const T &b) {
  return (a > b) ? a = b, true : false;
}

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

template <class T> inline void chadd(T &a, const T &b) {
  a += b, a %= MOD;
  if (a < 0)
    a += MOD;
}

int main() {
  // Input
  int numChild;
  int numCandy;
  std::cin >> numChild >> numCandy;
  assert(1 <= numChild and numChild <= 100);
  assert(0 <= numCandy and numCandy <= 1.0e+5);
  std::vector<long long> upper(1 + numChild, -1);
  for (int i = 1; i <= numChild; ++i) {
    std::cin >> upper[i];
    assert(0 <= upper[i] and upper[i] <= numCandy);
  }

  // Initialization
  std::vector<std::vector<long long>> dp(1 + numChild);
  for (int i = 1; i <= numChild; ++i)
    dp[i].assign(1 + numCandy, 0LL);

  // Initial condition
  for (int k = 0; k <= upper[1]; ++k)
    dp[1][k] = 1;

  // DP main
  for (int i = 1; i < numChild; ++i) {
    long long currentSum = 0LL;
    std::deque<long long> dque;
    for (int k = 0; k <= numCandy; ++k) {
      if (k <= upper[i + 1]) {
        dque.push_back(dp[i][k]);
        chadd(currentSum, dque.back());
        dp[i + 1][k] = currentSum;
      } else {
        assert(dque.size() == (size_t)upper[i + 1] + 1);
        dque.push_back(dp[i][k]);
        chadd(currentSum, dque.back());
        chadd(currentSum, -dque.front());
        assert(currentSum >= 0);
        dque.pop_front();
        dp[i + 1][k] = currentSum;
      }
    }
  }

  // Output
  // long long res = 0LL;
  // for (int k = 0; k <= upper[numChild]; ++k) {
  //   chadd(res, dp[numChild][k]);
  // }
  // std::cout << res << std::endl;
  std::cout << dp[numChild][numCandy] << std::endl;

  return 0;
}