Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

const int MAX = 10000000;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/****** define constant end ********/
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
/****** define variable start ******/
ll s;
/******* define variable end *******/
int solve() {
  cin >> s;
  vector<vector<ll>> dp(s / 3 + 1, vl(s + 1, 0));
  dp[0][0] = 1;
  vector<vector<bool>> flag(s / 3 + 1, vector<bool>(s + 1, false));
  flag[0][0] = true;
  REP(j, s + 1) {
    ll sum = 0;
    FOR(k, 3, j + 1) {
      if (j - k >= 0 && flag[0][j - k]) {
        sum += dp[0][j - k];
        sum %= MOD;
        flag[0][j] = true;
      }
    }
    dp[0][j] += sum;
    dp[0][j] %= MOD;
  }
  ll ans = 0;
  ans += dp[0][s] % MOD;
  ans %= MOD;

  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

const int MAX = 10000000;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/****** define constant end ********/
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
/****** define variable start ******/
ll s;
/******* define variable end *******/
int solve() {
  cin >> s;
  vector<vector<ll>> dp(s / 3 + 1, vl(s + 1, 0));
  dp[0][0] = 1;
  vector<vector<bool>> flag(s / 3 + 1, vector<bool>(s + 1, false));
  flag[0][0] = true;
  FOR(i, 0, s / 3) {
    REP(j, s + 1) {
      ll sum = 0;
      FOR(k, 3, j + 1) {
        if (j - k >= 0 && flag[i][j - k]) {
          sum += dp[i][j - k];
          sum %= MOD;
          flag[i + 1][j] = true;
        }
      }
      dp[i + 1][j] += sum;
      dp[i + 1][j] %= MOD;
    }
  }
  ll ans = 0;
  REP(i, s / 3 + 1) {
    ans += dp[i][s] % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}