alexjercan/bugnet huggingface dataset:
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x;
  cin >> x;
  vector<int> v(x);
  for (int i = 0; i < x; i++)
    cin >> v[i];
  long long sum = 0;

  for (int i = 1; i < x; i++) {
    if (v[i - 1] > v[i]) {
      sum += (v[i - 1] - v[i]);
      v[i] = v[i - 1];
    }
  }

  cout << sum << endl;
}


Error: TLE

Fixed submission:

// In the name of the Almighty

#include <bits/stdc++.h>
using namespace std;
#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int main() {

  optimize();
  int x;
  cin >> x;
  vector<int> v(x);
  vector<int> vd(x);
  for (int i = 0; i < x; i++) {
    cin >> v[i];
    vd[i] = v[i];
  }
  int mid = max_element(v.begin(), v.end()) - v.begin();
  int maxx = *max_element(v.begin(), v.end());
  if (mid < x - 1) {
    for (int i = mid; i < x; i++) {
      if (v[i] < maxx) {
        vd[i] = maxx;
      }
    }
  }
  if (mid > 0) {
    for (int i = mid - 1; i >= 0; i--) {
      if (vd[i] < vd[i - 1] && i > 0) {
        while (1) {
          if (vd[i] == v[i - 1])
            break;
          else
            vd[i]++;
        }
      }
    }
  }

  for (int i = 1; i < x; i++) {
    if (vd[i - 1] > vd[i]) {
      vd[i] = vd[i - 1];
    }
  }
  long long int sum = 0;

  for (int i = 0; i < x; i++)

  { // cout<<vd[i]<<" ";
    if (v[i] != vd[i]) {
      int def = vd[i] - v[i];
      sum = sum + def;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
int main() {
  // 整数の入力とカウンターの設定
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  long long ans = 0;

  for (long long i = 1; i < N; i++) {
    if (A.at(i) < A.at(i - 1)) {
      ans += A.at(i - 1) - A.at(i);
      A.at(i) += A.at(i - 1) - A.at(i);
    }
  }

  // 出力
  cout << ans << endl;

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  // 整数の入力とカウンターの設定
  int N;
  cin >> N;
  vector<int> A;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int ans = 0;

  for (int i = 1; i < N; i++) {
    if (A.at(i) > A.at(i - 1)) {
      ans += A.at(i) - A.at(i - 1);
      A.at(i) += A.at(i) - A.at(i - 1);
    }
  }

  // 出力
  cout << ans << endl;

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

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

int main() {
  int N;

  cin >> N;
  vector<long long> test(N);
  for (int i = 0; i < N; i++) {
    cin >> test.at(i);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      continue;
    }
    if (test.at(i - 1) > test.at(i)) {
      ans = ans + test.at(i - 1) - test.at(i);
      test.at(i) = test.at(i - 1);
    }
  }

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


Error: 0

Fixed submission:

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

int main() {
  int N;
  vector<long long> test(N);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> test.at(i);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      continue;
    }
    if (test.at(i - 1) > test.at(i)) {
      ans = ans + test.at(i - 1) - test.at(i);
      test.at(i) = test.at(i - 1);
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define MOD 1000000007
#define pb push_back
#define loop(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define loope(i, b) for (int i = 1; i <= (int)b; i++)
#define loopl(i, a, b) for (ll i = ll(a); i < (ll)b; i++)

int main() {
  int n;
  cin >> n;
  int hei[n];
  loop(i, 0, n) { cin >> hei[i]; }

  ll ans = 0;
  loop(i, 0, n - 1) {
    if (hei[i] > hei[i + 1]) {
      ans += hei[i] - hei[i + 1];
      hei[i + 1] = hei[i];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define MOD 1000000007
#define pb push_back
#define loop(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define loope(i, b) for (int i = 1; i <= (int)b; i++)
#define loopl(i, a, b) for (ll i = ll(a); i < (ll)b; i++)

int main() {
  int n;
  cin >> n;
  int hei[n];
  loop(i, 0, n) { cin >> hei[i]; }

  ll ans = 0;
  loop(i, 0, n) {
    int max = hei[i];
    loop(j, i + 1, n) {
      if (hei[j] < max) {
        ans = ans + (max - hei[j]);
        hei[j] = max;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  long long sum = 0;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 1; i < n; i++) {
    int d = A[i - 1] - A[i];
    if (d > 0) {
      A[i] = A[i] + d;
      sum += d;
    }
  }
  printf("%lld\\n", sum);
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 10000
int main() {
  int n, A[MAX];
  int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 1; i < n; i++) {
    int d = A[i - 1] - A[i];
    if (d > 0) {
      A[i] = A[i] + d;
      sum += d;
    }
  }
  printf("%d\\n", sum);
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int64_t u;
  int64_t sum = 0;
  int A[200010];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  if (N == 1) {
    cout << sum << endl;
  } else {
    for (int i = 0; i < N - 1; i++) {
      int s = i + 1;
      // cout<<A[i]<<A[s]<<endl;
      if (A[i] > A[s]) {
        u = A[i] - A[s];
        A[s] = A[i];
        sum += u;
        // cout << u <<endl;
      }
    }
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int64_t u;
  int64_t sum = 0;
  int A[100000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  if (N == 1) {
    cout << sum << endl;
  } else {
    for (int i = 0; i < N - 1; i++) {
      int s = i + 1;
      // cout<<A[i]<<A[s]<<endl;
      if (A[i] > A[s]) {
        u = A[i] - A[s];
        A[s] = A[i];
        sum += u;
        // cout << u <<endl;
      }
    }
    cout << sum << endl;
  }
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

long long a[2 * 100000 + 5];

int main() {
  long long n, min_sum = 0;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] > a[i + 1]) {
      min_sum += a[i] - a[i + 1];
      a[i + 1] += a[i] - a[i + 1];
    }
  }
  cout << min_sum << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

long long a[2 * 10 ^ 5 + 5];

int main() {
  long long n, min_sum = 0;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] > a[i + 1]) {
      min_sum += a[i] - a[i + 1];
      a[i + 1] += a[i] - a[i + 1];
    }
  }
  cout << min_sum << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pint pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define vint vector<int>
#define vpint vector<pair<int, int>>
#define mp make_pair

void reader() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

void solve() {

  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int b[n];
  rep(i, 0, n) b[i] = a[i];
  int sum = 0;
  rep(i, 0, n - 1) {
    if (a[i] > a[i + 1]) {
      sum += abs(a[i + 1] - a[i]);
      a[i + 1] = a[i];
    }
  }
  cout << sum;

  cout << "\\n";
}

signed main() {
  reader();
  fastIO();
  long long t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: -11

Fixed submission:

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

#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pint pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define vint vector<int>
#define vpint vector<pair<int, int>>
#define mp make_pair

void reader() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

void solve() {

  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int b[n];
  rep(i, 0, n) b[i] = a[i];
  int sum = 0;
  rep(i, 0, n - 1) {
    if (a[i] > a[i + 1]) {
      sum += abs(a[i + 1] - a[i]);
      a[i + 1] = a[i];
    }
  }
  cout << sum;

  cout << "\\n";
}

signed main() {
  reader();
  fastIO();
  long long t = 1;
  cin >> t;
  while (t--)
    solve();
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define ll long long
#define ld long double
#define pb push_back
#define MOD 1000000007
#define LLMN LONG_LONG_MIN
#define LLMX LONG_LONG_MAX
#define LDMX LDBL_MAX
#define LDMN LDBL_MIN
#define MAX 1000000
#define pll pair<ll, ll>
#define vl vector<ll>
#define vll vector<vector<ll>>
#define vb vector<bool>
#define vbb vector<vector<bool>>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k = 0, arr[1000001];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  for (int i = 1; i < n; i++) {
    k += max(arr[i - 1], arr[i]) - arr[i];
    arr[i] = max(arr[i - 1], arr[i]);
  }
  cout << k << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define ll long long
#define ld long double
#define pb push_back
#define MOD 1000000007
#define LLMN LONG_LONG_MIN
#define LLMX LONG_LONG_MAX
#define LDMX LDBL_MAX
#define LDMN LDBL_MIN
#define MAX 1000000
#define pll pair<ll, ll>
#define vl vector<ll>
#define vll vector<vector<ll>>
#define vb vector<bool>
#define vbb vector<vector<bool>>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k = 0, arr[100001];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  for (int i = 1; i < n; i++) {
    k += max(arr[i - 1], arr[i]) - arr[i];
  }
  cout << k << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> A(N);
  for (int i(0); i < N; ++i)
    cin >> A[i];

  long long ans = 0;
  int MaxNum = 0;
  for (int i = 0; i < N; ++i) {
    MaxNum = max(MaxNum, A[i]);
    ans += MaxNum - A[i];
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

int Count(int &counter, std::vector<int> list, int index) {
  if (index == 0) {
    return Count(counter, list, (index + 1));
  }

  const int current = list[index];
  const int prev = list[index - 1];
  const int diff = prev - current;

  if (diff <= 0) {

  } else {
    list[index] = current + diff;
    counter += diff;
  }

  if (list.size() > static_cast<std::vector<int>::size_type>(index + 1)) {
    return Count(counter, list, (index + 1));
  } else {
    return counter;
  }
}

int main() {
  int len(0), store(0), dai(0);
  std::vector<int> list;

  std::cin >> len;
  for (int i = 0; i < len; ++i) {
    std::cin >> store;
    list.push_back(store);
  }

  std::cout << Count(dai, list, 0) << std::endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define setval(arr, v) memset(arr, v, sizeof(arr));
#define lli long long int
#define ll long long
#define mod 1000000009
#define v vector
#define pb push_back
#define pf pop_front;
#define fi first
#define se second
using namespace std;

void solve() {
  int n;
  cin >> n;
  lli a[n];
  lli sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  lli cnt = 0;
  stack<int> st;
  cnt += a[0];
  st.push(a[0]);
  for (int i = 1; i < n; ++i) {
    if (a[i] < st.top())
      cnt += st.top();
    else {
      st.top() = a[i];
      cnt += st.top();
    }
  }
  lli diff = -sum + cnt;
  cout << diff;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define setval(arr, v) memset(arr, v, sizeof(arr));
#define lli long long int
#define ll long long
#define mod 1000000009
#define v vector
#define pb push_back
#define pf pop_front;
#define fi first
#define se second
using namespace std;

void solve() {
  int n;
  cin >> n;
  lli a[n];
  lli cnt = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[j] < a[i]) {
        cnt += a[i] - a[j];
        a[j] = a[i];
      }
    }
  }
  cout << cnt;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[212345], ans;
int main() {
  cin >> n;
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  ll mx = a[0];
  for (ll i = 1; i < n; ++i) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[112345], ans;
int main() {
  cin >> n;
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  ll mx = a[0];
  for (ll i = 1; i < n; ++i) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main(void) {

  int i, j, n;
  long long sum = 0;

  cin >> n;
  int a[n];

  for (j = 0; j < n; j++) {
    cin >> a[j];
  }

  for (i = 0; i < n - 1; i++) {

    if (a[i] > a[i + 1]) {
      sum = sum + (a[i] - a[i + 1]);
      a[i + 1] = a[i];
    }
  }
  cout << sum << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {

  int i, n, a[n], sum = 0;

  cin >> n;

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

  for (i = 0; i < n - 1; i++) {

    if (a[i] > a[i + 1]) {
      sum = sum + (a[i] - a[i + 1]);
      a[i + 1] = a[i];
    }
  }
  cout << sum << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  long long n;
  std::cin >> n;

  vector<long long> a(n);

  long long ans = 0;

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

  for (int i = 1; i < n; i++) {
    ans += max(ll(0), a[i - 1] - a[i]);
    a[i] += max(ll(0), a[i - 1] - a[i]);
  }

  std::cout << ans << '\\n';
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  vector<int> a(n);

  int ans = 0;

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

  for (int i = 1; i < n; i++) {
    ans += max(0, a[i - 1] - a[i]);
    a[i] += max(0, a[i - 1] - a[i]);
  }

  std::cout << ans << '\\n';
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long int N;
  cin >> N;
  if (N > 1) {
    vector<long int> vec(N, 0);
    for (int i = 0; i < N; i++) {
      cin >> vec.at(i);
    }
    vector<long int> dai(N - 1, 0);
    if (vec.at(0) > vec.at(1)) {
      dai.at(0) = vec.at(0) - vec.at(1);
    }
    for (int j = 0; j < N - 2; j++) {
      if (vec.at(j + 1) + dai.at(j) > vec.at(j + 2)) {
        dai.at(j + 1) = vec.at(j + 1) + dai.at(j) - vec.at(j + 2);
      }
    }
    long int s = 0;
    for (int k = 0; k < N - 1; k++) {
      s += dai.at(k);
    }
    cout << s << endl;
  } else {
    cout << 0 << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  long int N;
  cin >> N;
  if (N > 0) {
    vector<long int> vec(N, 0);
    for (int i = 0; i < N; i++) {
      cin >> vec.at(i);
    }
    vector<long int> dai(N - 1, 0);
    if (vec.at(0) > vec.at(1)) {
      dai.at(0) = vec.at(0) - vec.at(1);
    }
    for (int j = 0; j < N - 2; j++) {
      if (vec.at(j + 1) + dai.at(j) > vec.at(j + 2)) {
        dai.at(j + 1) = vec.at(j + 1) + dai.at(j) - vec.at(j + 2);
      }
    }
    long int s = 0;
    for (int k = 0; k < N - 1; k++) {
      s += dai.at(k);
    }
    cout << s << endl;
  } else {
    cout << 0 << endl;
  }
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int N;
int A[200005];
long long ans;

int main() {
  cin >> N;

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

  long long mn = 1LL * A[0];
  for (int i = 1; i < N; i++) {
    if (A[i] < mn) {
      ans += mn - 1LL * A[i];
    } else {
      mn = 1LL * A[i];
    }
  }

  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int N;
int A[100005];
long long ans;

int main() {
  cin >> N;

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

  long long mn = 1LL * A[0];
  for (int i = 1; i < N; i++) {
    if (A[i] < mn) {
      ans += mn - 1LL * A[i];
    } else {
      mn = 1LL * A[i];
    }
  }

  cout << ans;

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

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

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

  long long ans = 0;
  int MaxNum = 0;
  for (int i = 0; i < N; i++) {
    // MaxNumを使い回すことで計算を抑える
    MaxNum = max(MaxNum, A[i]);
    int Bi = MaxNum;
    ans += Bi - A[i];
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long step_num = 0;

void search(int n, long long *a) {

  if (n <= 0)
    return;

  long long max_h = a[0];
  int max_h_num = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > max_h) {
      max_h = a[i];
      max_h_num = i;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    if (i > max_h_num)
      step_num += max_h - a[i];
    else if (i == max_h_num)
      break;
  }

  search(max_h_num, a);
  return;
}

int main() {
  int n;
  long long a[200000];

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

  search(n, a);
  cout << step_num << endl;

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
long long a[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  long long sum = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      sum += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
long long a[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  long long sum = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      sum += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

int _;

int n, a[200050], maxn;
long long ans;

int main() {
  // for(scanf("%d",&_);_;_--)
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    maxn = max(maxn, a[i]);
    ans += max(maxn - a[i], 0);
  }
  printf("%lld", ans);
  return 0;
}

Error: WA

Fixed submission:

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

int _;

int n, a[100050], maxn;
long long ans;

int main() {
  // for(scanf("%d",&_);_;_--)
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    maxn = max(maxn, a[i]);
    ans += max(maxn - a[i], 0);
  }
  printf("%lld", ans);
  return 0;
}