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

#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
// #define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c), end(c)
// #define EXIST(s, e) ((s).find(e)!=(s).end())
// #define SORT(c) sort(begin(c),end(c))
// #define pb emplace_back
// #define MP make_pair
// #define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する
vector<ll> a, b;
int dp[2][52][52][52][52][5][2];
int used[2][52][52][52][52][5][2];
int n, m;

ll calc(int my_s, int my_e, int en_s, int en_e) {
  ll ret = 0;
  for (int i = my_s; i < my_e; i++) {
    if (a[i] > 0)
      ret += a[i];
  }
  for (int i = en_s; i < en_e; i++) {
    if (b[i] > 0)
      ret -= b[i];
  }
  return ret;
}

ll dfs(int player, int my_s, int my_e, int en_s, int en_e, int pass, int rest) {
  if (used[player][my_s][my_e][en_s][en_e][pass][rest]) {
    return dp[player][my_s][my_e][en_s][en_e][pass][rest];
  }
  used[player][my_s][my_e][en_s][en_e][pass][rest] = true;
  if (player == 0) {
    if (my_e == n) {
      if (rest == 0) {
        // 場に出ていない
        if (pass >= 1) {
          ll pass_score = calc(my_s, my_e, en_s, en_e);
          return dp[player][my_s][my_e][en_s][en_e][pass][rest] = pass_score;
        } else {
          int next_pass = pass;
          next_pass++; // rest == 0
          ll pass_score = calc(my_s, my_e, en_s, en_e) +
                          dfs(1, my_e, my_e, en_e, en_e, next_pass, rest);
          return dp[player][my_s][my_e][en_s][en_e][pass][rest] = pass_score;
        }
      } else {
        int next_pass = 0;
        ll pass_score = calc(my_s, my_e, en_s, en_e) +
                        dfs(1, my_e, my_e, en_e, en_e, next_pass, 0);
        return dp[player][my_s][my_e][en_s][en_e][pass][rest] = pass_score;
      }
    }
    if (pass >= 1) {
      ll pass_score = calc(my_s, my_e, en_s, en_e);
      int next_en_s = (a[my_e] != -1) ? en_s : en_e;
      ll put_score = dfs(1, my_s, my_e + 1, next_en_s, en_e, 0, 1);
      ll ret = max(pass_score, put_score);
      return dp[player][my_s][my_e][en_s][en_e][pass][rest] = ret;
    }
    int next_pass = pass;
    if (rest == 0)
      next_pass++;
    ll pass_score = calc(my_s, my_e, en_s, en_e) +
                    dfs(1, my_e, my_e, en_e, en_e, next_pass, 0);
    int next_en_s = (a[my_e] != -1) ? en_s : en_e;
    ll put_score = dfs(1, my_s, my_e + 1, next_en_s, en_e, 0, 1);
    ll ret = max(pass_score, put_score);

    return dp[player][my_s][my_e][en_s][en_e][pass][rest] = ret;
  } else {
    if (en_e == m) {
      if (rest == 0) {
        // 場に出ていない
        if (pass >= 1) {
          ll pass_score = calc(my_s, my_e, en_s, en_e);
          return dp[player][my_s][my_e][en_s][en_e][pass][rest] = pass_score;
        } else {
          int next_pass = pass;
          next_pass++;
          ll pass_score = calc(my_s, my_e, en_s, en_e) +
                          dfs(0, my_e, my_e, en_e, en_e, next_pass, 0);
          return dp[player][my_s][my_e][en_s][en_e][pass][rest] = pass_score;
        }
      } else {
        int next_pass = 0;
        ll pass_score = calc(my_s, my_e, en_s, en_e) +
                        dfs(0, my_e, my_e, en_e, en_e, next_pass, 0);
        return dp[player][my_s][my_e][en_s][en_e][pass][rest] = pass_score;
      }
    }
    if (pass >= 1) {
      ll pass_score = calc(my_s, my_e, en_s, en_e);
      int next_my_s = (b[en_e] != -1) ? my_s : my_e;
      ll put_score = dfs(0, next_my_s, my_e, en_s, en_e + 1, 0, 1);
      ll ret = min(pass_score, put_score);
      return dp[player][my_s][my_e][en_s][en_e][pass][rest] = ret;
    }
    int next_pass = pass;
    if (rest == 0)
      next_pass++;
    ll pass_score = calc(my_s, my_e, en_s, en_e) +
                    dfs(0, my_e, my_e, en_e, en_e, next_pass, 0);
    int next_my_s = (b[en_e] != -1) ? my_s : my_e;
    ll put_score = dfs(0, next_my_s, my_e, en_s, en_e + 1, 0, 1);
    ll ret = min(pass_score, put_score);

    return dp[player][my_s][my_e][en_s][en_e][pass][rest] = ret;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  a.resize(n);
  b.resize(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  cout << dfs(0, 0, 0, 0, 0, 0, 0) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
// #define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c), end(c)
// #define EXIST(s, e) ((s).find(e)!=(s).end())
// #define SORT(c) sort(begin(c),end(c))
// #define pb emplace_back
// #define MP make_pair
// #define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する
vector<ll> a, b;
// ll dp[2][52][52][52][52][2];
// bool used[2][52][52][52][52][2];
ll dp[2][52][52][52];
ll used[2][52][52][52];
int n, m;

ll calc(vector<pair<int, int>> deck) {
  ll my = 0;
  ll enemy = 0;
  bool my_flag = true;
  bool enemy_flag = true;
  reverse(all(deck));
  for (auto e : deck) {
    if (e.first == 0) {
      if (e.second == -1) {
        enemy_flag = false;
      } else if (my_flag) {
        my += e.second;
      }
    } else {
      if (e.second == -1) {
        my_flag = false;
      } else if (enemy_flag) {
        enemy += e.second;
      }
    }
  }
  return my - enemy;
}

// ll dfs(int player, int mytop, int mybottom, int enemytop, int enemybottom,
// int pass, vector<pair<int, int>> deck) {
//     //deck.firstはプレイや, deck.secondはカード
//     if (player == 0) {
//         if (mybottom == n) {
//             //これ以上は無理
//             return calc(deck);
//         }
//         if (pass >= 1 && mytop == mybottom && enemytop == enemybottom) {
//             //ゲームエンド
//             ll pass_score = calc(deck);
//             auto next_deck = deck;
//             next_deck.emplace_back(0, a[mybottom]);
//             ll put_score = dfs(1, mytop, mybottom + 1, enemytop, enemybottom,
//             0, next_deck); return max(pass_score, put_score);
//         }
//         ll pass_score = calc(deck) + dfs(1, mybottom, mybottom, enemybottom,
//         enemybottom, pass + 1, deck); auto next_deck = deck;
//         next_deck.emplace_back(0, a[mybottom]);
//         ll put_score = dfs(1, mytop, mybottom + 1, enemytop, enemybottom, 0,
//         next_deck); return max(pass_score, put_score);
//     } else {
//         if (enemybottom == m) {
//             return calc(deck);
//         }
//         if (pass >= 1 && mytop == mybottom && enemytop == enemybottom) {
//             ll pass_score = calc(deck);
//             auto next_deck = deck;
//             next_deck.emplace_back(1, b[enemybottom]);
//             ll put_score = dfs(0, mytop, mybottom, enemytop, enemybottom + 1,
//             0, next_deck); return min(pass_score, put_score);
//         }
//         ll pass_score = calc(deck) + dfs(1, mybottom, mybottom, enemybottom,
//         enemybottom, pass + 1, deck); auto next_deck = deck;
//         deck.emplace_back(1, b[enemybottom]);
//         ll put_score = dfs(0, mytop, mybottom, enemytop, enemybottom, 0,
//         next_deck); return min(pass_score, put_score);
//     }
// }

ll dfs(int player, int my_rest, int enemy_rest, int pass,
       vector<pair<int, int>> deck) {
  if (player == 0) {
    if (my_rest == n) {
      if (pass >= 1 && deck.size() == 0) {
        return calc(deck);
      } else {
        int next_pass = (deck.size() == 0) ? pass + 1 : 0;
        return calc(deck) +
               dfs(1, my_rest, enemy_rest, next_pass, vector<pair<int, int>>());
      }
    }
    if (pass >= 1 && deck.size() == 0) {
      ll pass_score = calc(deck);
      auto next_deck = deck;
      next_deck.emplace_back(0, a[my_rest]);
      ll put_score = dfs(1, my_rest + 1, enemy_rest, 0, next_deck);
      return max(pass_score, put_score);
    }
    int next_pass = (deck.size() == 0) ? pass + 1 : 0;
    ll pass_score = calc(deck) + dfs(1, my_rest, enemy_rest, next_pass,
                                     vector<pair<int, int>>());
    auto next_deck = deck;
    next_deck.emplace_back(0, a[my_rest]);
    ll put_score = dfs(1, my_rest + 1, enemy_rest, 0, next_deck);
    return max(pass_score, put_score);
  } else {
    if (enemy_rest == m) {
      if (pass >= 1 && deck.size() == 0) {
        return calc(deck);
      } else {
        int next_pass = (deck.size() == 0) ? pass + 1 : 0;
        return calc(deck) +
               dfs(0, my_rest, enemy_rest, next_pass, vector<pair<int, int>>());
      }
    }
    if (pass >= 1 && deck.size() == 0) {
      ll pass_score = calc(deck);
      auto next_deck = deck;
      next_deck.emplace_back(1, b[enemy_rest]);
      ll put_score = dfs(0, my_rest, enemy_rest + 1, 0, next_deck);
      return min(pass_score, put_score);
    }
    int next_pass = (deck.size() == 0) ? pass + 1 : 0;
    ll pass_score = calc(deck) + dfs(0, my_rest, enemy_rest, next_pass,
                                     vector<pair<int, int>>());
    auto next_deck = deck;
    next_deck.emplace_back(1, b[enemy_rest]);
    ll put_score = dfs(0, my_rest, enemy_rest + 1, 0, next_deck);
    return min(pass_score, put_score);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  a.resize(n);
  b.resize(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  //    cout << dfs(0, 0, 0, 0, 0, 0, vector<pair<int, int>>());
  cout << dfs(0, 0, 0, 0, vector<pair<int, int>>()) << endl;

  return 0;
}