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



#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(c) (c).begin(), (c).end()
#define sortall(x) sort(all(x))
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define sz(a) int((a).size())
#define mp make_pair
#define ll long long
#define F first
#define S second
#define PI 3.1415926535897932384626
// Input macros
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)

// Others
#define test(t)                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define endl "\\n"

using namespace std;

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

// const int mod = 1000000007;
// const int INF = 1e9+5;

int main() {

  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  ll n;
  cin >> n;

  vector<ll> heights(n, 0);

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

  ll ans = 0;
  ll m_height = heights[0];

  // 4 4 5 6 2

  for (int i = 1; i < n; i++) {
    if (m_height > heights[i]) {
      ans += m_height - heights[i];
      heights[i] = m_height;
    } else {
      m_height = heights[i];
    }
  }

  cout << ans << endl;
}


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


Fixed submission:



#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(c) (c).begin(), (c).end()
#define sortall(x) sort(all(x))
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define sz(a) int((a).size())
#define mp make_pair
#define ll long long
#define F first
#define S second
#define PI 3.1415926535897932384626
// Input macros
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)

// Others
#define test(t)                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define endl "\\n"

using namespace std;

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

// const int mod = 1000000007;
// const int INF = 1e9+5;

int main() {

  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

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

  vector<int> heights(n, 0);

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

  ll ans = 0;
  ll m_height = heights[0];

  // 4 4 5 6 2

  for (int i = 1; i < n; i++) {
    if (m_height > heights[i]) {
      ans += m_height - heights[i];
      heights[i] = m_height;
    } else {
      m_height = heights[i];
    }
  }

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

#include <iostream>
#define N 200005
using namespace std;
long long a[N];
int n;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long di = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++)
    if (a[i] < di)
      ans += di - a[i];
    else
      di = a[i];
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#define N 100000
using namespace std;
int a[N];
int n;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int di = 0;
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (a[i] < di)
      ans += di - a[i];
    else
      di = a[i];
  cout << ans;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, i1, n) for (int i = (int)(i1); i < (int)(n); i++)
#define ll long long
#define vi vector<int>
#define vll vector<long long>

int main() {
  int n = 0;
  cin >> n;
  vi vec(n);
  rep(i, 0, n) { cin >> vec.at(i); }
  ll wa = 0;
  int max = vec.at(0);

  rep(i, 0, n) {
    if (max > vec.at(i)) {
      wa += max - vec.at(i);
    } else {
      max = vec.at(i);
    }
  }
  cout << wa;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, i1, n) for (int i = (int)(i1); i < (int)(n); i++)
#define ll long long
#define vi vector<int>
#define vll vector<long long>

int main() {
  int n = 0;
  cin >> n;
  vi vec(n);
  rep(i, 0, n) { cin >> vec.at(i); }
  ll wa = 0;

  if (vec.at(0) > vec.at(1)) {
    wa += vec.at(0) - vec.at(1);
  }
  rep(i, 1, n) {
    if (vec.at(i - 1) > vec.at(i)) {
      wa += vec.at(i - 1) - vec.at(i);
    }
  }
  cout << wa;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)

int main() {
  ll n;
  std::cin >> n;
  std::vector<ll> a(n);
  rep(i, n) { std::cin >> a[i]; }
  ll sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[i] > a[j]) {
        sum += a[i] - a[j];
        a[j] += a[i] - a[j];
      } else {
        break;
      }
    }
  }
  std::cout << sum << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)

int main() {
  ll n;
  std::cin >> n;
  std::vector<ll> a(n);
  rep(i, n) { std::cin >> a[i]; }
  ll sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[i] > a[j]) {
        sum += a[i] - a[j];
        a[j] += a[i] - a[j];
      }
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

const int kMaxN = 2000001;

long long n, ans;
int a[kMaxN];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= n; i++) {
    ans += max(a[i - 1] - a[i], 0);
    a[i] = max(a[i], a[i - 1]);
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

const int kMaxN = 10;

long long n, ans;
int a[kMaxN];

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

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


Error: WA

Fixed submission:

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

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

#pragma region typedef
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
#pragma endregion

#pragma region define
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#pragma endregion

#pragma region const
const ld PI = 3.141592653589793;
#pragma endregion

#pragma region function
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T &b, T &c) {
  if (b < a) {
    a = b;
    c++;
    return 1;
  }
  return 0;
}
int sort_3int(int a, int b, int c) {
  if (a > b) {
    swap(a, b);
  }
  if (a > c) {
    swap(a, c);
  }
  if (b > c) {
    swap(b, c);
  }
  return true;
}
#pragma endregion

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;

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

  return 0;
}


Error: -6
/usr/include/c++/12/debug/vector:442:
In function:
    std::debug::vector<_Tp, _Allocator>::reference std::debug::vector<_Tp, 
    _Allocator>::operator[](size_type) [with _Tp = long long int; _Allocator 
    = std::allocator<long long int>; reference = long long int&; size_type = 
    long unsigned int]

Error: attempt to subscript container with out-of-bounds index 5, but 
container only holds 5 elements.

Objects involved in the operation:
    sequence "this" @ 0x7fff4b0fab80 {
      type = std::debug::vector<long long, std::allocator<long long> >;
    }


Fixed submission:

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

#pragma region typedef
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
#pragma endregion

#pragma region define
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#pragma endregion

#pragma region const
const ld PI = 3.141592653589793;
#pragma endregion

#pragma region function
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T &b, T &c) {
  if (b < a) {
    a = b;
    c++;
    return 1;
  }
  return 0;
}
int sort_3int(int a, int b, int c) {
  if (a > b) {
    swap(a, b);
  }
  if (a > c) {
    swap(a, c);
  }
  if (b > c) {
    swap(b, c);
  }
  return true;
}
#pragma endregion

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int ans = 0;

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

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

/*  CF_CC_
            4U7H0R:_C4551DY */

#include <bits/stdc++.h>
#include <fstream>

using namespace std;

typedef long long ll;

#define IC                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(a, b, c) for (ll a = b; a < c; a++)
#define lb lower_bound
#define pii pair<int, int>
#define vec vector<int>
#define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define len length()
#define rev reverse
#define con continue

const int mxi = 2e5 + 666;
const ll mod = 1e9;
const ll inf = 1e18;

ll n, c, a[mxi];

int main() {
  IC cin >> n;
  rep(i, 0, n) {
    cin >> a[i];
    if (i > 0 && a[i] < a[i - 1]) {
      c += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << c;
}


Error: WA

Fixed submission:

/*  CF_CC_
            4U7H0R:_C4551DY */

#include <bits/stdc++.h>
#include <fstream>

using namespace std;

typedef long long ll;

#define IC                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(a, b, c) for (ll a = b; a < c; a++)
#define lb lower_bound
#define pii pair<int, int>
#define vec vector<int>
#define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define len length()
#define rev reverse
#define con continue

const int mxi = 1e5 + 666;
const ll mod = 1e9;
const ll inf = 1e18;

ll n, c, a[mxi];

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

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

long long a[10000005], ans = 0;

int main() {
  long long n;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  printf("%lld", ans);
}

Error: WA

Fixed submission:

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

long long a[100005], ans = 0;

int main() {
  long long n;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  printf("%lld", ans);
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#define ll long long
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define FOR(i, stop) for (int i = 0; i < (stop); i++)
#define PI 3.141592653589793
using namespace std;

int main() {

  int n;
  cin >> n;
  ll a[n];
  FOR(i, n) cin >> a[i];

  ll ans = 0;
  FOR(i, n - 1) {
    if (a[i] > a[i + 1]) {
      ans += a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
  }

  cout << ans << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#define ll long long
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define FOR(i, stop) for (int i = 0; i < (stop); i++)
#define PI 3.141592653589793
using namespace std;

int main() {

  int n;
  cin >> n;
  int a[n];
  FOR(i, n) cin >> a[i];

  int ans = 0;
  FOR(i, n - 1) {
    if (a[i] > a[i + 1]) {
      ans = a[i + 1] - a[i];
      a[i + i] = a[i];
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
ll v[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  ll ans = 0;
  ll maxx = v[1];
  for (int i = 2; i <= n; i++) {
    if (v[i] > maxx) {
      maxx = v[i];
    } else {
      ans += (maxx - v[i]);
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
int main() {
  int n;
  vector<ll> v(n + 10);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  ll ans = 0;
  ll maxx = v[1];
  for (int i = 2; i <= n; i++) {
    if (v[i] > maxx) {
      maxx = v[i];
    } else {
      ans += (maxx - v[i]);
    }
  }
  cout << ans << endl;

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

#include <iostream>

using namespace std;

int main() {

  int n;
  int a[200000];
  long long int sum = 0;

  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 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  int n;
  int a[100000];
  int sum = 0;

  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 << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, largest, high;
  long long int ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> high;
    if (largest > high)
      ans += largest - high;
    if (high > largest)
      largest = high;
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
vector<ll> v(n);
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  ll high = 0, ans = 0;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < n; i++) {
    if (high <= v[i])
      high = v[i];
    else
      ans += high - v[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

//   ┏━━┓┏━━┓┏┓┏┓┏┳━┳┓┏━━┓┏━┳┓┏━━┓     ┏┳┓┏┳┓┏━┳━┓┏━━┓┏━┓
//   ┃┏┓┃┃━━┫┃┗┛┃┃┃┃┃┃┃┏┓┃┃┃┃┃┗┃┃┛     ┃┏┛┃┃┃┃┃┃┃┃┃┏┓┃┃╋┃
//   ┃┣┫┃┣━━┃┃┏┓┃┃┃┃┃┃┃┣┫┃┃┃┃┃┏┃┃┓     ┃┗┓┃┃┃┃┃┃┃┃┃┣┫┃┃┓┫
//   ┗┛┗┛┗━━┛┗┛┗┛┗━┻━┛┗┛┗┛┗┻━┛┗━━┛     ┗┻┛┗━┛┗┻━┻┛┗┛┗┛┗┻┛
#include "bits/stdc++.h"
using namespace std;
#define mod 1e9 + 7
#define FOR(a, c) for (int a = 0; a < c; a++)
#define FORL(a, b, c) for (int a = b; a < c; a++)
#define FORR(a, b, c) for (int a = b; a > c; a--)
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define PB push_back
#define POB pop_back
#define MP make_pair
int maxm(int a[], int i) {
  int ma = INT_MIN;
  for (int j = 0; j < i; j++) {
    if (a[j] > ma) {
      ma = a[j];
    }
  }
  return ma;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int a[n];
  ll ma = 0, c = 0;
  ;
  FOR(i, n) {
    cin >> a[i];
    if (a[i] > ma) {
      ma = a[i];
    } else {
      c += ma - a[i];
    }
  }
  cout << c;
}
// By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati.
//   ╔══╗╔══╗╔══╗     ╔══╗╔╦╗╔╦═╦╗╔══╗╔╗╔╗╔══╗╔══╗╔══╗
//   ╚║║╝╚║║╝╚╗╔╝     ║╔═╣║║║║║║║║║╔╗║║╚╝║║╔╗║╚╗╔╝╚║║╝
//   ╔║║╗╔║║╗─║║─     ║╚╗║║║║║║║║║║╠╣║║╔╗║║╠╣║─║║─╔║║╗
//   ╚══╝╚══╝─╚╝─     ╚══╝╚═╝╚═╩═╝╚╝╚╝╚╝╚╝╚╝╚╝─╚╝─╚══╝

Error: TLE

Fixed submission:

//   ┏━━┓┏━━┓┏┓┏┓┏┳━┳┓┏━━┓┏━┳┓┏━━┓     ┏┳┓┏┳┓┏━┳━┓┏━━┓┏━┓
//   ┃┏┓┃┃━━┫┃┗┛┃┃┃┃┃┃┃┏┓┃┃┃┃┃┗┃┃┛     ┃┏┛┃┃┃┃┃┃┃┃┃┏┓┃┃╋┃
//   ┃┣┫┃┣━━┃┃┏┓┃┃┃┃┃┃┃┣┫┃┃┃┃┃┏┃┃┓     ┃┗┓┃┃┃┃┃┃┃┃┃┣┫┃┃┓┫
//   ┗┛┗┛┗━━┛┗┛┗┛┗━┻━┛┗┛┗┛┗┻━┛┗━━┛     ┗┻┛┗━┛┗┻━┻┛┗┛┗┛┗┻┛
#include "bits/stdc++.h"
using namespace std;
#define mod 1e9 + 7
#define FOR(a, c) for (int a = 0; a < c; a++)
#define FORL(a, b, c) for (int a = b; a < c; a++)
#define FORR(a, b, c) for (int a = b; a > c; a--)
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define PB push_back
#define POB pop_back
#define MP make_pair
int maxm(int a[], int i) {
  int ma = INT_MIN;
  for (int j = 0; j < i; j++) {
    if (a[j] > ma) {
      ma = a[j];
    }
  }
  return ma;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int a[n];
  FOR(i, n) { cin >> a[i]; }
  ll c = 0;
  FORL(i, 1, n) {
    int ma = maxm(a, i);
    if (a[i] < ma) {
      c += (ma - a[i]);
    }
  }
  cout << c;
}
// By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati.
//   ╔══╗╔══╗╔══╗     ╔══╗╔╦╗╔╦═╦╗╔══╗╔╗╔╗╔══╗╔══╗╔══╗
//   ╚║║╝╚║║╝╚╗╔╝     ║╔═╣║║║║║║║║║╔╗║║╚╝║║╔╗║╚╗╔╝╚║║╝
//   ╔║║╗╔║║╗─║║─     ║╚╗║║║║║║║║║║╠╣║║╔╗║║╠╣║─║║─╔║║╗
//   ╚══╝╚══╝─╚╝─     ╚══╝╚═╝╚═╩═╝╚╝╚╝╚╝╚╝╚╝╚╝─╚╝─╚══╝
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

#define PI 3.14159265358979323846264338327950L
const int MOD = 1000000007;

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

  int n;
  cin >> n;

  map<int, int> v;
  vi vec(n);
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
    v[vec[i]] = 0;
  }

  ll count = 0;

  vi temp(vec);
  sort(all(temp));

  if (!is_sorted(vec.begin(), vec.end())) {
    int j = n - 1;
    for (int i = n - 1; i >= 0; --i) {
      if (v[temp[i]] == 0) {
        while (vec[j] != temp[i]) {
          count += (temp[i] - vec[j]);
          v[vec[j]] = 1;
          vec[j] = temp[i];
          --j;
        }
        --j;
      }
    }
  }
  cout << count;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

#define PI 3.14159265358979323846264338327950L
const int MOD = 1000000007;

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

  int n;
  cin >> n;

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

  auto it = v.end();
  ll count = 0;
  while (it != v.begin()) {
    auto temp = it;
    it = max_element(v.begin(), it);
    for (auto i = it; i != temp; ++i) {
      count += (*it - *i);
      *i = *it;
    }
  }

  cout << count;
}
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: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define forn(i, n) for (int i = 1; i <= n; i++)
#define ford(i, n) for (int i = n; i >= 0; i--)
#define REP(i, a, n) for (int i = a; i < n; i++)
#define foreach(itr, c)                                                        \\
  for (__typeof((c).begin(), (c).end()) itr = c.begin(); itr != c.end(); itr++)
#define fill(a, b) memset(a, b, sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<char> vs;
typedef vector<pi> vpi;

using namespace std;

int main() {
  int n;
  vi a(n);
  int temp = 0;
  int ans = 0;

  cin >> n;

  rep(i, n) {
    cin >> a[i];
    if (i > 0) {
      if (a[i] < a[i - 1]) {
        temp = a[i - 1] - a[i];
        ans += temp;
        a[i] += temp;
      }
    }
  }
  cout << ans;
}