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

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

int main() {
  int s, i = 1, m;
  cin >> s;
  m = s;
  while (s != 4 || m != 2 || m != 1) {
    if (s % 2 == 0) {
      s /= 2;
    } else
      s = 3 * s + 1;
    i++;
  }
  cout << i + 3 << endl;
  return 0;
}

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

int main() {
  int s, i = 1, m;
  cin >> s;
  m = s;
  while (s != 4) {
    if (m == 2 || m == 1)
      break;
    if (s % 2 == 0) {
      s /= 2;
    } else
      s = 3 * s + 1;
    i++;
  }
  cout << i + 3 << endl;
  return 0;
}
Problem p03146 (C++)

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

using namespace std;

int main() {
  int s, num;
  cin >> s;
  vector<int> a(101);
  int tmp = s;
  a[tmp]++;
  for (int i = 1;; i++) {
    if (tmp % 2 == 0)
      tmp /= 2;
    else
      tmp = 3 * tmp + 1;

    a[tmp]++;
    if (a[tmp] == 2) {
      cout << i + 1;
      return 0;
    }
  }
}

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

using namespace std;

int main() {
  int s, num;
  cin >> s;
  vector<int> a(1000001);
  int tmp = s;
  a[tmp]++;
  for (int i = 1;; i++) {
    if (tmp % 2 == 0)
      tmp /= 2;
    else
      tmp = 3 * tmp + 1;

    a[tmp]++;
    if (a[tmp] == 2) {
      cout << i + 1;
      return 0;
    }
  }
}
Problem p03146 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using ll = long long;
using vin = vector<int>;
using vll = vector<ll>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll INF = 1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int main() {
  int s;
  cin >> s;
  int ok[1100];
  bool fin = false;
  int ans = 0;
  while (!fin) {
    ans++;
    // cout<<s<<endl;
    if (ok[s] == 1) {
      // cout<<" "<<s<<endl;
      fin = true;

    } else {
      ok[s] = 1;
      if (s % 2 == 0)
        s /= 2;
      else
        s = 3 * s + 1;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using ll = long long;
using vin = vector<int>;
using vll = vector<ll>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll INF = 1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int main() {
  int s;
  cin >> s;
  int ok[11000];
  bool fin = false;
  int ans = 0;
  while (!fin) {
    ans++;
    // cout<<s<<endl;
    if (ok[s] == 1) {
      // cout<<" "<<s<<endl;
      fin = true;

    } else {
      ok[s] = 1;
      if (s % 2 == 0)
        s /= 2;
      else
        s = 3 * s + 1;
    }
  }
  cout << ans << endl;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> data(n);
  data[0] = 0;
  rep(i, 0, n) { cin >> data[i + 1]; }
  int ans = 0;
  rep(i, 0, n) {
    if (data[i + 1] > data[i]) {
      ans += data[i + 1] - data[i];
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> data(n + 1);
  data[0] = 0;
  rep(i, 0, n) { cin >> data[i + 1]; }
  int ans = 0;
  rep(i, 0, n) {
    if (data[i + 1] > data[i]) {
      ans += data[i + 1] - data[i];
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  REP(i, N) cin >> h[i];

  int ans = 0;
  for (int l = 0; l < N; ++l) {
    while (h[l] > 0) {
      int r = l;
      while (h[++r] != 0)
        ;

      for (int i = l; i < r; ++i)
        h[i]--;

      ans++;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  REP(i, N) cin >> h[i];

  int ans = 0;
  for (int l = 0; l < N; ++l) {
    while (h[l] > 0) {
      int r;
      for (r = l; r < N; ++r) {
        if (h[r] == 0)
          break;
      }

      for (int i = l; i < r; ++i)
        h[i]--;

      ans++;
    }
  }

  cout << ans << endl;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> h(N);

  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  int res = 0;
  while (true) {
    if (*max_element(h.begin(), h.end()) == 0)
      break;
    int it = 0;
    while (it < N) {
      if (h[it] == 0)
        it++;
      else {
        res++;
        while (h[it] > 0 && it < N) {
          h[it]--;
          it++;
        }
      }
    }
  }

  cout << res << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> h(N);

  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  int res = 0;
  while (true) {
    if (*max_element(h.begin(), h.end()) == 0)
      break;
    int it = 0;
    while (it < N) {
      if (h[it] == 0)
        it++;
      else {
        res++;
        while (it < N && h[it] > 0) {
          h[it]--;
          it++;
        }
      }
    }
  }

  cout << res << endl;
}
Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int h[101] = {0};
  int Min = 100;
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
    if (Min > h[i]) {
      Min = h[i];
    }
  }
  for (int i = 0; i < n; ++i) {
    h[i] -= Min;
  }
  int Sum = Min;
  int st, end;
  int flg = 0;
  while (1) {
    for (int i = 0; i < n; ++i) { // all 0 ?
      if (h[i] != 0) {
        break;
      }
      if (i == n - 1) {
        cout << Sum << endl;
        return 0;
      }
    }
    for (int i = 0; i < n; ++i) {
      if (h[i] != 0 && flg == 0) {
        flg = 1;
        st = i;
      }
    }
    for (int i = st + 1; i < n; ++i) {
      if (h[i] != 0 && h[i + 1] == 0) {
        end = i;
        flg = 0;
        Min = 100;
        for (int j = st; j <= end; ++j) {
          if (Min > h[j]) {
            Min = h[j];
          }
        }
        for (int j = st; j <= end; ++j) {
          h[j] -= Min;
        }
        Sum += Min;
        break;
      }
    }
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int h[101] = {0};
  int Min = 100;
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
    if (Min > h[i]) {
      Min = h[i];
    }
  }
  for (int i = 0; i < n; ++i) {
    h[i] -= Min;
  }
  int Sum = Min;
  int st, end;
  int flg = 0;
  while (1) {
    for (int i = 0; i < n; ++i) { // all 0 ?
      if (h[i] != 0) {
        break;
      }
      if (i == n - 1) {
        cout << Sum << endl;
        return 0;
      }
    }
    for (int i = 0; i < n; ++i) {
      if (h[i] != 0 && flg == 0) {
        flg = 1;
        st = i;
      }
      if (h[i] != 0 && h[i + 1] == 0) {
        end = i;
        flg = 0;
        Min = 100;
        for (int j = st; j <= end; ++j) {
          if (Min > h[j]) {
            Min = h[j];
          }
        }
        for (int j = st; j <= end; ++j) {
          h[j] -= Min;
        }
        Sum += Min;
        break;
      }
    }
  }
}
Problem p03147 (C++)

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

vector<int> buf;

int main() {
  int N, h[100], c = 0;
  vector<vector<int>> T(100, vector<int>(N + 1, 0));
  int ans = 0;
  cin >> N;
  for (int i = 0; i < 100; i++) {
    h[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      if (h[j] > i) {
        T[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      c = 0;
      while (T[i][j] == 1) {
        j++;
        c++;
      }
      if (c != 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

vector<int> buf;

int main() {
  int N, h[100], c = 0;
  vector<vector<int>> T(100, vector<int>(100, 0));
  int ans = 0;
  cin >> N;
  for (int i = 0; i < 100; i++) {
    h[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      if (h[j] > i) {
        T[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      c = 0;
      while (T[i][j] == 1) {
        j++;
        c++;
      }
      if (c != 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03147 (C++)

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

vector<int> buf;

int main() {
  int N, h[100], c = 0;
  vector<vector<int>> T(100, vector<int>(N, 0));
  int ans = 0;
  cin >> N;
  for (int i = 0; i < 100; i++) {
    h[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      if (h[j] > i) {
        T[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      c = 0;
      while (T[i][j] == 1) {
        j++;
        c++;
      }
      if (c != 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

vector<int> buf;

int main() {
  int N, h[100], c = 0;
  vector<vector<int>> T(1000, vector<int>(100, 0));
  int ans = 0;
  cin >> N;
  for (int i = 0; i < 100; i++) {
    h[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      if (h[j] > i) {
        T[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < N; j++) {
      c = 0;
      while (T[i][j] == 1) {
        j++;
        c++;
      }
      if (c != 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03147 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; ++i)
    cin >> h[i];
  int j = 0, cnt = 0;
  for (int i = 0; i < N; ++i) {
    while (h[i] > 0) {
      int j = i;
      while (h[j] > 0) {
        ++j;
      }
      int k = i;
      while (k < j) {
        --h[k];
        ++k;
      }
      ++cnt;
    }
  }
  cout << cnt << endl;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; ++i)
    cin >> h[i];
  int j = 0, cnt = 0;
  for (int i = 0; i < N; ++i) {
    while (h[i] > 0) {
      for (int j = i; j < N && h[j] > 0; ++j) {
        --h[j];
      }
      ++cnt;
    }
  }
  cout << cnt << endl;
}
Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ct(string) cout << string << endl

int main() {
  int n, minn, ans = 0, check, startt;
  cin >> n;
  vector<int> data(n);
  rep(i, n) cin >> data.at(i);
  while (true) {
    minn = 101;
    check = 0;
    startt = 0;
    rep(i, n) {
      if (data.at(i) == 0 || i == n - 1) {
        minn = (i == n - 1 ? min(minn, data.at(i)) : minn);
        for (int j = startt; i + 1 > j; j++) {
          data.at(j) = ((data.at(j) - minn < 1) ? 0 : data.at(j) - minn);
        }
        ans += (minn == 101 ? 0 : minn);
        startt = i;
        minn = 101;
        check += data.at(i);
        continue;
      }
      check += data.at(i);
      minn = min(minn, data.at(i));
    }
    if (check == 0)
      break;
  }
  ct(ans);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ct(string) cout << string << endl

int main() {
  int n, minn, ans = 0, check, startt;
  cin >> n;
  vector<int> data(n);
  rep(i, n) cin >> data.at(i);
  while (true) {
    minn = 101;
    check = 0;
    startt = 0;
    rep(i, n) {
      if (data.at(i) == 0 || i == n - 1) {
        minn = (data.at(i) != 0 ? min(minn, data.at(i)) : minn);
        for (int j = startt; i + 1 > j; j++) {
          data.at(j) = ((data.at(j) - minn < 1) ? 0 : data.at(j) - minn);
        }
        ans += (minn == 101 ? 0 : minn);
        startt = i;
        minn = 101;
        check += data.at(i);
        continue;
      }
      check += data.at(i);
      minn = min(minn, data.at(i));
    }
    if (check == 0)
      break;
  }
  ct(ans);
}
Problem p03147 (C++)

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

int main() {
  using namespace std;

  int n;
  scanf("%d", &n);

  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }

  int watering_times = 0;
  for (int i = 0; i < n; i++) {
    while (h[i]) {
      watering_times++;
      h[i]--;
      int j = i + 1;
      if (j == n)
        continue;
      while (h[j]) {
        h[j++]--;
      }
    }
  }

  cout << watering_times << endl;

  return 0;
}


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

int main() {
  using namespace std;

  int n;
  scanf("%d", &n);

  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }

  int watering_times = 0;
  for (int i = 0; i < n; i++) {
    while (h[i]) {
      watering_times++;
      h[i]--;
      int j = i + 1;
      if (j == n)
        continue;
      while (h[j]) {
        h[j]--;
        j++;
        if (j == n)
          break;
      }
    }
  }

  cout << watering_times << endl;

  return 0;
}
Problem p03147 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }

  int cnt;

  while (true) {
    bool allzero = true;
    for (int i = 0; i < N; i++) {
      if (h.at(i) != 0) {
        allzero = false;
      }
    }
    if (allzero)
      break;

    // ゼロでない一番左の値をほしい

    int l;
    while (h.at(l) == 0)
      l++;
    int r = l;

    while (r < N && h.at(r + 1) != 0)
      r++;
    cnt++;
    for (int i = l; i <= r; i++)
      h.at(i)--;
  }

  cout << cnt << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }

  int cnt;

  while (true) {
    bool allzero = true;
    for (int i = 0; i < N; i++) {
      if (h.at(i) != 0) {
        allzero = false;
      }
    }
    if (allzero)
      break;

    // ゼロでない一番左の値をほしい

    int l;
    while (h.at(l) == 0)
      l++;
    int r = l;

    while (r < N - 1 && h.at(r + 1) != 0)
      r++;
    cnt++;
    for (int i = l; i <= r; i++)
      h.at(i)--;
  }

  cout << cnt << endl;
}
Problem p03147 (C++)

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

int main() {
  int N, ans, r, j, i, l;
  cin >> N;
  int h[1100] = {0};
  for (int i = 0; i < N; ++i)
    cin >> h[i];
  ans = 0;
  for (int l = 0; l < N; l++) {
    while (h[l] > 0) {
      for (int r = l + 1; r < N; r++) {
        if (h[r] == 0)
          break;
      }

      for (int i = l; i < r; i++)
        --h[i];
      ans++;
    }
  }

  cout << ans << endl;
}


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

int main() {
  int N, ans, r, j, i, l;
  cin >> N;
  int h[1100] = {0};
  for (int i = 0; i < N; ++i)
    cin >> h[i];
  ans = 0;
  for (int l = 0; l < N; l++) {
    while (h[l] > 0) {
      int r;
      for (r = l + 1; r < N; r++) {
        if (h[r] == 0)
          break;
      }

      for (int i = l; i < r; i++)
        --h[i];
      ans++;
    }
  }

  cout << ans << endl;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp(a, b) make_pair(a, b)
#define pb(t) push_back(t)
#define ft first
#define sc second
#define pt(num) cout << num << "\n"
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 101010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

ll N;
ll h[111];

ll rec(ll l, ll r) {
  if (l >= r)
    return 0;

  ll res = 0;
  ll i;
  ll min = INF;
  for (i = l; i < r; i++)
    chmin(min, h[i]);
  res += min;

  vector<ll> ze;
  ze.pb(-1);
  for (i = l; i < r; i++) {
    h[i] -= min;
    if (h[i] == 0)
      ze.pb(i);
  }
  ze.pb(N);

  for (i = 0; i < ze.size() - 1; i++)
    res += rec(ze[i] + 1, ze[i + 1]);

  return res;
}

int main(void) {
  cin >> N;
  ll i;

  for (i = 0; i < N; i++)
    cin >> h[i];

  pt(rec(0, N));
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp(a, b) make_pair(a, b)
#define pb(t) push_back(t)
#define ft first
#define sc second
#define pt(num) cout << num << "\n"
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 101010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

ll N;
ll h[111];

ll rec(ll l, ll r) {
  if (l >= r)
    return 0;

  ll res = 0;
  ll i;
  ll min = INF;
  for (i = l; i < r; i++)
    chmin(min, h[i]);
  res += min;

  vector<ll> ze;
  ze.pb(l - 1);
  for (i = l; i < r; i++) {
    h[i] -= min;
    if (h[i] == 0)
      ze.pb(i);
  }
  ze.pb(N);

  for (i = 0; i < ze.size() - 1; i++)
    res += rec(ze[i] + 1, ze[i + 1]);

  return res;
}

int main(void) {
  cin >> N;
  ll i;

  for (i = 0; i < N; i++)
    cin >> h[i];

  pt(rec(0, N));
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ALL(x) x.begin(), x.end()
#define REVERSE(x) reverse(x.begin(), x.end())
#define SORT(x, n) sort(x, x)
#define P pair<int, int>
typedef long long LL;
const int INF = 1000000007;
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
int N;
int h[100] = {};
int count(int height, int i) {
  if (i == 0 && h[i] >= height) {
    return count(height, i + 1) + 1;
  }
  if (i == N - 1) {
    return 0;
  } else {
    if (h[i] < height && h[i + 1] >= height) {
      return count(height, i + 1) + 1;
    } else {
      return count(height, i + 1);
    }
  }
}
int main() {
  cin >> N;
  REP(i, N) { cin >> h[i]; }
  int ans = 0;
  FOR(x, 1, 101) { ans += count(x, 0); }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ALL(x) x.begin(), x.end()
#define REVERSE(x) reverse(x.begin(), x.end())
#define SORT(x, n) sort(x, x)
#define P pair<int, int>
typedef long long LL;
const int INF = 1000000007;
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
int N;
int h[100] = {};
int count(int height, int i) {
  if (i == 0 && h[i] >= height) {
    return count(height, i + 1) + 1;
  }
  if (i == N - 1) {
    return 0;
  } else {
    if (h[i] < height && h[i + 1] >= height) {
      return count(height, i + 1) + 1;
    } else {
      return count(height, i + 1);
    }
  }
}
int main() {
  cin >> N;
  REP(i, N) { cin >> h[i]; }
  if (N == 1) {
    cout << h[0];
  } else {
    int ans = 0;
    FOR(x, 1, 101) { ans += count(x, 0); }
    cout << ans;
  }
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

// 最大公約数
template <class T> inline T GCD(T m, T n) {
  T temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

// 最小公倍数
template <class T> inline T LCM(T m, T n) { return m * n / GCD(m, n); }

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

  int ans = 0;
  while (true) {
    auto mid = find(h.begin(), h.end(), 0);
    int m = *min_element(h.begin(), mid - 1);

    for (auto it = h.begin(); it < mid; ++it) {
      *it = *it - m > 0 ? *it - m : 0;
    }

    ans += m;

    auto iter = h.begin();
    for (auto it : h) {
      if (it != 0) {
        break;
      }
      iter++;
    }
    h.erase(h.begin(), iter);

    if (h.size() == 0) {
      break;
    }
  }

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

Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

// 最大公約数
template <class T> inline T GCD(T m, T n) {
  T temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

// 最小公倍数
template <class T> inline T LCM(T m, T n) { return m * n / GCD(m, n); }

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

  int ans = 0;
  while (true) {
    auto mid = find(h.begin(), h.end(), 0);
    int m = *min_element(h.begin(), distance(h.begin(), mid) ? mid - 1 : mid);

    for (auto it = h.begin(); it < mid; ++it) {
      *it = *it - m > 0 ? *it - m : 0;
    }

    ans += m;

    auto iter = h.begin();
    for (auto it : h) {
      if (it != 0) {
        break;
      }
      iter++;
    }
    h.erase(h.begin(), iter);

    if (h.size() == 0) {
      break;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> h(n);
  for (int i = 0; i < n; i++) {
    std::cin >> h[i];
  }

  int tmp = *std::max_element(h.begin(), h.end());
  std::vector<int> flag(0);
  for (int i = 0; i < tmp; i++) {
    for (int j = 0; j < n; j++) {
      if (h[j] > 0) {
        flag.push_back(1);
      } else {
        flag.push_back(0);
      }
      h[j]--;
    }
    flag.push_back(0);
  }

  int count = 0;
  for (int i = 0; i < flag.size() - 1; i++) {
    if (flag[i] == 1 && flag[i + 1] == 0) {
      count++;
    }
  }

  std::cout << count << std::endl;

  return 0;
}

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

int main() {
  int n;
  std::cin >> n;
  std::vector<int> h(n);
  for (int i = 0; i < n; i++) {
    std::cin >> h[i];
  }

  int tmp = *std::max_element(h.begin(), h.end());
  std::vector<int> flag(0);
  for (int i = 0; i < tmp; i++) {
    for (int j = 0; j < n; j++) {
      if (h[j] > 0) {
        flag.push_back(1);
      } else {
        flag.push_back(0);
      }
      h[j]--;
    }
    flag.push_back(0);
  }

  int count = 0;
  for (int i = 0; i < (int)flag.size() - 1; i++) {
    if (flag[i] == 1 && flag[i + 1] == 0) {
      count++;
    }
  }

  std::cout << count << std::endl;

  return 0;
}
Problem p03147 (C++)

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

using namespace std;
unsigned int *h;
unsigned int N;
unsigned int findMax() {
  unsigned max = 0;
  unsigned int found = N;
  for (unsigned int i = 0; i < N; i++) {
    if (max < h[i]) {
      max = h[i];
      found = i;
    }
  }
  return found;
}

std::pair<unsigned int, unsigned int> searchRound(unsigned int orig) {
  std::pair<unsigned int, unsigned int> ret = {orig, orig};
  unsigned int i;
  for (i = orig; 0 < i; i--) {
    if (h[i - 1] > 0) {
      if (i == 1) {
        ret.first = 0;
        break;
      }
      continue;
    }
    ret.first = i;
    break;
  }
  for (i = orig; i < (N - 1); i++) {
    if (h[i + 1] > 0) {
      if (i == (N - 2)) {
        ret.second = N - 1;
        break;
      }
      continue;
    }
    ret.second = i;
    break;
  }
  return ret;
}

void give(std::pair<unsigned int, unsigned int> targets) {
  for (unsigned int i = targets.first; i <= targets.second; i++) {
    h[i]--;
  }
}

void show() {
  for (unsigned int i = 0; i < N; i++) {
    cout << h[i] << ' ';
  }
  cout << endl;
}
int main() {
  cin >> N;
  h = new unsigned int(N);
  for (unsigned int i = 0; i < N; i++) {
    cin >> h[i];
  }

  unsigned int count = 0;
  do {
    unsigned int target = findMax();
    if (target == N)
      break;
    count++;
    std::pair<unsigned int, unsigned int> targets = searchRound(target);
    give(targets);
    // show();
  } while (1);

  cout << count << endl;
  delete[] h;
  return 0;
}


Fixed submission:
#include <iostream>
#include <utility>

using namespace std;
unsigned int *h;
unsigned int N;
unsigned int findMax() {
  unsigned max = 0;
  unsigned int found = N;
  for (unsigned int i = 0; i < N; i++) {
    if (max < h[i]) {
      max = h[i];
      found = i;
    }
  }
  return found;
}

std::pair<unsigned int, unsigned int> searchRound(unsigned int orig) {
  std::pair<unsigned int, unsigned int> ret = {orig, orig};
  unsigned int i;
  for (i = orig; 0 < i; i--) {
    if (h[i - 1] > 0) {
      if (i == 1) {
        ret.first = 0;
        break;
      }
      continue;
    }
    ret.first = i;
    break;
  }
  for (i = orig; i < (N - 1); i++) {
    if (h[i + 1] > 0) {
      if (i == (N - 2)) {
        ret.second = N - 1;
        break;
      }
      continue;
    }
    ret.second = i;
    break;
  }
  return ret;
}

void give(std::pair<unsigned int, unsigned int> targets) {
  for (unsigned int i = targets.first; i <= targets.second; i++) {
    h[i]--;
  }
}

void show() {
  for (unsigned int i = 0; i < N; i++) {
    cout << h[i] << ' ';
  }
  cout << endl;
}
int main() {
  cin >> N;
  h = new unsigned int(N);
  for (unsigned int i = 0; i < N; i++) {
    cin >> h[i];
  }

  unsigned int count = 0;
  do {
    unsigned int target = findMax();
    if (target == N)
      break;
    count++;
    std::pair<unsigned int, unsigned int> targets = searchRound(target);
    give(targets);
    // show();
  } while (1);

  cout << count << endl;
  // delete [] h;
  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
// #define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  int N, ans = 0;
  vi max, min;
  bool flag = true; // true なら↑　falseなら↓
  cin >> N;
  vi h(N);
  REP(i, N) cin >> h.at(i);
  min.push_back(0);
  if (h.at(0) > h.at(1)) {
    max.push_back(h[0]);
    flag = false;
  } else {
    flag = true;
  }

  for (int i = 1; i < N - 1; i++) {
    if (h.at(i) < h.at(i + 1) && !flag) {
      min.push_back(h.at(i));
      flag = true;
    } else if (h.at(i) > h.at(i + 1) && flag) {
      max.push_back(h.at(i));
      flag = false;
    }
  }
  if (flag == true)
    max.push_back(h.at(N - 1));
  for (int j = 0; j < max.size(); j++) {
    ans += max.at(j) - min.at(j);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
// #define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  int N, ans = 0;
  vi max, min;
  bool flag = true; // true なら↑　falseなら↓
  cin >> N;
  vi h(N);
  REP(i, N) cin >> h.at(i);
  min.push_back(0);
  if (N == 1) {
    cout << h.at(0) << endl;
    return 0;
  }
  if (h.at(0) > h.at(1)) {
    max.push_back(h[0]);
    flag = false;
  } else {
    flag = true;
  }

  for (int i = 1; i < N - 1; i++) {
    if (h.at(i) < h.at(i + 1) && !flag) {
      min.push_back(h.at(i));
      flag = true;
    } else if (h.at(i) > h.at(i + 1) && flag) {
      max.push_back(h.at(i));
      flag = false;
    }
  }
  if (flag == true)
    max.push_back(h.at(N - 1));
  for (int j = 0; j < max.size(); j++) {
    ans += max.at(j) - min.at(j);
  }
  cout << ans << endl;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> u(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  v.push_back(0);
  u[0] = v[0];
  for (int i = 0; i < n + 1; i++) {
    if (0 < i)
      u[i] = v[i] - v[i - 1];
  }
  int ans = 0;
  for (int i = 0; i < n + 1; i++) {
    if (u[i] > 0)
      ans += u[i];
  }
  // for (int i = 0; i < n;i++){
  //	cout << u[i] << " ";
  // }
  // cout << endl;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> u(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  v.push_back(0);
  u[0] = v[0];
  for (int i = 0; i < n + 1; i++) {
    if (0 < i)
      u[i] = v[i] - v[i - 1];
  }
  int ans = 0;
  for (int i = 0; i < n + 1; i++) {
    if (u[i] > 0)
      ans += u[i];
  }
  // for (int i = 0; i < n;i++){
  //	cout << u[i] << " ";
  // }
  // cout << endl;
  cout << ans << endl;

  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
#define N 100009

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

  bool color[n + 1];
  for (int i = 0; i < n; i++)
    color[i] = false;
  color[n] = true;

  int high[n + 1];
  for (int i = 0; i < n; i++) {
    high[i] = 0;
    if (data[i] == 0)
      color[i] = true;
  }

  int count = 0;
  bool ans = true;

  while (ans) {
    ans = false;
    int p, q;
    for (int i = 0; i < n; i++) {
      if (color[i] == false) {
        p = i;
        break;
      }
    }
    for (int j = p + 1; j <= n; j++) {
      if (color[j] == true) {
        q = j - 1;
        break;
      }
    }

    for (int i = p; i <= q; i++) {
      high[i]++;
      if (high[i] == data[i])
        color[i] = true;
    }

    // for(int i=0; i<n; i++) cout<<color[i];
    // cout<<endl;

    count++;

    for (int i = 0; i < n; i++) {
      if (color[i] == false) {
        ans = true;
      }
    }
  }

  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
#define N 100009

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

  bool color[n + 1];
  for (int i = 0; i < n; i++)
    color[i] = false;
  color[n] = true;

  int high[n + 1];
  for (int i = 0; i < n; i++) {
    high[i] = 0;
    if (data[i] == 0)
      color[i] = true;
  }

  int count = 0;
  bool ans = false;
  for (int i = 0; i < n; i++) {
    if (data[i] != 0) {
      ans = true;
      break;
    }
  }

  while (ans) {
    ans = false;
    int p, q;
    for (int i = 0; i < n; i++) {
      if (color[i] == false) {
        p = i;
        break;
      }
    }
    for (int j = p + 1; j <= n; j++) {
      if (color[j] == true) {
        q = j - 1;
        break;
      }
    }

    for (int i = p; i <= q; i++) {
      high[i]++;
      if (high[i] == data[i])
        color[i] = true;
    }

    // for(int i=0; i<n; i++) cout<<color[i];
    // cout<<endl;

    count++;

    for (int i = 0; i < n; i++) {
      if (color[i] == false) {
        ans = true;
      }
    }
  }

  cout << count << endl;
}
Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int i = 0, n, m = 0, j = 0, a[114];
  for (cin >> n; i < n; cin >> a[i++])
    ;
  for (a[i] = 0; j++ < 114;)
    for (i = 0; i<n; m += a[i]--> 0 && a[++i] <= 0)
      ;
  cout << m << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int i = 0, n, m = 0, j = 0, a[114];
  for (cin >> n; i < n; cin >> a[i++])
    ;
  for (a[i] = 0; j++ < 114;)
    for (i = 0; i<n; m += a[i++]--> 0 && a[i] <= 0)
      ;
  cout << m << endl;
}
Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main() {
  int n, h[111], flag1 = 1, flag2, ans = 0, i, j, k, min;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &h[i]);
    if (h[i] != 0)
      flag1 = 0;
  }
  while (flag1 == 0) {
    k = 0;
    flag2 = 1;
    min = 111;
    for (i = 0; i < n; i++) {
      if (h[i] == 0) {
        for (j = k + 1; j < i; j++)
          h[j] -= min;
        if (min < 111)
          ans += min;
        k = i;
        min = 111;
      } else if (h[i] < min) {
        min = h[i];
        flag2 = 0;
      }
    }
    for (j = k + 1; j < i; j++)
      h[j] -= min;
    if (min < 111)
      ans += min;
    if (flag2 == 1)
      flag1 = 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <stdio.h>
int main() {
  int n, h[111], flag1 = 1, flag2, ans = 0, i, j, k, min;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &h[i]);
    if (h[i] != 0)
      flag1 = 0;
  }
  while (flag1 == 0) {
    k = -1;
    flag2 = 1;
    min = 111;
    for (i = 0; i < n; i++) {
      if (h[i] == 0) {
        for (j = k + 1; j < i; j++)
          h[j] -= min;
        if (min < 111)
          ans += min;
        k = i;
        min = 111;
      } else if (h[i] < min) {
        min = h[i];
        flag2 = 0;
      }
    }
    for (j = k + 1; j < i; j++)
      h[j] -= min;
    if (min < 111)
      ans += min;
    if (flag2 == 1)
      flag1 = 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03147 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  int ans = 0;
  bool st = true;
  while (st) {
    int i = 0;
    if (h[i] == 0) {
      while (h[i] == 0 && i < n) {
        i++;
      }
      if (i == n) {
        st = false;
        break;
      }
    }
    while (h[i] > 0) {
      h[i]--;
      i++;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> h(n + 1);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  int ans = 0;
  bool st = true;
  while (st) {
    int i = 0;
    if (h[i] == 0) {
      while (h[i] == 0 && i < n) {
        i++;
      }
      if (i == n) {
        st = false;
        break;
      }
    }
    while (h[i] > 0) {
      h[i]--;
      i++;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03147 (C++)

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

using namespace std;

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

  vector<int> H(N);
  vector<vector<int>> table(1);
  int index = 0;
  for (int i = 0; i < N; i++) {
    cin >> H[i];

    if (H[i] == 0) {
      index++;
      table.push_back(vector<int>());
      continue;
    }

    table[index].push_back(H[i]);
  }

  int ans = 0;
  for (int i = 0; i < table.size(); i++) {
    if (table[i].size() == 1) {
      ans += table[i][0];
      continue;
    }

    int min = *min_element(table[i].begin(), table[i].end());

    index++;
    table.push_back(vector<int>());
    if (table[i][0] != min) {
      table[index].push_back(table[i][0] - min);
    }

    for (int k = 1; k < table[i].size() - 1; k++) {
      if (table[i][k] == min && table[index].size() > 0) {
        index++;
        table.push_back(vector<int>());
      } else {
        table[index].push_back(table[i][k] - min);
      }
    }

    if (table[i][table[i].size() - 1] != min) {
      table[index].push_back(table[i][table[i].size() - 1] - min);
    }

    if (table[index].size() == 0) {
      table.erase(table.begin() + index);
      index--;
    }

    ans += min;
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

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

  vector<int> H(N);
  vector<vector<int>> table(1);
  int index = 0;
  for (int i = 0; i < N; i++) {
    cin >> H[i];

    if (H[i] == 0) {
      index++;
      table.push_back(vector<int>());
      continue;
    }

    table[index].push_back(H[i]);
  }

  int ans = 0;
  for (int i = 0; i < table.size(); i++) {
    if (table[i].size() == 0) {
      continue;
    }

    if (table[i].size() == 1) {
      ans += table[i][0];
      continue;
    }

    int min = *min_element(table[i].begin(), table[i].end());

    index++;
    table.push_back(vector<int>());
    if (table[i][0] != min) {
      table[index].push_back(table[i][0] - min);
    }

    for (int k = 1; k < table[i].size() - 1; k++) {
      if (table[i][k] == min && table[index].size() > 0) {
        index++;
        table.push_back(vector<int>());
      } else {
        table[index].push_back(table[i][k] - min);
      }
    }

    if (table[i][table[i].size() - 1] != min) {
      table[index].push_back(table[i][table[i].size() - 1] - min);
    }

    if (table[index].size() == 0) {
      table.erase(table.begin() + index);
      index--;
    }

    ans += min;
  }

  cout << ans << endl;

  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
// const int N = 1000000;
const ll mod = 1000000007;
// using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dh[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int dw[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * (B / gcd(A, B)); }
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;
Graph G;

/*------------------------------------------------------------------*/

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  int MAX = *max_element(h.begin(), h.end());
  int ans = 0;

  while (true) {
    int mi = 101;
    MAX = *max_element(h.begin(), h.end());
    if (MAX <= 0)
      break;

    rep(i, n) {
      if (h[i] <= 0 and mi == 101)
        continue;
      if (i == n - 1) {
        mi = min(h[i], mi);
        ans += mi;
        rep(j, n) { h[j] -= mi; }
        break;
      }
      if (h[i] > 0) {
        mi = min(h[i], mi);
      }
      if (h[i] <= 0) {
        ans += mi;
        rep(j, i) { h[j] -= mi; }
        break;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
// const int N = 1000000;
const ll mod = 1000000007;
// using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dh[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int dw[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * (B / gcd(A, B)); }
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;
Graph G;

/*------------------------------------------------------------------*/

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  int MAX = *max_element(h.begin(), h.end());
  int ans = 0;

  while (true) {
    int mi = 101;
    MAX = *max_element(h.begin(), h.end());
    if (MAX <= 0)
      break;

    rep(i, n) {
      if (h[i] <= 0 and mi == 101)
        continue;
      if (i == n - 1 and h[i] > 0) {
        mi = min(h[i], mi);
        ans += mi;
        rep(j, n) { h[j] -= mi; }
        break;
      }
      if (h[i] > 0) {
        mi = min(h[i], mi);
      }
      if (h[i] <= 0) {
        ans += mi;
        rep(j, i) { h[j] -= mi; }
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p03147 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
H = list(map(int, input().split()))
ans = 0

while max(H) > 0:
    grp = [[]]
    for i, h in enumerate(H):
        if h == 0:
            if len(grp[-1]) > 0:
                grp.append([])
        else:
            grp[-1].append((i, h))
    for g in grp:
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)


Fixed submission:
N = int(input())
H = list(map(int, input().split()))
ans = 0

while max(H) > 0:
    grp = [[]]
    for i, h in enumerate(H):
        if h == 0:
            if len(grp[-1]) > 0:
                grp.append([])
        else:
            grp[-1].append((i, h))
    for g in grp:
        if len(g) == 0:
            continue
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)
Problem p03147 (Python)

Buggy submission (status: Runtime Error):
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    h = list(map(int, input().split()))

    dflag = 0
    cmin = 0
    cmax = h[0]
    count = 0
    for i in range(1, N):
        if h[i - 1] < h[i] and dflag == 1:
            count += cmax - cmin
            cmax = h[i]
            dflag = 0
            cmin = h[i - 1]

        elif h[i - 1] > h[i]:
            dflag = 1

        elif h[i - 1] < h[i]:
            cmax = h[i]

    if dflag == 1:
        count += cmax - cmin
    else:
        count += max(cmax, h[i]) - cmin

    print(count)


if __name__ == "__main__":
    main()


Fixed submission:
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    if N == 1:
        print(input())
        exit()
    h = list(map(int, input().split()))

    dflag = 0
    cmin = 0
    cmax = h[0]
    count = 0
    for i in range(1, N):
        if h[i - 1] < h[i] and dflag == 1:
            count += cmax - cmin
            cmax = h[i]
            dflag = 0
            cmin = h[i - 1]

        elif h[i - 1] > h[i]:
            dflag = 1

        elif h[i - 1] < h[i]:
            cmax = h[i]

    if dflag == 1:
        count += cmax - cmin
    else:
        count += max(cmax, h[i]) - cmin

    print(count)


if __name__ == "__main__":
    main()
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int nibun(int *, int, int, int);
int main(void) {
  int n, h[200];
  int ans;

  ans = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }

  ans = nibun(h, 0, n, ans);

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

  return 0;
}
int nibun(int *h, int start, int end, int ans) {
  int i, j, k;
  int min, mini;

  min = 999;
  mini = -1;

  for (i = start; i < end; i++) {
    if (min > h[i]) {
      min = h[i];
      mini = i;
    }
  }
  if (min != 0 && min != 99) {
    ans += min;
    for (i = start; i < end; i++) {
      h[i] -= min;
    }
  }

  if (mini != 0) {
    ans = nibun(h, start, mini, ans);
  }
  if (mini + 1 != end) {
    ans = nibun(h, mini + 1, end, ans);
  }

  return ans;
}


Fixed submission:
#include <stdio.h>
int nibun(int *, int, int, int);
int main(void) {
  int n, h[200];
  int ans;

  ans = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }

  ans = nibun(h, 0, n, ans);

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

  return 0;
}
int nibun(int *h, int start, int end, int ans) {
  int i, j, k;
  int min, mini;

  min = 999;
  mini = -1;

  for (i = start; i < end; i++) {
    if (min > h[i]) {
      min = h[i];
      mini = i;
    }
  }
  if (min != 0 && min != 99) {
    ans += min;
    for (i = start; i < end; i++) {
      h[i] -= min;
    }
  }

  if (mini != start) {
    ans = nibun(h, start, mini, ans);
  }
  if (mini + 1 != end) {
    ans = nibun(h, mini + 1, end, ans);
  }

  return ans;
}
Problem p03147 (C++)

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

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

  // 高さが全部 0 になるまでやる
  int res = 0;
  while (true) {
    // 最高高さが 0 だったらおしまい
    if (*max_element(h.begin(), h.end()) == 0)
      break;

    // 区間分割する
    int i = 0;
    while (i < N) {
      if (h[i] == 0)
        ++i; // 0 なら何もせずに次に進む
      else {
        ++res; // 区間の始まり
        while (h[i] > 0) {
          --h[i]; // ついでに引いとく
          ++i;    // 区間の終わりまで一気に
        }
      }
    }
  }
  cout << res << endl;
}

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

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

  // 高さが全部 0 になるまでやる
  int res = 0;
  while (true) {
    // 最高高さが 0 だったらおしまい
    if (*max_element(h.begin(), h.end()) == 0)
      break;

    // 区間分割する
    int i = 0;
    while (i < N) {
      if (h[i] == 0)
        ++i; // 0 なら何もせずに次に進む
      else {
        ++res; // 区間の始まり
        while (i < N && h[i] > 0) {
          --h[i]; // ついでに引いとく
          ++i;    // 区間の終わりまで一気に
        }
      }
    }
  }
  cout << res << endl;
}