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

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

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int w, h, c;
  cin >> w >> h >> c;
  if (w < h)
    swap(w, h);
  cout << w * h / gcd(w, h) / gcd(w, h) * c << endl;

  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int ans;
  for (int i; i < min(a, b); i++) {
    if (a % i == 0 && b % i == 0) {
      ans = a * b / i / i;
    }
  }
  cout << c * ans << endl;

  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

// 2017/07/18 Tazoe

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int main() {
  map<int, bool> memo;

  int N;
  cin >> N;

  int cnt = 0;

  for (int i = 0; i < N; i++) {
    int p[3];
    cin >> p[0] >> p[1] >> p[2];

    sort(p, p + 3);

    //		cout << p[0] << ' ' << p[1] << ' ' << p[2] << endl;

    int k = ((p[0] - 1) * 1000 + (p[1] - 1)) * 1000 + (p[2] - 1);

    if (memo.find(k) != memo.end())
      cnt++;
    else
      memo[k] = true;
  }

  cout << cnt << endl;

  return 0;
}

Error: -11

Fixed submission:

// 2017/07/18 Tazoe

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

int main() {
  bool memo[1001][1001][1001];
  for (int i = 1; i <= 1000; i++)
    for (int j = 1; j <= 1000; j++)
      for (int k = 1; k <= 1000; k++)
        memo[i][j][k] = false;

  int N;
  cin >> N;

  int cnt = 0;

  for (int i = 0; i < N; i++) {
    int p[3];
    cin >> p[0] >> p[1] >> p[2];

    sort(p, p + 3);

    //		cout << p[0] << ' ' << p[1] << ' ' << p[2] << endl;

    if (memo[p[0]][p[1]][p[2]])
      cnt++;
    else
      memo[p[0]][p[1]][p[2]] = true;
  }

  cout << cnt << endl;

  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long i64;
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n;
  cin >> n;
  std::set<std::vector<int>> st;
  for (int i = 0; i < n; i++) {
    std::vector<int> vec(3);
    for (int j = 0; j < 3; j++)
      cin >> vec[j];
    sort(begin(vec), end(vec));

    st.insert(vec);
  }

  cout << n - st.size() << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;

const int MAX_N = 10000;
int n, p[3], list[235];

int main() {
  cin >> n;

  fill(list, list + sizeof(list), -1);
  int c = 0;
  for (int i = 0; i < n; i++) {
    cin >> p[0] >> p[1] >> p[2];
    sort(p, p + 3);
    int k = p[0] * 100 + p[1] * 10 + p[2];
    list[k]++;
  }
  for (int i = 0; i < 235; i++)
    if (list[i] != -1)
      c += list[i];

  cout << c << endl;
  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

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

int kumi[1000][1000][1000] = {0};

int main() {
  int N;
  int count = 0;

  int p[3003];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[(3 * i)] >> p[(3 * i) + 1] >> p[(3 * i) + 2];
    sort(p + (3 * i), p + (3 * i) + 3);
    if (kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] == 0) {
      kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] = 1;
    } else {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int kumi[100][100][100] = {0};

int main() {
  int N;
  int count = 0;

  int p[3003];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[(3 * i)] >> p[(3 * i) + 1] >> p[(3 * i) + 2];
    sort(p + (3 * i), p + (3 * i) + 3);
    if (kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] == 0) {
      kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] = 1;
    } else {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int N, sum = 0;
  cin >> N;
  int data[N][3], data2[N][3] = {0}, so[3] = {0};
  for (int A = 0; A < N; A++) {
    cin >> so[0] >> so[1] >> so[2];
    sort(so, so + 3);
    data[A][0] = so[0];
    data[A][1] = so[1];
    data[A][2] = so[2];
    data2[A][0] = so[0];
    data2[A][1] = so[1];
    data2[A][2] = so[2];
  }
  for (int A = 0; A < N; A++) {
    data2[A][0] = 0;
    data2[A][1] = 0;
    data2[A][2] = 0;
    for (int B = 0; B < N; B++) {
      if (data[A][0] == data2[B][0] && data[A][1] == data2[B][1] &&
          data[A][2] == data2[B][2] && A != B) {
        sum++;
        data2[B][0] = 0;
        data2[B][1] = 0;
        data2[B][2] = 0;
      }
    }
  }
  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int a;
  cin >> a;
  static long long int YA[100][100][100] = {0}, dataa[3] = {0}, sum = 0;
  for (int A = 0; A < a; A++) {
    cin >> dataa[0] >> dataa[1] >> dataa[2];
    sort(dataa, dataa + 3);
    if (YA[dataa[0]][dataa[1]][dataa[2]] == 1)
      sum++;
    YA[dataa[0]][dataa[1]][dataa[2]] = 1;
  }
  cout << sum << endl;
  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> p(n, vector<int>(3, 0));
  vector<unsigned long> x(n, 0);

  for (int i = 0; i < n; ++i) {
    cin >> p[i][0] >> p[i][1] >> p[i][2];
    sort(p[i].begin(), p[i].end());
    x[i] = 100000000 * p[i][0] + 10000 * p[i][1] + p[i][2];
  }
  sort(x.begin(), x.end());

  int cnt = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (x[i] == x[i + 1])
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> p(n, vector<int>(3, 0));
  vector<int> x(n);

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j)
      cin >> p[i][j];
    sort(p[i].begin(), p[i].end());
    x[i] = 100 * p[i][0] + 10 * p[i][1] + p[i][2];
  }

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

  int num = 0;
  int j = 0;
  for (int i = 0; i < n; ++i) {
    for (;; ++j) {
      if (x[j] == x[j + 1])
        num++;
      else {
        j++;
        break;
      }
    }
  }
  cout << num << endl;

  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int main() {
  int n;
  cin >> n;
  map<int, int> s;
  int d[3];
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> d[j];
    }
    sort(d, d + 3);
    s[d[0] * 1000000 + d[1] * 10000 + d[2]] = 1;
  }
  cout << n - s.size() << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int main() {
  int n;
  cin >> n;
  int s[100000];
  for (int i = 0; i < 100000; i++) {
    s[i] = 0;
  }
  int d[3];
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> d[j];
    }
    sort(d, d + 3);
    if (s[100 * d[0] + 10 * d[1] + d[2]] != 0) {
      count++;
    } else {
      s[100 * d[0] + 10 * d[1] + d[2]] = 1;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int te[1000 + 1][3];
  bool ka[1000 + 1] = {0};
  int n;
  int cnt;
  int i, j;
  int tmp;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> te[i][0] >> te[i][1] >> te[i][2];
    if (te[i][0] > te[i][1]) {
      tmp = te[i][0];
      te[i][0] = te[i][1];
      te[i][1] = tmp;
    }
    if (te[i][1] > te[i][2]) {
      tmp = te[i][1];
      te[i][1] = te[i][2];
      te[i][2] = tmp;
      if (te[i][0] > te[i][1]) {
        tmp = te[i][0];
        te[i][0] = te[i][1];
        te[i][1] = tmp;
      }
    }
  }

  cnt = 0;
  for (i = 0; i < n; i++) {
    if (!ka[i]) {
      for (j = i + 1; j < n; j++) {
        if (te[i][0] == te[j][0] && te[i][1] == te[j][1] &&
            te[i][2] == te[j][2]) {
          cnt++;
          ka[j] = true;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int te[100][3];
  bool ka[100] = {0};
  int n;
  int cnt;
  int i, j;
  int tmp;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> te[i][0] >> te[i][1] >> te[i][2];
    if (te[i][0] > te[i][1]) {
      tmp = te[i][0];
      te[i][0] = te[i][1];
      te[i][1] = tmp;
    }
    if (te[i][1] > te[i][2]) {
      tmp = te[i][1];
      te[i][1] = te[i][2];
      te[i][2] = tmp;
      if (te[i][0] > te[i][1]) {
        tmp = te[i][0];
        te[i][0] = te[i][1];
        te[i][1] = tmp;
      }
    }
  }

  cnt = 0;
  for (i = 0; i < n; i++) {
    if (!ka[i]) {
      for (j = i + 1; j < n; j++) {
        if (te[i][0] == te[j][0] && te[i][1] == te[j][1] &&
            te[i][2] == te[j][2]) {
          cnt++;
          ka[j] = true;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p00334 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class triangle {
public:
  int position[3];
};
bool compare(triangle &a, triangle &b) {
  for (int i = 0; i < 3; ++i) {
    if (a.position[i] < b.position[i]) {
      return true;
    } else if (a.position[i] > b.position[i]) {
      return false;
    }
  }
  return false;
}
void input(vector<triangle> &line) {
  int num;
  cin >> num;
  line.resize(num);
  for (int i = 0; i < num; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> line[i].position[j];
    }
    sort(line[i].position, line[i].position + 3);
  }
  sort(line.begin(), line.end(), compare);
}
int func(vector<triangle> &line) {
  int res = 0;
  for (int i = 1; i < line.size(); ++i) {
    bool flag = true;
    for (int j = 0; j < 3; ++j) {
      if (line[i - 1].position[j] != line[i].position[j]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      ++res;
    }
  }
  return res;
}
int main() {
  vector<triangle> line;
  input(line);
  cout << func(line) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class triangle {
public:
  int position[3];
};
bool compare(triangle &a, triangle &b) {
  for (int i = 0; i < 3; ++i) {
    if (a.position[i] < b.position[i]) {
      return true;
    } else if (a.position[i] > b.position[i]) {
      return false;
    }
  }
  return true;
}
void input(vector<triangle> &line) {
  int num;
  cin >> num;
  line.resize(num);
  for (int i = 0; i < num; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> line[i].position[j];
    }
    sort(line[i].position, line[i].position + 3);
  }
  sort(line.begin(), line.end(), compare);
}
int func(vector<triangle> &line) {
  int res = 0;
  for (int i = 1; i < line.size(); ++i) {
    bool flag = true;
    for (int j = 0; j < 3; ++j) {
      if (line[i - 1].position[j] != line[i].position[j]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      ++res;
    }
  }
  return res;
}
int main() {
  vector<triangle> line;
  input(line);
  cout << func(line) << endl;
  return 0;
}
Problem p00335 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define Rep(i, N) for (int i = 0; i < N; i++)

int main() {

  int N;

  vector<int> data(5005, 0);

  vector<int> temp(5005, 0);

  cin >> N;

  Rep(i, N) cin >> data[i];

  temp = data;

  int sum = INT_MAX;

  Rep(p, data[0] + 1) {

    int ans = 0;

    data[0] -= p;

    ans += p;

    Rep(i, N - 1) {

      ans += data[i] * 2;

      data[i + 1] -= data[i];

      data[i] = 0;

      if (data[i + 1] < 0)
        data[i + 1] = 0;
    }

    ans += data[N - 1];

    sum = min(ans, sum);

    data = temp;
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define Rep(i, N) for (int i = 0; i < N; i++)

int main() {

  int N;

  vector<int> data(5005, 0);

  vector<int> temp(5005, 0);

  while (cin >> N, N) {

    Rep(i, N) cin >> data[i];

    temp = data;

    int sum = INT_MAX;

    Rep(p, data[0] + 1) {

      int ans = 0;

      data[0] -= p;

      ans += p;

      Rep(i, N - 1) {

        ans += data[i] * 2;

        data[i + 1] -= data[i];

        data[i] = 0;

        if (data[i + 1] < 0)
          data[i + 1] = 0;
      }

      ans += data[N - 1];

      sum = min(ans, sum);

      data = temp;
    }

    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \\
  if (flg) {                                                                   \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }
#define Vi vector<int>
#define Vl vector<long long>
#define pb(a) push_back(a)
using namespace std;

typedef long long ll;
const int inf = 1000000000;
struct edge {
  int myc, to, c, cost;
};
struct pos {
  int to, cost, color;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  int n;
  cin >> n;
  int p;
  int a[4] = {};
  int b[4] = {};
  rep(i, n) {
    rep(j, 4) a[j] = b[j];
    rep(j, 4) b[j] = inf;
    cin >> p;
    if (p == 0) {
      rep(j, 4) b[0] = min(b[0], a[j] + j);
    }
    if (p == 1) {
      for (int j = 1; j < 4; ++j)
        b[0] = min(b[0], a[j] + j);
      b[1] = a[0] + 1;
    }
    if (p == 2) {
      for (int j = 2; j < 4; ++j)
        b[0] = min(b[0], a[j] + j);
      b[1] = a[1] + 2;
      b[2] = a[0] + 2;
    }
    if (p == 3) {
      b[0] = a[3] + 3;
      b[1] = a[2] + 3;
      b[2] = a[1] + 3;
      b[3] = a[0] + 3;
    }
  }
  int total = inf;
  rep(i, 4) total = min(total, b[i]);
  cout << total << endl;

  return 0;
}


Error: TLE

Fixed submission:

//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;
int w, h, c;
int d[50001] = {};

int main() {
  int n;
  int total1, total2;
  int r1, r2, d1, d2;
  while (1) {
    cin >> n;
    if (n == 0)
      return 0;
    cin >> d1;
    total1 = d1;
    total2 = d1;
    r1 = d1;
    r2 = 0;

    for (int i = 2; i < n + 1; ++i) {
      cin >> d1;
      if (d1 >= r1) {
        total1 += d1;
        r1 = d1 - r1;
      } else {
        total1 += r1;
        r1 = 0;
      }
      if (d1 >= r2) {
        total2 += d1;
        r2 = d1 - r2;
      } else {
        total2 += r2;
        r2 = 0;
      }
    }
    cout << min(total1, total2) << endl;
  }
  return 0;
}
Problem p00335 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int mincount = 100000;
  int p[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int k = 0; k <= p[1]; k++) {
    int count = 0;
    int c[n + 1];
    for (int i = 1; i <= n; i++) {
      c[i] = p[i];
    }
    c[1] -= k;
    count += k;
    for (int i = 1; i <= n - 1; i++) {
      if (c[i] <= 0) {
        continue;
      }
      count += c[i] * 2;
      c[i + 1] -= c[i];
    }
    if (c[n] > 0) {
      count += c[n];
    }
    mincount = min(count, mincount);
  }

  cout << mincount << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n != 0) {
    int count = 0;
    int p[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> p[i];
    }
    if (p[1] <= p[2]) {
      count += p[1] * 2;
      p[2] -= p[1];
      p[1] = 0;
    } else {
      count += p[1] + p[2];
      p[2] = 0;
      p[1] = 0;
    }
    if (p[n] <= p[n - 1]) {
      count += p[n] * 2;
      p[n - 1] -= p[n];
      p[n] = 0;
    } else {
      count += p[n] + p[n - 1];
      p[n - 1] = 0;
      p[n] = 0;
    }
    for (int i = 2; i < n; i++) {
      if ((p[i - 1] + p[i + 1] <= p[i]) && (p[i] != 0)) {
        count += p[i] * 2;
        p[i - 1] = 0;
        p[i] = 0;
        p[i + 1] = 0;
      }
    }
    cout << count << endl;
    cin >> n;
  }
  return 0;
}
Problem p00335 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int n;
int p[5000];
int z[5000];
signed main() {
  while (cin >> n) {
    rep(i, n) cin >> p[i];

    int ans = 1001001001ll;
    for (int t = 0; t <= p[0]; t++) {
      int cost = t;
      memset(z, 0, sizeof(z));
      z[0] = t;
      for (int i = 1; i < n; i++) {
        cost += (p[i - 1] - z[i - 1]) * 2;
        z[i] += p[i - 1] - z[i - 1];
        chmin(z[i], p[i]);
      }
      cost += p[n - 1] - z[n - 1];
      chmin(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int n;
int p[5000];
int z[5000];
signed main() {
  while (cin >> n, n) {
    rep(i, n) cin >> p[i];

    int ans = 1001001001ll;
    for (int t = 0; t <= p[0]; t++) {
      int cost = t;
      memset(z, 0, sizeof(z));
      z[0] = t;
      for (int i = 1; i < n; i++) {
        cost += (p[i - 1] - z[i - 1]) * 2;
        z[i] += p[i - 1] - z[i - 1];
        chmin(z[i], p[i]);
      }
      cost += p[n - 1] - z[n - 1];
      chmin(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00335 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  int min_sum = 20000;

  for (int I = 0; I <= p[0]; I++) {
    int q[5000];
    for (int j = 0; j < N; j++) {
      q[j] = p[j];
    }

    int sum = I;
    q[0] -= I;

    for (int i = 0; i <= N - 2; i++) {
      if (q[i] <= 0)
        continue;

      sum += q[i] * 2;
      q[i + 1] -= q[i];
    }

    if (q[N - 1] > 0) {
      sum += q[N - 1];
    }

    min_sum = min(min_sum, sum);
  }

  cout << min_sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  int min_sum = 20000;

  for (int i = 0; i <= p[0]; i++) {
    int q[5000];
    for (int j = 0; j < N; j++) {
      q[j] = p[j];
    }

    int sum = i;
    q[0] -= i;

    for (int j = 0; j <= N - 2; i++) {
      if (q[j] <= 0)
        continue;

      sum += q[j] * 2;
      q[j + 1] -= q[j];
    }

    if (q[N - 1] > 0) {
      sum += q[N - 1];
    }

    min_sum = min(min_sum, sum);
  }

  cout << min_sum << endl;

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

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), aa(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i], aa[i] = a[i];

  int ans = 1e9;
  for (int j = 0; j <= a[0]; ++j) {
    // cout << j << endl;
    int ret = j;
    a[0] -= j;
    for (int i = 1; i < n - 1; ++i) {
      while (a[i] > 0) {
        if (a[i - 1] > 0) {
          a[i - 1]--;
          a[i]--;
          ret += 2;
        } else {
          a[i]--;
          a[i + 1]--;
          ret += 2;
        }
      }
    }
    ret += max(0, a[0]) + max(0, a[n - 1]);
    // cout << ret << endl;
    for (int i = 0; i < n; ++i)
      a[i] = aa[i];
    ans = min(ans, ret);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  int ret = 0;
  for (int i = 1; i < n - 1; ++i) {
    while (a[i]) {
      if (a[i - 1] > 0) {
        a[i - 1]--;
        a[i]--;
        ret += 2;
      } else {
        a[i]--;
        a[i + 1]--;
        ret += 2;
      }
    }
  }
  ret += max(0, a[0]) + max(0, a[n - 1]);

  cout << ret << endl;

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

#include <iostream> //????????????????????§??????????????????
using namespace std;
int main() {
  int a[5005];
  int n;
  int i, j, k;
  int s, b, c;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  s = 0, b = 0, c = 0;
  for (i = 3; i > 0; i--) {
    for (j = 0; j < n; j++) {
      if (a[j] == i) {
        if (c == 0)
          b = j;
        c++;
      } else if (c != 0) {
        if (b == 0 || c % 2 == 0)
          s += c;
        else {
          if (a[j] > a[b - 1])
            a[j] = (a[j] > 0 ? a[j] - 1 : 0);
          else
            a[b - 1] = (a[b - 1] > 0 ? a[b - 1] - 1 : 0);
          s += c + 1;
        }
        for (k = b; k < b + c; k++) {
          a[k]--;
        }
        b = 0, c = 0;
      }
    }
    s += c;
    for (j = b; j < b + c; j++) {
      a[j]--;
    }
    b = 0, c = 0;
  }
  cout << s << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int a[5000], sum = 0;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    while (true) {
      int min1 = 4, min2, max;
      for (int i = 0; i < n; i++) {

        if (min1 > a[i] && a[i] > 0) {
          min1 = a[i];
          min2 = i;
        }
      }

      if (min1 == 4) {
        cout << sum << '\\n';
        break;
      }
      if (min2 == 0)
        max = 1;
      else if (min2 == n - 1)
        max = n - 2;
      else if (a[min2 - 1] < a[min2 + 1])
        max = min2 + 1;
      else if (a[min2 - 1] >= a[min2 + 1])
        max = min2 - 1;

      a[min2]--;
      if (a[max] > 0)
        a[max]--;

      sum += 2;
    }
  }

  return 0;
}