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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y)                                                            \\
  { x = max(x, y); }
#define chmin(x, y)                                                            \\
  { x = min(x, y); }

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a.at(i); }
  ll ans = 0;
  int maxnum = 0;
  rep(i, n) {
    chmax(maxnum, a.at(i));
    ans += maxnum - a.at(i);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y)                                                            \\
  { x = max(x, y); }

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a.at(i); }
  int min = 0;
  rep(i, n - 1) {
    if (a.at(i) > a.at(i + 1)) {
      while (a.at(i) > a.at(i + 1)) {
        a.at(i + 1)++;
        min++;
      }
    }
  }
  cout << min << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {

  int N;
  long int A[200001], ans = 0;
  cin >> N;
  rep(i, N) cin >> A[i];

  for (int 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 <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {

  int N;
  long int A[20000], ans = 0;
  cin >> N;
  rep(i, N) cin >> A[i];

  for (int 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;

const int N = 200010;

int a[N];
int n, m;

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

  long long s = 0;
  for (int i = 1; i < n; i++) {
    int x = a[i + 1] - a[i];
    if (x < 0)
      a[i + 1] += -x;
    s += x < 0 ? -x : 0;
  }
  cout << s << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

const int N = 100010;

int a[N];
int n, m;

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

  int s = 0;
  for (int i = 1; i < n; i++) {
    int x = a[i + 1] - a[i];
    if (x < 0)
      a[i + 1] += -x;
    s += x < 0 ? -x : 0;
  }
  cout << s << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
ll a[202020], n, ans;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i) {
      if (a[i] < a[i - 1]) {
        ans += a[i - 1] - a[i];
        a[i] = a[i - 1];
      }
    }
  }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
ll a[101010], n, ans;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i) {
      if (a[i] < a[i - 1]) {
        ans += a[i - 1] - a[i];
        a[i] = a[i - 1];
      }
    }
  }
  cout << ans << '\\n';
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

int N;
long long A[200005];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", A + i);
  }

  long long m = A[0];
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += max(0LL, m - A[i]);
    m = max(m, A[i]);
  }
  printf("%lld\\n", ans);
}


Error: 0

Fixed submission:

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

int N;
long long A[20005];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", A + i);
  }

  long long m = A[0];
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += max(0LL, m - A[i]);
    m = max(m, A[i]);
  }
  printf("%lld\\n", ans);
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, r = 0;
  cin >> n;
  ll a[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]) {
      ll k = a[i] - a[i + 1];
      a[i + 1] = a[i];
      r = r + k;
    }
  }
  cout << r << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, c;
  cin >> n;
  ll a[n];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  c = 0;
  for (ll i = 1; i <= n; i++) {
    if (a[i] != a[i + 1]) {
      c++;
    }
  }
  cout << c - 1 << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int intinput() {
  int x;
  cin >> x;
  return x;
}
vector<int> forintinput(int n) {
  vector<int> x(200000);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  return x;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  a = forintinput(n);
  int Max = 0;
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > Max) {
      Max = a[i];
    }
    if (a[i] < Max) {
      cnt += (Max - a[i]);
    }
  }
  cout << cnt << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int intinput() {
  int x;
  cin >> x;
  return x;
}
vector<int> forintinput(int n) {
  vector<int> x(100000);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  return x;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  a = forintinput(n);
  int Max = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > Max) {
      Max = a[i];
    }
    if (a[i] < Max) {
      cnt += (Max - a[i]);
    }
  }
  cout << cnt << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main(void) {
  int n;
  long long ans;
  int h[200010];

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  ans = 0;
  for (int i = 1; i < n; i++) {
    if (h[i - 1] > h[i]) {
      ans += h[i - 1] - h[i];
      h[i] = h[i - 1];
    }
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {
  int n, ans;
  int h[100010];

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  ans = 0;
  for (int i = 1; i < n; i++) {
    if (h[i - 1] > h[i]) {
      ans += h[i - 1] - h[i];
      h[i] = h[i - 1];
    }
  }
  cout << ans << endl;

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

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

int main() {
  int n;

  cin >> n;
  vector<ll> a(n + 1);
  a.at(0) = 0;
  ll sum = 0;
  ll c;
  for (int i = 1; i < n + 1; i++) {
    cin >> a.at(i);
    if (a.at(i - 1) > a.at(i)) {

      c = a.at(i - 1) - a.at(i);
      sum += c;
      a.at(i) = a.at(i - 1);
    }
  }

  cout << sum;

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int main() {
  int n;
  vector<ll> a(n + 1);
  cin >> n;
  a.at(0) = 0;
  ll sum = 0;
  ll c;
  for (int i = 1; i < n + 1; i++) {
    cin >> a.at(i);
    if (a.at(i - 1) > a.at(i)) {

      c = a.at(i - 1) - a.at(i);
      sum += c;
      a.at(i) = a.at(i - 1);
    }
  }

  cout << sum;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 100005;
int a[2 * N];
void solve() {
  int n, k = 1, m = 0, i = 1, j, x, y, l, r, p, q, z;
  ll ans = 0;
  string s;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
    ans += m - a[i];
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 100005;
ll dp[3003][3004] = {};
void solve() {
  int n, k = 1, m = 0, i = 1, j, x, y, l, r, p, q, z, a[3000];
  ll ans = 0;
  string s;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
    ans += m - a[i];
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;
int f() {
  LL n;
  LL a, b;
  LL ans = 0;
  LL dif = 0;
  cin >> n;
  cin >> a;
  forn(i, n - 1) {
    cin >> b;
    dif = a - b;
    if (dif > 0) {
      ans += dif;
      a = b + dif;
    } else
      a = b;
  }
  cout << ans << endl;
  return 0;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  f();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;
int f() {
  int n;
  int a, b;
  int ans = 0;
  int dif = 0;
  cin >> n;
  cin >> a;
  forn(i, n - 1) {
    cin >> b;
    dif = a - b;
    if (dif > 0) {
      ans += dif;
      a = b + dif;
    } else
      a = b;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  f();
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

int n, a[200000];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    long long up = max(0, a[i - 1] - a[i]);
    a[i] += up, ans += up;
  }
  cout << ans << '\\n';
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

int n, a[200000];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long ans = 0;
  for (int i = n - 2; i >= 0; i++) {
    long long up = max(0, a[i + 1] - a[i]);
    a[i] += up, ans += up;
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

/**
 * CODE
 * BY
 * VIKAS VERMA
 *
 * $$Always Check for Constraints
 */
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, n) for (int i = 1; k <= n; i++)
#define f_range(l, r) for (int i = l; i < r; i++)
#define jaldi ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define PI 3.1415926535897932384626
#define endl "\\n"
#define F first
#define S second
#define PB push_back
#define MP make_pair
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef long long ll;
//=======================
const int MOD = 1'000'000'007;
const int N = 2e6 + 13;
//=======================
vi g[N];
ll a[N];
//=======================

void solve() {
  int n;
  cin >> n;
  fo(i, n) { cin >> a[i]; }
  ll count = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      ll diff = a[i - 1] - a[i];
      a[i] += diff;
      count += diff;
      ;
    }
  }
  cout << count << endl;
}

int main() {
  jaldi
      // #ifndef ONLINE_JUDGE
      //       freopen("input.txt", "r", stdin);
      //   freopen("output.txt", "w", stdout);
      // #endif
      int t = 1;

  // cin >> t;

  while (t--) {
    solve();
  }

  return 0;
}

Error: WA

Fixed submission:

/**
 * CODE
 * BY
 * VIKAS VERMA
 *
 * $$Always Check for Constraints
 */
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, n) for (int i = 1; k <= n; i++)
#define f_range(l, r) for (int i = l; i < r; i++)
#define jaldi ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define PI 3.1415926535897932384626
#define endl "\\n"
#define F first
#define S second
#define PB push_back
#define MP make_pair
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef long long ll;
//=======================
const int MOD = 1'000'000'007;
const int N = 2e6 + 13;
//=======================
vi g[N];
ll a[N];
//=======================

void solve() {
  int n;
  cin >> n;
  fo(i, n) { cin >> a[i]; }
  ll count = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      ll diff = a[i - 1] - a[i];
      a[i] += diff;
      count += diff;
      ;
    }
  }
  cout << count << endl;
}

int main() {
  jaldi
      // #ifndef ONLINE_JUDGE
      //       freopen("input.txt", "r", stdin);
      //   freopen("output.txt", "w", stdout);
      // #endif
      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;
#define ALL(obj) (obj).begin(), (obj).end()
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007, MAX = 200005, INF = 1 << 30;

int main() {
  ll n;
  ll a[10001000];

  cin >> n;
  rep(i, n) { cin >> a[i]; }

  ll ans = 0;
  ll high = a[0];

  for (int i = 1; i < n; i++) {
    if (high > a[i]) {
      ans += high - a[i];
    }

    else if (high < a[i]) {
      high = a[i];
    }
  }

  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007, MAX = 200005, INF = 1 << 30;

int main() {
  ll n;
  ll a[n];

  cin >> n;
  rep(i, n) { cin >> a[i]; }

  ll ans = 0;
  ll high = a[0];

  for (int i = 1; i < n; i++) {
    if (high > a[i]) {
      ans += high - a[i];
    }

    else if (high < a[i]) {
      high = a[i];
    }
  }

  cout << ans << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  long long ans = 0;
  int Max = 0;
  for (int i = 0; i < N; i++) {
    Max = max(Max, v[i]);
    int Bi = Max;
    ans += Bi - v[i];
  }
  cout << ans << endl;
}


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


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v.at(i);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    while (v.at(i) > v.at(i + 1)) {
      v.at(i + 1) += 1;
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll sum = 0;
  ll best = a[0];
  for (ll i = 1; i < n; i++) {
    if (a[i] < best) {
      sum += abs(best - a[i]);
      // cout<<sum<<"**";
    }
    best = max(best, a[i]);
  }
  cout << sum << "\\n";
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

// Getting TlE
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define ll long long
#define dd double
using namespace std;
const ll MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  ll i, j;
  ll s = 0;
  ll max = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > max) {
      max = a[i];
    }

    if (max > a[i]) {
      ll sub = max - a[i];
      s = s + sub;
    }
  }
  cout << s << endl;

  return 0;
}

Error: TLE

Fixed submission:

// Getting TlE
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define ll long long
#define dd double
using namespace std;
const ll MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  ll i, j;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  ll s = 0;
  for (i = n - 1; i > 0; i--) {
    ll d = *max_element(a, a + i);
    if (d > a[i]) {
      ll sub = d - a[i];
      s = s + sub;
      a[i] = a[i] + sub;
    }
  }

  // cout<<endl;
  // for(i=0;i<n;i++){
  // cout<<a[i]<<" ";
  //}
  // cout<<endl;
  cout << s << endl;

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

#include <cstdio>
int n, a[200003], max;
long long ans; // ans要开long long
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  max = a[1]; // max初值为a[1]
  for (int i = 2; i <= n; i++) {
    if (max > a[i])
      ans += max - a[i];
    else
      max = a[i];
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  ll n, sum = 0;
  ll a[n + 5];

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

  for (int i = 1; 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;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {

  int n;
  cin >> n;
  int a[n];
  long long int ans = 0;

  for (int i = 0; i < n; i++) {

    cin >> a[i];
  }

  for (int i = 1; i < n; i++) {

    if (a[i] < a[i - 1]) {

      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  int n;
  cin >> n;
  int a[n];
  long long int ans = 0;

  for (int i = 0; i < n; i++) {

    cin >> a[i];
  }
  for (int i = n - 1; i > -1; i--) {

    for (int j = i + 1; j < n; j++) {

      if (a[j] < a[i]) {

        ans += a[i] - a[j];
        a[j] = a[i];
      } else {

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

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

int main() {
  ll n, ans = 0, mx = 0, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x < mx)
      ans += (mx - x);

    mx = max(mx, x);
  }
  cout << ans << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, cnt = 0, temp;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    while (a[i] < a[i - 1] && i != 0) {
      a[i]++;
      cnt++;
    }
  }
  cout << cnt << '\\n';

  return 0;
}