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

#include <bits/stdc++.h>
#include <iostream>
#define MAX 100;

using namespace std;
int N, M, K;
int a[20];
int b[20];

typedef pair<int, int> ii;

void input() {
  cin >> N >> M >> K;

  vector<ii> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    vec[i].first = a[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> b[i];
    vec[i].second = b[i];
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < N; ++i) {
    a[i] = vec[i].first;
    b[i] = vec[i].second;
  }
}
int field[3];
int array[100000];
int main() {
  int spa = 0;
  input();
  int maxi = M;
  if (K == 1) {
    for (int k = 0; k < N; k++) {
      int ans = 0;
      int las_Imo = M;
      field[0] = k;
      for (int i = 0; i >= 0; i--) {
        for (int t = 0; t < b[field[i]]; t++) {
          if (las_Imo) {
            las_Imo--;
            ans = ans + a[field[i]];
          }
        }
      }
      ans = ans + ((las_Imo >= 0) ? las_Imo : 0);
      maxi = max(maxi, ans);
    }
  } else if (K == 2) {
    for (int k = 0; k < N; k++) {
      for (int l = k + 1; l < N; l++) {
        int ans = 0;
        int las_Imo = M;
        field[0] = k;
        field[1] = l;
        for (int i = 1; i >= 0; i--) {
          for (int t = 0; t < b[field[i]]; t++) {
            las_Imo--;
            if (las_Imo < 0)
              break;
            ans = ans + a[field[i]];
          }
        }
        ans = ans + ((las_Imo >= 0) ? las_Imo : 0);
        maxi = max(maxi, ans);
      }
    }
  } else {
    for (int k = 0; k < N; k++) {
      for (int l = k + 1; l < N; l++) {
        for (int m = l + 1; m < N; m++) {
          int ans = 0;
          int las_Imo = M;
          field[0] = k;
          field[1] = l;
          field[2] = m;
          for (int i = 2; i >= 0; i--) {
            for (int t = 0; t < b[field[i]]; t++) {
              las_Imo--;
              if (las_Imo < 0)
                break;
              ans = ans + a[field[i]];
            }
          }

          ans = ans + ((las_Imo >= 0) ? las_Imo : 0);
          array[spa++] = ans;
          maxi = max(maxi, ans);
        }
      }
    }
  }
  // sort(array,array+100000,greater<int>());
  // cout<<array[0]<<endl;
  cout << maxi << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;

struct Edge {
  int dst;
  ll w;
};

struct Data {
  int cur;
  ll w;
  bool operator<(const Data &data) const {
    if (w != data.w)
      return w < data.w;
    return cur < data.cur;
  }
};

const int MAX_V = 100010;

int V, E;
vector<Edge> G[MAX_V], rG[MAX_V];
ll maxi[MAX_V];
bool valid[MAX_V];
map<int, set<int>> mp;

bool check(ll limit) {
  memset(maxi, -1, sizeof maxi);
  priority_queue<Data> Q;
  /*
  for(set<int>::iterator itr=mp[limit].begin();itr!=mp[limit].end();itr++) {
    maxi[*itr] = limit;
    Q.push((Data){*itr,limit});
  }
  */
  rep(i, (int)G[V - 1].size()) {
    if (G[V - 1][i].w < limit)
      continue;
    maxi[G[V - 1][i].dst] = G[V - 1][i].w;
    Q.push((Data){G[V - 1][i].dst, G[V - 1][i].w});
  }
  while (!Q.empty()) {
    Data data = Q.top();
    Q.pop();
    if (data.cur == 0) {
      return true;
    }
    rep(i, (int)G[data.cur].size()) {
      Edge &e = G[data.cur][i];
      if (e.dst == V - 1)
        continue;
      if (!valid[e.dst])
        continue;
      if (!(data.w >= e.w))
        continue;
      if (maxi[e.dst] < e.w) {
        maxi[e.dst] = e.w;
        Q.push((Data){e.dst, e.w});
      }
    }
  }
  return false;
}

void pre_calc() {
  memset(valid, false, sizeof valid);
  deque<int> deq;
  deq.push_back(0);
  valid[0] = valid[V - 1] = true;
  while (!deq.empty()) {
    int cur = deq.front();
    deq.pop_front();
    if (cur == V - 1)
      continue;
    rep(i, (int)rG[cur].size()) {
      int next = rG[cur][i].dst;
      if (!valid[next]) {
        valid[next] = true;
        deq.push_back(next);
      }
    }
  }
}

void compute() {
  pre_calc();
  vector<ll> vec;

  rep(i, (int)G[V - 1].size()) {
    if (valid[G[V - 1][i].dst]) {
      vec.push_back(G[V - 1][i].w);
    }
  }
  if (vec.empty()) {
    puts("-1");
    return;
  }
  sort(vec.begin(), vec.end());
  int L = 0, R = (int)vec.size();
  while (R - L) {
    int M = (L + R) / 2;
    if (check(vec[M]))
      L = M + 1;
    else
      R = M;
  }
  if (L - 1 < 0) {
    puts("-1");
  } else {
    cout << vec[L - 1] << endl;
  }
}

int main() {
  cin >> V >> E;
  rep(i, E) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    G[b].push_back((Edge){a, c});
    rG[a].push_back((Edge){b, c});
  }
  compute();
  return 0;
}