prithivMLmods/Coder-Stat huggingface dataset:
Problem p03339 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = input()

min_count = float("inf")
for i in range(n):
    count = 0
    for j in range(n):
        if i == j:
            continue
        elif j < i and s[j] == "W":
            count += 1
        elif j > i and s[j] == "E":
            count += 1
    min_count = min(min_count, count)
print(min_count)


Fixed submission:
n = int(input())
s = input()

count = [float("inf")] * n

# 左端
count[0] = 0
for i in range(1, n):
    if s[i] == "E":
        count[0] += 1

# 尺取
for i in range(1, n):
    count[i] = count[i - 1]
    if s[i - 1] == "W":
        count[i] += 1
    if s[i] == "E":
        count[i] -= 1

print(min(count))
Problem p03339 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = input()
while s.count("WE"):
    s = s.replace("WE", "")
ans = (n - len(s)) // 2
print(ans)


Fixed submission:
n = int(input())
s = input()
wcount = 0
wecount = 0
prev = ""
for c in s:
    if c == "W":
        if prev == "E":
            wcount = 0
        wcount += 1
    if wcount and c == "E":
        wcount -= 1
        wecount += 1
print(wecount)
Problem p03339 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = str(input())
ans = N
for i in range(N):
    ans = min(S[:i].count("W") + S[i + 1 :].count("E"), ans)
print(ans)


Fixed submission:
N = int(input())
S = str(input())
x = S[1:].count("E")
ans = x
for i in range(1, N):
    if S[i] == "E":
        x -= 1
    if S[i - 1] == "W":
        x += 1
    ans = min(ans, x)
print(ans)
Problem p03339 (C++)

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

#define _for(i, j, N) for (int i = (j); i < (N); i++)

using namespace std;
const int maxn = 30005;

string s;

int w[maxn];
int e[maxn];

int main() {
  int N;
  scanf("%d", &N);
  cin >> s;
  int cnt = 0;
  _for(i, 0, N) {
    if (s[i] == 'W') {
      w[i] = cnt++;
    } else {
      w[i] = cnt;
    }
  }

  cnt = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (s[i] == 'E') {
      e[i] = cnt++;
    } else {
      e[i] = cnt;
    }
  }

  int res = maxn;
  _for(i, 0, N) { res = min(w[i] + e[i], res); }

  printf("%d\n", res);

  return 0;
}

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

#define _for(i, j, N) for (int i = (j); i < (N); i++)

using namespace std;
const int maxn = 300005;

string s;

int w[maxn];
int e[maxn];

int main() {
  int N;
  scanf("%d", &N);
  cin >> s;
  int cnt = 0;
  _for(i, 0, N) {
    if (s[i] == 'W') {
      w[i] = cnt++;
    } else {
      w[i] = cnt;
    }
  }

  cnt = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (s[i] == 'E') {
      e[i] = cnt++;
    } else {
      e[i] = cnt;
    }
  }

  int res = maxn;
  _for(i, 0, N) { res = min(w[i] + e[i], res); }

  printf("%d\n", res);

  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int a[27], b[27];
int main() {
  int n;
  string s;
  cin >> n >> s;
  int minn = 100000;
  for (int i = 0; i < n; ++i) {
    int sum = 0;
    for (int j = 0; j < i; ++j)
      if (s[j] == 'W')
        sum++;
    for (int j = i + 1; j < n; ++j)
      if (s[j] == 'E')
        sum++;
    minn = min(minn, sum);
  }
  cout << minn;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[27], b[27];
int main() {
  int n;
  string s;
  cin >> n >> s;
  int a = 0, b = 0;
  for (int i = 1; i < n; ++i)
    if (s[i] == 'E')
      b++;
  int minn = b;
  for (int i = 1; i < n; ++i) {
    if (s[i] == 'E')
      b--;
    if (s[i - 1] == 'W')
      a++;
    minn = min(minn, a + b);
  }
  cout << minn;
  return 0;
}
Problem p03339 (C++)

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

using namespace std;
string s;
int Min = 999999; // a==W,,b=E
int num[100005];
int main() {
  int n;
  int tmp;
  cin >> n >> s;
  tmp = 0;
  for (int i = 0; i < n; i++) {
    num[i] = tmp;
    if (s[i] == 'W')
      tmp++;
  }
  tmp = 0;
  for (int i = n - 1; i >= 0; i--) {
    num[i] += tmp;
    if (s[i] == 'E')
      tmp++;
  }
  for (int i = 0; i < n; i++)
    Min = min(Min, num[i]);
  cout << Min << endl;
  return 0;
}


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

using namespace std;
string s;
int Min = 999999; // a==W,,b=E
int num[1000005];
int main() {
  int n;
  int tmp;
  cin >> n >> s;
  tmp = 0;
  for (int i = 0; i < n; i++) {
    num[i] = tmp;
    if (s[i] == 'W')
      tmp++;
  }
  tmp = 0;
  for (int i = n - 1; i >= 0; i--) {
    num[i] += tmp;
    if (s[i] == 'E')
      tmp++;
  }
  for (int i = 0; i < n; i++)
    Min = min(Min, num[i]);
  cout << Min << endl;
  return 0;
}
Problem p03339 (C++)

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

int main() {
  int n;
  int l[MAXN];
  int r[MAXN];
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  cin >> n;
  char c;
  for (int i = 1; i <= n; i++) {
    cin >> c;
    l[i + 1] = l[i];
    if (c == 'W')
      l[i + 1]++;
  }
  for (int i = n - 1; i >= 0; --i)
    r[i] = n - i - l[n + 1] + l[i + 1];
  int ans = n;
  for (int i = 1; i <= n; i++)
    ans = min(ans, l[i] + r[i]);
  cout << ans << endl;
  return 0;
}


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

int main() {
  int n;
  int l[MAXN];
  int r[MAXN];
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  cin >> n;
  char c;
  for (int i = 1; i <= n; i++) {
    cin >> c;
    l[i + 1] = l[i];
    if (c == 'W')
      l[i + 1]++;
  }
  for (int i = n - 1; i >= 0; --i)
    r[i] = n - i - l[n + 1] + l[i + 1];
  int ans = n;
  for (int i = 1; i <= n; i++)
    ans = min(ans, l[i] + r[i]);
  cout << ans << endl;
  return 0;
}
Problem p03339 (C++)

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

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int64_t ans = N;
  for (int i = 0; i < N; i++) {
    int64_t memo = 0;
    for (int j = 0; j < N; j++) {
      if (j < i) {
        if (S.at(j) == 'W') {
          memo++;
        }
      } else if (j > i) {
        if (S.at(j) == 'E') {
          memo++;
        }
      }
      if (memo > ans) {
        break;
      }
    }
    ans = min(ans, memo);
  }
  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int64_t ans = 0;
  int64_t memo = 0;
  for (int i = 1; i < N; i++) {
    if (S.at(i) == 'E') {
      memo++;
      ans++;
    }
  }
  for (int i = 1; i < N; i++) {
    if (S.at(i) == 'E') {
      memo--;
    }
    if (S.at(i - 1) == 'W') {
      memo++;
    }
    ans = min(ans, memo);
  }
  cout << ans << endl;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)

int main() {
  long long N;
  cin >> N;
  string S;
  cin >> S;
  long long ans = 3000000;
  long long WC = 0;
  rep(i, 0, N) {
    long long EC = 0;
    if (S[i - 1] != 'E' && i > 0) {

      WC++;
    }
    rep(j, i + 1, N) {
      if (S[j] != 'W') {
        EC++;
      }
    }

    ans = min(ans, WC + EC);
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)

int main() {
  long long N;
  cin >> N;
  string S;
  cin >> S;
  long long ans = 3000000;
  vector<long long> WCL(N + 1);
  vector<long long> ECL(N + 1);
  WCL[0] = 0;
  ECL[0] = 0;
  rep(i, 1, N + 1) {
    ECL[i] = ECL[i - 1];
    WCL[i] = WCL[i - 1];
    if (S[i - 1] == 'E')
      ECL[i]++;
    else
      WCL[i]++;
  }
  for (int i = 1; i <= N; i++) {
    ans = min(ans, WCL[i - 1] + (ECL[N] - ECL[i]));
  }
  cout << ans << endl;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(vec) vec.begin(), vec.end()
using ll = long long;

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int ans = N;
  int subsum = 0;
  rep(i, N) {
    int tans = 0;
    if (i != 0) {
      if (S.at(i - 1) == 'W')
        subsum++;
    }
    for (int j = i + 1; j < N; j++) {
      if (S.at(j) == 'E')
        tans++;
    }
    tans += subsum;
    ans = min(tans, ans);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(vec) vec.begin(), vec.end()
using ll = long long;

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int ans = N;
  int sumE = 0;
  int sumW = 0;

  for (int i = 1; i < N; i++) { //(0)がリーダー
    if (S.at(i) == 'E')
      sumE++;
  }
  ans = min(sumE, ans);
  for (int i = 1; i < N; i++) { //(1からNがリーダー
    if (S.at(i - 1) == 'W')
      sumW++;
    if (S.at(i) == 'E')
      sumE--;
    int tans = sumE + sumW;
    ans = min(tans, ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  char buf[16] = {0};
  int N;
  char *str = NULL;

  fgets(buf, sizeof(buf), stdin);
  sscanf(buf, "%d", &N);

  str = (char *)malloc(sizeof(char) * (N + 2));
  fgets(str, sizeof(char) * (N + 2), stdin);

  int count;
  int min = N + 1;
  for (int i = 0; i < N; i++) {
    int j;
    count = 0;
    int flag = 0;
    for (j = 0; j < i; j++) {
      if (str[j] == 'W')
        count++;
      if (count >= min) {
        flag = 1;
        break;
      }
    }
    if (flag)
      continue;
    j++;

    for (; j < N; j++) {
      if (str[j] == 'E')
        count++;
      if (count >= min) {
        flag = 1;
        break;
      }
    }
    if (flag)
      continue;
    if (count < min)
      min = count;
  }

  printf("%d\n", min);

  free(str);
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  char buf[16] = {0};
  int N;
  char *str = NULL;

  fgets(buf, sizeof(buf), stdin);
  sscanf(buf, "%d", &N);

  str = (char *)malloc(sizeof(char) * (N + 2));
  fgets(str, sizeof(char) * (N + 2), stdin);

  int count = 0;
  for (int i = 1; i < N; i++) {
    if (str[i] == 'E')
      count++;
  }
  int min = count;
  int count_tmp = count;
  for (int i = 1; i < N; i++) {
    if (str[i] == 'E')
      count_tmp--;
    if (str[i - 1] == 'W')
      count_tmp++;
    if (count_tmp < min)
      min = count_tmp;
  }

  printf("%d\n", min);

  free(str);
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "iostream"
#include "string"
using namespace std;

int main() {
  int n, count = 0, min;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    count = 0;
    for (int j = 0; j < i; j++) {
      if (s[j] == 'W')
        count++;
    }
    for (int j = i + 1; j < n; j++) {
      if (s[j] == 'E')
        count++;
    }
    if (i == 0)
      min = count;
    else if (count < min)
      min = count;
  }
  cout << min << endl;
  return 0;
}

Fixed submission:
#include "iostream"
#include "string"
using namespace std;

int main() {
  int n, count = 0, min;
  string s;
  cin >> n >> s;
  for (int i = 1; i < n; i++) {
    if (s[i] == 'E')
      count++;
  }
  min = count;
  for (int i = 1; i < n; i++) {
    if (s[i - 1] == 'W')
      count++;
    if (s[i] == 'E')
      count--;
    if (min > count)
      min = count;
  }
  cout << min << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

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

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

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

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

  int WW, EE; // リーダーよりWest(East)にいてWest(East)を向いている人の数
  WW = 0;
  EE = 0;
  for (int i = 1; i < N; i++) {
    if (S[i] == 'E')
      EE++;
  }

  int ans;
  ans = EE;
  for (int i = 1; i < N; i++) {
    if (S[i - 1] == 'W')
      WW++;
    if (S[i] == 'E')
      EE--;
    int sum = WW + EE;
    ans = min(ans, sum);
  }

  cout << ans << "\n";

  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define sqr(x) (x) * (x)
#define llx(c, itr)                                                            \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
using namespace std;
int n, m, i, j, pre[200005], suf[200005], ans = 1 << 30;
string st;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> st;
  st = " " + st;
  for (i = 1; i <= n; i++) {
    pre[i] = pre[i - 1] + (st[i] == 'W');
  }
  for (i = n; i; i--) {
    suf[i] = suf[i + 1] + (st[i] == 'E');
  }
  for (i = 1; i <= n; i++)
    ans = min(ans, pre[i - 1] + suf[i + 1]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define sqr(x) (x) * (x)
#define llx(c, itr)                                                            \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
using namespace std;
int n, m, i, j, pre[300005], suf[300005], ans = 1 << 30;
string st;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> st;
  st = " " + st;
  for (i = 1; i <= n; i++) {
    pre[i] = pre[i - 1] + (st[i] == 'W');
  }
  for (i = n; i; i--) {
    suf[i] = suf[i + 1] + (st[i] == 'E');
  }
  for (i = 1; i <= n; i++)
    ans = min(ans, pre[i - 1] + suf[i + 1]);
  cout << ans << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ld long double
const int maxn = 2e5 + 10, mod = 1e9 + 7;

ll n, l[maxn], r[maxn];
string s;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    l[i] = l[i - 1] + (s[i - 1] == 'E');
  ll res = n;
  for (int i = 1; i <= n; i++) {
    // cout << i << ' ' << i-1 - l[i-1] + l[n] - l[i] << endl;
    res = min(res, i - 1 - l[i - 1] + l[n] - l[i]);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ld long double
const int maxn = 3e5 + 10, mod = 1e9 + 7;

ll n, l[maxn], r[maxn];
string s;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    l[i] = l[i - 1] + (s[i - 1] == 'E');
  ll res = n;
  for (int i = 1; i <= n; i++) {
    // cout << i << ' ' << i-1 - l[i-1] + l[n] - l[i] << endl;
    res = min(res, i - 1 - l[i - 1] + l[n] - l[i]);
  }
  cout << res << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[30005];
int main() {
  string s;
  int n;
  cin >> n;
  cin >> s;

  for (int i = 1; i < n; i++) {
    if (s[i] == 'E')
      dp[0]++;
  }

  for (int i = 1; i < n; i++) {
    if (s[i - 1] != 'E')
      dp[i]++;
    if (s[i] == 'E')
      dp[i]--;
    dp[i] += dp[i - 1];
  }

  int ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    ans = min(dp[i], ans);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[300005];
int main() {
  string s;
  int n;
  cin >> n;
  cin >> s;

  for (int i = 1; i < n; i++) {
    if (s[i] == 'E')
      dp[0]++;
  }

  for (int i = 1; i < n; i++) {
    if (s[i - 1] != 'E')
      dp[i]++;
    if (s[i] == 'E')
      dp[i]--;
    dp[i] += dp[i - 1];
  }

  int ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    ans = min(dp[i], ans);
  }
  cout << ans;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include "cstdio"
#include "iostream"

#define MAXN
using namespace std;
typedef long long LL;
#define INF 0x7fffffff

char S[30050];

int main() {
  int N;
  scanf("%d", &N);
  scanf("%s", S + 1);
  int cnt = 0;
  int ans = 0;
  for (int i = 1; i < N; i++)
    if (S[i] == 'W')
      cnt++;
  ans = cnt;
  for (int i = N - 1; i >= 1; i--) {
    if (S[i + 1] == 'E')
      cnt++;
    if (S[i] == 'W')
      cnt--;
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "cstdio"
#include "iostream"

#define MAXN
using namespace std;
typedef long long LL;
#define INF 0x7fffffff

char S[300050];

int main() {
  int N;
  scanf("%d", &N);
  scanf("%s", S + 1);
  int cnt = 0;
  int ans = 0;
  for (int i = 1; i < N; i++)
    if (S[i] == 'W')
      cnt++;
  ans = cnt;
  for (int i = N - 1; i >= 1; i--) {
    if (S[i + 1] == 'E')
      cnt++;
    if (S[i] == 'W')
      cnt--;
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3 * 10000 + 10;
int n, pre[maxn], sub[maxn], ans = 9999999;
char str[maxn];
int main() {
  scanf("%d%s", &n, str + 1);
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] + (str[i] == 'W');
  for (int i = n; i >= 1; i--)
    sub[i] = sub[i + 1] + (str[i] == 'E');
  for (int i = 1; i <= n; i++)
    ans = min(ans, pre[i - 1] + sub[i + 1]);
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3 * 100000 + 10;
int n, pre[maxn], sub[maxn], ans = 9999999;
char str[maxn];
int main() {
  scanf("%d%s", &n, str + 1);
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] + (str[i] == 'W');
  for (int i = n; i >= 1; i--)
    sub[i] = sub[i + 1] + (str[i] == 'E');
  for (int i = 1; i <= n; i++)
    ans = min(ans, pre[i - 1] + sub[i + 1]);
  printf("%d", ans);
  return 0;
}
Problem p03341 (C++)

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

int E[200005], n;
char S[200005];

int main() {
  scanf("%d", &n);
  scanf("%*c%s", S + 1);
  for (int i = 1; i <= n; i++) {
    E[i] = E[i - 1] + (S[i] == 'E');
  }
  int ans = n;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, (i - 1 - E[i - 1] + E[n] - E[i]));
  }
  cout << ans << '\n';
}


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

int E[300005], n;
char S[300005];

int main() {
  scanf("%d", &n);
  scanf("%*c%s", S + 1);
  for (int i = 1; i <= n; i++) {
    E[i] = E[i - 1] + (S[i] == 'E');
  }
  int ans = n;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, (i - 1 - E[i - 1] + E[n] - E[i]));
  }
  cout << ans << '\n';
}
Problem p03341 (C++)

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

using namespace std;

const int N = 2e5 + 5;

char s[N];
int cnt[N];
int main() {
  int n;
  scanf("%d %s", &n, s);
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    cnt[i] = cur;
    cur += s[i] == 'W';
  }
  int ans = cur;
  cur = 0;
  for (int i = n - 1; i >= 0; --i) {
    ans = min(ans, cnt[i] + cur);
    cur += s[i] == 'E';
  }
  ans = min(ans, cur);
  printf("%d\n", ans);
  return 0;
}

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

using namespace std;

const int N = 4e5 + 5;

char s[N];
int cnt[N];
int main() {
  int n;
  scanf("%d %s", &n, s);
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    cnt[i] = cur;
    cur += s[i] == 'W';
  }
  int ans = cur;
  cur = 0;
  for (int i = n - 1; i >= 0; --i) {
    ans = min(ans, cnt[i] + cur);
    cur += s[i] == 'E';
  }
  ans = min(ans, cur);
  printf("%d\n", ans);
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int fl[100005];
int a[100005];
int main() {
  int n;
  cin >> n;
  memset(fl, 0, sizeof(fl));
  memset(a, 0, sizeof(a));
  char d;
  scanf("%c", &d);
  for (int i = 1; i <= n; i++) {
    char c;
    scanf("%c", &c);
    fl[i] = fl[i - 1];
    if (c == 'W') {
      fl[i]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    a[i] += fl[i - 1];
    a[i] += (n - i) - (fl[n] - fl[i]);
  }

  int minl;
  minl = 1;
  for (int i = 2; i <= n; i++) {
    if (a[minl] > a[i]) {
      minl = i;
    }
  }
  cout << a[minl];
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int fl[300005];
int a[300005];
int main() {
  int n;
  cin >> n;
  memset(fl, 0, sizeof(fl));
  memset(a, 0, sizeof(a));
  char d;
  scanf("%c", &d);
  for (int i = 1; i <= n; i++) {
    char c;
    scanf("%c", &c);
    fl[i] = fl[i - 1];
    if (c == 'W') {
      fl[i]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    a[i] += fl[i - 1];
    a[i] += (n - i) - (fl[n] - fl[i]);
  }

  int minl;
  minl = 1;
  for (int i = 2; i <= n; i++) {
    if (a[minl] > a[i]) {
      minl = i;
    }
  }
  cout << a[minl];
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
// const int MAX = 300005;
const int MAX = 10;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main() {
  int N;
  string S;
  cin >> N >> S;
  char W = 'W', E = 'E';
  vector<int> cntW(MAX), cntE(MAX);
  for (int i = 0; i < N; i++) {
    cntW[i] += S[i] == W;
    cntE[i] += S[N - 1 - i] == E;
  }
  for (int i = 0; i < N - 1; i++) {
    cntW[i + 1] += cntW[i];
    cntE[i + 1] += cntE[i];
  }
  int ans = INF;
  for (int i = 0; i < N; i++) {
    ans = min(ans, cntW[i] + cntE[N - 1 - i] - 1);
  }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 300005;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main() {
  int N;
  string S;
  cin >> N >> S;
  char W = 'W', E = 'E';
  vector<int> cntW(MAX), cntE(MAX);
  for (int i = 0; i < N; i++) {
    cntW[i] += S[i] == W;
    cntE[i] += S[N - 1 - i] == E;
  }
  for (int i = 0; i < N - 1; i++) {
    cntW[i + 1] += cntW[i];
    cntE[i + 1] += cntE[i];
  }
  int ans = INF;
  for (int i = 0; i < N; i++) {
    ans = min(ans, cntW[i] + cntE[N - 1 - i] - 1);
  }
  cout << ans << '\n';
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
string s;
int n, a[30010], cnt;
int main() {
  cin >> n >> s;
  cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    a[i] += cnt;
    if (s[i] == 'E') {
      cnt++;
    }
  }
  cnt = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    a[i] += cnt;
    if (s[i] == 'W') {
      cnt++;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > a[ans]) {
      ans = i;
    }
  }
  cout << n - a[ans] - 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
string s;
int n, a[300010], cnt;
int main() {
  cin >> n >> s;
  cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    a[i] += cnt;
    if (s[i] == 'E') {
      cnt++;
    }
  }
  cnt = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    a[i] += cnt;
    if (s[i] == 'W') {
      cnt++;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > a[ans]) {
      ans = i;
    }
  }
  cout << n - a[ans] - 1 << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define INF 0x7f7f7f7f
const int MAXN = 1e6;
int n;
int ans = INF;
char s[MAXN];
int sum[MAXN];
using namespace std;
int min(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= strlen(s + 1); i++) {
    int now = 0;
    if (s[i] == 'E')
      now = 1;
    sum[i] = sum[i - 1] + now;
  }
  for (int i = 1; i <= n; i++) {
    ans = min(ans, (i - 1) - sum[i - 1] + (sum[n] - sum[i]));
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define INF 0x7f7f7f7f
const int MAXN = 1e6;
int n;
int ans = INF;
char s[MAXN];
int sum[MAXN];
using namespace std;
int min(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    int now = 0;
    if (s[i] == 'E')
      now = 1;
    sum[i] = sum[i - 1] + now;
  }
  for (int i = 1; i <= n; i++) {
    ans = min(ans, (i - 1) - sum[i - 1] + (sum[n] - sum[i]));
  }
  printf("%d", ans);
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
ll w[3030], e[3030];
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W')
      w[i] = 1;
    else
      e[i] = 1;
    if (i) {
      w[i] += w[i - 1];
      e[i] += e[i - 1];
    }
  }
  ll ans = 1e9;
  for (int i = 0; i < s.size(); i++) {
    ll cnt;
    if (i) {
      if (i == s.size() - 1)
        cnt = w[i - 1];
      else
        cnt = w[i - 1] + e[n - 1] - e[i];
    } else {
      cnt = e[n - 1] - e[i];
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
ll w[300030], e[300030];
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W')
      w[i] = 1;
    else
      e[i] = 1;
    if (i) {
      w[i] += w[i - 1];
      e[i] += e[i - 1];
    }
  }
  ll ans = 1e9;
  for (int i = 0; i < s.size(); i++) {
    ll cnt;
    if (i) {
      if (i == s.size() - 1)
        cnt = w[i - 1];
      else
        cnt = w[i - 1] + e[n - 1] - e[i];
    } else {
      cnt = e[n - 1] - e[i];
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 100100;
char s[maxn];
int sum[maxn];
int main(void) {
  int n;
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] == 'E')
      sum[i] = sum[i - 1] + 1;
    else
      sum[i] = sum[i - 1];
  int mx = n;
  for (int i = 1; i <= n; i++) {
    mx = min(mx, (i - 1 - sum[i - 1]) + (sum[n] - sum[i]));
  }
  printf("%d\n", mx);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 300100;
char s[maxn];
int sum[maxn];
int main(void) {
  int n;
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] == 'E')
      sum[i] = sum[i - 1] + 1;
    else
      sum[i] = sum[i - 1];
  int mx = n;
  for (int i = 1; i <= n; i++) {
    mx = min(mx, (i - 1 - sum[i - 1]) + (sum[n] - sum[i]));
  }
  printf("%d\n", mx);
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int n, w[102400], e[102400], minv = 999999;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char aa;
    cin >> aa;
    aa == 'W' ? w[i]++ : e[i]++;
    w[i] += w[i - 1];
    e[i] += e[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    int ii = w[i - 1] + e[n] - e[i];
    minv = min(minv, ii);
  }
  cout << minv;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int n, w[10240000], e[10240000], minv = 99999999;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char aa;
    cin >> aa;
    aa == 'W' ? w[i]++ : e[i]++;
    w[i] += w[i - 1];
    e[i] += e[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    int ii = w[i - 1] + e[n] - e[i];
    minv = min(minv, ii);
  }
  cout << minv;
  return 0;
}
Problem p03341 (C++)

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T> void operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    cin >> e;
}

typedef long long ll;
int const inf = 1 << 29;
int L[100001], R[100001];
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  rep(i, N) {
    if (s[i] == 'W')
      L[i]++;
    if (i > 0)
      L[i] += L[i - 1];
  }
  for (int i = N - 1; i > 0; i--) {
    if (s[i] == 'E')
      R[i]++;
    if (i < N - 1)
      R[i] += R[i + 1];
  }
  int ans = 1e9;
  rep(i, N) {
    int l = i > 0 ? L[i - 1] : 0;
    int r = i < N - 1 ? R[i + 1] : 0;
    int cand = l + r;
    ans = min(ans, cand);
  }
  cout << ans << endl;
}

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T> void operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    cin >> e;
}

typedef long long ll;
int const inf = 1 << 29;
int L[300001], R[300001];
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  rep(i, N) {
    if (s[i] == 'W')
      L[i]++;
    if (i > 0)
      L[i] += L[i - 1];
  }
  for (int i = N - 1; i > 0; i--) {
    if (s[i] == 'E')
      R[i]++;
    if (i < N - 1)
      R[i] += R[i + 1];
  }
  int ans = 1e9;
  rep(i, N) {
    int l = i > 0 ? L[i - 1] : 0;
    int r = i < N - 1 ? R[i + 1] : 0;
    int cand = l + r;
    ans = min(ans, cand);
  }
  cout << ans << endl;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, sum[200020][2], A[200020];
int main() {
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++) {
    char ch;
    cin >> ch;
    if (ch == 'W')
      A[i] = 1, sum[i][1]++;
    else
      sum[i][0]++;
    sum[i][1] += sum[i - 1][1];
    sum[i][0] += sum[i - 1][0];
  }
  int ans = n;
  for (i = 1; i <= n; i++)
    ans = min(ans, sum[i - 1][1] + sum[n][0] - sum[i][0]);
  cout << ans;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, sum[2000020][2], A[2000020];
int main() {
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++) {
    char ch;
    cin >> ch;
    if (ch == 'W')
      A[i] = 1, sum[i][1]++;
    else
      sum[i][0]++;
    sum[i][1] += sum[i - 1][1];
    sum[i][0] += sum[i - 1][0];
  }
  int ans = n;
  for (i = 1; i <= n; i++)
    ans = min(ans, sum[i - 1][1] + sum[n][0] - sum[i][0]);
  cout << ans;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define MAXN 200005
// #define ivorysi
using namespace std;
typedef long long int64;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10)
    out(x / 10);
  putchar('0' + x % 10);
}

int N, sum[2][MAXN];
char s[MAXN];
void Solve() {
  read(N);
  scanf("%s", s + 1);
  for (int i = 1; i <= N; ++i) {
    sum[0][i] = sum[0][i - 1];
    sum[1][i] = sum[1][i - 1];
    if (s[i] == 'E')
      sum[0][i]++;
    else
      sum[1][i]++;
  }
  int ans = N;
  for (int i = 1; i <= N; ++i) {
    ans = min(i - 1 - sum[0][i - 1] + (N - i - (sum[1][N] - sum[1][i])), ans);
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define MAXN 300005
// #define ivorysi
using namespace std;
typedef long long int64;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10)
    out(x / 10);
  putchar('0' + x % 10);
}

int N, sum[2][MAXN];
char s[MAXN];
void Solve() {
  read(N);
  scanf("%s", s + 1);
  for (int i = 1; i <= N; ++i) {
    sum[0][i] = sum[0][i - 1];
    sum[1][i] = sum[1][i - 1];
    if (s[i] == 'E')
      sum[0][i]++;
    else
      sum[1][i]++;
  }
  int ans = N;
  for (int i = 1; i <= N; ++i) {
    ans = min(i - 1 - sum[0][i - 1] + (N - i - (sum[1][N] - sum[1][i])), ans);
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
}
Problem p03341 (C++)

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

using namespace std;

typedef long long ll;
typedef double long dl;
typedef pair<int, int> pii;
typedef pair<pii, pii> p2i;
const int maxn = (int)2e5 + 1;
const int mod = (int)1e9 + 7;
const ll inf = (ll)1e18;

#define fi first
#define se second
#define acel ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define next continue

int n;
string s;
int a[maxn], b[maxn], ans = 1e9;
int main() {
  cin >> n >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      a[i - 1]++;
    } else {
      b[i + 1]++;
    }
  }
  for (int i = 0; i < s.size(); i++) {
    b[i] += b[i - 1];
  }
  for (int i = s.size() - 1; i >= 0; i--) {
    a[i] += a[i + 1];
  }
  for (int i = 0; i < n; i++) {
    ans = min(ans, (n - 1) - (a[i] + b[i]));
  }
  cout << ans;
}


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

using namespace std;

typedef long long ll;
typedef double long dl;
typedef pair<int, int> pii;
typedef pair<pii, pii> p2i;
const int maxn = (int)3e5 + 1;
const int mod = (int)1e9 + 7;
const ll inf = (ll)1e18;

#define fi first
#define se second
#define acel ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define next continue

int n;
string s;
int a[maxn], b[maxn], ans = 1e9;
int main() {
  cin >> n >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      a[i - 1]++;
    } else {
      b[i + 1]++;
    }
  }
  for (int i = 0; i < s.size(); i++) {
    b[i] += b[i - 1];
  }
  for (int i = s.size() - 1; i >= 0; i--) {
    a[i] += a[i + 1];
  }
  for (int i = 0; i < n; i++) {
    ans = min(ans, (n - 1) - (a[i] + b[i]));
  }
  cout << ans;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
int read() {
  int x = 0, c;
  while (!isdigit(c = getchar()))
    ;
  while (x = x * 10 + c - '0', isdigit(c = getchar()))
    ;
  return x;
}
int n, sum[maxn];
char s[maxn];
int main() {
  n = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + (s[i] == 'W');
  }
  int ans = n, res;
  for (int i = 1; i <= n; i++) {
    res = sum[i - 1] + n - i - sum[n] + sum[i];
    ans = min(ans, res);
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 500005
using namespace std;
int read() {
  int x = 0, c;
  while (!isdigit(c = getchar()))
    ;
  while (x = x * 10 + c - '0', isdigit(c = getchar()))
    ;
  return x;
}
int n, sum[maxn];
char s[maxn];
int main() {
  n = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + (s[i] == 'W');
  }
  int ans = n, res;
  for (int i = 1; i <= n; i++) {
    res = sum[i - 1] + n - i - sum[n] + sum[i];
    ans = min(ans, res);
  }
  printf("%d", ans);
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define erep(i, a, b) for (int i = a; i <= (int)(b); ++i)
#define per(i, a, b) for (int i = (a); i > (b); --i)
#define eper(i, a, b) for (int i = (a); i >= b; --i)
#define pb push_back
#define mp make_pair
#define INF (1 << 30) - 1
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define vii vector<int>
#define vll vector<long long>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int n, ans = INF;
string s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> s;
  vii e(n + 2), w(n + 2);
  e[n] = w[0] = 0;
  eper(i, n, 0) e[i - 1] = e[i] + (s[i - 1] == 'E' ? 1 : 0);
  rep(i, 0, n) w[i + 1] = w[i] + (s[i] == 'W' ? 1 : 0);
  rep(i, 1, n) {
    // cout << w[i+1] << " " << e[i] << endl;
    ans = min(ans, w[i] + e[i]);
  }
  ans = min(ans, min(w[n], e[0]));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define erep(i, a, b) for (int i = a; i <= (int)(b); ++i)
#define per(i, a, b) for (int i = (a); i > (b); --i)
#define eper(i, a, b) for (int i = (a); i >= b; --i)
#define pb push_back
#define mp make_pair
#define INF (1 << 30) - 1
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define vii vector<int>
#define vll vector<long long>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int n, ans = INF;
string s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> s;
  vii d(n + 1);
  rep(i, 0, n) d[i + 1] = d[i] + (s[i] == 'E' ? 1 : 0);

  ans = n;
  rep(i, 0, n) { ans = min(ans, i - d[i] + d[n] - d[i + 1]); }
  cout << ans << endl;
  return 0;
}
Problem p03341 (C++)

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

int n;
char s[MAX];
int l[MAX]; // face w
int wtot;

int minn = INT_MAX;

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

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

int n;
char s[MAX];
int l[MAX]; // face w
int wtot;

int minn = INT_MAX;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<lint, lint> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int sum[200100], s2[200100];
int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  sum[0] = s2[n] = 0;
  rep(i, n) {
    sum[i + 1] = sum[i];
    if (s[i] == 'W')
      sum[i + 1]++;
  }
  for (int i = n - 1; i >= 0; i--) {
    s2[i] = s2[i + 1];
    if (s[i] == 'E')
      s2[i]++;
  }
  int out = n;
  rep(i, n) out = min(out, sum[i] + s2[i + 1]);
  cout << out << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<lint, lint> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int sum[300100], s2[300100];
int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  sum[0] = s2[n] = 0;
  rep(i, n) {
    sum[i + 1] = sum[i];
    if (s[i] == 'W')
      sum[i + 1]++;
  }
  for (int i = n - 1; i >= 0; i--) {
    s2[i] = s2[i + 1];
    if (s[i] == 'E')
      s2[i]++;
  }
  int out = n;
  rep(i, n) out = min(out, sum[i] + s2[i + 1]);
  cout << out << endl;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
// #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define PI acos(-1.0)
typedef long long ll;
typedef unsigned long long ull;
#define gcd __gcd
const int INF = 0x3f3f3f3f;
const ll LINF = 1000000000000000000;
// priority_queue<int, vector<int>, greater<int> >p;
#define N 200005
ll n, m, l, x = 0, y, k, sum, g, mid, h, v;
// ll d[4][2]={1,0,-1,0,0,1,0,-1};
ll a[N], b[N];
char c[300005];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  cin >> c + 1;
  for (int i = 1; i < n; i++)
    if (c[i] == 'W')
      a[i + 1] = a[i] + 1;
    else
      a[i + 1] = a[i];
  for (int i = n; i >= 1; i--)
    if (c[i] == 'E')
      b[i - 1] = b[i] + 1;
    else
      b[i - 1] = b[i];
  sum = INF;
  for (int i = 1; i <= n; i++)
    sum = min(sum, a[i] + b[i]);
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
// #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define PI acos(-1.0)
typedef long long ll;
typedef unsigned long long ull;
#define gcd __gcd
const int INF = 0x3f3f3f3f;
const ll LINF = 1000000000000000000;
// priority_queue<int, vector<int>, greater<int> >p;
#define N 300005
ll n, m, l, x = 0, y, k, sum, g, mid, h, v;
// ll d[4][2]={1,0,-1,0,0,1,0,-1};
ll a[N], b[N];
char c[300005];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  cin >> c + 1;
  for (int i = 1; i < n; i++)
    if (c[i] == 'W')
      a[i + 1] = a[i] + 1;
    else
      a[i + 1] = a[i];
  for (int i = n; i >= 1; i--)
    if (c[i] == 'E')
      b[i - 1] = b[i] + 1;
    else
      b[i - 1] = b[i];
  sum = INF;
  for (int i = 1; i <= n; i++)
    sum = min(sum, a[i] + b[i]);
  cout << sum << endl;
  return 0;
}
Problem p03341 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  vector<int> num(N);
  for (int i = 0; i < N; i++) {
    if (S[i] == 'E') {
      num[i] = 1;
    } else {
      num[i] = 0;
    }
  }
  vector<int> ruisekiwa(N + 1);
  ruisekiwa[0] = 0;
  for (int i = 0; i < N + 1; i++) {
    ruisekiwa[i + 1] = ruisekiwa[i] + num[i];
  }

  vector<int> points(N);
  for (int i = 0; i < N; i++) {
    points[i] = ruisekiwa[i] + (N - i - ruisekiwa[N] + ruisekiwa[i]);
  }
  points[N - 1] = ruisekiwa[N - 1] + 1;
  sort(points.begin(), points.end());
  cout << N - points[N - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  vector<int> num(N);
  for (int i = 0; i < N; i++) {
    if (S[i] == 'E') {
      num[i] = 1;
    } else {
      num[i] = 0;
    }
  }
  vector<int> ruisekiwa(N + 2);
  ruisekiwa[0] = 0;
  for (int i = 0; i < N + 1; i++) {
    ruisekiwa[i + 1] = ruisekiwa[i] + num[i];
  }

  vector<int> points(N);
  for (int i = 0; i < N; i++) {
    points[i] = ruisekiwa[i] + (N - i - ruisekiwa[N] + ruisekiwa[i]);
  }
  points[N - 1] = ruisekiwa[N - 1] + 1;
  sort(points.begin(), points.end());
  cout << N - points[N - 1] << endl;
}