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

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#include <random>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a; c > 0 ? i < b : i > b; i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define dump(x) cerr << #x << " = " << (x) << endl
#define int long long
#ifdef _MSC_VER
const bool test = true;
#else
const bool test = false;
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;
const ll INFL = (ll)1 << 58;
ll mod = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class InputIterator> int sum(InputIterator begin, InputIterator end) {
  return accumulate(begin, end, 0ll);
}

string reverse_str(string s) {
  reverse(all(s));
  return s;
}

// double dist(double a, double b) {
//	while (ans < 0) ans += 360;
//	while (ans > 360) ans -= 360;
//
//	while (ans < 0) ans += 360;
//	while (ans > 360) ans -= 360;
// }

int N;
int r[MAX];
vector<int> a[3];

void solve() {
  cin >> N;
  rep(i, 3) a[i].resize(N);
  rep(i, N) cin >> r[i];
  rep(i, N) {
    rep(j, 3) { cin >> a[j][i]; }
  }

  int min_cost = INF;
  rep(i, 3) {
    rep(v, 102) {
      vector<pair<int, int>> score(N);
      rep(k, N) score[k].first = 0;
      rep(k, N) score[k].second = k;
      rep(k, 3) {
        vector<pair<int, int>> tmp(N);
        rep(j, N) {
          tmp[j].first = a[k][j];
          tmp[j].second = j;
        }
        if (i == k)
          tmp[0].first += v;
        map<int, int> m;
        sort(all(tmp), greater<pair<int, int>>());
        rep(j, N) {
          if (m.count(tmp[j].first) == 0)
            m[tmp[j].first] = j;
          score[tmp[j].second].first += r[m[tmp[j].first]];
        }
      }
      sort(all(score), greater<pair<int, int>>());
      map<int, int> m;
      rep(j, N) {
        if (m.count(score[j].first) == 0)
          m[score[j].first] = j;
        if (score[j].second == 0 && m[score[j].first] >= 8) {
          goto next;
        }
      }
      chmin(min_cost, v);

      // if (score.back().second != 0
      //	|| score[N-1].first == score[N-2].first) {
      //	//rep(k, N) cout << score[k].first << " ";
      //	//cout << endl;
      //	//dump(i);
      //	//dump(v);
      //	chmin(min_cost, v);
      //	break;
      // }
    next:;
    }
  }
  if (min_cost == INF)
    cout << "Saiko" << endl;
  else
    cout << min_cost << endl;
}

signed main() {
  srand(time(NULL));
  int T = (int)1e15;
  solve();
  cout << fixed << setprecision(15);
  rep(i, T) {
    char s[MAX];
    if (scanf("%s", s) == EOF)
      break;
    int n = strlen(s);
    for (int i = n - 1; i > -1; i--) {
      ungetc(s[i], stdin);
    }
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#include <random>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a; c > 0 ? i < b : i > b; i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define dump(x) cerr << #x << " = " << (x) << endl
#define int long long
#ifdef _MSC_VER
const bool test = true;
#else
const bool test = false;
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;
const ll INFL = (ll)1 << 58;
ll mod = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class InputIterator> int sum(InputIterator begin, InputIterator end) {
  return accumulate(begin, end, 0ll);
}

string reverse_str(string s) {
  reverse(all(s));
  return s;
}

// double dist(double a, double b) {
//	while (ans < 0) ans += 360;
//	while (ans > 360) ans -= 360;
//
//	while (ans < 0) ans += 360;
//	while (ans > 360) ans -= 360;
// }

int N;
int r[MAX];
vector<int> a[3];
int pre_score[3][MAX];

void solve() {
  cin >> N;
  rep(i, 3) a[i].resize(N);
  rep(i, N) cin >> r[i];
  rep(i, N) {
    rep(j, 3) { cin >> a[j][i]; }
  }

  // rep(i, 3) {
  //	vector<int> tmp = a[i];
  //	map<int, int> m;
  //	sort(all(tmp), greater<int>());
  //	rep(j, N) {
  //		if (m.count(tmp[j]) == 0) m[tmp[j]] = j;
  //		pre_score[i][j] = r[m[tmp[j]]];
  //	}
  // }

  int min_cost = INF;
  rep(i, 3) {
    rep(v, 102) {
      vector<pair<int, int>> score(N);
      rep(k, N) score[k].first = 0;
      rep(k, N) score[k].second = k;
      rep(k, 3) {
        vector<pair<int, int>> tmp(N);
        rep(j, N) {
          tmp[j].first = a[k][j];
          tmp[j].second = j;
        }
        if (i == k)
          tmp[0].first += v;
        map<int, int> m;
        sort(all(tmp), greater<pair<int, int>>());
        rep(j, N) {
          if (m.count(tmp[j].first) == 0)
            m[tmp[j].first] = j;
          score[tmp[j].second].first += r[m[tmp[j].first]];
        }
      }
      sort(all(score), greater<pair<int, int>>());
      if (score.back().second != 0 ||
          score.back().first == score[N - 2].first) {
        rep(k, N) cout << score[k].first << " ";
        cout << endl;
        dump(i);
        dump(v);
        chmin(min_cost, v);
        break;
      }
    }
  }
  if (min_cost == INF)
    cout << "Saiko" << endl;
  else
    cout << min_cost << endl;
}

signed main() {
  srand(time(NULL));
  int T = (int)1e15;
  solve();
  cout << fixed << setprecision(15);
  rep(i, T) {
    char s[MAX];
    if (scanf("%s", s) == EOF)
      break;
    int n = strlen(s);
    for (int i = n - 1; i > -1; i--) {
      ungetc(s[i], stdin);
    }
    solve();
  }
  return 0;
}