Problem p03958 (C++)

Buggy submission (status: Runtime Error):
// #pragma warning(disable:4996)
#include <assert.h>
#include <memory.h>
#include <stdio.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long int64;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define ALL(x) (x).begin(), (x).end()
#define CLEAR(x) (x).clear()
#define RESET(x) memset((x), 0, sizeof(x))
#define MP(X, Y) make_pair(X, Y) // NOTES:MP(

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define RE(i, n) FOR(i, 0, (n))
#define FOREACH(i, x, y) for (int i = (x); i <= (y); ++i)

const ll maxn = (ll)77777;
const ll maxm = (ll)1e6 + 10;
const ll MOD = 786433;
const ll INF = 1e15;

int fast_read() {
  int input;
  char c = 0;
  while (c < 33)
    c = getchar();
  input = 0;
  while (c > 33) {
    input = input * 10 + c - '0';
    c = getchar();
  }
  return input;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int k, t;
  while (cin >> k >> t) {
    vi a[110];
    RE(i, t) {
      int x;
      cin >> x;
      a[x].push_back(i);
    }
    int p, q;
    vector<pair<pii, int>> match;
    while (true) {
      p = 100;
      while (p > 0 && a[p].empty())
        --p;
      if (p <= 0) {
        break;
      }
      int t1 = a[p].back();
      a[p].pop_back();

      q = p;
      while (q > 0 && a[q].empty())
        --q;
      if (q <= 0) {
        a[p].push_back(t1);
        break;
      }
      int t2 = a[q].back();
      a[q].pop_back();
      match.push_back(MP(MP(t1, t2), q));
      if (q < p) {
        a[p - q].push_back(t1);
      }
    }
    if (p > 0 && !a[p].empty()) {
      int tt = a[p].back();
      for (auto m : match) {
        if (m.first.first != tt && m.first.second != tt) {
          p -= m.second;
        }
      }
    }
    int ans = max(0, p - 1);
    cout << ans << endl;
  }

  // cout << (float)(clock() - start_time)/CLOCKS_PER_SEC << "s" << endl;
  return 0;
}


Fixed submission:
// #pragma warning(disable:4996)
#include <assert.h>
#include <memory.h>
#include <stdio.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long int64;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define ALL(x) (x).begin(), (x).end()
#define CLEAR(x) (x).clear()
#define RESET(x) memset((x), 0, sizeof(x))
#define MP(X, Y) make_pair(X, Y) // NOTES:MP(

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define RE(i, n) FOR(i, 0, (n))
#define FOREACH(i, x, y) for (int i = (x); i <= (y); ++i)

const ll maxn = (ll)77777;
const ll maxm = (ll)1e6 + 10;
const ll MOD = 786433;
const ll INF = 1e15;

int fast_read() {
  int input;
  char c = 0;
  while (c < 33)
    c = getchar();
  input = 0;
  while (c > 33) {
    input = input * 10 + c - '0';
    c = getchar();
  }
  return input;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int k, t;
  while (cin >> k >> t) {
    vi a[110];
    RE(i, t) {
      int x;
      cin >> x;
      a[x].push_back(i);
    }
    int p, q;
    vector<pair<pii, int>> match;
    while (true) {
      p = 100;
      while (p > 0 && a[p].empty())
        --p;
      if (p <= 0) {
        break;
      }
      int t1 = a[p].back();
      a[p].pop_back();

      q = p;
      while (q > 0 && a[q].empty())
        --q;
      if (q <= 0) {
        a[p].push_back(t1);
        break;
      }
      int t2 = a[q].back();
      a[q].pop_back();
      match.push_back(MP(MP(t1, t2), q));
      if (q < p) {
        a[p - q].push_back(t1);
      }
    }
    if (p > 0 && !a[p].empty()) {
      int tt = a[p].back();
      for (auto m : match) {
        if (m.first.first != tt && m.first.second != tt) {
          p -= m.second;
        }
      }
    }
    int ans = max(0, p - 1);
    cout << ans << endl;
  }

  // cout << (float)(clock() - start_time)/CLOCKS_PER_SEC << "s" << endl;
  return 0;
}