Problem p03343 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<double> VD;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int n, k, q;
  cin >> n >> k >> q;

  VI a(n);
  vector<P> p(n);
  REP(i, n) {
    a[i] = in();
    p[i] = P(a[i], i);
  }
  sort(ALL(p));

  VI non_use(n);
  int ans = 1e9 + 10;
  REP(i, n) {
    multiset<int> st;
    VI tmp;
    REP(j, n) {
      if (non_use[j]) {
        sort(ALL(tmp));
        REP(x, (int)tmp.size() - k + 1) { st.insert(tmp[x]); }
        tmp.clear();
      } else {
        tmp.push_back(a[j]);
      }
    }
    sort(ALL(tmp));
    REP(x, (int)tmp.size() - k + 1) { st.insert(tmp[x]); }
    // cout << i << endl;
    // for (int x : st) cout << x << " ";
    // cout << endl;
    if (st.size() >= q) {
      while (st.size() > q) {
        st.erase(*st.rbegin());
      }
      int tmp = *st.rbegin() - *st.begin();
      ans = min(ans, tmp);
    }
    non_use[p[i].second] = 1;
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<double> VD;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int n, k, q;
  cin >> n >> k >> q;

  VI a(n);
  vector<P> p(n);
  REP(i, n) {
    a[i] = in();
    p[i] = P(a[i], i);
  }
  sort(ALL(p));

  VI non_use(n);
  int ans = 1e9 + 10;
  REP(i, n) {
    multiset<int> st;
    VI tmp;
    REP(j, n) {
      if (non_use[j]) {
        sort(ALL(tmp));
        REP(x, (int)tmp.size() - k + 1) { st.insert(tmp[x]); }
        tmp.clear();
      } else {
        tmp.push_back(a[j]);
      }
    }
    sort(ALL(tmp));
    REP(x, (int)tmp.size() - k + 1) { st.insert(tmp[x]); }
    // cout << i << endl;
    // for (int x : st) cout << x << " ";
    // cout << endl;
    if (st.size() >= q) {
      while (st.size() > q) {
        st.erase(st.find(*st.rbegin()));
      }
      int tmp = *st.rbegin() - *st.begin();
      ans = min(ans, tmp);
    }
    non_use[p[i].second] = 1;
  }

  cout << ans << endl;
  return 0;
}