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

Buggy submission (status: Runtime Error):
#define boost
#ifdef boost
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#endif
#include <bits/stdc++.h>
#define int long long
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
#define __ read()
using namespace std;
const int maxn(1e5 + 5);
void rda(int *a, int n) {
  for (int i = 1; i <= n; ++i)
    a[i] = __;
}
void print(int *a, int n) {
  for (int i = 1; i <= n; ++i)
    printf("%lld ", a[i]);
  puts("");
}
int n, a[maxn];
signed main() {
  n = __;
  rda(a, n);
  int ans = 0;
  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;
}


Fixed submission:
#define boost
#ifdef boost
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#endif
#include <bits/stdc++.h>
#define int long long
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
#define __ read()
using namespace std;
const int maxn(2e5 + 5);
void rda(int *a, int n) {
  for (int i = 1; i <= n; ++i)
    a[i] = __;
}
void print(int *a, int n) {
  for (int i = 1; i <= n; ++i)
    printf("%lld ", a[i]);
  puts("");
}
int n, a[maxn];
signed main() {
  n = __;
  rda(a, n);
  int ans = 0;
  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;
}
Problem p02578 (C++)

Buggy submission (status: 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;
}

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 (status: 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) {
    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;
}

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 - 1) {
    if (hei[i] > hei[i + 1]) {
      ans += hei[i] - hei[i + 1];
      hei[i + 1] = hei[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int64_t u;
  int sum = 0;
  int A[100000000];
  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;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int64_t u;
  int64_t sum = 0;
  int A[1000000];
  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 (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int64_t u;
  int64_t sum = 0;
  int A[100000000];
  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;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int64_t u;
  int64_t sum = 0;
  int A[200000];
  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 (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll t, n, k, a[maxn], res;
int main() {
  // freopen("in.txt","r",stdin);
  cin >> t;
  for (int i = 0; i < t; i++)
    cin >> a[i];
  for (int i = 0; i < t - 1; i++) {
    if (a[i] > a[i + 1]) {
      res += a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll t, n, k, a[maxn], res;
int main() {
  // freopen("in.txt","r",stdin);
  cin >> t;
  for (int i = 0; i < t; i++)
    cin >> a[i];
  for (int i = 0; i < t - 1; i++) {
    if (a[i] > a[i + 1]) {
      res += a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02578 (C++)

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

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

  vector<long long> A(N);

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

  long long cnt = 0;

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

  cout << cnt << endl;
}


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

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

  vector<long long> A(N);

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

  long long cnt = 0;

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

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

Buggy submission (status: Runtime Error):
#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);
}


Fixed submission:
#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);
}
Problem p02578 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[114514];
int main() {
  int cnt = 0;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 2; i <= n; i++) {
    int added = a[i - 1] - a[i];
    if (added < 0)
      added = 0;
    a[i] += added;
    cnt += added;
  }
  cout << cnt;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[11114514];
signed main() {
  int cnt = 0;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 2; i <= n; i++) {
    int added = a[i - 1] - a[i];
    if (added < 0)
      added = 0;
    a[i] += added;
    cnt += added;
  }
  cout << cnt;
  return 0;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
/*
  The Island Was Silent before.
  .....
  And One day again it became Silent.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long
#define modd(a, b) ((a + 2 * b) % b)
#define debug(a) cout << #a << ": " << (a) << "\n"
#define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#define ffe                                                                    \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);

int main() {
  ioso
#ifndef ONLINE_JUDGE
      ffe
#endif
          ll n;
  cin >> n;
  int a[100010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll s = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      // cout << a[i-1]-a[i] << endl;
      s += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << s << endl;
}

Fixed submission:
/*
  The Island Was Silent before.
  .....
  And One day again it became Silent.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long
#define modd(a, b) ((a + 2 * b) % b)
#define debug(a) cout << #a << ": " << (a) << "\n"
#define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#define ffe                                                                    \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);

int main() {
  ioso
#ifndef ONLINE_JUDGE
      ffe
#endif
          ll n;
  cin >> n;
  int a[200010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll s = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      // cout << a[i-1]-a[i] << endl;
      s += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << s << endl;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll arr[100000];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (arr[i] > arr[i + 1]) {
      ans += arr[i] - arr[i + 1];
      arr[i + 1] = arr[i];
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll arr[1000000];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (arr[i] > arr[i + 1]) {
      ans += arr[i] - arr[i + 1];
      arr[i + 1] = arr[i];
    }
  }
  cout << ans;
}
Problem p02578 (C++)

Buggy submission (status: 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];
  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.
//   ╔══╗╔══╗╔══╗     ╔══╗╔╦╗╔╦═╦╗╔══╗╔╗╔╗╔══╗╔══╗╔══╗
//   ╚║║╝╚║║╝╚╗╔╝     ║╔═╣║║║║║║║║║╔╗║║╚╝║║╔╗║╚╗╔╝╚║║╝
//   ╔║║╗╔║║╗─║║─     ║╚╗║║║║║║║║║║╠╣║║╔╗║║╠╣║─║║─╔║║╗
//   ╚══╝╚══╝─╚╝─     ╚══╝╚═╝╚═╩═╝╚╝╚╝╚╝╚╝╚╝╚╝─╚╝─╚══╝

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];
  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.
//   ╔══╗╔══╗╔══╗     ╔══╗╔╦╗╔╦═╦╗╔══╗╔╗╔╗╔══╗╔══╗╔══╗
//   ╚║║╝╚║║╝╚╗╔╝     ║╔═╣║║║║║║║║║╔╗║║╚╝║║╔╗║╚╗╔╝╚║║╝
//   ╔║║╗╔║║╗─║║─     ║╚╗║║║║║║║║║║╠╣║║╔╗║║╠╣║─║║─╔║║╗
//   ╚══╝╚══╝─╚╝─     ╚══╝╚═╝╚═╩═╝╚╝╚╝╚╝╚╝╚╝╚╝─╚╝─╚══╝
Problem p02578 (C++)

Buggy submission (status: 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;
}

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 (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define fix(a)                                                                 \
  cout << setprecision(a);                                                     \
  cout << fixed;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define clr(a) memset(a, 0, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((int)(x).size())
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef long double ld;

int main() {
  fast;
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  ll ans = 0;
  for (int it = n - 1; it > 0; it--) {
    int idx = max_element(arr.begin(), arr.begin() + (it + 1)) - arr.begin();
    while (it != idx) {
      ans += arr[idx] - arr[it];
      it--;
    }
  }
  cout << ans;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define fix(a)                                                                 \
  cout << setprecision(a);                                                     \
  cout << fixed;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define clr(a) memset(a, 0, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((int)(x).size())
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef long double ld;

int main() {
  fast;
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    int del = 0;
    if (arr[i] > arr[i + 1])
      del = arr[i] - arr[i + 1];
    ans += del;
    arr[i + 1] += del;
  }
  cout << ans;
}
Problem p02578 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long;
#define ld long double
#define pb push_back
const int mod = 1e9 + 7;
const int mxN = 2e6 + 3;

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

  long long sum = 0;
  for (int i = 0, maxnum = 0; i < n;) {
    maxnum = max(a.at(i), maxnum);
    sum += maxnum - a.at(i);
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long;
#define ld long double
#define pb push_back
const int mod = 1e9 + 7;
const int mxN = 2e6 + 3;

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

  long long sum = 0;
  for (int i = 0, maxnum = 0; i < n; i++) {
    maxnum = max(a.at(i), maxnum);
    sum += maxnum - a.at(i);
  }
  cout << sum << endl;
  return 0;
}
Problem p02578 (C++)

Buggy submission (status: 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];
      }
    }
  }
  std::cout << sum << std::endl;
  return 0;
}


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];
      } else {
        break;
      }
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02578 (C++)

Buggy submission (status: 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 = 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;
}


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 = 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;
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#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);
}

Fixed submission:
#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);
}
Problem p02578 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long int n, a[100000000], sum = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long 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 << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long int n, a[10000000], sum = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long 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 << "\n";
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>

using namespace std;

#define int long long

const int N = 1e3 + 10;

int dp[N][N];
char s[N][N];
bool vis[N][N];
int h, w, ch, cw;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};

void dijkstra() {
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, {ch, cw}});
  while (!q.empty()) {
    auto z = q.top();
    int x = z.second.first;
    int y = z.second.second;
    q.pop();
    if (!vis[x][y]) {
      vis[x][y] = 1;
    } else if (dp[x][y] < -z.first) {
      continue;
    }
    dp[x][y] = -z.first;
    for (int i = 0; i < 5; ++i) {
      for (int j = 0; j < 5; ++j) {
        int u = x - 2 + i;
        int v = y - 2 + j;
        if (u < 1 || u > h || v < 1 || v > w || s[u][v] == '#') {
          continue;
        }
        if (dp[u][v] > dp[x][y] + (abs(u - x) + abs(v - y) > 1)) {
          q.push({-(dp[x][y] + (abs(u - x) + abs(v - y) > 1)), {u, v}});
        }
      }
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  for (int i = 1; i <= h; ++i) {
    cin >> s[i] + 1;
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      dp[i][j] = 1e18;
    }
  }
  dijkstra();
  if (dp[dh][dw] == 1e18) {
    cout << -1;
  } else {
    cout << dp[dh][dw];
  }
  return 0;
}


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

using namespace std;

#define int long long

const int N = 1e3 + 10;

int dp[N][N];
char s[N][N];
bool vis[N][N];
int h, w, ch, cw;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};

void dijkstra() {
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, {ch, cw}});
  while (!q.empty()) {
    auto z = q.top();
    int x = z.second.first;
    int y = z.second.second;
    q.pop();
    if (!vis[x][y]) {
      vis[x][y] = 1;
    } else if (dp[x][y] <= -z.first) {
      continue;
    }
    dp[x][y] = -z.first;
    for (int i = 0; i < 5; ++i) {
      for (int j = 0; j < 5; ++j) {
        int u = x - 2 + i;
        int v = y - 2 + j;
        if (u < 1 || u > h || v < 1 || v > w || s[u][v] == '#') {
          continue;
        }
        if (dp[u][v] > dp[x][y] + (abs(u - x) + abs(v - y) > 1)) {
          q.push({-(dp[x][y] + (abs(u - x) + abs(v - y) > 1)), {u, v}});
        }
      }
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  for (int i = 1; i <= h; ++i) {
    cin >> s[i] + 1;
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      dp[i][j] = 1e18;
    }
  }
  dijkstra();
  if (dp[dh][dw] == 1e18) {
    cout << -1;
  } else {
    cout << dp[dh][dw];
  }
  return 0;
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 2305843009213693951;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

int main() {
  ll H, W, ch, cw, dh, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string field[1000];
  for (ll i = 0; i < H; i++) {
    cin >> field[i];
  }

  deque<pair<ll, ll>> Q;
  ll cost[1020][1020];
  for (ll i = 0; i < 1020; i++) {
    for (ll j = 0; j < 1020; j++) {
      cost[i][j] = INF;
    }
  }
  cost[ch][cw] = 0;
  Q.push_front({ch, cw});
  while (!Q.empty()) {
    pair<ll, ll> t = Q.front();
    Q.pop_front();
    ll i = t.first, j = t.second;
    for (ll k = 0; k < 4; k++) {
      ll x = i + dx[k], y = j + dy[k];
      if (x < 0 || x >= H)
        continue;
      if (y < 0 || y >= W)
        continue;
      if (field[x][y] != '.')
        continue;
      if (cost[x][y] <= cost[i][j])
        continue;
      Q.push_front({x, y});
      cost[x][y] = cost[i][j];
    }
    for (ll k = -2; k <= 2; k++) {
      for (ll l = -2; l <= 2; l++) {
        ll x = i + k, y = j + l;
        if (x < 0 || x >= H)
          continue;
        if (y < 0 || y >= W)
          continue;
        if (field[x][y] != '.')
          continue;
        if (cost[x][y] <= cost[i][j] + 1)
          continue;
        Q.push_front({x, y});
        cost[x][y] = cost[i][j] + 1;
      }
    }
  }
  if (cost[dh][dw] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[dh][dw] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 2305843009213693951;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

int main() {
  ll H, W, ch, cw, dh, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string field[1000];
  for (ll i = 0; i < H; i++) {
    cin >> field[i];
  }

  deque<pair<ll, ll>> Q;
  ll cost[1020][1020];
  for (ll i = 0; i < 1020; i++) {
    for (ll j = 0; j < 1020; j++) {
      cost[i][j] = INF;
    }
  }
  cost[ch][cw] = 0;
  Q.push_front({ch, cw});
  while (!Q.empty()) {
    pair<ll, ll> t = Q.front();
    Q.pop_front();
    ll i = t.first, j = t.second;
    for (ll k = 0; k < 4; k++) {
      ll x = i + dx[k], y = j + dy[k];
      if (x < 0 || x >= H)
        continue;
      if (y < 0 || y >= W)
        continue;
      if (field[x][y] != '.')
        continue;
      if (cost[x][y] <= cost[i][j])
        continue;
      Q.push_front({x, y});
      cost[x][y] = cost[i][j];
    }
    for (ll k = -2; k <= 2; k++) {
      for (ll l = -2; l <= 2; l++) {
        ll x = i + k, y = j + l;
        if (x < 0 || x >= H)
          continue;
        if (y < 0 || y >= W)
          continue;
        if (field[x][y] != '.')
          continue;
        if (cost[x][y] <= cost[i][j] + 1)
          continue;
        Q.push_back({x, y});
        cost[x][y] = cost[i][j] + 1;
      }
    }
  }
  if (cost[dh][dw] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[dh][dw] << endl;
  }
}
Problem p02579 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import deque


def main():
    def f(hi, wi):
        return hi * w + wi

    h, w = map(int, input().split())
    sh, sw = map(int, input().split())
    gh, gw = map(int, input().split())

    s = [input() for _ in range(h)]
    INF = 10**9

    sh -= 1
    sw -= 1
    gh -= 1
    gw -= 1

    dq = deque([f(sh, sw)])
    dist = [[INF] * w for _ in range(h)]
    dist[sh][sw] = 0

    while dq:
        i = dq.popleft()
        hi, wi = divmod(i, w)

        for dh in range(-2, 3):
            for dw in range(-2, 3):
                if dh == 0 and dw == 0:
                    continue
                nh = hi + dh
                nw = wi + dw
                if 0 <= nh <= h - 1 and 0 <= nw <= w - 1 and s[nh][nw] == ".":
                    if abs(dh) + abs(dw) == 1:
                        if dist[hi][wi] < dist[nh][nw]:
                            dist[nh][nw] = dist[hi][wi]
                            dq.appendleft(f(nh, nw))
                    else:
                        if dist[hi][wi] + 1 < dist[nh][nw]:
                            dist[nh][nw] = dist[hi][wi] + 1
                            dq.append(f(nh, nw))

    ans = dist[gh][gw]
    if ans == INF:
        ans = -1

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    def f(hi, wi):
        return hi * w + wi

    h, w = map(int, input().split())
    sh, sw = map(int, input().split())
    gh, gw = map(int, input().split())

    s = [input() for _ in range(h)]
    INF = 10**9

    sh -= 1
    sw -= 1
    gh -= 1
    gw -= 1

    dq = deque([f(sh, sw)])
    dist = [[INF] * w for _ in range(h)]
    dist[sh][sw] = 0

    while dq:
        i = dq.popleft()
        hi, wi = divmod(i, w)

        for dh in range(-2, 3):
            for dw in range(-2, 3):
                nh = hi + dh
                nw = wi + dw
                if 0 <= nh <= h - 1 and 0 <= nw <= w - 1 and s[nh][nw] == ".":
                    if abs(dh) + abs(dw) == 1:
                        if dist[hi][wi] < dist[nh][nw]:
                            dist[nh][nw] = dist[hi][wi]
                            dq.appendleft(f(nh, nw))
                    else:
                        if dist[hi][wi] + 1 < dist[nh][nw]:
                            dist[nh][nw] = dist[hi][wi] + 1
                            dq.append(f(nh, nw))

    ans = dist[gh][gw]
    if ans == INF:
        ans = -1

    print(ans)


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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  rep(i, 1002) rep(j, 1002) {
    if (i % (h + 1) == 0 || j % (w + 1) == 0)
      mp[i][j] = -1;
    else
      mp[i][j] = 1e9;
  }
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] != '.') mp[i + 1][j + 1] = -1;
  }
  deque<pair<int, pair<int, int>>> q;
  mp[sy][sx] = 0;
  q.emplace_back(0, make_pair(sy, sx));
  while (q.size()) {
    deque<pair<int, pair<int, int>>> cp;
    swap(q, cp);
    while (cp.size()) {
      int cost = cp.front().first;
      int y = cp.front().second.first;
      int x = cp.front().second.second;
      cp.pop_front();
      if (mp[y][x] < cost)
        continue;
      for (int i = y - 2; i <= y + 2; i++) {
        for (int j = x - 2; j <= x + 2; j++) {
          if (i < 1 || h < i || j < 1 || w < j)
            continue;
          if (i == y && j == x)
            continue;
          if (abs(i - y) + abs(j - x) == 1 && mp[i][j] > cost) {
            mp[i][j] = cost;
            q.emplace_front(cost, make_pair(i, j));
          } else if (mp[i][j] > cost + 1) {
            mp[i][j] = cost + 1;
            q.emplace_back(cost + 1, make_pair(i, j));
          }
        }
      }
    }
  }
  if (mp[gy][gx] == 1e9)
    mp[gy][gx] = -1;
  cout << mp[gy][gx] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  rep(i, 1002) rep(j, 1002) {
    if (i % (h + 1) == 0 || j % (w + 1) == 0)
      mp[i][j] = -1;
    else
      mp[i][j] = 1e9;
  }
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] != '.') mp[i + 1][j + 1] = -1;
  }
  deque<pair<int, pair<int, int>>> q;
  mp[sy][sx] = 0;
  q.emplace_back(0, make_pair(sy, sx));
  while (q.size()) {
    int cost = q.front().first;
    int y = q.front().second.first;
    int x = q.front().second.second;
    q.pop_front();
    if (mp[y][x] < cost)
      continue;
    for (int i = y - 2; i <= y + 2; i++) {
      for (int j = x - 2; j <= x + 2; j++) {
        if (i < 1 || h < i || j < 1 || w < j)
          continue;
        if (i == y && j == x)
          continue;
        if (abs(i - y) + abs(j - x) == 1 && mp[i][j] > cost) {
          mp[i][j] = cost;
          q.emplace_front(cost, make_pair(i, j));
        } else if (mp[i][j] > cost + 1) {
          mp[i][j] = cost + 1;
          q.emplace_back(cost + 1, make_pair(i, j));
        }
      }
    }
  }
  if (mp[gy][gx] == 1e9)
    mp[gy][gx] = -1;
  cout << mp[gy][gx] << endl;
  return 0;
}
Problem p02579 (C++)

Buggy submission (status: 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>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_front(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}

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

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}