Problem p02578 (C++) — buggy submission, verdict: Runtime Error

// problem link:

#include <bits/stdc++.h>

#define PI 3.1415926535897932384626
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, a, b) for (i = a; i < b; i++)
#define ll long long
#define pb push_back
#define popb pop_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define ini(a) memset(a, -1, sizeof(a))
#define M (long long)(1e9 + 7)

#define chk(a) cout << endl << #a << " : " << a << endl
#define chk2(a, b)                                                             \\
  cout << endl << #a << " : " << a << "\\t" << #b << " : " << b << endl
#define chk3(a, b, c)                                                          \\
  cout << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << endl
#define chk4(a, b, c, d)                                                       \\
  cout << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << "\\t" << #d << " : " << d << endl
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;

using namespace std;

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<ll, ll> pl;
typedef pair<ll, pl> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

//========================================================================

void solve() {

  int n;
  cin >> n;
  ll a[n];
  ll dp[n];
  ll h[n];
  int i;
  fo(i, n) {
    cin >> a[i];
    h[i] = a[i];
  }
  dp[0] = 0;
  for (i = 1; i < n; i++) {
    if (a[i] < h[i - 1]) {
      h[i] = a[i] + abs(a[i] - h[i - 1]);
      dp[i] = dp[i - 1] + abs(a[i] - h[i - 1]);
    } else {
      dp[i] = dp[i - 1];
    }
  }
  cout << dp[n - 1] << endl;
}

int main() {

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

  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

// problem link:

#include <bits/stdc++.h>

#define PI 3.1415926535897932384626
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, a, b) for (i = a; i < b; i++)
#define ll long long
#define pb push_back
#define popb pop_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define ini(a) memset(a, -1, sizeof(a))
#define M (long long)(1e9 + 7)

#define chk(a) cout << endl << #a << " : " << a << endl
#define chk2(a, b)                                                             \\
  cout << endl << #a << " : " << a << "\\t" << #b << " : " << b << endl
#define chk3(a, b, c)                                                          \\
  cout << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << endl
#define chk4(a, b, c, d)                                                       \\
  cout << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << "\\t" << #d << " : " << d << endl
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;

using namespace std;

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<ll, ll> pl;
typedef pair<ll, pl> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

//========================================================================

void solve() {

  int n;
  cin >> n;
  int a[n];
  int dp[n];
  int h[n];
  int i;
  fo(i, n) {
    cin >> a[i];
    h[i] = a[i];
  }
  dp[0] = 0;
  for (i = 1; i < n; i++) {
    if (a[i] < h[i - 1]) {
      h[i] = a[i] + abs(a[i] - h[i - 1]);
      dp[i] = dp[i - 1] + abs(a[i] - h[i - 1]);
    } else {
      dp[i] = dp[i - 1];
    }
  }
  cout << dp[n - 1] << endl;
}

int main() {

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

  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}