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

/*
        rupc2017day1_E: Graduation_Ceremony
        https://github.com/ixmelrd/rupc2017/blob/master/Graduation_Ceremony/Yazaten/main.cc
*/
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[2][2][2001][2001];

int main() {
  memset(dp, 0, sizeof(dp));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int k;
  cin >> k;

  int n = s.length();
  map<char, int> mp[2][2];
  mp[0][0]['U'] = 0, mp[0][0]['D'] = 0, mp[0][0]['R'] = +1, mp[0][0]['L'] = -1;
  mp[0][1]['U'] = 0, mp[0][1]['D'] = 0, mp[0][1]['R'] = -1, mp[0][1]['L'] = +1;
  mp[1][0]['U'] = +1, mp[1][0]['D'] = -1, mp[1][0]['R'] = 0, mp[1][0]['L'] = 0;
  mp[1][1]['U'] = -1, mp[1][1]['D'] = +1, mp[1][1]['R'] = 0, mp[1][1]['L'] = 0;

  rep(x_y, 2) {
    rep(pos_neg, 2) {
      for (int i = 1; i <= n; ++i) {
        for (int j = 0; j <= k; ++j) {
          int sign = (j % 2 == 0 ? +1 : -1);
          int add = mp[x_y][pos_neg][s[i - 1]] * sign;
          dp[x_y][pos_neg][i][j] = dp[x_y][pos_neg][i - 1][j] + add;
          if (j - 1 >= 0) {
            dp[x_y][pos_neg][i][j] = max(dp[x_y][pos_neg][i][j],
                                         dp[x_y][pos_neg][i - 1][j - 1] - add);
          } // end if
        }   // end for
      }     // end for
    }       // end rep
  }         // end rep

  vector<int> x(k + 1, 0), y(k + 1, 0);
  for (int i = 0; i <= k; ++i) {
    x[i] = max(abs(dp[0][0][n][i]), abs(dp[0][1][n][i]));
  }                                             // end for
  rep(i, k) { x[i + 1] = max(x[i + 1], x[i]); } // end rep
  for (int i = 0; i <= k; ++i) {
    y[i] = max(abs(dp[1][0][n][i]), abs(dp[1][1][n][i]));
  }                                             // end for
  rep(i, k) { y[i + 1] = max(y[i + 1], y[i]); } // end for

  int ans = -1;
  for (int i = 0; i <= k; ++i) {
    ans = max(ans, x[i] + y[k - i]);
  } // end for
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

string dps[2002][3][2002]; // [0]-[1]:???????????? [1]:U <-> D ????????? [2]: L
                           // <-> R ????????? j ??????????????????
int dp[2002][3]
      [2002]; // [i????????????][j???????????¶???][???????????????????????°]

int calc_manhattan(string str) {
  int n = str.length();
  int currX = 0;
  int currY = 0;
  rep(i, n) {
    char c = str[i];
    switch (c) {
    case 'U':
      ++currY;
      break;
    case 'D':
      --currY;
      break;
    case 'L':
      ++currX;
      break;
    case 'R':
      --currX;
      break;
    } // end switch
  }   // end rep
  int res = abs(currX) + abs(currY);
  return res;
}

string trans_str(string s, int start_pos, int type) {
  if (type == 0)
    return s;

  int n = s.length();
  string t = s;
  switch (type) {
  case 1:
    for (int k = start_pos; k < n; ++k) {
      if (t[k] == 'U') {
        t[k] = 'D';
      } else if (t[k] == 'D') {
        t[k] = 'U';
      } // end if
    }   // end for
    break;
  case 2:
    for (int k = start_pos; k < n; ++k) {
      if (t[k] == 'L') {
        t[k] = 'R';
      } else if (t[k] == 'R') {
        t[k] = 'L';
      } // edn if
    }   // end for
    break;
  } // end switch;
  return t;
}

int main() {
  memset(dp, 0, sizeof(dp));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string S;
  cin >> S;
  int K;
  cin >> K;

  dp[0][0][K] = calc_manhattan(S);
  string s1 = trans_str(S, 0, 1);
  dp[0][1][K - 1] = calc_manhattan(s1);
  string s2 = trans_str(S, 0, 2);
  dp[0][2][K - 1] = calc_manhattan(s2);

  dps[0][0][K] = S;
  dps[0][1][K - 1] = s1;
  dps[0][2][K - 1] = s2;

  int n = S.length();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      int curr = 0;
      for (int k2 = K; k2 >= 0; --k2) {
        if (dp[i][j][k2] != 0) {
          for (int k = 0; k < 3; ++k) {
            string ot = dps[i][j][k2];
            if (k == 0) {
              if (dp[i + 1][k][k2] < dp[i][j][k2]) {
                dp[i + 1][k][k2] = dp[i][j][k2];
                dps[i + 1][k][k2] = ot;
              } // end if
            } else if (k == 1) {
              string t1 = trans_str(ot, i, 1);
              int curr = calc_manhattan(t1);
              if (k2 >= 1) {
                if (dp[i + 1][k][k2 - 1] < curr) {
                  dp[i + 1][k][k2 - 1] = curr;
                  dps[i + 1][k][k2 - 1] = t1;
                } else if (dp[i + 1][k][k2 - 1] < dp[i][j][k2 - 1]) {
                  dp[i + 1][k][k2 - 1] = dp[i][j][k2 - 1];
                  dps[i + 1][k][k2 - 1] = dps[i][j][k2 - 1];
                } else if (dp[i + 1][k][k2 - 1] < dp[i][j][k2]) {
                  dp[i + 1][k][k2 - 1] = dp[i][j][k2];
                  dps[i + 1][k][k2 - 1] = dps[i][j][k2];
                }    // end if
              }      // end if
            } else { // if (k == 2)
              string t2 = trans_str(ot, i, 2);
              int curr = calc_manhattan(t2);
              if (k2 >= 1) {
                if (dp[i + 1][k][k2 - 1] < curr) {
                  dp[i + 1][k][k2 - 1] = curr;
                  dps[i + 1][k][k2 - 1] = t2;
                } else if (dp[i + 1][k][k2 - 1] < dp[i][j][k2 - 1]) {
                  dp[i + 1][k][k2 - 1] = dp[i][j][k2 - 1];
                  dps[i + 1][k][k2 - 1] = dps[i][j][k2 - 1];
                } else if (dp[i + 1][k][k2 - 1] < dp[i][j][k2]) {
                  dp[i + 1][k][k2 - 1] = dp[i][j][k2];
                  dps[i + 1][k][k2 - 1] = dps[i][j][k2];
                } // end if
              }   // end if
            }     // end if
          }       // end for
        }         // end if
      }           // end for
    }             // end for
  }               // end for
                  /*
                          for (int i = 0; i < 3; ++i){
                                  for (int j = K; j >= 0; --j){
                                          cerr << dps[n][i][j] << ' ';
                                  } // end for
                                  cerr << endl;
                          } // end for
                  */
  int res = 0;
  for (int j = 0; j < 3; ++j) {
    for (int k = K; k >= 0; --k) {
      res = max(res, dp[n][j][k]);
    } // end for
  }   // end for
  cout << res << endl;

  return 0;
}