Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \\
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, num = 0, sum = 0, flag = 0;
  string S;
  cin >> S;
  N = S.size();
  DV(dp, int, 2, 2019, 0); // dp[i文字目まで見た][2019で割った余りがj]
  dp[0][(S[0] - '0')] = 1;
  rep(i, 1, N) {
    if (i % 2 == 1)
      flag = 1;
    else
      flag = 0;
    rep(j, 0, 2019) dp[flag][(10 * j + (S[i] - '0')) % 2019] += dp[flag ^ 1][j];
    dp[flag][S[i] - '0']++;
    sum += dp[flag][0];
    rep(j, 0, 2019) dp[flag ^ 1][j] = 0;
  }
  Out(sum);
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \\
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int A, B, C, D, E, H, W, M, K, L, R, N, num = 0, sum = 0, flag = 0;
  string S, T;
  cin >> S;
  N = S.size();
  DV(dp, int, N, 2019, 0); // dp[i文字目まで見た][2019で割った余りがj]
  dp[0][(S[0] - '0')] = 1;
  rep(i, 1, N) {
    rep(j, 0, 2019) dp[i][(10 * j + (S[i] - '0')) % 2019] += dp[i - 1][j];
    dp[i][S[i] - '0']++;
  }
  rep(i, 0, N) sum += dp[i][0];
  Out(sum);
}