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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int pow_int(int a, int n) {
  int i;
  int ans = 1;
  for (i = 0; i < n; ++i) {
    ans *= a;
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int i, j;
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  vector<ll> p(n);
  for (i = 0; i < n; ++i) {
    cin >> x.at(i);
    cin >> y.at(i);
    cin >> p.at(i);
  }
  int bit = 0;

  vector<vector<int>> xmiti((1 << n), vector<int>(15, 0));
  vector<vector<int>> ymiti((1 << n), vector<int>(15, 0));
  for (bit = 0; bit < (1ll << n); ++bit) {
    set<int> yy;
    set<int> tt;
    for (i = 0; i < n; ++i) {
      if (bit & (1 << i)) {
        yy.insert(x.at(i));
        tt.insert(y.at(i));
      }
    }
    yy.insert(0);
    tt.insert(0);
    vector<int> yoko;
    vector<int> tate;
    for (auto xx : yy) {
      yoko.push_back(xx);
    }
    for (auto xx : tt) {
      tate.push_back(xx);
    }

    for (i = 0; i < n; ++i) {
      int xa = lower_bound(all(yoko), x.at(i)) - yoko.begin();
      int ya = lower_bound(all(tate), y.at(i)) - tate.begin();
      int xd, yd;

      if (xa >= yoko.size()) {
        xd = abs(x.at(i) - yoko.back());
      } else if (xa == 0) {
        xd = abs(x.at(i) - yoko.front());
      } else {
        xd = min(abs(x.at(i) - yoko.at(xa)), abs(x.at(i) - yoko.at(xa - 1)));
      }

      if (ya >= tate.size()) {
        yd = abs(y.at(i) - tate.back());
      } else if (ya == 0) {
        yd = abs(y.at(i) - tate.front());
      } else {
        yd = min(abs(y.at(i) - tate.at(ya)), abs(y.at(i) - tate.at(ya - 1)));
      }

      xmiti.at(bit).at(i) = xd;
      ymiti.at(bit).at(i) = yd;
    }
  }

  vector<ll> ans(n + 1, (1ll << 60));

  int INF = pow_int(3, n);
  for (bit = 0; bit < INF; ++bit) {
    int waru = INF / 3;
    int tmpbit = bit;
    int m = 0;
    int bit1 = 0;
    int bit2 = 0;
    for (i = 0; i < n; ++i) {
      int tmp = tmpbit / waru;
      tmpbit -= tmp * waru;
      waru /= 3;

      if (tmp)
        ++m;
      if (tmp == 1)
        bit1 += (1 << i);
      else if (tmp == 2)
        bit2 += (1 << i);
    }

    ll tmpans = 0;
    for (i = 0; i < n; ++i) {
      ll xd = xmiti.at(bit1).at(i);
      ll yd = ymiti.at(bit2).at(i);
      tmpans += p.at(i) * min(xd, yd);
    }
    ans.at(m) = min(ans.at(m), tmpans);
  }

  for (i = 0; i <= n; ++i) {
    cout << ans.at(i) << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll i, j;
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (i = 0; i < n; ++i) {
    cin >> a.at(i);
  }
  ll ans = 1000;
  ll pre = a.at(0);
  ll now = a.at(0);
  bool flag = true;
  for (i = 1; i < n; ++i) {
    if (flag) {
      if (a.at(i) >= now)
        now = a.at(i);
      else {
        ll kabu = ans / pre;
        ans -= kabu * pre;
        ans += kabu * now;
        now = a.at(i);
        flag = false;
      }
    } else {
      if (a.at(i) <= now)
        now = a.at(i);
      else {
        pre = now;
        now = a.at(i);
        flag = true;
      }
    }
  }
  if (flag && now > pre) {
    ll kabu = ans / pre;
    ans -= kabu * pre;
    ans += kabu * now;
  }
  cout << ans << endl;

  return 0;
}