Problem p01541 (C++) — buggy submission, verdict: Runtime Error

/*
    ?????????????????¨??????
*/

#define __1__

#ifdef __1__
#include <algorithm>
#include <cstdio>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define _MAX 7
#define INF 7e10
#define MIN(a, b) (a) > (b) ? (b) : (a)
using namespace std;
typedef long long int lint;

int dist, n, m;
lint tm[_MAX]
       [_MAX + 2]; // tm[][0]?????????????????????,tm[][m+1]???????????´??????
lint S[_MAX];
lint D[_MAX];

lint rettm() {
  lint ans = 0;
  rep(i, n) { ans = max(ans, tm[i][m + 1]); }
  return ans;
}

void maketm(int x) {
  rep(i, m) { tm[x][i + 1] = tm[x][0] + D[i] * S[x]; }
  tm[x][m + 1] = tm[x][0] + S[x] * dist;
  return;
}

void show() {
  rep(i, n) {
    rep(j, m + 2) { printf("%4lld ", tm[i][j]); }
    cout << endl;
  }
  cout << endl;
  return;
}

int flag[5] = {0};

bool check() {
  rep(i, n - 1) {
    if (tm[i][0] >= tm[i + 1][0]) {
      return false;
    }
  }
  rep(i, n) {
    rep(j, n) {
      if (i < j && tm[i][m + 1] > tm[j][m + 1]) {
        bool hoge = 0;
        rep(k, m) {
          if (tm[i][k + 1] == tm[j][k + 1]) {
            rep(l, n) {
              if (l != i && l != j && tm[i][k + 1] == tm[l][k + 1])
                return false;
            }
            hoge = true;
          }
        }
        if (hoge == 0)
          return false;
      }
    }
  }
  return true;
}

lint dfs(int d) {
  lint ans = INF;
  if (d == n) {
    if (check()) {
      //            show();
      return rettm();
    } else {
      return 7e10;
    }
  }
  rep(i, n) {
    if (flag[i] == 1)
      continue;
    flag[i] = 1;
    if (i == 0) {
      tm[i][0] = 0;
      maketm(i);
      ans = min(dfs(d + 1), ans);
    } else if (flag[i - 1] == 1) {
      tm[i][0] = tm[i - 1][0] + 1;
      maketm(i);
      ans = min(dfs(d + 1), ans);
    }
    rep(j, n) {
      if (j == i || flag[j] == 0)
        continue;
      rep(k, m) {
        tm[i][0] = tm[j][k + 1] - D[k] * S[i];
        maketm(i);
        ans = min(dfs(d + 1), ans);
      }
      tm[i][0] = tm[j][m + 1] - dist * S[i];
      maketm(i);
      ans = min(dfs(d + 1), ans);
    }
    flag[i] = 0;
  }
  return ans;
}

int main() {
  cin >> dist >> n;
  rep(i, n) { cin >> S[i]; }
  cin >> m;
  rep(i, m) { cin >> D[i]; }
  cout << dfs(0) << endl;
  return 0;
}
#endif

Error: 1

Fixed submission:

/*
    ?????????????????¨??????
*/

#define __1__

#ifdef __1__
#include <algorithm>
#include <cstdio>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define _MAX 7
#define INF 7e10
#define MIN(a, b) (a) > (b) ? (b) : (a)
using namespace std;
typedef long long int lint;

int dist, n, m;
lint tm[_MAX]
       [_MAX + 2]; // tm[][0]?????????????????????,tm[][m+1]???????????´??????
lint S[_MAX];
lint D[_MAX];

void maketm(int x) {
  rep(i, m) { tm[x][i + 1] = tm[x][0] + D[i] * S[x]; }
  tm[x][m + 1] = tm[x][0] + S[x] * dist;
  return;
}

void show() {
  rep(i, n) {
    rep(j, m + 2) { printf("%4lld ", tm[i][j]); }
    cout << endl;
  }
  cout << endl;
  return;
}

int flag[5] = {0};

bool check() {
  rep(i, n - 1) {
    if (tm[i][0] >= tm[i + 1][0]) {
      return false;
    }
  }
  rep(i, n) {
    rep(j, n) {
      if (i < j && tm[i][m + 1] > tm[j][m + 1]) {
        bool hoge = 0;
        rep(k, m) {
          if (tm[i][k + 1] == tm[j][k + 1]) {
            rep(l, n) {
              if (l != i && l != j && tm[i][k + 1] == tm[l][k + 1])
                return false;
            }
            hoge = true;
          }
        }
        if (hoge == 0)
          return false;
      }
    }
  }
  return true;
}

lint dfs(int d) {
  lint ans = INF;
  if (d == n) {
    if (check()) {
      return tm[n - 1][m + 1];
    } else {
      return 7e10;
    }
  }
  rep(i, n) {
    if (flag[i] == 1)
      continue;
    flag[i] = 1;
    if (i == 0) {
      tm[i][0] = 0;
      maketm(i);
      ans = min(dfs(d + 1), ans);
    } else if (flag[i - 1] == 1) {
      tm[i][0] = tm[i - 1][0] + 1;
      maketm(i);
      ans = min(dfs(d + 1), ans);
    }
    rep(j, n) {
      if (j == i || flag[j] == 0)
        continue;
      rep(k, m) {
        tm[i][0] = tm[j][k + 1] - D[k] * S[i];
        maketm(i);
        ans = min(dfs(d + 1), ans);
      }
      tm[i][0] = tm[j][m + 1] - dist * S[i];
      maketm(i);
      ans = min(dfs(d + 1), ans);
    }
    flag[i] = 0;
  }
  return ans;
}

int main() {
  cin >> dist >> n;
  rep(i, n) { cin >> S[i]; }
  cin >> m;
  rep(i, m) { cin >> D[i]; }
  cout << dfs(0) << endl;
  return 1;
}
#endif