alexjercan/bugnet huggingface dataset:
Problem p01687 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string S, T = "AIZUNYAN", U = "AADINNUY";
int main() {
  cin >> S;
  int W = S.size() - 7;
  for (int i = 0; i < W; i++) {
    string V = S.substr(i, 8);
    sort(V.begin(), V.end());
    if (V == U) {
      for (int j = 0; j < 8; j++) {
        S[i + j] = T[j];
      }
      i += 7;
    }
  }
  cout << S << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string S, T = "AIZUNYAN", U = "AADINNUY";
int main() {
  cin >> S;
  for (int i = 0; i < S.size() - 7; i++) {
    string V = S.substr(i, 8);
    sort(V.begin(), V.end());
    if (V == U) {
      for (int j = 0; j < 8; j++) {
        S[i + j] = T[j];
      }
    }
  }
  cout << S << endl;
  return 0;
}
Problem p01687 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string s;
  cin >> s;
  string find = "AIDUNYAN";
  map<char, int> mp;
  for (char c : find)
    mp[c]++;
  rep(i, (int)s.size() - (int)find.size() + 1) {
    map<char, int> se;
    rep(j, find.size()) se[s[i + j]]++;
    if (se == mp) {
      s.replace(i, find.size(), "AIZUNYAN");
      i += find.size() - 1;
    }
  }
  cout << s << endl;
}

Error: -6

Fixed submission:

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

int main() {
  string s;
  cin >> s;
  string find = "AIDUNYAN";
  rep(i, (int)s.size() - (int)find.size() + 1) {
    set<char> se;
    rep(j, find.size()) se.insert(s[i + j]);
    bool ok = true;
    for (char c : find) {
      if (se.find(c) == se.end()) {
        ok = false;
        break;
      }
    }
    if (ok) {
      if (se.size() != find.size())
        abort();
      s.replace(i, find.size(), "AIZUNYAN");
      i += find.size();
    }
  }
  cout << s << endl;
}
Problem p01687 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s;

bool check(string s) {
  sort(s.begin(), s.end());
  if (s == "AADINNUY")
    return true;
  return false;
}

void change(int x) {
  string t = "AIZUNYAN";
  for (int i = 0; i < (int)t.size(); i++, x++)
    s[x] = t[i];
}

int main() {
  cin >> s;
  for (int i = 0; i < (int)s.size() - 7; i++)
    if (check(s.substr(i, 8)))
      change(i), i += 7;
  cout << s << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s;

bool check(string s) {
  sort(s.begin(), s.end());
  if (s == "AADINNUY")
    return true;
  return false;
}

void change(int x) {
  string t = "AIZUNYAN";
  for (int i = 0; i < t.size(); i++, x++)
    s[x] = t[i];
}

int main() {
  cin >> s;
  for (int i = 0; i < s.size() - 7; i++)
    if (check(s.substr(i, 8)))
      change(i), i += 7;
  cout << s << endl;
  return 0;
}
Problem p01687 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

string s;
string t = "AIZUNYAN", u = "AIDUNYAN";
vi w(26, 0);

bool calc(int n) {
  vi v(26, 0);
  for (int i = 0; i < 8 && i + n < s.size(); i++)
    v[s[i + n] - 'A']++;
  for (int i = 0; i < 26; i++) {
    if (v[i] != w[i])
      return false;
    ;
  }
  for (int i = 0; i < 8; i++) {
    s[i + n] = t[i];
  }
  return true;
}

int main() {
  for (int i = 0; i < u.size(); i++)
    w[u[i] - 'A']++;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    bool f = calc(i);
    if (f)
      i += 7;
  }
  cout << s << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

string s;
string t = "AIZUNYAN", u = "AIDUNYAN";
vi w(26, 0);

bool calc(int n) {
  vi v(26, 0);
  for (int i = 0; i < 8 && i < s.size(); i++)
    v[s[i + n] - 'A']++;
  for (int i = 0; i < 26; i++) {
    if (v[i] != w[i])
      return false;
    ;
  }
  for (int i = 0; i < 8; i++) {
    s[i + n] = t[i];
  }
  return true;
}

int main() {
  for (int i = 0; i < u.size(); i++)
    w[u[i] - 'A']++;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    bool f = calc(i);
    if (f)
      i += 7;
  }
  cout << s << endl;
}
Problem p01688 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

int main() {

  int D;
  int x;
  string a;
  int p;
  int y;
  string bc;
  int q;
  int dp[14] = {0};
  int max;

  cin >> D;
  for (int i = 1; i <= D; i++)
    dp[i] = 0;

  cin >> x;

  for (int i = 0; i < x; i++) {
    cin >> a >> p;
    if (a == "D") {
      for (int j = D - 1; j >= 0; j--) {
        if ((dp[j] != 0 || j == 0) && dp[j] + p > dp[j + 1]) {
          dp[j + 1] = dp[j] + p;
        }
      }
    }
  }

  cin >> y;

  for (int i = 0; i < y; i++) {
    cin >> bc >> q;
    if (bc == "DD") {
      for (int j = D - 2; j >= 0; j--) {
        if ((dp[j] != 0 || j == 0) && dp[j] + q > dp[j + 2]) {
          dp[j + 2] = dp[j] + q;
        }
      }
    }
  }

  max = dp[0];
  for (int i = 1; i <= D; i++) {
    if (max < dp[i])
      max = dp[i];
  }

  cout << max << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int D, x, y;
vector<bool> xu;
vector<bool> yu;
vector<int> xq;
vector<int> yq;

int DFS(int Duse) {
  int ans = 0;
  for (int i = 0; i < xq.size(); i++) {
    if (Duse + 1 <= D && xu[i] == false) {
      xu[i] = true;
      ans = max(ans, DFS(Duse + 1) + xq[i]);
      xu[i] = false;
    }
  }
  for (int i = 0; i < yq.size(); i++) {
    if (Duse + 2 <= D && yu[i] == false) {
      yu[i] = true;
      ans = max(ans, DFS(Duse + 2) + yq[i]);
      yu[i] = false;
    }
  }
  return ans;
}

int main(void) {

  cin >> D;
  cin >> x;

  string a;
  int p;

  for (int i = 0; i < x; i++) {
    cin >> a >> p;
    if (a == "D") {
      xq.push_back(p);
      xu.push_back(false);
    }
  }

  cin >> y;

  string bc;
  int q;

  for (int i = 0; i < y; i++) {
    cin >> bc >> q;
    if (bc == "DD") {
      yq.push_back(q);
      yu.push_back(false);
    }
  }

  cout << DFS(0) << endl;
}
Problem p01688 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(T) (T).begin(), (T).end()

using namespace std;

typedef vector<int> vi;

int main(void) {
  int d;
  cin >> d;
  vi dp(d + 1);
  fill(ALL(dp), -1);
  dp[0] = 0;

  int x;
  cin >> x;
  rep(i, x) {
    string ans;
    int s;
    cin >> ans >> s;

    if (ans != "D")
      continue;

    for (int j = d - 1; j >= 0; j--) {
      if (!~dp[j])
        continue;
      dp[j + 1] = max(dp[j + 1], dp[j] + s);
    }
  }

  int y;
  cin >> y;
  rep(i, y) {
    string ans;
    int s;
    cin >> ans >> s;

    if (ans != "DD")
      continue;

    for (int j = d - 2; j >= 0; j--) {
      if (!~dp[j])
        continue;
      dp[j + 2] = max(dp[j + 2], dp[j] + s);
    }
  }

  cout << *max_element(ALL(dp)) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(T) (T).begin(), (T).end()

using namespace std;

typedef vector<int> vi;

int main(void) {
  int d;
  cin >> d;
  vi dp(d + 1);
  fill(ALL(dp), -1);
  dp[0] = 0;

  int x;
  cin >> x;
  rep(i, x) {
    string ans;
    int s;
    cin >> ans >> s;

    if (ans != "D")
      continue;

    for (int j = d - 1; j >= 0; j--) {
      if (!~dp[j] && j + 1 > d)
        continue;
      dp[j + 1] = max(dp[j + 1], dp[j] + s);
    }
  }

  int y;
  cin >> y;
  rep(i, y) {
    string ans;
    int s;
    cin >> ans >> s;

    if (ans != "DD")
      continue;

    for (int j = d - 1; j >= 0; j--) {
      if (!~dp[j] && j + 2 > d)
        continue;
      dp[j + 2] = dp[j] + s;
    }
  }

  cout << *max_element(ALL(dp)) << endl;

  return 0;
}
Problem p01688 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int memo[16][16];

int Solve(int n, int d, int maxd, const vector<P> &score) {
  if (d >= maxd) {
    return 0;
  }
  if (n == score.size()) {
    return 0;
  }
  if (memo[n][d] != 0) {
    return memo[n][d];
  }

  int r1 = 0, r2 = 0;
  if (maxd - d >= 2 && score[n].second == 2) {
    r1 = score[n].first + Solve(n + 1, d + 2, maxd, score);
  }
  if (score[n].second == 1) {
    r2 = score[n].first + Solve(n + 1, d + 1, maxd, score);
  }
  int r = max(r1, r2);

  return memo[n][d] = max(r, Solve(n + 1, d, maxd, score));
}

int main() {
  int d, x;
  cin >> d >> x;

  vector<P> score;
  for (int i = 0; i < x; i++) {
    char a;
    int p;
    cin >> a >> p;
    if (a == 'D') {
      score.push_back(P(p, 1));
    }
  }
  int y;
  cin >> y;
  for (int i = 0; i < y; i++) {
    string bc;
    int q;
    cin >> bc >> q;
    if (bc == "DD") {
      score.push_back(P(q, 2));
    }
  }

  cout << Solve(0, 0, d, score) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#include <Windows.h>
double get_ms() { return (double)GetTickCount64() / 1000; }
#else
#include <sys/time.h>
double get_ms() {
  struct timeval t;
  gettimeofday(&t, NULL);
  return (double)t.tv_sec * 1000 + (double)t.tv_usec / 1000;
}
#endif

typedef pair<int, int> P;

int main() {
  double start_time = get_ms();
  int d, x;
  cin >> d >> x;

  vector<P> score;
  for (int i = 0; i < x; i++) {
    char a;
    int p;
    cin >> a >> p;
    if (a == 'D') {
      score.push_back(P(p, 1));
    }
  }
  int y;
  cin >> y;
  for (int i = 0; i < y; i++) {
    string bc;
    int q;
    cin >> bc >> q;
    if (bc == "DD") {
      score.push_back(P(q, 2));
    }
  }

  if (score.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  for (int loop = 0; loop < 1e3; loop++) {
    bool used[100] = {false};
    int used_count = 0;
    int tans = 0;
    while (used_count < d) {
      int num;
      while (true) {
        num = rand() % score.size();
        if (!used[num] && d - used_count >= score[num].second) {
          break;
        }
      }
      tans += score[num].first;
      used[num] = true;
      used_count += score[num].second;
    }
    ans = max(ans, tans);
  }

  cout << ans << endl;
  return 0;
}
Problem p01688 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define between(a, x, b) ((a) <= (x) && (x) <= (b))
using namespace std;

int main() {
  int d, x, y, p;
  char a[16];
  vector<int> D, DD;
  scanf("%d", &d);
  scanf("%d", &x);
  rep(i, x) {
    scanf("%s%d", a, &p);
    if (a[0] == 'D')
      D.push_back(p);
  }
  scanf("%d", &y);
  rep(i, y) {
    scanf("%s%d", a, &p);
    if (a[0] == 'D' && a[1] == 'D')
      DD.push_back(p);
  }
  sort(D.begin(), D.end(), greater<int>());
  sort(DD.begin(), DD.end(), greater<int>());
  int res = 0;
  //  fprintf(stderr, "input\\n");
  while (d) {
    int f = true;
    //    fprintf(stderr, "%d\\n", d);
    if (DD.empty() && D.empty())
      break;
    if (d >= 2 && !DD.empty()) {
      if ((D.size() > 1 && D[0] + D[1] < DD[0]) ||
          (D.size() == 1 && D[0] < DD[0]) || D.empty()) {
        res += DD[0];
        DD.erase(DD.begin());
        d -= 2;
        f = false;
      }
    }
    if (f && !D.empty()) {
      res += D[0];
      D.erase(D.begin());
      d--;
      f = false;
    }
    if (f)
      break;
    //    if(DD.empty() && D.empty()) break;
  }
  printf("%d\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define between(a, x, b) ((a) <= (x) && (x) <= (b))
using namespace std;

int main() {
  int d, x, y, p;
  char a[16];
  vector<int> D, DD;
  scanf("%d", &d);
  scanf("%d", &x);
  rep(i, x) {
    scanf("%s%d", a, &p);
    if (a[0] == 'D')
      D.push_back(p);
  }
  scanf("%d", &y);
  rep(i, y) {
    scanf("%s%d", a, &p);
    if (a[0] == 'D' && a[1] == 'D')
      DD.push_back(p);
  }
  sort(D.begin(), D.end(), greater<int>());
  sort(DD.begin(), DD.end(), greater<int>());
  int res = 0;
  //  fprintf(stderr, "input\\n");
  while (d) {
    int f = true;
    //    fprintf(stderr, "%d\\n", d);
    if (DD.empty() && D.empty())
      break;
    if (d >= 2 && !DD.empty()) {
      if ((D.size() > 1 && D[0] + D[1] < DD[0]) ||
          (D.size() == 1 && D[0] < DD[0]) || D.empty()) {
        res += DD[0];
        DD.erase(DD.begin());
        d -= 2;
        f = false;
      }
    }
    if (f && !D.empty()) {
      res += D[0];
      D.erase(D.begin());
      d--;
    }
    //    if(DD.empty() && D.empty()) break;
  }
  printf("%d\\n", res);
  return 0;
}
Problem p01690 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dp1[110][110];
int dp2[110][110][110];
int dp3[110][110 * 110];
int e[100], c[100], h[100], a[100];
int N;
int S;
int U;
int T;
int O;
int D;

int dfs(int t, int hp, int u) {
  if (u < 0)
    return -1e9;
  if (hp < 0)
    return -1e9;
  if (t == -1)
    return u == 0 ? 0 : -1e9;
  if (~dp2[t][hp][u])
    return dp2[t][hp][u];
  int ans = -1e9;
  ans = max(ans, dfs(t - 1, hp, u));
  ans = max(ans, dfs(t - 1, hp - e[t], u - 1) + c[t]);
  return dp2[t][hp][u] = ans;
}
int dfs3(int n, int hp) {
  if (hp < 0)
    return -1e9;
  if (n < 0)
    return 0;
  if (~dp3[n][hp])
    return dp3[n][hp];
  int ans = -1e9;
  ans = max(ans, dfs3(n, hp - a[n]) + h[n]);
  ans = max(ans, dfs3(n - 1, hp));
  return dp3[n][hp] = ans;
}
int dfs2(int d, int hp) {
  if (hp < 0)
    return -1e9;
  if (d < 0)
    return 0;
  hp = min(S, hp + O);
  if (~dp1[d][hp])
    return dp1[d][hp];

  int ans = -1e9;
  for (int i = 0; i <= hp; i++)
    ans = max(ans, dfs2(d - 1, hp - i) + dfs3(N - 1, dfs(T - 1, i, U)));
  return dp1[d][hp] = ans;
}

int main() {
  memset(dp1, -1, sizeof(dp1));
  memset(dp2, -1, sizeof(dp2));
  memset(dp3, -1, sizeof(dp3));
  cin >> S >> T >> U >> N >> O >> D;
  for (int i = 0; i < T; i++) {
    cin >> e[i] >> c[i];
  }
  for (int i = 0; i < N; i++)
    cin >> h[i] >> a[i];
  cout << max(-1, dfs2(D - 1, S)) << endl;
};

Error: TLE

Fixed submission:

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

int dp1[110][110];
int dp2[110][110][110];
int dp3[110][110];
int e[100], c[100], h[100], a[100];
int N;
int S;
int U;
int T;
int O;
int D;

int dfs(int t, int hp, int u) {
  if (hp < 0)
    return -1e9;
  if (t == -1)
    return u == 0 ? 0 : -1e9;
  if (~dp2[t][hp][u])
    return dp2[t][hp][u];

  int ans = -1e9;
  ans = max(ans, dfs(t - 1, hp, u));
  ans = max(ans, dfs(t - 1, hp - e[t], u - 1) + c[t]);
  return dp2[t][hp][u] = ans;
}
int dfs3(int n, int hp) {
  if (hp < 0)
    return -1e9;
  if (n < 0)
    return 0;
  if (~dp3[n][hp])
    return dp3[n][hp];
  int ans = -1e9;
  ans = max(ans, dfs3(n, hp - a[n]) + h[n]);
  ans = max(ans, dfs3(n - 1, hp));
  return dp3[n][hp] = ans;
}
int dfs2(int d, int hp) {
  if (hp < 0)
    return -1e9;
  if (d < 0)
    return 0;
  if (~dp1[d][hp])
    return dp1[d][hp];
  hp = min(S, hp + O);
  int ans = -1e9;
  for (int i = 0; i <= hp; i++)
    ans = max(ans, dfs2(d - 1, hp - i) + dfs3(N - 1, dfs(T - 1, i, U)));
  return dp1[d][hp] = ans;
}

int main() {
  memset(dp1, -1, sizeof(dp1));
  memset(dp2, -1, sizeof(dp2));
  memset(dp3, -1, sizeof(dp3));
  cin >> S >> T >> U >> N >> O >> D;
  for (int i = 0; i < T; i++)
    cin >> e[i] >> c[i];
  for (int i = 0; i < N; i++)
    cin >> h[i] >> a[i];
  // cout << dfs(T-1,100,1) << endl;
  // cout << dfs3(N-1,100) << endl;
  cout << max(-1, dfs2(D - 1, S)) << endl;
};
Problem p01693 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
int n, arr[101];
int cal(int a, int b) { return (arr[a] + arr[b]) * (abs(a - b)); }
void Min(int &a, int b) { a = min(a, b); }

int mem[3][101][101][101];
int dfs(int f, int num, int l, int r) {
  if (l > r)
    return 0;
  if (l == r)
    return (arr[l] == l + 1) ? INF : 0;
  if (r - l == 1)
    return (arr[l] == l + 1 || arr[r] == r + 1) ? cal(l, r) : 0;
  if (mem[f][num][l][r] != -1)
    return mem[f][num][l][r];

  int res = INF;
  for (int i = l + 1; i < r; i++) {
    if (f == 0 || f == 1)
      if (arr[i] != l + 1) {
        swap(arr[l], arr[i]);
        Min(res, cal(l, i) + dfs(0, 0, l + 1, i - 1) + dfs(1, arr[i], i, r));
        swap(arr[l], arr[i]);
      }

    if (f == 0 || f == 2)
      if (arr[i] != r + 1) {
        swap(arr[i], arr[r]);
        Min(res, cal(i, r) + dfs(2, arr[i], l, i) + dfs(0, 0, i + 1, r - 1));
        swap(arr[i], arr[r]);
      }
  }

  for (int i = l; i < r; i++) {
    if (f == 1)
      Min(res, dfs(1, num, l, i) + dfs(0, 0, i + 1, r));
    else if (f == 2)
      Min(res, dfs(0, 0, l, i) + dfs(2, num, i + 1, r));
    else
      Min(res, dfs(0, 0, l, i) + dfs(0, 0, i + 1, r));
  }

  return mem[f][num][l][r] = res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  memset(mem, -1, sizeof(mem));
  cout << dfs(0, 0, 0, n - 1) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, arr[101];

int dfs(int idx) {
  if (idx == n)
    return 0;
  if (arr[idx] != idx + 1)
    return dfs(idx + 1);

  int res = 1e9;
  for (int i = 0; i < n; i++) {
    if (i <= idx && arr[idx] == i + 1)
      continue;
    int cost = (arr[idx] + arr[i]) * (abs(i - idx));
    swap(arr[idx], arr[i]);
    res = min(res, cost + dfs(idx + 1));
    swap(arr[idx], arr[i]);
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << dfs(0) << endl;
}
Problem p01694 (C++) — buggy submission, verdict: Runtime Error


#include <iostream>
using namespace std;
int main() {
  int a = 0, b = 0, n[150], nos, sum[150] = {0};
  char str[150][100][3];
  while (1) {
    cin >> n[a];
    if (n[a] == 0)
      break;
    for (b = 0; b < n[a]; b++) {
      cin >> str[a][b];
    }
    a++;
  }
  nos = a;
  for (a = 0; a < nos; a++) {
    for (b = 0; b + 1 < n[a]; b += 2) {
      if (str[a][b][0] - str[a][b + 1][0])
        sum[a]++;
    }
    cout << sum[a] << endl;
  }
  return 0;
}

Error: 1

Fixed submission:


#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int a = 0, b = 0, n[150], state, t = 0, sum[150] = {0};
  char str[150][100][3];
  while (1) {
    cin >> n[a];
    if (n[a] == 0)
      break;
    for (b = 0; b < n[a]; b++) {
      cin >> str[a][b];
    }
    a++;
  }
  for (t = 0; t < a; t++) {
    for (b = 0; b < n[t] / 2; b++) {
      if (str[t][2 * b][0] - str[t][2 * b + 1][0] != 0)
        sum[t]++;
    }
    cout << sum[t] << endl;
  }
  return 1;
}
Problem p01694 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  for (int n; cin >> n, n;) {
    int cur = 0, prev = 0, cnt = 0;
    while (n--) {
      string f;
      cin >> f;
      if (f == "lu")
        cur |= 2;
      if (f == "ru")
        cur |= 1;
      if (f == "ld")
        cur &= 1;
      if (f == "rd")
        cur &= 2;
      if ((cur ^ prev) == 3)
        cnt++, prev = cur;
    }
    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int cur = 0, prev = 0, ans = 0;
    while (n--) {
      string f;
      cin >> f;
      cur =
          f[1] == 'u' ? (cur | 1 << (f[0] == 'l')) : (cur & 0 << (f[0] == 'l'));
      if ((cur ^ prev) == 3)
        ans++, prev = cur;
    }
    cout << ans << endl;
  }
}
Problem p01694 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main() {
  int i, place, cou, a;
  char o[3], t[3], sute[3];
  while (1) {
    scanf("%d", &a);
    if (a == 0)
      break;
    cou = 0;
    place = 0;
    for (i = 0; i < a / 2; i++) {
      scanf("%s %s", o, t);
      // printf("%c %c\\n",o[1],t[1]);///
      if (place == 0 && o[1] == 'u' && t[1] == 'u') {
        cou++;
        place = 1;
      } else if (place == 1 && o[1] == 'd' && t[1] == 'd') {
        cou++;
        place = 0;
      }
    }
    if (a % 2 == 1)
      scanf("%s", sute);
    printf("%d\\n", cou);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  char o[3], n[3], sute;
  int k, cou, i, stay;
  while (1) {
    scanf("%d", &k);
    // printf("%d\\n",k);///
    if (k == 0)
      break;
    stay = 0;
    cou = 0;
    for (i = 0; i < k / 2; i++) {
      scanf("%s %s", o, n);
      // printf("まず%sそして%s\\n",o,n);///
      if (o[1] == 'u' && n[1] == 'u' && stay == 0) {
        cou++;
        stay = 1;
      } else if (o[1] == 'd' && n[1] == 'd' && stay == 1) {
        cou++;
        stay = 0;
      }
    }
    if (k % 2 == 1)
      scanf("%s", sute);
    printf("%d\\n", cou);
  }
  return 0;
}