prithivMLmods/Coder-Stat huggingface dataset:
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI (acos(-1.0))
#define mod 1000000007
#define maxn 200005
#define IN freopen("nocross.in", "r", stdin)
#define OUT freopen("nocross.out", "w", stdout)
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG pf("Hi\n")
#define INF 2000000000
#define i64 long long int
#define eps (1e-8)
#define xx first
#define yy second
#define ln 17
#define off 2

using namespace __gnu_pbds;
using namespace std;

typedef tree<i64, null_type, less<i64>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

char s[200005];
int w[200005], e[200005];

int main() {
  int i, j, k, l, m, n;

  scanf("%d", &n);

  scanf("%s", s + 1);

  for (i = 1; i <= n; i++) {
    w[i] = w[i - 1];
    if (s[i] == 'W')
      w[i] += 1;
  }
  for (i = n; i >= 1; i--) {
    e[i] = e[i + 1];
    if (s[i] == 'E')
      e[i] += 1;
  }
  int ans = 200000;

  for (i = 1; i <= n; i++)
    ans = min(ans, w[i - 1] + e[i + 1]);
  // for(i=1 ; i<=n ;i++) printf("%d %d\n",w[i],e[i]) ;

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI (acos(-1.0))
#define mod 1000000007
#define maxn 200005
#define IN freopen("nocross.in", "r", stdin)
#define OUT freopen("nocross.out", "w", stdout)
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG pf("Hi\n")
#define INF 2000000000
#define i64 long long int
#define eps (1e-8)
#define xx first
#define yy second
#define ln 17
#define off 2

using namespace __gnu_pbds;
using namespace std;

typedef tree<i64, null_type, less<i64>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

char s[300005];
int w[300005], e[300005];

int main() {
  int i, j, k, l, m, n;

  scanf("%d", &n);

  scanf("%s", s + 1);

  for (i = 1; i <= n; i++) {
    w[i] = w[i - 1];
    if (s[i] == 'W')
      w[i] += 1;
  }
  for (i = n; i >= 1; i--) {
    e[i] = e[i + 1];
    if (s[i] == 'E')
      e[i] += 1;
  }
  int ans = 200000;

  for (i = 1; i <= n; i++)
    ans = min(ans, w[i - 1] + e[i + 1]);
  // for(i=1 ; i<=n ;i++) printf("%d %d\n",w[i],e[i]) ;

  cout << ans << endl;

  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;

#define INF 1 << 30
#define REP(i, n) for (ll i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (ll i = (k); i < (int)(n); i++)

vector<bool> gen_sosuu(vector<bool> sosuu) {
  int size = sosuu.size();
  REP(i, size) { sosuu[i] = true; }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i, 2, sqrt(size) + 1) {
    if (sosuu[i] == false)
      continue;
    for (int j = 2; i * j < size; j++) {
      sosuu[i * j] = false;
    }
  }
  return sosuu;
}

int n;
string s;
int a[30001];

int main() {
  cin >> n >> s;
  a[0] = 0;
  REP(i, s.size()) { a[i + 1] = a[i] + (s[i] == 'E'); }
  int mi = INF;
  FOR(i, 1, n + 1) {
    int l = i - 1 - a[i - 1]; // W見てる人
    int r = a[n] - a[i];      // E見てる人
    // cout << i << " "  << l << " " << r <<endl;
    mi = min(mi, l + r);
  }
  cout << mi << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;

#define INF 1 << 30
#define REP(i, n) for (ll i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (ll i = (k); i < (int)(n); i++)

vector<bool> gen_sosuu(vector<bool> sosuu) {
  int size = sosuu.size();
  REP(i, size) { sosuu[i] = true; }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i, 2, sqrt(size) + 1) {
    if (sosuu[i] == false)
      continue;
    for (int j = 2; i * j < size; j++) {
      sosuu[i * j] = false;
    }
  }
  return sosuu;
}

int n;
string s;
int a[300001];

int main() {
  cin >> n >> s;
  a[0] = 0;
  REP(i, s.size()) { a[i + 1] = a[i] + (s[i] == 'E'); }
  int mi = INF;
  FOR(i, 1, n + 1) {
    int l = i - 1 - a[i - 1]; // W見てる人
    int r = a[n] - a[i];      // E見てる人
    // cout << i << " "  << l << " " << r <<endl;
    mi = min(mi, l + r);
  }
  cout << mi << endl;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
int n;
char S[200100];
long long ans = 0x3f3f3f3f3f3f3f3f, a[200100];
long long calc(long long x) { return (x - 1) - a[x - 1] + a[n] - a[x]; }
int main() {
  scanf("%d", &n);
  scanf("%s", S + 1);
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + (S[i] == 'E');
  for (int i = 1; i <= n; i++)
    ans = std::min(ans, calc(i));
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
int n;
char S[300100];
long long ans = 0x3f3f3f3f3f3f3f3f, a[300100];
long long calc(long long x) { return (x - 1) - a[x - 1] + a[n] - a[x]; }
int main() {
  scanf("%d", &n);
  scanf("%s", S + 1);
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + (S[i] == 'E');
  for (int i = 1; i <= n; i++)
    ans = std::min(ans, calc(i));
  printf("%lld\n", ans);
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
// by Balloons
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)

using namespace std;

typedef long long LL;

const int inf = 1e9, maxn = 2e5 + 5;

int n;
char s[maxn];
int sum[maxn];

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + (s[i] == 'E');
  int ans = inf;
  for (int i = 1; i <= n; i++) {
    int r = i - 1 - sum[i - 1] + (sum[n] - sum[i]);
    ans = min(ans, r);
  }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
// by Balloons
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)

using namespace std;

typedef long long LL;

const int inf = 1e9, maxn = 3e5 + 5;

int n;
char s[maxn];
int sum[maxn];

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + (s[i] == 'E');
  int ans = inf;
  for (int i = 1; i <= n; i++) {
    int r = i - 1 - sum[i - 1] + (sum[n] - sum[i]);
    ans = min(ans, r);
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f(z) for (int i = 1; i <= z; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define mod 1000000007
#define cl(n, m) (n - 1) / m + 1;
#define T()                                                                    \
  int TT;                                                                      \
  cin >> TT;                                                                   \
  while (TT--)
#define P pair<int, int>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int N = 2e5 + 5;
int e[N], w[N];
signed main() {
  int n, mx = LLONG_MAX;

  string s;
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      w[i] = w[i - 1] + 1;
    else
      w[i] = w[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'E')
      e[i] = e[i + 1] + 1;
    else
      e[i] = e[i + 1];
  }

  for (int i = 0; i < n; i++) {
    mx = min(mx, w[i - 1] + e[i + 1]);
  }
  cout << mx;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f(z) for (int i = 1; i <= z; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define mod 1000000007
#define cl(n, m) (n - 1) / m + 1;
#define T()                                                                    \
  int TT;                                                                      \
  cin >> TT;                                                                   \
  while (TT--)
#define P pair<int, int>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int N = 3e5 + 5;
int e[N], w[N];
signed main() {
  int n, mx = LLONG_MAX;

  string s;
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      w[i] = w[i - 1] + 1;
    else
      w[i] = w[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'E')
      e[i] = e[i + 1] + 1;
    else
      e[i] = e[i + 1];
  }

  for (int i = 0; i < n; i++) {
    mx = min(mx, w[i - 1] + e[i + 1]);
  }
  cout << mx;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

const int maxn = 1e5 + 10;

int n;
int l[maxn], r[maxn];
char str[maxn];
int maxx = 0;

int main() {
  scanf("%d", &n);
  scanf("%s", str);

  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));

  for (int i = 0; i < n; i++) {
    l[i] = l[i - 1];
    if (str[i] == 'E') {
      l[i]++;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    r[i] = r[i + 1];
    if (str[i] == 'W') {
      r[i]++;
    }
  }

  for (int i = 0; i < n; i++) {
    r[i] = r[i] + l[i];
    maxx = max(r[i], maxx);
  }

  cout << n - maxx << endl;

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

const int maxn = 1e6 + 10;

int n;
int l[maxn], r[maxn];
char str[maxn];
int maxx = 0;

int main() {
  scanf("%d", &n);
  scanf("%s", str);

  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));

  for (int i = 0; i < n; i++) {
    l[i] = l[i - 1];
    if (str[i] == 'E') {
      l[i]++;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    r[i] = r[i + 1];
    if (str[i] == 'W') {
      r[i]++;
    }
  }

  for (int i = 0; i < n; i++) {
    r[i] = r[i] + l[i];
    maxx = max(r[i], maxx);
  }

  cout << n - maxx << endl;

  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define BUF (1000)
#define ll long long
#define INF 1000000005
int n;
char s[BUF];

int main(void) {
  cin >> n;
  cin >> s;
  int e = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == 'E')
      e++;
  int w = n - e;

  int cnt = 0;
  int ans = INF;
  for (int i = 0; i < n; i++) {
    int tmp;
    if (s[i] == 'W')
      cnt++;

    if (s[i] == 'W') {
      tmp = cnt - 1 + e - i - 1 + cnt;
    } else {
      tmp = cnt + e - i - 1 + cnt;
    }

    ans = min(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define BUF (300005)
#define ll long long
#define INF 1000000005
int n;
char s[BUF];

int main(void) {
  cin >> n;
  cin >> s;
  int e = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == 'E')
      e++;
  int w = n - e;

  int cnt = 0;
  int ans = INF;
  for (int i = 0; i < n; i++) {
    int tmp;
    if (s[i] == 'W')
      cnt++;

    if (s[i] == 'W') {
      tmp = cnt - 1 + e - i - 1 + cnt;
    } else {
      tmp = cnt + e - i - 1 + cnt;
    }

    ans = min(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::string S;
  std::cin >> S;

  int ans = N;
  for (int i = 0; i < N; i++) {
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (j < i) {
        if (S[j] == 'W')
          cnt++;
      } else if (j > i) {
        if (S[j] == 'E')
          cnt++;
      }
    }
    ans = std::min(ans, cnt);
  }

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

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::string S;
  std::cin >> S;

  int ans = 0;
  for (int i = 1; i < N; i++) {
    if (S[i] == 'E')
      ans++;
  }
  int cnt = ans;
  for (int i = 1; i < N; i++) {
    if (S[i] == 'E')
      cnt--;
    if (S[i - 1] == 'W')
      cnt++;
    ans = std::min(cnt, ans);
  }

  std::cout << ans << std::endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int N = 2e5 + 5;

int n, sum[N][2];
char s[N];

int main() {
  scanf("%d %s", &n, s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'W')
      sum[i][0] = sum[i - 1][0], sum[i][1] = sum[i - 1][1] + 1;
    else
      sum[i][0] = sum[i - 1][0] + 1, sum[i][1] = sum[i - 1][1];
  }
  int ans = n;
  for (int i = 1; i <= n; i++) {
    ans = min(sum[n][0] - sum[i][0] + sum[i - 1][1], ans);
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int N = 3e5 + 5;

int n, sum[N][2];
char s[N];

int main() {
  scanf("%d %s", &n, s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'W')
      sum[i][0] = sum[i - 1][0], sum[i][1] = sum[i - 1][1] + 1;
    else
      sum[i][0] = sum[i - 1][0] + 1, sum[i][1] = sum[i - 1][1];
  }
  int ans = n;
  for (int i = 1; i <= n; i++) {
    ans = min(sum[n][0] - sum[i][0] + sum[i - 1][1], ans);
  }
  printf("%d\n", ans);
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> e(n), w(n);
  rep(i, n - 1) {
    w[i + 1] += w[i];
    if (s[i] == 'W')
      w[i + 1]++;
  }

  for (ll i = n - 1; i >= 0; i--) {
    e[i - 1] += e[i];
    if (s[i] == 'E')
      e[i - 1]++;
  }

  ll res = 1e10;
  rep(i, n) chmin(res, w[i] + e[i]);
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> e(n), w(n);
  rep(i, n - 1) {
    w[i + 1] += w[i];
    if (s[i] == 'W')
      w[i + 1]++;
  }

  for (ll i = n - 1; i > 0; i--) {
    e[i - 1] += e[i];
    if (s[i] == 'E')
      e[i - 1]++;
  }

  ll res = 1e10;
  rep(i, n) chmin(res, w[i] + e[i]);
  cout << res << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;

int n;
string s;
int W[MAXN], E[MAXN];
void solve() {
  int i = 0;
  int w = 0, e = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == 'W')
      w++;
    else
      e++;
    W[i] = w;
    E[i] = e;
  }
  int mn = MAXN;
  for (i = 0; i < n; i++) {
    if (i == 0)
      mn = min(mn, E[n - 1] - E[i]);
    else if (i == n - 1)
      mn = min(mn, W[i - 1]);
    else
      mn = min(mn, E[n - 1] - E[i] + W[i - 1]);
  }
  cout << mn;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test = 1;
  // cin >> test;
  while (test--) {
    cin >> n >> s;

    solve();
  }
  // 4689772647015
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;

int n;
string s;
int W[MAXN], E[MAXN];
void solve() {
  int i = 0;
  int w = 0, e = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == 'W')
      w++;
    else
      e++;
    W[i] = w;
    E[i] = e;
  }
  int mn = MAXN;
  for (i = 0; i < n; i++) {
    if (i == 0)
      mn = min(mn, E[n - 1] - E[i]);
    else if (i == n - 1)
      mn = min(mn, W[i - 1]);
    else
      mn = min(mn, E[n - 1] - E[i] + W[i - 1]);
  }
  cout << mn;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test = 1;
  // cin >> test;
  while (test--) {
    cin >> n >> s;

    solve();
  }
  // 4689772647015
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

char str[200005];

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str);
  int now = 0, ans = 0;
  for (int i = 1; i < n; i++) {
    if (str[i] == 'E')
      now++;
  }
  ans = now;
  for (int i = 1; i < n; i++) {
    if (str[i] == 'E')
      now--;
    if (str[i - 1] == 'W')
      now++;
    ans = min(ans, now);
  }
  printf("%d\n", ans);
}


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

char str[300005];

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str);
  int now = 0, ans = 0;
  for (int i = 1; i < n; i++) {
    if (str[i] == 'E')
      now++;
  }
  ans = now;
  for (int i = 1; i < n; i++) {
    if (str[i] == 'E')
      now--;
    if (str[i - 1] == 'W')
      now++;
    ans = min(ans, now);
  }
  printf("%d\n", ans);
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll MOD = 1e9 + 7;
// LLONG_MIN

// WA

int main(void) {
  int N, dp[100010] = {}, dpi[100010] = {}, s = 1;
  char S[100010];
  scanf("%d%s", &N, S);
  dp[0] = 0;
  rep(i, N - 1) {
    dp[i + 1] = dp[i];
    if (S[i] == 'W')
      dp[i + 1]++;
  }
  dpi[N - 1] = 0;
  ireg(i, 1, N - 1) {
    dpi[i - 1] = dpi[i];
    if (S[i] == 'E')
      dpi[i - 1]++;
  }
  rep(i, N) {
    // printf("%c\n",S[i]);
    if (dp[s] + dpi[s] > dp[i] + dpi[i])
      s = i;
  }
  printf("%d\n", dp[s] + dpi[s]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll MOD = 1e9 + 7;
// LLONG_MIN

// WA

int main(void) {
  int N, dp[300010] = {}, dpi[300010] = {}, s = 0;
  char S[300010];
  scanf("%d%s", &N, S);
  dp[0] = 0;
  rep(i, N - 1) {
    dp[i + 1] = dp[i];
    if (S[i] == 'W')
      dp[i + 1]++;
  }
  dpi[N - 1] = 0;
  ireg(i, 1, N - 1) {
    dpi[i - 1] = dpi[i];
    if (S[i] == 'E')
      dpi[i - 1]++;
  }
  rep(i, N) {
    // printf("%c\n",S[i]);
    if (dp[s] + dpi[s] > dp[i] + dpi[i])
      s = i;
  }
  printf("%d\n", dp[s] + dpi[s]);
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
const int N = 1e5 + 5;
int n, esum[N];
char str[N];

namespace utils {
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
template <class T> inline void apn(T &x, const T y) { x = x < y ? x : y; }
template <class T> inline void apx(T &x, const T y) { x = x > y ? x : y; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

int main() {
  n = nxi();
  scanf("%s", str + 1);
  for (int i = 1; i <= n; ++i) {
    esum[i] = esum[i - 1] + (str[i] == 'E');
  }
  int ans = 1e9;
  for (int i = 1; i <= n; ++i) {
    apn(ans, (esum[n] - esum[i]) + (i - 1 - esum[i - 1]));
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
const int N = 3e5 + 5;
int n, esum[N];
char str[N];

namespace utils {
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
template <class T> inline void apn(T &x, const T y) { x = x < y ? x : y; }
template <class T> inline void apx(T &x, const T y) { x = x > y ? x : y; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

int main() {
  n = nxi();
  scanf("%s", str + 1);
  for (int i = 1; i <= n; ++i) {
    esum[i] = esum[i - 1] + (str[i] == 'E');
  }
  int ans = 1e9;
  for (int i = 1; i <= n; ++i) {
    apn(ans, (esum[n] - esum[i]) + (i - 1 - esum[i - 1]));
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sc(n) scanf("%d", &n)
#define scll(n) scanf("%lld", &n)
using namespace std;
int main() {
  int n, a[30005], t, l, k, j, o, w, e, i;
  sc(n);
  getchar();
  memset(a, 0, sizeof(a));
  for (i = 0, w = 0; i < n; i++) {
    a[i] = getchar();
    if (a[i] == 'W') {
      w++;
    }
  }
  if (a[0] == 'W') {
    w--;
  }
  e = w;
  for (i = 0; i < n - 1; i++) {
    if (a[i] == 'E') {
      w++;
    }
    if (a[i + 1] == 'W') {
      w--;
    }
    e = max(e, w);
  }
  printf("%d", n - 1 - e);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define sc(n) scanf("%d", &n)
#define scll(n) scanf("%lld", &n)
using namespace std;
int main() {
  int n, a[300005], t, l, k, j, o, w, e, i;
  sc(n);
  getchar();
  memset(a, 0, sizeof(a));
  for (i = 0, w = 0; i < n; i++) {
    a[i] = getchar();
    if (a[i] == 'W') {
      w++;
    }
  }
  if (a[0] == 'W') {
    w--;
  }
  e = w;
  for (i = 0; i < n - 1; i++) {
    if (a[i] == 'E') {
      w++;
    }
    if (a[i + 1] == 'W') {
      w--;
    }
    e = max(e, w);
  }
  printf("%d", n - 1 - e);
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <string>
using namespace std;
const int N = 1e5 + 5;
int a[N], pref[N], suff[N];
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 2; i <= n; ++i) {
    pref[i] = pref[i - 1] + (s[i - 2] == 'W');
  }
  for (int i = n - 1; i >= 1; --i) {
    suff[i] = suff[i + 1] + (s[i] == 'E');
  }
  int ans = N;
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, pref[i] + suff[i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <string>
using namespace std;
const int N = 3e5 + 5;
int a[N], pref[N], suff[N];
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 2; i <= n; ++i) {
    pref[i] = pref[i - 1] + (s[i - 2] == 'W');
  }
  for (int i = n - 1; i >= 1; --i) {
    suff[i] = suff[i + 1] + (s[i] == 'E');
  }
  int ans = N;
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, pref[i] + suff[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a, b) memset(a, b, sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define SZ(a) ((int)a.size())
using namespace std;
const int N = 150;
const int M = 1e9 + 7;

int f[N];
string s;
int n;
int main() {
  Accel cin >> n;
  cin >> s;
  s = " " + s;
  int c = 0;
  FOR(i, 1, n) {
    f[i] += c;
    if (s[i] == 'W')
      c++;
  }
  c = 0;
  for (int i = n; i; i--) {
    f[i] += c;
    if (s[i] == 'E')
      c++;
  }
  int t = f[1];
  FOR(i, 2, n)
  t = min(t, f[i]);
  cout << t << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a, b) memset(a, b, sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define SZ(a) ((int)a.size())
using namespace std;
const int N = 1e6 + 10;
const int M = 1e9 + 7;

int f[N];
string s;
int n;
int main() {
  Accel cin >> n;
  cin >> s;
  s = " " + s;
  int c = 0;
  FOR(i, 1, n) {
    f[i] += c;
    if (s[i] == 'W')
      c++;
  }
  c = 0;
  for (int i = n; i; i--) {
    f[i] += c;
    if (s[i] == 'E')
      c++;
  }
  int t = f[1];
  FOR(i, 2, n)
  t = min(t, f[i]);
  cout << t << endl;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define digit(a) to_string(a).size()
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

const long long MOD = 1e9 + 7;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<char> s(n);
  rep(i, 0, n) cin >> s[i];
  vector<ll> cusum(n, 0);
  if (s[0] == 'W')
    cusum[0] = 1;
  rep(i, 0, n) {
    if (s[i + 1] == 'W')
      cusum[i + 1] = cusum[i] + 1;
    else
      cusum[i + 1] = cusum[i];
  }
  ll ans = INF;
  rep(i, 0, n) {
    ll left, right;
    if (i == 0)
      left = 0, right = n - 1 - i - (cusum[n - 1] - cusum[i]);
    else if (i == n - 1)
      left = cusum[i], right = 0;
    else
      left = cusum[i], right = n - 1 - i - (cusum[n - 1] - cusum[i]);
    ans = min(ans, right + left);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define digit(a) to_string(a).size()
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

const long long MOD = 1e9 + 7;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<char> s(n);
  rep(i, 0, n) cin >> s[i];
  vector<ll> cusum(n, 0);
  if (s[0] == 'W')
    cusum[0] = 1;
  rep(i, 0, n - 1) {
    if (s[i + 1] == 'W')
      cusum[i + 1] = cusum[i] + 1;
    else
      cusum[i + 1] = cusum[i];
  }
  ll ans = INF;
  rep(i, 0, n) {
    ll left, right;
    if (i == 0)
      left = 0, right = n - 1 - i - (cusum[n - 1] - cusum[i]);
    else if (i == n - 1)
      left = cusum[i], right = 0;
    else
      left = cusum[i], right = n - 1 - i - (cusum[n - 1] - cusum[i]);
    ans = min(ans, right + left);
  }
  cout << ans << endl;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define MAXN 100001
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, w[MAXN] = {}, e[MAXN] = {};
  cin >> n;
  string s;
  cin >> s;
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    w[i] = cur;
    if (s[i] == 'W')
      w[i] = cur++;
  }
  cur = 0;
  for (int i = n - 1; i >= 0; --i) {
    e[i] = cur;
    if (s[i] == 'E')
      e[i] = cur++;
  }
  int ans = n;
  for (int i = 0; i < n; ++i) {
    ans = min(ans, e[i] + w[i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define MAXN 300001
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, w[MAXN] = {}, e[MAXN] = {};
  cin >> n;
  string s;
  cin >> s;
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    w[i] = cur;
    if (s[i] == 'W')
      w[i] = cur++;
  }
  cur = 0;
  for (int i = n - 1; i >= 0; --i) {
    e[i] = cur;
    if (s[i] == 'E')
      e[i] = cur++;
  }
  int ans = n;
  for (int i = 0; i < n; ++i) {
    ans = min(ans, e[i] + w[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03341 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = list(input())
cmin = n + 1
for i in range(n):
    c = s[:i].count("W") + s[i:].count("E")
    if cmin > c:
        cmin = c
print(cmin)


Fixed submission:
n = int(input())
s = list(input())
c = s[1:].count("E")
ans = c
for i in range(1, n):
    c -= 1 if s[i] == "E" else 0
    c += 1 if s[i - 1] == "W" else 0
    ans = min(c, ans)
print(ans)
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, ans;
int a[100100], b[100100];
char s[100100];

int main() {
  scanf("%d", &n);
  ans = n * 5;
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = a[i - 1];
    if (s[i] == 'W')
      a[i]++;
  }
  for (int i = n; i; i--) {
    b[i] = b[i + 1];
    if (s[i] == 'E')
      b[i]++;
  }
  for (int i = 1; i <= n; i++)
    ans = min(ans, a[i - 1] + b[i + 1]);
  printf("%d\n", ans);
  return 0;
}

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

int n, ans;
int a[300100], b[300100];
char s[300100];

int main() {
  scanf("%d", &n);
  ans = n * 5;
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = a[i - 1];
    if (s[i] == 'W')
      a[i]++;
  }
  for (int i = n; i; i--) {
    b[i] = b[i + 1];
    if (s[i] == 'E')
      b[i]++;
  }
  for (int i = 1; i <= n; i++)
    ans = min(ans, a[i - 1] + b[i + 1]);
  printf("%d\n", ans);
  return 0;
}
Problem p03343 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define x first
#define y second
#define ll long long
#define ull unsigned ll
#define db double
#define pii pair<int, int>
#define INF 1e9 + 7

const int N = 2010;
int n, K, Q;
vector<int> A, B, C;

int main() {
  scanf("%d %d %d", &n, &K, &Q);
  A.resize(n + 1), B.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
  }
  int ans = INF;
  for (int i = 1; i <= n; i++) {
    C.clear();
    int lb = A[i];
    int lp = i, rp = i;
    while (lp > 1 && A[lp - 1] >= lb)
      lp--;
    while (rp < n && A[rp + 1] >= lb)
      rp++;
    if (rp - lp + 1 < K)
      continue;
    lp = rp = 1;
    int cnt = 1;
    while (lp <= n) {
      while (lp <= n && A[lp] < lb)
        lp++;
      if (lp > n)
        break;
      rp = lp;
      while (rp < n && A[rp + 1] >= lb)
        rp++;
      int width = rp - lp + 1;
      if (width >= K) {
        for (int i = 1, j = lp; j <= rp; j++, i++)
          B[i] = A[j];
        sort(B.begin() + 1, B.begin() + 1 + width);
        for (int i = 1; i <= width - K + 1; i++) {
          if (cnt && B[i] == lb)
            cnt--;
          else
            C.push_back(B[i]);
        }
      }
      lp = rp + 1;
    }
    if (C.size() < Q - 1)
      continue;
    sort(all(C));
    ans = min(ans, C[Q - 2] - lb);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define x first
#define y second
#define ll long long
#define ull unsigned ll
#define db double
#define pii pair<int, int>
#define INF 1e9 + 7

const int N = 2010;
int n, K, Q;
vector<int> A, B, C;

int main() {
  scanf("%d %d %d", &n, &K, &Q);
  A.resize(n + 1), B.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
  }
  if (Q == 1)
    return puts("0"), 0;
  int ans = INF;
  for (int i = 1; i <= n; i++) {
    C.clear();
    int lb = A[i];
    int lp = i, rp = i;
    while (lp > 1 && A[lp - 1] >= lb)
      lp--;
    while (rp < n && A[rp + 1] >= lb)
      rp++;
    if (rp - lp + 1 < K)
      continue;
    lp = rp = 1;
    int cnt = 1;
    while (lp <= n) {
      while (lp <= n && A[lp] < lb)
        lp++;
      if (lp > n)
        break;
      rp = lp;
      while (rp < n && A[rp + 1] >= lb)
        rp++;
      int width = rp - lp + 1;
      if (width >= K) {
        for (int i = 1, j = lp; j <= rp; j++, i++)
          B[i] = A[j];
        sort(B.begin() + 1, B.begin() + 1 + width);
        for (int i = 1; i <= width - K + 1; i++) {
          if (cnt && B[i] == lb)
            cnt--;
          else
            C.push_back(B[i]);
        }
      }
      lp = rp + 1;
    }
    if (C.size() < Q - 1)
      continue;
    sort(all(C));
    ans = min(ans, C[Q - 2] - lb);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int N, K, Q;
int A[2010];

bool judge(int mid) {
  // printf("mid : %d\n", mid);

  int mem[2010] = {0};
  for (int i = 0; i < N; i++) {
    memset(mem, 0, sizeof(mem));
    // printf(" bottom : %d\n",A[i]);
    for (int j = 0; j < N; j++) {
      if (A[j] < A[i])
        mem[j] = 2;
      else if (A[j] <= A[i] + mid)
        mem[j] = 1;
      else
        mem[j] = 0;
    }

    /*
    printf("  mem : ");
    for(int j = 0; j < N; j++)printf("%d ",mem[j]);printf("\n");
    */

    int q = 0;
    int cnt = 0, len = 0;
    for (int j = 0; j <= N; j++) {
      if (j == N || mem[j] == 2) {
        q += max(min(len - K + 1, cnt), 0);
        cnt = len = 0;
      } else {
        if (mem[j] == 1)
          cnt++;
        len++;
      }
    }

    // printf("  q : %d\n",q);

    if (q >= Q)
      return true;
  }

  return false;
}

int main() {
  cin >> N >> K >> Q;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int l = 0, r = 1000000001;
  for (int i = 0; i < 100; i++) {
    int mid = (l + r) / 2;
    if (judge(mid))
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;
  return 0;
}


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

int N, K, Q;
int A[2010];

bool judge(int mid) {
  // printf("mid : %d\n", mid);

  int mem[2010] = {0};
  for (int i = 0; i < N; i++) {
    memset(mem, 0, sizeof(mem));
    // printf(" bottom : %d\n",A[i]);
    for (int j = 0; j < N; j++) {
      if (A[j] < A[i])
        mem[j] = 2;
      else if (A[j] <= A[i] + mid)
        mem[j] = 1;
      else
        mem[j] = 0;
    }

    /*
    printf("  mem : ");
    for(int j = 0; j < N; j++)printf("%d ",mem[j]);printf("\n");
    */

    int q = 0;
    int cnt = 0, len = 0;
    for (int j = 0; j <= N; j++) {
      if (j == N || mem[j] == 2) {
        q += max(min(len - K + 1, cnt), 0);
        cnt = len = 0;
      } else {
        if (mem[j] == 1)
          cnt++;
        len++;
      }
    }

    // printf("  q : %d\n",q);

    if (q >= Q)
      return true;
  }

  return false;
}

int main() {
  cin >> N >> K >> Q;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int l = 0, r = 1000000001;
  for (int i = 0; i < 50; i++) {
    int mid = (l + r) / 2;
    if (judge(mid))
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;
  return 0;
}
Problem p03343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k, q;
  cin >> n >> k >> q;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
  for (int i = 0; i < n; ++i)
    pq.push(make_pair(a[i], i));
  vector<int> b(n);
  int ans = n == 1 ? 0 : 1e9;
  for (int i = 0; i < n - q; ++i) {
    priority_queue<int, vector<int>, greater<>> p;
    for (int j = 0; j < n; ++j) {
      priority_queue<int, vector<int>, greater<>> r;
      for (int l = 0; l < k - 1; ++l)
        r.push(a[j + l]);
      for (int l = j; l < n; ++l) {
        if (b[l] == 1) {
          j = l;
          break;
        }
        if (l - j + 1 >= k)
          r.push(a[l]);
        if (l == n - 1)
          j = n - 1;
      }
      int m = max(0, (int)r.size() - k + 1);
      for (int l = 0; l < m; ++l) {
        p.push(r.top());
        r.pop();
      }
    }
    if (p.size() < q)
      continue;
    int mi = p.top();
    for (int j = 0; j < q - 1; ++j)
      p.pop();
    int ma = p.top();
    ans = min(ans, ma - mi);
    pair<int, int> x = pq.top();
    pq.pop();
    b[x.second] = 1;
  }
  cout << ans << "\n";
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k, q;
  cin >> n >> k >> q;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
  for (int i = 0; i < n; ++i)
    pq.push(make_pair(a[i], i));
  vector<int> b(n);
  int ans = n == 1 ? 0 : 1e9;
  for (int i = 0; i < n - q; ++i) {
    priority_queue<int, vector<int>, greater<>> p;
    for (int j = 0; j < n; ++j) {
      priority_queue<int, vector<int>, greater<>> r;
      for (int l = 0; l < k - 1; ++l)
        r.push(a[j + l]);
      for (int l = j; l < n; ++l) {
        if (b[l] == 1) {
          j = l;
          break;
        }
        if (l - j + 1 >= k)
          r.push(a[l]);
        if (l == n - 1)
          j = n - 1;
      }
      int m = max(0, (int)r.size() - k + 1);
      for (int l = 0; l < m; ++l) {
        p.push(r.top());
        r.pop();
      }
    }
    if (p.size() < q)
      break;
    int mi = p.top();
    for (int j = 0; j < q - 1; ++j)
      p.pop();
    int ma = p.top();
    ans = min(ans, ma - mi);
    pair<int, int> x = pq.top();
    pq.pop();
    b[x.second] = 1;
  }
  cout << ans << "\n";
}
Problem p03343 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main() {
  ll N, K, Q;
  cin >> N >> K >> Q;
  ll a[N];
  for (ll i = 0; i < N; i++)
    cin >> a[i];
  ll ans = 1145148931919180;
  for (ll i = 0; i < N; i++) {
    vector<ll> b;
    for (ll j = 0; j < N; j++)
      if (i != j)
        b.push_back(a[j]);
    vector<ll> v;
    v.push_back(-1);
    for (ll j = 0; j < N - 1; j++)
      if (b[j] < a[i])
        v.push_back(j);
    v.push_back(N - 1);
    vector<ll> u;
    for (ll j = 0; j < v.size() - 1; j++) {
      vector<ll> tmp;
      for (ll k = v[j] + 1; k < v[j + 1]; k++)
        tmp.push_back(b[k]);
      sort(tmp.begin(), tmp.end());
      for (ll k = 0; tmp.size() - k >= K; k++)
        u.push_back(tmp[k]);
    }
    if (u.size() < Q - 1)
      continue;
    sort(u.begin(), u.end());
    ans = min(ans, u[Q - 2] - a[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main() {
  ll N, K, Q;
  cin >> N >> K >> Q;
  ll a[N];
  for (ll i = 0; i < N; i++)
    cin >> a[i];
  if (Q == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 1145148931919180;
  for (ll i = 0; i < N; i++) {
    vector<ll> b;
    for (ll j = 0; j < N; j++)
      if (i != j)
        b.push_back(a[j]);
    vector<ll> v;
    v.push_back(-1);
    for (ll j = 0; j < N - 1; j++)
      if (b[j] < a[i])
        v.push_back(j);
    v.push_back(N - 1);
    vector<ll> u;
    for (ll j = 0; j < v.size() - 1; j++) {
      vector<ll> tmp;
      for (ll k = v[j] + 1; k < v[j + 1]; k++)
        tmp.push_back(b[k]);
      sort(tmp.begin(), tmp.end());
      for (ll k = 0; tmp.size() - k >= K; k++)
        u.push_back(tmp[k]);
    }
    if (u.size() < Q - 1)
      continue;
    sort(u.begin(), u.end());
    ans = min(ans, u[Q - 2] - a[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03343 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

int main() {
  long long N, K, Q, ANS = 1000000000;
  cin >> N >> K >> Q;
  vector<long long> A(N);
  vector<pair<long long, long long>> B(N);
  vector<bool> C(N, true);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[i] = {A[i], i};
  }
  sort(B.begin(), B.end());
  for (int i = 0; i < N; i++) {
    vector<vector<long long>> V;
    V.clear();
    V.push_back(vector<long long>(0));
    for (int j = 0; j < N; j++) {
      if (C[j])
        V[V.size() - 1].push_back(A[j]);
      else
        V.push_back(vector<long long>(0));
    }
    vector<long long> W;
    for (int j = 0; j < V.size(); j++) {
      if (V[j].empty())
        continue;
      sort(V[j].begin(), V[j].end());
      for (int k = 0; k < V[j].size() - K + 1; k++) {
        W.push_back(V[j][k]);
      }
    }
    sort(W.begin(), W.end());
    if (W.size() < Q) {
      break;
    }
    ANS = min(ANS, W[Q - 1] - W[0]);
    C[B[i].second] = false;
  }
  cout << ANS << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  long long N, K, Q, ANS = 1000000000;
  cin >> N >> K >> Q;
  vector<long long> A(N);
  vector<pair<long long, long long>> B(N);
  vector<bool> C(N, true);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[i] = {A[i], i};
  }
  sort(B.begin(), B.end());
  for (int i = 0; i < N; i++) {
    vector<vector<long long>> V;
    V.clear();
    V.push_back(vector<long long>(0));
    for (int j = 0; j < N; j++) {
      if (C[j])
        V[V.size() - 1].push_back(A[j]);
      else
        V.push_back(vector<long long>(0));
    }
    vector<long long> W;
    for (int j = 0; j < V.size(); j++) {
      if (V[j].size() < K)
        continue;
      sort(V[j].begin(), V[j].end());
      for (int k = 0; k < V[j].size() - K + 1; k++) {
        W.push_back(V[j][k]);
      }
    }
    sort(W.begin(), W.end());
    if (W.size() < Q) {
      break;
    }
    ANS = min(ANS, W[Q - 1] - W[0]);
    C[B[i].second] = false;
  }
  cout << ANS << endl;
}
Problem p03343 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;

#define MAXN 2001
#define MOD 1000000007

int N, K, Q;
int A[MAXN];

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);

  cin >> N >> K >> Q;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int ans = 1e9 + 1;
  for (int i = 0; i < N; i++) {
    int lo = A[i];

    vector<int> smallest;
    int l = 0, r;
    while (l < N) {
      while (l < N && A[l] < lo)
        l++;
      if (l == N)
        break;

      r = l;
      vector<int> V;
      while (r < N && A[r] >= lo) {
        V.push_back(A[r]);
        r++;
      }

      sort(V.begin(), V.end());
      for (int k = 0; k < V.size() - K + 1; k++)
        smallest.push_back(V[k]);

      l = r;
    }

    sort(smallest.begin(), smallest.end());

    if (smallest.size() >= Q)
      ans = min(ans, smallest[Q - 1] - smallest[0]);
  }

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

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;

#define MAXN 2001
#define MOD 1000000007

int N, K, Q;
int A[MAXN];

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);

  cin >> N >> K >> Q;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int ans = 1e9 + 1;
  for (int i = 0; i < N; i++) {
    int lo = A[i];

    vector<int> smallest;
    int l = 0, r;
    while (l < N) {
      while (l < N && A[l] < lo)
        l++;
      if (l == N)
        break;

      r = l;
      vector<int> V;
      while (r < N && A[r] >= lo) {
        V.push_back(A[r]);
        r++;
      }

      sort(V.begin(), V.end());
      for (int k = 0; k < (int)V.size() - K + 1; k++)
        smallest.push_back(V[k]);

      l = r;
    }

    sort(smallest.begin(), smallest.end());

    if (smallest.size() >= Q)
      ans = min(ans, smallest[Q - 1] - smallest[0]);
  }

  cout << ans << endl;
  return 0;
}
Problem p03343 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k, q;
  cin >> n >> k >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<bool> b(n, true);
  vector<P> s(n);
  for (int i = 0; i < n; i++) {
    s[i].first = a[i];
    s[i].second = i;
  }
  sort(s.begin(), s.end());
  int res = 2e9;
  for (int i = 0; i < n - k + 1; i++) {
    vector<int> c;
    int l = 0, r = 1;
    while (true) {
      while (r < n and b[r])
        r++;
      vector<int> tmp;
      tmp.assign(a.begin() + l, a.begin() + r);
      sort(tmp.begin(), tmp.end());
      for (int j = 0; j < r - l - k + 1; j++) {
        c.push_back(tmp[j]);
      }
      if (r == n)
        break;
      l = r;
      while (l < n and not b[l])
        l++;
      r = l + 1;
    }
    if (c.size() < q)
      continue;
    sort(c.begin(), c.end());
    res = min(res, c[q - 1] - c[0]);
    b[s[i].second] = false;
  }
  cout << res << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k, q;
  cin >> n >> k >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<bool> b(n, true);
  vector<P> s(n);
  for (int i = 0; i < n; i++) {
    s[i].first = a[i];
    s[i].second = i;
  }
  sort(s.begin(), s.end());
  int res = 2e9;
  for (int i = 0; i < n - k + 1; i++) {
    vector<int> c;
    int l = 0, r = 1;
    while (r <= n) {
      while (r < n and b[r])
        r++;
      vector<int> tmp;
      tmp.assign(a.begin() + l, a.begin() + r);
      sort(tmp.begin(), tmp.end());
      for (int j = 0; j < r - l - k + 1; j++) {
        c.push_back(tmp[j]);
      }
      if (r == n)
        break;
      l = r;
      while (l < n and not b[l])
        l++;
      r = l + 1;
    }
    if (c.size() < q)
      continue;
    sort(c.begin(), c.end());
    res = min(res, c[q - 1] - c[0]);
    b[s[i].second] = false;
  }
  cout << res << '\n';
  return 0;
}