Problem p03403 (C++)

Buggy submission (status: Runtime Error):
/*
              *******
When winter comes,You'll hear no lions roar,
no stags grazing the fields,
no roses growing in the meadows,
no snakes in the sand.
The krakens will freeze where they swim.
The flayed men will rot and wither.
No trouts swimming in the river and no falcons flying in the air.
Not even the dragons breath will warm you in your halls.
Only the wolfs howl in the night.
            WINTER IS COMING
*/
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define si(x) scanf("%d", &x)
#define pi(x) printf("%d\n", x)
#define s(x) scanf("%lld", &x)
#define p(x) printf("%lld\n", x)
#define sc(x) scanf("%s", x)
#define pc(x) printf("%s", x)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define inf 4e18
#define prec(x) fixed << setprecision(15) << x
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mem(x, y) memset(x, y, sizeof(x))
#define PQG priority_queue<int, std::vector<int>, std::greater<int>>
#define PQL priority_queue<int, std::vector<int>, std::less<int>>
#define PQPL priority_queue<pii, vector<pii>, less<pii>>
#define PQPG priority_queue<pii, vector<pii>, greater<pii>>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace std;

const int N = 1e5 + 7;
int a[N], l_sum[N], r_sum[N], l_diff[N], r_diff[N];

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("output.txt","w",stdout);
#endif
  fast_io;
  int n;
  cin >> n;
  a[0] = 0, a[n + 1] = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  mem(l_sum, 0);
  mem(r_sum, 0);
  mem(l_diff, 0);
  mem(r_diff, 0);
  for (int i = 1; i <= n; i++) {
    l_diff[i] = abs(a[i] - a[i - 1]);
    l_sum[i] = l_sum[i - 1] + l_diff[i];
  }
  for (int i = n; i >= 1; i--) {
    r_diff[i] = abs(a[i] - a[i + 1]);
    r_sum[i] = r_sum[i + 1] + r_diff[i];
  }
  for (int i = 1; i <= n; i++) {
    int ans = l_sum[i - 1] + r_sum[i + 1] + abs(a[i + 1] - a[i - 1]);
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
/*
              *******
When winter comes,You'll hear no lions roar,
no stags grazing the fields,
no roses growing in the meadows,
no snakes in the sand.
The krakens will freeze where they swim.
The flayed men will rot and wither.
No trouts swimming in the river and no falcons flying in the air.
Not even the dragons breath will warm you in your halls.
Only the wolfs howl in the night.
            WINTER IS COMING
*/
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define si(x) scanf("%d", &x)
#define pi(x) printf("%d\n", x)
#define s(x) scanf("%lld", &x)
#define p(x) printf("%lld\n", x)
#define sc(x) scanf("%s", x)
#define pc(x) printf("%s", x)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define inf 4e18
#define prec(x) fixed << setprecision(15) << x
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mem(x, y) memset(x, y, sizeof(x))
#define PQG priority_queue<int, std::vector<int>, std::greater<int>>
#define PQL priority_queue<int, std::vector<int>, std::less<int>>
#define PQPL priority_queue<pii, vector<pii>, less<pii>>
#define PQPG priority_queue<pii, vector<pii>, greater<pii>>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace std;

const int N = 1e5 + 7;
int a[N], l_sum[N], r_sum[N], l_diff[N], r_diff[N];

int main() {
  fast_io;
  int n;
  cin >> n;
  a[0] = 0, a[n + 1] = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  mem(l_sum, 0);
  mem(r_sum, 0);
  mem(l_diff, 0);
  mem(r_diff, 0);
  for (int i = 1; i <= n; i++) {
    l_diff[i] = abs(a[i] - a[i - 1]);
    l_sum[i] = l_sum[i - 1] + l_diff[i];
  }
  for (int i = n; i >= 1; i--) {
    r_diff[i] = abs(a[i] - a[i + 1]);
    r_sum[i] = r_sum[i + 1] + r_diff[i];
  }
  for (int i = 1; i <= n; i++) {
    int ans = l_sum[i - 1] + r_sum[i + 1] + abs(a[i + 1] - a[i - 1]);
    cout << ans << endl;
  }

  return 0;
}