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

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

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

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

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

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 100050;
// ここまでテンプレ
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int E[SIZE] = {}, W[SIZE] = {};
  for (int i = 0; i < N; i++) {
    if (S[i] == 'E')
      E[i + 1]++;
    else
      W[i + 1]++;
  }
  for (int i = 1; i <= N; i++) {
    E[i] += E[i - 1];
    W[i] += W[i - 1];
  }
  int mini = INF;
  for (int i = 0; i < N; i++) {
    int temp = W[i] + (E[N] - E[i + 1]);
    mini = min(temp, mini);
  }
  cout << mini << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 300050;
// ここまでテンプレ
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int E[SIZE] = {}, W[SIZE] = {};
  for (int i = 0; i < N; i++) {
    if (S[i] == 'E')
      E[i + 1]++;
    else
      W[i + 1]++;
  }
  for (int i = 1; i <= N; i++) {
    E[i] += E[i - 1];
    W[i] += W[i - 1];
  }
  int mini = INF;
  for (int i = 0; i < N; i++) {
    int temp = W[i] + (E[N] - E[i + 1]);
    mini = min(temp, mini);
  }
  cout << mini << endl;
  return 0;
}
Problem p03339 (C++)

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

int main() {
  ll n;
  string s;
  cin >> n >> s;

  vector<ll> c(n, 0);

  for (ll i = 0; i < n; i++) {
    c[i + 1] = c[i];
    if (s[i] == 'W') {
      c[i + 1]++;
    }
  }

  ll ans = n;
  for (ll i = 1; i < n + 1; i++) {
    ans = min(ans, c[i - 1] + (n - i) - (c[n] - c[i]));
  }
  cout << ans << endl;
}


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

int main() {
  ll n;
  string s;
  cin >> n >> s;

  vector<ll> c(n + 1, 0);

  for (ll i = 0; i < n; i++) {
    c[i + 1] = c[i];
    if (s[i] == 'W') {
      c[i + 1]++;
    }
  }

  ll ans = n;
  for (ll i = 1; i < n + 1; i++) {
    ans = min(ans, c[i - 1] + (n - i) - (c[n] - c[i]));
  }
  cout << ans << endl;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
typedef vector<int> vi;

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define out(x) cout << (x) << endl

int main() {
  int i, j, k, n, m, l, f, x, y, z;
  string s;
  cin >> n >> s;
  vi a(n + 1, 0), b(n + 1, 0);
  REP(i, 1, n + 1) {
    a[i] = a[i - 1];
    b[n - i - 1] = b[n - i];
    if (s[i - 1] == 'W')
      ++a[i];
    if (s[n - i] == 'E')
      ++b[n - i - 1];
  }

  f = 1e6;
  rep(i, n) { f = min(f, a[i] + b[i]); }
  cout << f << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
typedef vector<int> vi;

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define out(x) cout << (x) << endl

int main() {
  int i, j, k, n, m, l, f, x, y, z;
  string s;
  cin >> n >> s;
  vi a(n + 1, 0), b(n + 1, 0);
  REP(i, 1, n) {
    a[i] = a[i - 1];
    b[n - i - 1] = b[n - i];
    if (s[i - 1] == 'W')
      ++a[i];
    if (s[n - i] == 'E')
      ++b[n - i - 1];
  }

  f = 1e6;
  rep(i, n) { f = min(f, a[i] + b[i]); }
  cout << f << endl;
}
Problem p03339 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
east_side = list(input())
west_side = []
ans = []
for i in range(len(east_side)):
    WE_sum = east_side.count("E") + west_side.count("W")
    ans.append(WE_sum)
    west_side.append(east_side[0])
    east_side.pop(0)
print(min(ans))


Fixed submission:
n = int(input())
s = input()
rev_counter = []
rev_num = s.count("E")
if s[0] == "E":
    rev_num -= 1
rev_counter.append(rev_num)
for i in range(1, n):
    if s[i] == "E" and s[i - 1] == "E":
        rev_num -= 1
    elif s[i] == "W" and s[i - 1] == "W":
        rev_num += 1
    rev_counter.append(rev_num)
print(min(rev_counter))
Problem p03339 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def main():
    n = int(input())
    s = input()
    ans = float("inf")
    for i in range(n):
        tmp = s[:i].count("W") + s[i:].count("E")
        if ans > tmp:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def main():
    n = int(input())
    s = input()
    cnt = s.count("E")
    m = cnt
    for i in s:
        if i == "E":
            cnt -= 1
        else:
            cnt += 1
        m = min(m, cnt)
    print(m)


if __name__ == "__main__":
    main()
Problem p03339 (Python)

Buggy submission (status: Time Limit Exceeded):
def solve(string):
    n, s = string.split()
    return str(min([s.count("W", 0, i) + s.count("E", i + 1) for i in range(int(n))]))


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))


Fixed submission:
def solve(string):
    n, s = string.split()
    n = int(n)
    es = {i for i, _s in enumerate(s) if _s == "E"}
    num_e = len(es)
    num_w = n - num_e
    count_e = 0
    count_w = 0

    ans = n
    for i, _s in enumerate(s):
        tmp = [count_e, count_w]
        if i in es:
            count_e += 1
        else:
            count_w += 1
        tmp += [num_e - count_e, num_w - count_w]
        ans = min(ans, tmp[1] + tmp[2])
    return str(ans)


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define FOR(i, a, b) for (size_t i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const int MAX_V = 200001;
int w_table[MAX_V];
int e_table[MAX_V];

void calc_table(const int &n, const string &s) {
  w_table[0] = 0;
  e_table[0] = 0;
  for (size_t i = 1; i <= n; ++i)
    if (s[i - 1] == 'W') {
      w_table[i] = w_table[i - 1] + 1;
      e_table[i] = e_table[i - 1];
    } else {
      e_table[i] = e_table[i - 1] + 1;
      w_table[i] = w_table[i - 1];
    }
}

int solve(const int n, string s) {
  int count = 1 << 30;
  FOR(i, 1, n + 1) {
    count = min(count, w_table[i - 1] + e_table[n] - e_table[i]);
  }
  return count;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  calc_table(n, s);
  // REP(i, n + 1)
  //     cout << w_table[i] << " ";
  // cout << endl;
  // REP(i, n + 1)
  //     cout << e_table[i] << " ";
  // cout << endl;
  cout << solve(n, s) << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define FOR(i, a, b) for (size_t i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const int MAX_V = 2000001;
int w_table[MAX_V];
int e_table[MAX_V];

void calc_table(const int &n, const string &s) {
  w_table[0] = 0;
  e_table[0] = 0;
  for (size_t i = 1; i <= n; ++i)
    if (s[i - 1] == 'W') {
      w_table[i] = w_table[i - 1] + 1;
      e_table[i] = e_table[i - 1];
    } else {
      e_table[i] = e_table[i - 1] + 1;
      w_table[i] = w_table[i - 1];
    }
}

int solve(const int n, string s) {
  int count = 1 << 30;
  FOR(i, 1, n + 1) {
    count = min(count, w_table[i - 1] + e_table[n] - e_table[i]);
  }
  return count;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  calc_table(n, s);
  // REP(i, n + 1)
  //     cout << w_table[i] << " ";
  // cout << endl;
  // REP(i, n + 1)
  //     cout << e_table[i] << " ";
  // cout << endl;
  cout << solve(n, s) << endl;
}
Problem p03339 (C++)

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

const int Nmax = 200010;

int main() {
  int N, A[Nmax] = {0};
  char S[Nmax];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    if (S[i] == 'E')
      A[i] = 1;
    else
      A[i] = 2;
  }
  int S1[Nmax] = {}, S2[Nmax] = {}, mac = Nmax;
  if (A[0] == 2)
    S1[0] = 1;
  if (A[N - 1] == 1)
    S2[N - 1] = 1;
  for (int i = 1; i < N; i++) {
    if (A[i] == 2)
      S1[i] = S1[i - 1] + 1;
    else
      S1[i] = S1[i - 1];
  }
  for (int i = N - 2; i >= 0; i--) {
    if (A[i] == 1)
      S2[i] = S2[i + 1] + 1;
    else
      S2[i] = S2[i + 1];
  }
  mac = S2[0];
  for (int i = 1; i < N - 1; i++) {
    mac = min(mac, S1[i - 1] + S2[i + 1]);
  }
  mac = min(S1[N - 1], mac);
  cout << mac << endl;
  return 0;
}

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

// const int Nmax=200010;
const ll Nmax = 3000010;
int main() {
  int N, A[Nmax] = {0};
  char S[Nmax];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    if (S[i] == 'E')
      A[i] = 1;
    else
      A[i] = 2;
  }
  int S1[Nmax] = {}, S2[Nmax] = {}, mac = Nmax;
  if (A[0] == 2)
    S1[0] = 1;
  if (A[N - 1] == 1)
    S2[N - 1] = 1;
  for (int i = 1; i < N; i++) {
    if (A[i] == 2)
      S1[i] = S1[i - 1] + 1;
    else
      S1[i] = S1[i - 1];
  }
  for (int i = N - 2; i >= 0; i--) {
    if (A[i] == 1)
      S2[i] = S2[i + 1] + 1;
    else
      S2[i] = S2[i + 1];
  }
  mac = S2[0];
  for (int i = 1; i < N - 1; i++) {
    mac = min(mac, S1[i - 1] + S2[i + 1]);
  }
  mac = min(S1[N - 1], mac);
  cout << mac << endl;
  return 0;
}
Problem p03339 (C++)

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

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<int> W(N, 0);
  for (int i = 0; i < N; i++) {
    if (i == 0 && S.at(i) == 'W')
      W.at(i) = 1;
    else if (S.at(i) == 'W')
      W.at(i) = W.at(i - 1) + 1;
    else
      W.at(i) = W.at(i - 1);
  }
  int p_min = 0;
  for (int i = 0; i < N; i++) {
    if (i == 0)
      p_min = N - 1 - W.at(N - 1) + W.at(i);
    else if (p_min > W.at(i - 1) + (N - (i + 1)) - (W.at(N - 1) - W.at(i))) {
      p_min = W.at(i - 1) + (N - (i + 1)) - (W.at(N - 1) - W.at(i));
    }
  }
  cout << p_min << endl;
}

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (ll)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
using namespace std;
int w[30001];
int e[30001];
int main() {
  int n;
  string s;
  cin >> n >> s;
  reps(i, n - 1) {
    if (s[i - 1] == 'W')
      w[i] = w[i - 1] + 1;
    else
      w[i] = w[i - 1];
  }
  for (int i = n - 2; i >= 0; i--) {
    if (s[i + 1] == 'E')
      e[i] = e[i + 1] + 1;
    else
      e[i] = e[i + 1];
  }
  int mn = 1 << 30;
  rep(i, n) { mn = min(mn, w[i] + e[i]); }
  cout << mn << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (ll)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
using namespace std;
int w[300001];
int e[300001];
int main() {
  int n;
  string s;
  cin >> n >> s;
  reps(i, n - 1) {
    if (s[i - 1] == 'W')
      w[i] = w[i - 1] + 1;
    else
      w[i] = w[i - 1];
  }
  for (int i = n - 2; i >= 0; i--) {
    if (s[i + 1] == 'E')
      e[i] = e[i + 1] + 1;
    else
      e[i] = e[i + 1];
  }
  int mn = 1 << 30;
  rep(i, n) { mn = min(mn, w[i] + e[i]); }
  cout << mn << endl;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <math.h>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;

int main(void) {
  int N;
  cin >> N;
  bool S[1000000];
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == 'E') {
      S[i] = false;
    } else {
      S[i] = true;
    }
  }
  int ans = N;
  for (int i = 0; i < N; i++) {
    int num = 0;
    for (int j = 0; j < N; j++) {
      if (j < i) {
        if (S[j] == true)
          num++;
      } else if (j > i) {
        if (S[j] == false)
          num++;
      }
    }
    if (num < ans)
      ans = num;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <math.h>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;

int main(void) {
  int N;
  cin >> N;
  bool S[1000000];
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == 'E') {
      S[i] = false;
    } else {
      S[i] = true;
    }
  }
  int ans = N;
  int num;
  for (int j = 0; j < N; j++) {
    if (j > 0) {
      if (S[j] == false)
        num++;
    }
  }
  ans = num;
  for (int i = 1; i < N; i++) {
    if (S[i] == false) {
      num--;
    }
    if (S[i - 1] == true) {
      num++;
    }
    if (num < ans)
      ans = num;
  }
  cout << ans << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;

int dp[2][100100];

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;

  if (s[0] == 'E') {
    dp[0][0] = 1;
  } else {
    dp[1][0] = 1;
  }
  rep(i, n - 1) {
    if (s[i + 1] == 'E') {
      dp[0][i + 1] = 1 + dp[0][i];
      dp[1][i + 1] = dp[1][i];
    } else {
      dp[1][i + 1] = 1 + dp[1][i];
      dp[0][i + 1] = dp[0][i];
    }
  }
  int ans = 100100100;
  rep(i, n) {
    int cnt = 0;
    if (i == 0) {
      cnt += dp[0][n - 1] - dp[0][0];
    } else if (i == n - 1) {
      cnt += dp[1][n - 2];
    } else {
      cnt += dp[1][i - 1] + dp[0][n - 1] - dp[0][i];
    }

    ans = min(cnt, ans);
  }

  cout << ans << endl;

  return 0;
}


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

int dp[2][1000100];

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;

  if (s[0] == 'E') {
    dp[0][0] = 1;
  } else {
    dp[1][0] = 1;
  }
  rep(i, n - 1) {
    if (s[i + 1] == 'E') {
      dp[0][i + 1] = 1 + dp[0][i];
      dp[1][i + 1] = dp[1][i];
    } else {
      dp[1][i + 1] = 1 + dp[1][i];
      dp[0][i + 1] = dp[0][i];
    }
  }
  int ans = 100100100;
  rep(i, n) {
    int cnt = 0;
    if (i == 0) {
      cnt += dp[0][n - 1] - dp[0][0];
    } else if (i == n - 1) {
      cnt += dp[1][n - 2];
    } else {
      cnt += dp[1][i - 1] + dp[0][n - 1] - dp[0][i];
    }

    ans = min(cnt, ans);
  }

  cout << ans << endl;

  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> P;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
struct Accelerate_Cin {
  Accelerate_Cin() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
};

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

  int cont[300005];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (j == i) {
        continue;
      } else if (j > i && S[j] == 'E') {
        cont[i]++;
      } else if (j < i && S[j] == 'W') {
        cont[i]++;
      }
    }
  }

  int min = INF;
  for (int t = 0; t < N; t++) {
    if (cont[t] < min)
      min = cont[t];
  }

  cout << min << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> P;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
struct Accelerate_Cin {
  Accelerate_Cin() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
};

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

  int cont[300005];
  for (int i = 1; i < N; i++) {
    if (S[i] == 'E')
      cont[0]++;
  }

  for (int t = 1; t < N; t++) {
    if (S[t - 1] == 'W' && S[t] == 'W') {
      cont[t] = cont[t - 1] + 1;
    } else if (S[t - 1] == 'E' && S[t] == 'E') {
      cont[t] = cont[t - 1] - 1;
    } else {
      cont[t] = cont[t - 1];
    }
  }

  int min = INF;
  for (int t = 0; t < N; t++) {
    if (cont[t] < min)
      min = cont[t];
  }

  cout << min << "\n";
  return 0;
}
Problem p03339 (C++)

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

const int mx = 30005;
char s[mx];
int n, E[mx], W[mx];
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

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


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

const int mx = 300005;
char s[mx];
int n, E[mx], W[mx];
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'E') {
      E[i + 1] = E[i] + 1;
      W[i + 1] = W[i];
    } else {
      E[i + 1] = E[i];
      W[i + 1] = W[i] + 1;
    }
  }
  int ans = 1e9;
  for (int i = 1; i <= n; i++)
    ans = min(ans, W[i - 1] + E[n] - E[i]);
  cout << ans << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)

int main() {
  int n, c, cmin, c1, c2;
  string s, s1, s2;
  cin >> n >> s;
  s2 = s.substr(1, n - 1);
  c1 = 0;
  c2 = count(s2.cbegin(), s2.cend(), 'E');
  cmin = c2;
  REP(i, 1, n) {
    s1 = s.substr(0, i);
    s2 = s.substr(i + 1, n - i - 1);
    if (s[i - 1] == 'W')
      c1 += 1;
    if (s[i] == 'E') {
      c2 -= 1;
      c = c1 + c2;
      if (cmin > c)
        cmin = c;
    }
  }
  cout << cmin << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)

int main() {
  int n, c, cmin, c1, c2;
  string s, s1, s2;
  cin >> n >> s;
  s2 = s.substr(1, n - 1);
  c1 = 0;
  c2 = count(s2.cbegin(), s2.cend(), 'E');
  cmin = c2;
  REP(i, 1, n) {
    if (s[i - 1] == 'W')
      c1 += 1;
    if (s[i] == 'E') {
      c2 -= 1;
      c = c1 + c2;
      if (cmin > c)
        cmin = c;
    }
  }
  cout << cmin << endl;
}
Problem p03339 (C++)

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

using namespace std;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  int n;
  cin >> n;
  cin >> s;
  int cst = 400000;
  for (int i = 0; i < n; ++i) {
    int c = 0;
    for (int j = 0; j < n; ++j) {
      if (i == j)
        continue;
      else if (i < j) {
        if (s[j] == 'E')
          ++c;

      } else {
        if (s[j] == 'W')
          ++c;
      }
    }
    cst = min(cst, c);
  }
  cout << cst << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  int n;
  cin >> n;
  cin >> s;
  int c = 0;
  for (int i = 1; i < n; ++i)
    if (s[i] == 'E')
      ++c;
  int cst = c;
  for (int i = 1; i < n; ++i) {
    if (s[i - 1] == 'W')
      ++c;
    if (s[i] == 'E')
      --c;
    if (c < cst)
      cst = c;
  }
  cout << cst << endl;

  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long lW[200002], lE[200002], rW[200002], rE[200002];
int main() {
  long long n;
  string s;
  cin >> n;
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      lW[i] = lW[i - 1] + 1;
      lE[i] = lE[i - 1];
    } else {
      lE[i] = lE[i - 1] + 1;
      lW[i] = lW[i - 1];
    }
  }
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 'W') {
      rW[i] = rW[i + 1] + 1;
      rE[i] = rE[i + 1];
    } else {
      rE[i] = rE[i + 1] + 1;
      rW[i] = rW[i + 1];
    }
  }
  long long target = 1000000;
  for (int i = 0; i < s.size(); i++) {
    target = min(target, lW[i - 1] + rE[i + 1]);
  }
  cout << target;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long lW[300002], lE[300002], rW[300002], rE[300002];
int main() {
  long long n;
  string s;
  cin >> n;
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      lW[i] = lW[i - 1] + 1;
      lE[i] = lE[i - 1];
    } else {
      lE[i] = lE[i - 1] + 1;
      lW[i] = lW[i - 1];
    }
  }
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 'W') {
      rW[i] = rW[i + 1] + 1;
      rE[i] = rE[i + 1];
    } else {
      rE[i] = rE[i + 1] + 1;
      rW[i] = rW[i + 1];
    }
  }
  long long target = 1000000;
  for (int i = 0; i < s.size(); i++) {
    target = min(target, lW[i - 1] + rE[i + 1]);
  }
  cout << target;
}
Problem p03339 (C++)

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

typedef long long ll;
#define mod 1000000007
#define ALL(a) a.begin(), a.end()

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n;
  string s;
  cin >> n >> s;

  vector<ll> w(30001, 0);
  vector<ll> e(30001, 0);
  for (ll i = 1; i <= n; i++) {
    e[i] = e[i - 1] + (s[i - 1] == 'E');
    w[i] = w[i - 1] + (s[i - 1] == 'W');
  }

  ll ans = 10e9;
  for (ll i = 0; i <= n; i++) {
    ll east = e[n] - e[i + 1];
    ll west = w[i] - w[0];
    ans = min(ans, east + west);
  }
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
#define mod 1000000007
#define ALL(a) a.begin(), a.end()

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n;
  string s;
  cin >> n >> s;

  vector<ll> w(300001, 0);
  vector<ll> e(300001, 0);
  for (ll i = 1; i <= n; i++) {
    e[i] = e[i - 1] + (s[i - 1] == 'E');
    w[i] = w[i - 1] + (s[i - 1] == 'W');
  }

  ll ans = 10e9;
  for (ll i = 0; i <= n; i++) {
    ll east = e[n] - e[i + 1];
    ll west = w[i] - w[0];
    ans = min(ans, east + west);
  }
  cout << ans << endl;
  return 0;
}
Problem p03339 (C++)

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

int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  int count[100000] = {};
  int mincount;
  for (int i = 1; i < N; i++) {
    if (S[i] == 'E')
      count[0]++;
  }
  mincount = count[0];

  for (int i = 1; i < N; i++) {
    if (S[i - 1] == 'W')
      count[i] = count[i - 1] + 1;
    else
      count[i] = count[i - 1];
    if (S[i] == 'E')
      count[i] -= 1;
    if (count[i] < mincount)
      mincount = count[i];
  }
  /*for (int i = 0; i < N; i++){
      cout << count[i] << endl;
  }*/
  cout << mincount << endl;

  // iはリーダーの位置
  /*int count[1000000] = {};
  for (int i = 0; i < N; i++){
      for (int j = 0; j < i; j++){
          if (S[j] == 'E') count[i]++;
      }
      for (int j = i + 1; j < N; j++){
          if (S[j] == 'W') count[i]++;
      }
  }
  int maxcount = 0;
  for (int i = 0; i < N; i++){
      if (maxcount < count[i]) maxcount = count[i];
  }
  cout << N - 1 - maxcount << endl;
  */
  return 0;
}

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

int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  int count[1000000] = {};
  int mincount;
  for (int i = 1; i < N; i++) {
    if (S[i] == 'E')
      count[0]++;
  }
  mincount = count[0];

  for (int i = 1; i < N; i++) {
    if (S[i - 1] == 'W')
      count[i] = count[i - 1] + 1;
    else
      count[i] = count[i - 1];
    if (S[i] == 'E')
      count[i] -= 1;
    if (count[i] < mincount)
      mincount = count[i];
  }
  /*for (int i = 0; i < N; i++){
      cout << count[i] << endl;
  }*/
  cout << mincount << endl;

  // iはリーダーの位置
  /*int count[1000000] = {};
  for (int i = 0; i < N; i++){
      for (int j = 0; j < i; j++){
          if (S[j] == 'E') count[i]++;
      }
      for (int j = i + 1; j < N; j++){
          if (S[j] == 'W') count[i]++;
      }
  }
  int maxcount = 0;
  for (int i = 0; i < N; i++){
      if (maxcount < count[i]) maxcount = count[i];
  }
  cout << N - 1 - maxcount << endl;
  */
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
/// However long the night,
///     The dawn will break
/// ICPC next year

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;

int n, l[N], r[N];
string str;
int main() {
  cin >> n >> str;
  int cur = 0;
  for (int i = 0; i < str.size(); ++i) {
    if (str[i] == 'W')
      ++cur;
    l[i + 1] = cur;
  }
  cur = 0;
  for (int i = str.size() - 1; i >= 0; --i) {
    if (str[i] == 'E')
      ++cur;
    r[i + 1] = cur;
  }
  int ans = 1e9;
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, l[i - 1] + r[i + 1]);
  }
  cout << ans;
  return 0;
}


Fixed submission:
/// However long the night,
///     The dawn will break
/// ICPC next year

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

int n, l[N], r[N];
string str;
int main() {
  cin >> n >> str;
  int cur = 0;
  for (int i = 0; i < str.size(); ++i) {
    if (str[i] == 'W')
      ++cur;
    l[i + 1] = cur;
  }
  cur = 0;
  for (int i = str.size() - 1; i >= 0; --i) {
    if (str[i] == 'E')
      ++cur;
    r[i + 1] = cur;
  }
  int ans = 1e9;
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, l[i - 1] + r[i + 1]);
  }
  cout << ans;
  return 0;
}
Problem p03339 (C++)

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = n;
  for (int i = 0; i < n; i++) {
    int num = 0;
    for (int j = 0; j < i; j++) {
      if (s[j] == 'W')
        num++;
    }
    for (int j = i; j < n; j++) {
      if (s[j] == 'E')
        num++;
    }
    if (num < ans)
      ans = num;
  }
  cout << ans << endl;
}

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

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

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  int sum, min = 300010;
  for (int i = 0; i < n; ++i) {
    sum = 0;
    for (int j = 0; j < n; ++j) {
      if ((j < i && s[j] == 'W') || (j > i && s[j] == 'E')) {
        sum++;
      }
    }
    if (sum < min) {
      min = sum;
    }
  }
  cout << min << endl;
}

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

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

Buggy submission (status: Time Limit Exceeded):
n = int(input())
S = input()
s = [0 for i in range(n)]

for i in range(n):
    if S[i] == "E":
        s[i] = 1
min = n

for i in range(n):
    count = 0
    for j in range(n):
        if i < j and s[j] == 1:
            count += 1
        elif i > j and s[j] == 0:
            count += 1
    if count < min:
        min = count
print(min)


Fixed submission:
n = int(input())
S = input()
s = [0 for i in range(n)]

for i in range(n):
    if S[i] == "E":
        s[i] = 1
min = n

count = 0
for j in range(n):
    i = 0
    if i < j and s[j] == 1:
        count += 1
    elif i > j and s[j] == 0:
        count += 1
if count < min:
    min = count

for i in range(1, n):
    if s[i - 1] == 0:
        count += 1
    if s[i] == 1:
        count -= 1
    if count < min:
        min = count
print(min)
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int sum[100000];
int main() {
  int n;
  // int sum
  while (cin >> n) {
    memset(sum, 0, sizeof(sum));
    scanf("%s", s);
    int sume = 0, sumw = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'E')
        sume++;
      else
        sumw++;
    }
    int e = 0, w = 0;
    for (int i = 0; i < n; i++) {
      sum[i] = w;
      if (s[i] == 'W') {
        w++;
      }

      if (s[i] == 'E') {
        e++;
      }
      sum[i] = sum[i] + sume - e;
    }
    int minn = 999999;
    for (int i = 0; i < n; i++) {
      if (sum[i] < minn)
        minn = sum[i];
    }
    // sort(sum,sum+n);
    cout << minn << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char s[300005];
int sum[300000];
int main() {
  int n;
  // int sum
  while (cin >> n) {
    memset(sum, 0, sizeof(sum));
    scanf("%s", s);
    int sume = 0, sumw = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'E')
        sume++;
      else
        sumw++;
    }
    int e = 0, w = 0;
    for (int i = 0; i < n; i++) {
      sum[i] = w;
      if (s[i] == 'W') {
        w++;
      }

      if (s[i] == 'E') {
        e++;
      }
      sum[i] = sum[i] + sume - e;
    }
    int minn = 999999;
    for (int i = 0; i < n; i++) {
      if (sum[i] < minn)
        minn = sum[i];
    }
    // sort(sum,sum+n);
    cout << minn << endl;
  }
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

/* //vector
int main()
{
        int N, ans = 0, check = 0;
        char c;
        string S;

        cin >> N;
        cin >> S;

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



        cout << ans;

        char qq;
        cin >> qq;
        return 0;
}
*/

int main() {
  int N, ans = 0, check = 0;
  char c;
  char S[30005];

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

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

  cout << ans;

  char qq;
  cin >> qq;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

/* //vector
int main()
{
        int N, ans = 0, check = 0;
        char c;
        string S;

        cin >> N;
        cin >> S;

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



        cout << ans;

        char qq;
        cin >> qq;
        return 0;
}
*/

int main() {
  int N, ans = 0, check = 0;
  char c;
  char S[300005];

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

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

  cout << ans;

  char qq;
  cin >> qq;
  return 0;
}
Problem p03339 (C++)

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

int main() {
  int N;
  cin >> N;
  int min_num = N;
  string S;
  cin >> S;
  vector<int> E(N), W(N);

  for (int i = 0; i < N; i++) {
    if (S[i] == 'W') {
      if (i == 0)
        W[0]++;
      else
        W[i] = W[i - 1] + 1;
    } else
      W[i] = W[i - 1];

    if (S[N - 1 - i] == 'E') {
      if (i == 0)
        E[N - i - 1]++;
      else
        E[N - 1 - i] = E[N - i] + 1;
    } else
      E[N - 1 - i] = E[N - i];
  }

  for (int i = 0; i < N; i++) {
    string sub1 = S.substr(0, i);
    string sub2 = S.substr(i + 1, N - (i + 1));
    int num;

    if (i == 0)
      num = E[1];
    else if (i == N - 1)
      num = W[N - 1];
    else
      num = E[i + 1] + W[i - 1];

    min_num = min(min_num, num);
  }

  cout << min_num;
  return 0;
}


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

int main() {
  int N;
  cin >> N;
  int min_num = N;
  string S;
  cin >> S;
  vector<int> E(N), W(N);

  for (int i = 0; i < N; i++) {
    if (S[i] == 'W') {
      if (i == 0)
        W[0]++;
      else
        W[i] = W[i - 1] + 1;
    } else
      W[i] = W[i - 1];

    if (S[N - 1 - i] == 'E') {
      if (i == 0)
        E[N - i - 1]++;
      else
        E[N - 1 - i] = E[N - i] + 1;
    } else
      E[N - 1 - i] = E[N - i];
  }

  for (int i = 0; i < N; i++) {
    int num;

    if (i == 0)
      num = E[1];
    else if (i == N - 1)
      num = W[N - 1];
    else
      num = E[i + 1] + W[i - 1];

    min_num = min(min_num, num);
  }

  cout << min_num;
  return 0;
}
Problem p03339 (Python)

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

p = [0] * n
for i in range(n):
    p[i] = s[:i].count("W") + s[i:].count("E")
print(min(p))


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

p = [0] * n
p[0] = s[1:].count("E")
for i in range(1, n):
    p[i] = p[i - 1]
    if s[i - 1] == "W":
        p[i] += 1
    if s[i] == "E":
        p[i] -= 1
print(min(p))
Problem p03339 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N = int(input())
S = list(input())

counter = Counter(S)
E = counter.get("E")
W = counter.get("W")
# print('E W', E, W)

ans = 10**6
e, w = 0, 0
for i in range(N):
    v = w + E - e
    if S[i] == "E":
        v -= 1
        e += 1
    else:
        w += 1
    # print('v, e, w', v, e, w)
    ans = min(ans, v)
print(ans)


Fixed submission:
from collections import Counter

N = int(input())
S = list(input())

counter = Counter(S)
E = counter.get("E", 0)
W = counter.get("W", 0)
# print('E W', E, W)

ans = 10**6
e, w = 0, 0
for i in range(N):
    v = w + E - e
    if S[i] == "E":
        v -= 1
        e += 1
    else:
        w += 1
    # print('v, e, w', v, e, w)
    ans = min(ans, v)
print(ans)
Problem p03339 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
S = input()
S_list = list(S)
face = [-1, 1]
ans_index = N // 2
while ans_index != N - 1 and S[ans_index] == S[ans_index + face[S[ans_index] == "E"]]:
    ans_index += face[S[ans_index] == "E"]
# print(ans_index)
ans = 0
for i in range(len(S)):
    if i < ans_index and S[i] == "W":
        ans += 1
    elif ans_index == i:
        continue
    elif i > ans_index and S[i] == "E":
        ans += 1
print(ans)


Fixed submission:
N = int(input())
S = input()
E_cost = S.count("E")
W_cost = 0
ans = N
for s in S:
    if s == "E":
        E_cost -= 1
    ans = min(ans, E_cost + W_cost)
    if s == "W":
        W_cost += 1
print(ans)
Problem p03339 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
s = input()

e = [0 for _ in range(n)]
w = [0 for _ in range(n)]

for i in range(1, n):
    w[i] = w[i - 1] + 1 if s[i - 1] == "W" else w[i - 1]
    e[-i - 1] = e[-i] + 1 if s[-i] == "E" else e[-i]

res = [0 for _ in range(n)]

for i in range(n):
    res = w[i] + e[i]

print(min(res))


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

e = [0 for _ in range(n)]
w = [0 for _ in range(n)]

for i in range(1, n):
    w[i] = w[i - 1] + 1 if s[i - 1] == "W" else w[i - 1]
    e[-i - 1] = e[-i] + 1 if s[-i] == "E" else e[-i]

res = [0 for _ in range(n)]

for i in range(n):
    res[i] = w[i] + e[i]

print(min(res))
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;
  int ans = N;
  for (int i = 0; i < N; i++) {
    int x = 0;
    for (int j = 0; j < N; j++) {
      if (i > j && S[j] == 'W')
        x++;
      if (i < j && S[j] == 'E')
        x++;
    }
    ans = min(ans, x);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
char s[101];
int f1[300001], f2[300001], g1[300001], g2[300001];
int main() {
  int n, t, ans = 0x3f3f3f3f;
  scanf("%d%s", &n, s);
  for (int i = 0; s[i]; i++)
    f1[i] = f1[i - 1] + (s[i] == 'W');
  for (int i = 0; s[i]; i++)
    g1[i] = g1[i - 1] + (s[i] == 'E');
  for (int i = n - 1; i >= 0; i--)
    f2[i] = f2[i + 1] + (s[i] == 'W');
  for (int i = n - 1; i >= 0; i--)
    g2[i] = g2[i + 1] + (s[i] == 'E');
  ans = min(f2[0], g2[0]);
  for (int i = 0; s[i]; i++)
    ans = min(ans, f1[i] + g2[i + 1]);
  printf("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
char s[300002];
int f1[300002], f2[300002], g1[300002], g2[300002];
int main() {
  int n, t, ans = 0x3f3f3f3f;
  scanf("%d%s", &n, s);
  for (int i = 0; s[i]; i++)
    f1[i] = f1[i - 1] + (s[i] == 'W');
  for (int i = 0; s[i]; i++)
    g1[i] = g1[i - 1] + (s[i] == 'E');
  for (int i = n - 1; i >= 0; i--)
    f2[i] = f2[i + 1] + (s[i] == 'W');
  for (int i = n - 1; i >= 0; i--)
    g2[i] = g2[i + 1] + (s[i] == 'E');
  ans = min(f2[0], g2[0]);
  for (int i = 0; s[i]; i++)
    ans = min(ans, f1[i] + g2[i + 1]);
  printf("%d", ans);
}
Problem p03339 (C++)

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

int count(char *s, int size, int center) {
  int ctr = 0;
  for (int i = 0; i < center; i++) {
    ctr += (s[i] == 'W');
  }
  for (int i = center + 1; i < size; i++) {
    ctr += (s[i] == 'E');
  }
  return ctr;
}

int main(void) {
  int size;
  scanf("%d", &size);
  char s[1000000];
  scanf("%s", s);

  int min = 100000000;
  int buf;
  for (int split = 0; split < size; split++) {
    buf = count(s, size, split);
    min = (buf < min ? buf : min);
  }

  printf("%d", min);
  return 0;
}


Fixed submission:
#include <stdio.h>

int count(char *s, int size, int center) {
  int ctr = 0;
  for (int i = 0; i < center; i++) {
    ctr += (s[i] == 'W');
  }
  for (int i = center + 1; i < size; i++) {
    ctr += (s[i] == 'E');
  }
  return ctr;
}

int main(void) {
  int size;
  scanf("%d", &size);
  char s[1000000];
  scanf("%s", s);

  int min = 100000000;
  int buf = count(s, size, 0);
  min = buf;
  for (int split = 1; split < size; split++) {
    if (s[split - 1] == 'W') {
      buf++;
    } else {
    }
    if (s[split] == 'E') {
      buf--;
    } else {
    }
    min = buf < min ? buf : min;
  }

  printf("%d", min);
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int t[300005];
int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 0; i <= n - 1; i++) {
    for (int j = 0; j <= n - 1; j++) {
      if (j < i && s[j] == 'W') {
        t[i]++;
      }
      if (j > i && s[j] == 'E') {
        t[i]++;
      }
    }
  }
  int min = t[0];
  for (int i = 0; i <= n - 1; i++) {
    if (t[i] < min) {
      min = t[i];
    }
  }
  cout << min;
}

Fixed submission:
#include <iostream>
using namespace std;
int t[300005];
int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 1; i <= n - 1; i++) {
    if (s[i] == 'E') {
      t[0]++;
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    t[i] = t[i - 1];
    if (s[i - 1] == 'W') {
      t[i]++;
    }
    if (s[i] == 'E') {
      t[i]--;
    }
  }
  int min = t[0];
  for (int i = 0; i <= n - 1; i++) {
    if (t[i] < min) {
      min = t[i];
    }
  }
  cout << min;
}
Problem p03339 (C++)

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

int main(void) {
  int N;
  string S;
  int cnt, min_cnt = 100010;
  int cnt_EW[3][100010];
  cin >> N >> S;
  // 配列に持っておく。
  for (int i = 0; i < N; i++) {
    cnt = 0;
    if (i == 0) {
      if (S[i] == 'E')
        cnt_EW[0][0] = 1, cnt_EW[1][0] = 0;
      else
        cnt_EW[0][0] = 0, cnt_EW[1][0] = 1;
    } else if (S[i] == 'E') {
      cnt_EW[0][i] = cnt_EW[0][i - 1] + 1;
      cnt_EW[1][i] = cnt_EW[1][i - 1];
    } else if (S[i] == 'W') {
      cnt_EW[0][i] = cnt_EW[0][i - 1];
      cnt_EW[1][i] = cnt_EW[1][i - 1] + 1;
    }
    // cout << cnt_EW[0][i] << " " << cnt_EW[1][i] << endl;
  }
  // 得られた配列を参照しながら，最小値を求める。
  for (int i = 0; i < N; i++) {
    if (i == 0)
      cnt = cnt_EW[0][N - 1];
    else
      cnt = cnt_EW[1][i - 1] + (cnt_EW[0][N - 1] - cnt_EW[0][i]);
    // cout << cnt << endl;
    if (min_cnt > cnt)
      min_cnt = cnt;
  }

  cout << min_cnt << endl;
  return 0;
}


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

int main(void) {
  int N;
  string S;
  int cnt, min_cnt = 300010;
  int cnt_EW[2][300010];
  cin >> N >> S;
  // 配列に持っておく。
  for (int i = 0; i < N; i++) {
    cnt = 0;
    if (i == 0) {
      if (S[i] == 'E')
        cnt_EW[0][0] = 1, cnt_EW[1][0] = 0;
      else
        cnt_EW[0][0] = 0, cnt_EW[1][0] = 1;
    } else if (S[i] == 'E') {
      cnt_EW[0][i] = cnt_EW[0][i - 1] + 1;
      cnt_EW[1][i] = cnt_EW[1][i - 1];
    } else if (S[i] == 'W') {
      cnt_EW[0][i] = cnt_EW[0][i - 1];
      cnt_EW[1][i] = cnt_EW[1][i - 1] + 1;
    }
    // cout << cnt_EW[0][i] << " " << cnt_EW[1][i] << endl;
  }
  // 得られた配列を参照しながら，最小値を求める。
  for (int i = 0; i < N; i++) {
    if (i == 0)
      cnt = cnt_EW[0][N - 1];
    else
      cnt = cnt_EW[1][i - 1] + (cnt_EW[0][N - 1] - cnt_EW[0][i]);
    // cout << cnt << endl;
    if (min_cnt > cnt)
      min_cnt = cnt;
  }

  cout << min_cnt << endl;
  return 0;
}