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

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007
#define llMAX 9223372036854775807
#define llMIN -9223372036854775808
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, k, x, y;

// 2つの要素をまとめてソート、前の変数が優先。flag=0で小さい順、1で大きい順
void mysort2(vi &a, vi &b, ll flag) {
  if (a.size() > 0) {
    vector<pair<ll, ll>> p(a.size());
    REP(i, a.size()) {
      p[i].first = a[i];
      p[i].second = b[i];
    }
    sort(p.begin(), p.end());
    if (flag == 1) {
      reverse(p.begin(), p.end());
    }
    REP(i, a.size()) {
      a[i] = p[i].first;
      b[i] = p[i].second;
    }
  }
  return;
}

int main() {
  ll ans = 0;
  cin >> n;
  vector<string> s(n);
  vi inc(n), higt(n), mns(n);
  REP(i, n) cin >> s[i];

  string ss;

  REP(i, n) {
    ss = s[i];

    ll mn = 0;
    for (ll k = 0; k < ss.length(); k++) {
      inc[i] += 1 - (ll)(ss[k] - 40) * 2;
      mn = min(mn, inc[i]);
    }
    mns[i] = mn;
    higt[i] = min(inc[i] - mn, -mn);
  }

  ll tmp = 0;
  ll plusn = 0;
  for (ll i = 0; i < n; i++) {
    tmp += inc[i];
    if (inc[i] >= 0)
      plusn++;
  }
  if (tmp != 0) {
    cout << "No" << endl;
    return 0;
  }

  // 高くしていく
  // incがプラスでheightが低い順に
  tmp = 0;
  vi incp(plusn), higtp(plusn);

  for (ll i = 0; i < n; i++) {
    if (inc[i] >= 0) {
      incp[tmp] = inc[i];
      higtp[tmp] = higt[i];
      tmp++;
    }
  }

  mysort2(higtp, incp, 0);

  ll nowhight = 0;

  for (ll i = 0; i < plusn; i++) {
    if (nowhight < higtp[i]) {
      cout << "No" << endl;
      return 0;
    }
    nowhight += incp[i];
  }

  // 下げていく
  nowhight = 0;
  ll mnusn = n - plusn;
  vi incm(mnusn), higtm(mnusn);

  tmp = 0;
  for (ll i = 0; i < n; i++) {
    if (inc[i] < 0) {
      incm[tmp] = -inc[i];
      higtm[tmp] = higt[i];
      tmp++;
    }
  }

  mysort2(higtm, incm, 0);

  for (ll i = 0; i < mnusn; i++) {
    if (nowhight < higtm[i]) {
      cout << "No" << endl;
      return 0;
    }
    nowhight += incm[i];
  }

  /*
  mysort2(higtm,incm,1);


  for(ll i=0;i<mnusn;i++){
    nowhight+=incm[i];
    if (nowhight<higtm[i]){
      cout<<"No"<<endl;
      return 0;
    }
  }
  */

  cout << "Yes" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007
#define llMAX 9223372036854775807
#define llMIN -9223372036854775808
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, k, x, y;

// 2つの要素をまとめてソート、前の変数が優先。flag=0で小さい順、1で大きい順
void mysort2(vi &a, vi &b, ll flag) {
  vector<pair<ll, ll>> p(a.size());
  REP(i, n) {
    p[i].first = a[i];
    p[i].second = b[i];
  }
  sort(p.begin(), p.end());
  if (flag == 1) {
    reverse(p.begin(), p.end());
  }
  REP(i, n) {
    a[i] = p[i].first;
    b[i] = p[i].second;
  }
  return;
}

int main() {
  ll ans = 0;
  cin >> n;
  vector<string> s(n);
  vi inc(n), higt(n);
  REP(i, n) cin >> s[i];

  string ss;

  REP(i, n) {
    ss = s[i];

    ll teiidx = ss.length();
    ll h = 0;
    for (ll k = 0; k < ss.length(); k++) {
      inc[i] += 1 - (ll)(ss[k] - 40) * 2;
      if (h > inc[i]) {
        h = inc[i];
        teiidx = k + 1;
      }
    }

    for (ll k = teiidx; k < ss.length(); k++) {
      higt[i] += 1 - (ss[k] - 40) * 2;
    }
  }

  ll tmp = 0;
  ll plusn = 0;
  for (ll i = 0; i < n; i++) {
    tmp += inc[i];
    if (inc[i] >= 0)
      plusn++;
  }
  if (tmp != 0) {
    cout << "No" << endl;
    return 0;
  }

  // 高くしていく
  // incがプラスでheightが低い順に
  tmp = 0;
  vi incp(plusn + 1), higtp(plusn + 1);

  for (ll i = 0; i < n; i++) {
    if (inc[i] >= 0) {
      incp[tmp] = inc[i];
      higtp[tmp] = higt[i];
      tmp++;
    }
  }

  if (plusn != 0) {
    mysort2(higtp, incp, 0);
  }

  ll nowhight = 0;
  for (ll i = 0; i < plusn; i++) {
    if (nowhight < higtp[i] - incp[i]) {
      cout << "No" << endl;
      return 0;
    }
    nowhight += incp[i];
  }

  // 下げていく
  ll mnusn = n - plusn;
  vi incm(mnusn + 1), higtm(mnusn + 1);

  tmp = 0;
  for (ll i = 0; i < n; i++) {
    if (inc[i] < 0) {
      incm[tmp] = inc[i];
      higtm[tmp] = higt[i];
      tmp++;
    }
  }

  if (mnusn != 0) {
    mysort2(higtm, incm, 1);
  }

  for (ll i = 0; i < mnusn; i++) {
    nowhight += incm[i];
    if (nowhight < higtm[i]) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  return 0;
}