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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
typedef long long ll;
int main() {
  int N;
  cin >> N;
  vector<int> A(N + 2, 0);
  rep(i, 1, N + 1) { cin >> A[i]; }
  int preA = 0;
  ll ans = 0;
  rep(i, 1, N + 1) {
    rep(j, 1, A.size()) {
      if (j == i)
        continue;

      ans += abs(A[j] - preA);

      preA = A[j];
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
typedef long long ll;
int main() {
  int N;
  cin >> N;
  vector<int> A(N + 2);
  rep(i, 0, N) cin >> A[i + 1];
  int sum = 0;
  rep(i, 0, N + 2) { sum += abs(A[i] - A[i + 1]); }
  rep(i, 0, N) {
    int tmp = sum - abs(A[i + 1] - A[i]) - abs(A[i + 1] - A[i + 2]) +
              abs(A[i] - A[i + 2]);
    cout << tmp << endl;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
  int N;
  int arr[5005];
  int Max = 0;
  int money;
  cin >> N;

  arr[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> arr[i];
    Max += abs(arr[i - 1] - arr[i]);
  }
  arr[N + 1] = 0;
  Max += abs(arr[N] - arr[N + 1]);

  for (int i = 1; i <= N; i++) {
    money = Max - abs(arr[i - 1] - arr[i]) - abs(arr[i] - arr[i + 1]) +
            abs(arr[i - 1] - arr[i + 1]);
    cout << money << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
  long long N;
  int arr[100005];
  long long Max = 0;
  long long money;
  cin >> N;

  arr[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> arr[i];
    Max += abs(arr[i - 1] - arr[i]);
  }
  arr[N + 1] = 0;
  Max += abs(arr[N] - arr[N + 1]);

  for (int i = 1; i <= N; i++) {
    money = Max - abs(arr[i - 1] - arr[i]) - abs(arr[i] - arr[i + 1]) +
            abs(arr[i - 1] - arr[i + 1]);
    cout << money << endl;
  }

  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
#define LOOP1(i, N) for (int i = 1; i <= N; i++)
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;

unsigned int uns(int N) {
  if (N < 0)
    return N * -1;
  return N;
}

int main() {
  int a[10002] = {};
  unsigned int d[10001];
  int nm = 0;
  int N;
  cin >> N;
  LOOP1(i, N) {
    cin >> a[i];
    nm += uns(a[i] - a[i - 1]);
  }
  nm += uns(a[N]);
  int dm = nm;
  LOOP1(i, N) {
    nm = dm;
    // iを通らない。
    // 不通過処理
    nm -= uns(a[i] - a[i - 1]) + uns(a[i + 1] - a[i]);
    nm += uns(a[i + 1] - a[i - 1]);
    cout << nm;
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
#define LOOP1(i, N) for (int i = 1; i <= N; i++)
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;

unsigned int uns(int N) {
  if (N < 0)
    return N * -1;
  return N;
}

int main() {
  int a[100002] = {};
  int nm = 0;
  int N;
  cin >> N;
  LOOP1(i, N) {
    cin >> a[i];
    nm += uns(a[i] - a[i - 1]);
  }
  nm += uns(a[N]);
  int dm = nm;
  LOOP1(i, N) {
    nm = dm;
    // iを通らない。
    // 不通過処理
    nm -= uns(a[i] - a[i - 1]) + uns(a[i + 1] - a[i]);
    nm += uns(a[i + 1] - a[i - 1]);
    cout << nm;
    cout << endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, x) for (int i = 0; i < x; i++)
#define re(i, x, y) for (int i = x; i < y; i++)
int INF = 1e9;
const long long mod = 1e9 + 7;
#define ll long long

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int sum = 0;
  a[0] = 0, a[n + 1] = 0;
  for (int i = 1; i <= n + 1; i++) {
    sum += (abs(a[i] - a[i - 1]));
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] >= a[i - 1] && a[i] <= a[i + 1]) {
      b[i] = sum;
    } else if (a[i] <= a[i - 1] && a[i] >= a[i + 1]) {
      b[i] = sum;
    } else if (a[i] >= a[i - 1] && a[i] >= a[i + 1]) {
      b[i] = sum -
             ((a[i] - a[i - 1]) + (a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1]));
    } else if (a[i - 1] >= a[i] && a[i] <= a[i + 1]) {
      b[i] = sum -
             ((a[i - 1] - a[i]) + (a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]));
    }
    cout << b[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, x) for (int i = 0; i < x; i++)
#define re(i, x, y) for (int i = x; i < y; i++)
int INF = 1e9;
const long long mod = 1e9 + 7;
#define ll long long

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 2), b(n + 2);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int sum = 0;
  a[0] = 0, a[n + 1] = 0;
  for (int i = 1; i <= n + 1; i++) {
    sum += (abs(a[i] - a[i - 1]));
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] >= a[i - 1] && a[i] <= a[i + 1]) {
      b[i] = sum;
    } else if (a[i] <= a[i - 1] && a[i] >= a[i + 1]) {
      b[i] = sum;
    } else if (a[i] >= a[i - 1] && a[i] >= a[i + 1]) {
      b[i] = sum -
             ((a[i] - a[i - 1]) + (a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1]));
    } else if (a[i - 1] >= a[i] && a[i] <= a[i + 1]) {
      b[i] = sum -
             ((a[i - 1] - a[i]) + (a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]));
    }
    cout << b[i] << endl;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int tmp[105], num[105];
int main() {
  int n, Sum = 0;
  cin >> n;
  tmp[0] = 0;
  tmp[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> tmp[i];
    Sum += abs(tmp[i] - tmp[i - 1]);
  }
  Sum += abs(tmp[n]);
  for (int i = 1; i <= n; i++) {
    num[i] = abs(tmp[i] - tmp[i - 1]) + abs(tmp[i + 1] - tmp[i]) -
             abs(tmp[i + 1] - tmp[i - 1]);
  }
  for (int i = 1; i <= n; i++)
    cout << Sum - num[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int tmp[100005], num[100005];
int main() {
  int n, Sum = 0;
  cin >> n;
  tmp[0] = 0;
  tmp[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> tmp[i];
    Sum += abs(tmp[i] - tmp[i - 1]);
  }
  Sum += abs(tmp[n]);
  for (int i = 1; i <= n; i++) {
    num[i] = abs(tmp[i] - tmp[i - 1]) + abs(tmp[i + 1] - tmp[i]) -
             abs(tmp[i + 1] - tmp[i - 1]);
  }
  for (int i = 1; i <= n; i++)
    cout << Sum - num[i] << endl;
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int n, i, s = 0, z, k, a1, a2, a3;
  int b[10000] = {0};

  scanf("%d", &n);

  for (i = 1; i <= n; i++)
    scanf("%d", &b[i]);

  for (i = 1; i <= n + 1; i++) {
    k = 0;
    k = b[i] - b[i - 1];

    if (0 > k) {
      k = k * -1;
    }

    s = s + k;
  }

  for (i = 1; i <= n; i++) {
    a1 = 0;
    a2 = 0;
    a3 = 0;

    a1 = b[i] - b[i - 1];
    a2 = b[i + 1] - b[i];
    a3 = b[i + 1] - b[i - 1];

    if (0 > a1)
      a1 = a1 * -1;
    if (0 > a2)
      a2 = a2 * -1;
    if (0 > a3)
      a3 = a3 * -1;

    printf("%d\n", s - a1 - a2 + a3);
  }
  return 0;
}


Fixed submission:
#include <stdio.h>
int main(void) {
  int n, i, s = 0, z, k, a1, a2, a3;
  int b[100000] = {0};

  scanf("%d", &n);

  for (i = 1; i <= n; i++)
    scanf("%d", &b[i]);

  for (i = 1; i <= n + 1; i++) {
    k = 0;
    k = b[i] - b[i - 1];

    if (0 > k) {
      k = k * -1;
    }

    s = s + k;
  }

  for (i = 1; i <= n; i++) {
    a1 = 0;
    a2 = 0;
    a3 = 0;

    a1 = b[i] - b[i - 1];
    a2 = b[i + 1] - b[i];
    a3 = b[i + 1] - b[i - 1];

    if (0 > a1)
      a1 = a1 * -1;
    if (0 > a2)
      a2 = a2 * -1;
    if (0 > a3)
      a3 = a3 * -1;

    printf("%d\n", s - a1 - a2 + a3);
  }
  return 0;
}
Problem p03401 (C++)

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

int main(void) {
  int N;
  cin >> N;
  vector<int> A;
  A.push_back(0);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  A.push_back(0);

  for (int i = 1; i <= N; i++) { // i番目を通らない
    int cost = 0;
    for (int i = 0; i < N + 2; i++)
      cost += abs(A[i] - A[i + 1]);
    cost = cost - abs(A[i] - A[i + 1]) - abs(A[i - 1] - A[i]) +
           abs(A[i - 1] - A[i + 1]);
    printf("%d\n", cost);
  }
}

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

int main(void) {
  int N;
  cin >> N;
  vector<int> A;
  A.push_back(0);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  A.push_back(0);

  int totalCost = 0;
  for (int i = 0; i < N + 2; i++)
    totalCost += abs(A[i + 1] - A[i]);

  for (int i = 1; i <= N; i++) { // i番目は通らない
    printf("%d\n", totalCost - abs(A[i] - A[i - 1]) - abs(A[i + 1] - A[i]) +
                       abs(A[i + 1] - A[i - 1]));
  }
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
int N, ans = 0, s = 0;
vector<int> A(10000, 0);
vector<int> a(10000, 0);

int main() {

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  A[0] = 0;
  A[N + 1] = 0;
  for (int i = 1; i <= N + 1; i++) {
    s += abs(A[i - 1] - A[i]);
  }
  for (int i = 1; i <= N; i++) {
    ans = s + abs(A[i - 1] - A[i + 1]) -
          (abs(A[i - 1] - A[i]) + abs(A[i] - A[i + 1]));
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
int N, ans = 0, s = 0;
vector<int> A(100001, 0);
vector<int> a(100001, 0);

int main() {

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  A[0] = 0;
  A[N + 1] = 0;
  for (int i = 1; i <= N + 1; i++) {
    s += abs(A[i - 1] - A[i]);
  }
  for (int i = 1; i <= N; i++) {
    ans = s + abs(A[i - 1] - A[i + 1]) -
          (abs(A[i - 1] - A[i]) + abs(A[i] - A[i + 1]));
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
int a[10010];

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a[0] = 0;
  int s = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s += abs(a[i] - a[i - 1]);
  }
  a[n + 1] = 0;
  s += abs(a[n + 1] - a[n]);
  // cout<<"s:"<<s<<endl;

  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
      cout << s - (a[i] - max(a[i - 1], a[i + 1])) * 2 << endl;
    } else if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
      cout << s - (min(a[i - 1], a[i + 1]) - a[i]) * 2 << endl;
    } else {
      cout << s << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
int a[100100];

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a[0] = 0;
  int s = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s += abs(a[i] - a[i - 1]);
  }
  a[n + 1] = 0;
  s += abs(a[n + 1] - a[n]);
  // cout<<"s:"<<s<<endl;

  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
      cout << s - (a[i] - max(a[i - 1], a[i + 1])) * 2 << endl;
    } else if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
      cout << s - (min(a[i - 1], a[i + 1]) - a[i]) * 2 << endl;
    } else {
      cout << s << endl;
    }
  }

  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int main() {
  int N;
  int A[10002];
  int sum = 0;
  A[0] = 0;
  cin >> N;
  rep(i, N) {
    cin >> A[i + 1];
    sum += abs(A[i + 1] - A[i]);
  }
  sum += abs(A[N]);
  A[N + 1] = 0;
  rep(i, N) {
    cout << sum + abs(A[i + 2] - A[i]) - abs(A[i + 1] - A[i]) -
                abs(A[i + 2] - A[i + 1])
         << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int main() {
  int N;
  int A[100002];
  int sum = 0;
  A[0] = 0;
  cin >> N;
  rep(i, N) {
    cin >> A[i + 1];
    sum += abs(A[i + 1] - A[i]);
  }
  sum += abs(A[N]);
  A[N + 1] = 0;
  rep(i, N) {
    cout << sum + abs(A[i + 2] - A[i]) - abs(A[i + 1] - A[i]) -
                abs(A[i + 2] - A[i + 1])
         << endl;
  }
  return 0;
}
Problem p03401 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = [0, *map(int, input().split()), 0]

total = sum(abs(a[i] - a[i + 1]) for i in range(0, n + 1))
for i in range(1, n + 1):
    print(
        total - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]) + abs(a[i - 1] - a[i + 1])
    )


Fixed submission:
n = int(input())
a = [0] + list(map(int, input().split())) + [0]

total = sum(abs(a[i] - a[i + 1]) for i in range(0, n + 1))
for i in range(1, n + 1):
    print(
        total - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]) + abs(a[i - 1] - a[i + 1])
    )
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <stdlib.h>

using namespace std;
int main(void) {
  int N;
  int A[10000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // 通常ルート
  int now = 0;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += abs(now - A[i]);
    now = A[i];
  }
  sum += abs(A[N - 1]);

  int moto = 0;
  int tugi = 0;

  for (int i = 0; i < N; i++) {
    if (i == 0) {
      moto = abs(0 - A[i]) + abs(A[i + 1] - A[i]);
      tugi = abs(A[i + 1]);
      cout << sum - (moto - tugi) << endl;
      continue;
    }
    if (i == N - 1) {
      moto = abs(A[i - 1] - A[i]) + abs(0 - A[i]);
      tugi = abs(A[i - 1]);
      cout << sum - (moto - tugi) << endl;
      continue;
    }
    moto = abs(A[i - 1] - A[i]) + abs(A[i + 1] - A[i]);
    tugi = abs(A[i + 1] - A[i - 1]);
    cout << sum - (moto - tugi) << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <stdlib.h>

using namespace std;
int main(void) {
  int N;
  int A[100000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // 通常ルート
  int now = 0;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += abs(now - A[i]);
    now = A[i];
  }
  sum += abs(A[N - 1]);

  int moto = 0;
  int tugi = 0;

  for (int i = 0; i < N; i++) {
    if (i == 0) {
      moto = abs(0 - A[i]) + abs(A[i + 1] - A[i]);
      tugi = abs(A[i + 1]);
      cout << sum - (moto - tugi) << endl;
      continue;
    }
    if (i == N - 1) {
      moto = abs(A[i - 1] - A[i]) + abs(0 - A[i]);
      tugi = abs(A[i - 1]);
      cout << sum - (moto - tugi) << endl;
      continue;
    }
    moto = abs(A[i - 1] - A[i]) + abs(A[i + 1] - A[i]);
    tugi = abs(A[i + 1] - A[i - 1]);
    cout << sum - (moto - tugi) << endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  for (ll i = 0; i < N; i++) {
    ll p = 0;
    ll m = 0;
    for (int j = 0; j < N; j++) {
      if (i == j)
        continue;
      m += abs(A.at(j) - p);
      p = A.at(j);
    }
    m += abs(p - 0);
    cout << m << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 2);
  rep(i, N) cin >> A.at(i + 1);
  A[0] = 0;
  A[N + 1] = 0;
  vector<ll> B(N + 1);
  int all = 0;
  for (int i = 0; i < N + 1; i++) {
    B.at(i) = abs(A.at(i + 1) - A.at(i));
    all += B[i];
  }
  for (ll i = 1; i <= N; i++) {
    int res = 0;
    if (i == 1)
      res = all - B[0] - B[1] + abs(A[2]);
    else if (i == N)
      res = all - B[N - 1] - B[N] + abs(A[N - 1]);
    else
      res = all - B[i - 1] - B[i] + abs(A[i + 1] - A[i - 1]);
    cout << res << endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}

using namespace std;
int main() {
  int n, sum = 0;
  cin >> n;
  vector<int> vec(n + 1), rui(n + 1);
  vec[0] = 0;
  FOR(i, 1, n + 1) { cin >> vec[i]; }

  vec[n + 1] = 0;

  rui[0] = vec[0];
  FOR(i, 1, n + 2) { rui[i] = rui[i - 1] + abs(vec[i - 1] - vec[i]); }

  FOR(i, 1, n + 1) {
    int sa, r, h;
    sa = abs(vec[i - 1] - vec[i]) + abs(vec[i] - vec[i + 1]);
    r = abs(vec[i - 1] - vec[i + 1]);
    h = sa - r;

    cout << rui[n + 1] - h << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}

using namespace std;
int main() {
  int n, sum = 0;
  cin >> n;
  vector<int> vec(n + 2), rui(n + 2);
  vec[0] = 0;
  FOR(i, 1, n + 1) { cin >> vec[i]; }

  vec[n + 1] = 0;

  rui[0] = vec[0];
  FOR(i, 1, n + 2) { rui[i] = rui[i - 1] + abs(vec[i - 1] - vec[i]); }

  FOR(i, 1, n + 1) {
    int sa, r, h;
    sa = abs(vec[i - 1] - vec[i]) + abs(vec[i] - vec[i + 1]);
    r = abs(vec[i - 1] - vec[i + 1]);
    h = sa - r;

    cout << rui[n + 1] - h << endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>

int main(void) {
  int N;
  scanf("%d", &N);
  int *A = new int(N);
  long long int allsum = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", (A + i));
    if (i > 0 && i < N) {
      allsum += abs(A[i] - A[i - 1]);
    } else if (i == 0) {
      allsum += abs(A[0]);
    }
  }
  allsum += abs(A[N - 1]);

  for (int i = 0; i < N; i++) {
    if (i > 0 && i < N - 1) {
      printf("%lld\n", allsum - abs(A[i] - A[i - 1]) - abs(A[i] - A[i + 1]) +
                           abs(A[i - 1] - A[i + 1]));
    } else if (i == 0) {
      printf("%lld\n", allsum - abs(A[0]) - abs(A[1] - A[0]) + abs(A[1]));
    } else if (i == N - 1) {
      printf("%lld\n",
             allsum - abs(A[N - 1]) - abs(A[N - 1] - A[N - 2]) + abs(A[N - 2]));
    }
  }
  delete[] A;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>

int main(void) {
  int N;
  scanf("%d", &N);
  int *A = new int[N];
  long long int allsum = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", (A + i));
    if (i > 0 && i < N) {
      allsum += abs(A[i] - A[i - 1]);
    } else if (i == 0) {
      allsum += abs(A[0]);
    }
  }
  allsum += abs(A[N - 1]);

  for (int i = 0; i < N; i++) {
    if (i > 0 && i < N - 1) {
      printf("%lld\n", allsum - abs(A[i] - A[i - 1]) - abs(A[i] - A[i + 1]) +
                           abs(A[i - 1] - A[i + 1]));
    } else if (i == 0) {
      printf("%lld\n", allsum - abs(A[0]) - abs(A[1] - A[0]) + abs(A[1]));
    } else if (i == N - 1) {
      printf("%lld\n",
             allsum - abs(A[N - 1]) - abs(A[N - 1] - A[N - 2]) + abs(A[N - 2]));
    }
  }
  delete[] A;
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
  int n;
  ll val1[10010];
  ll val2[10010];
  cin >> n;
  ll back1;
  ll back2;
  ll sumdis = 0;
  imfor(i, n) {
    int a;
    cin >> a;
    if (i == 0) {
      val1[i] = abs(a);
      back1 = a;
    } else if (i == 1) {
      val1[i] = abs(a - back1);
      val2[i] = abs(a);
      back2 = back1;
      back1 = a;
    } else {
      val1[i] = abs(a - back1);
      val2[i] = abs(a - back2);
      back2 = back1;
      back1 = a;
    }
    sumdis += val1[i];
  }
  val1[n] = abs(back1);
  val2[n] = abs(back2);
  sumdis += val1[n];
  imfor(i, n) {
    cout << sumdis - (val1[i] + val1[i + 1] - val2[i + 1]) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
  int n;
  ll val1[100100];
  ll val2[100100];
  cin >> n;
  ll back1;
  ll back2;
  ll sumdis = 0;
  imfor(i, n) {
    int a;
    cin >> a;
    if (i == 0) {
      val1[i] = abs(a);
      back1 = a;
    } else if (i == 1) {
      val1[i] = abs(a - back1);
      val2[i] = abs(a);
      back2 = back1;
      back1 = a;
    } else {
      val1[i] = abs(a - back1);
      val2[i] = abs(a - back2);
      back2 = back1;
      back1 = a;
    }
    sumdis += val1[i];
  }
  val1[n] = abs(back1);
  val2[n] = abs(back2);
  sumdis += val1[n];
  imfor(i, n) {
    cout << sumdis - (val1[i] + val1[i + 1] - val2[i + 1]) << endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define MOD 1000000007
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<pair<int, int>> vp;
typedef pair<int, int> p;
typedef vector<int> vi;
int n, totalcost;
vi a(10005);
vi costs(10005);
vi fix(10005);

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; i++) {
    if (i == 0)
      totalcost += abs(a[0]);
    if (i == n)
      totalcost += abs(a[i]);
    else
      totalcost += abs(a[i] - a[i + 1]);
  }
  for (int i = 1; i <= n; i++) {
    if (i == n)
      fix[i] = (totalcost - (abs(a[i - 1] - a[i]) + abs(a[i])) + abs(a[i - 1]));
    else
      fix[i] = (totalcost - (abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1])) +
                abs(a[i - 1] - a[i + 1]));
  }
  for (int i = 1; i <= n; i++) {
    cout << fix[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define MOD 1000000007
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<pair<int, int>> vp;
typedef pair<int, int> p;
typedef vector<int> vi;
int n, totalcost;
vi a(100005);
vi costs(100005);
vi fix(100005);

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; i++) {
    if (i == 0)
      totalcost += abs(a[0]);
    if (i == n)
      totalcost += abs(a[i]);
    else
      totalcost += abs(a[i] - a[i + 1]);
  }
  for (int i = 1; i <= n; i++) {
    if (i == n)
      fix[i] = (totalcost - (abs(a[i - 1] - a[i]) + abs(a[i])) + abs(a[i - 1]));
    else
      fix[i] = (totalcost - (abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1])) +
                abs(a[i - 1] - a[i + 1]));
  }
  for (int i = 1; i <= n; i++) {
    cout << fix[i] << endl;
  }

  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void) {
  int n;
  int count = 0, pa = 0;
  int cost[10001];
  int a[10001];
  cin >> n;
  cost[0] = 0;
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cost[i] = cost[i - 1] + abs(a[i] - a[i - 1]);
    // cout << a[i] << cost[i] << endl;
  }
  cost[n + 1] = cost[n] + abs(a[n]);
  a[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i - 1] < a[i] && a[i] < a[i + 1] ||
        a[i - 1] > a[i] && a[i] > a[i + 1])
      cout << cost[n + 1] << endl;
    else
      cout << cost[n + 1] -
                  2 * (min(cost[i] - cost[i - 1], cost[i + 1] - cost[i]))
           << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void) {
  int n;
  int count = 0, pa = 0;
  int cost[100001];
  int a[100001];
  cin >> n;
  cost[0] = 0;
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cost[i] = cost[i - 1] + abs(a[i] - a[i - 1]);
    // cout << a[i] << cost[i] << endl;
  }
  cost[n + 1] = cost[n] + abs(a[n]);
  a[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i - 1] < a[i] && a[i] < a[i + 1] ||
        a[i - 1] > a[i] && a[i] > a[i + 1])
      cout << cost[n + 1] << endl;
    else
      cout << cost[n + 1] -
                  2 * (min(cost[i] - cost[i - 1], cost[i + 1] - cost[i]))
           << endl;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 20000 + 20;
int n, a[N], sz[N];
int mabs(int x) {
  if (x < 0)
    return x * -1;
  return x;
}
int main() {
  // freopen("out.txt","w",stdout);
  while (~scanf("%d", &n)) {
    sz[0] = 0;
    a[0] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      sz[i] = sz[i - 1] + mabs(a[i] - a[i - 1]);
    }
    printf("%d\n", sz[n] - sz[2] + mabs(a[n]) + mabs(a[2]));
    for (int i = 2; i < n; i++)
      printf("%d\n", sz[n] - sz[i + 1] + mabs(a[i + 1] - a[i - 1]) + sz[i - 1] +
                         mabs(a[n]));
    printf("%d\n", sz[n - 1] + mabs(a[n - 1]));
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 100000 + 20;
int n, a[N], sz[N];
int mabs(int x) {
  if (x < 0)
    return x * -1;
  return x;
}
int main() {
  // freopen("out.txt","w",stdout);
  while (~scanf("%d", &n)) {
    sz[0] = 0;
    a[0] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      sz[i] = sz[i - 1] + mabs(a[i] - a[i - 1]);
    }
    printf("%d\n", sz[n] - sz[2] + mabs(a[n]) + mabs(a[2]));
    for (int i = 2; i < n; i++)
      printf("%d\n", sz[n] - sz[i + 1] + mabs(a[i + 1] - a[i - 1]) + sz[i - 1] +
                         mabs(a[n]));
    printf("%d\n", sz[n - 1] + mabs(a[n - 1]));
  }

  return 0;
}
Problem p03401 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
#define PINT pair<int, int>
#define f1 first
#define s2 second
const int INF = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> vi(n);
  REP(i, n) cin >> vi[i];
  REP(i, n) {
    ll ans = 0, now = 0;
    REP(j, n) {
      if (j == i)
        continue;
      ans += abs(vi[j] - now);
      now = vi[j];
    }
    ans += abs(now - 0);
    cout << ans << endl;
  }
  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
#define PINT pair<int, int>
#define f1 first
#define s2 second
const int INF = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> vi(n + 2), sa(n + 1);
  vi[0] = vi[n + 1] = 0;
  FOR(i, 1, n + 1) cin >> vi[i];
  ll ki = 0;
  REP(i, n + 1) {
    sa[i] = abs(vi[i + 1] - vi[i]);
    ki += sa[i];
  }
  FOR(i, 1, n + 1) {
    ll ans = ki - sa[i - 1] - sa[i];
    ans += abs(vi[i + 1] - vi[i - 1]);
    cout << ans << endl;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Max_size 10000
using namespace std;
int main() {
  int n, a[Max_size], sum = 0;
  cin >> n;
  a[0] = 0;
  a[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += abs(a[i - 1] - a[i]);
  }
  sum += abs(a[n] - a[n + 1]);
  for (int i = 1; i <= n; i++) {
    cout << sum - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]) +
                abs(a[i - 1] - a[i + 1])
         << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define Max_size 100000
using namespace std;
int main() {
  int n, a[Max_size], sum = 0;
  cin >> n;
  a[0] = 0;
  a[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += abs(a[i - 1] - a[i]);
  }
  sum += abs(a[n] - a[n + 1]);
  for (int i = 1; i <= n; i++) {
    cout << sum - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]) +
                abs(a[i - 1] - a[i + 1])
         << endl;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
typedef uint uint32_t;

int A[10002];

int main() {
  uint N;
  cin >> N;
  int sum = 0, pre = 0, i;
  for (i = 0; i < N; i++) {
    cin >> A[i];
    sum += abs(A[i] - pre);
    pre = A[i];
  }
  A[i] = 0;
  sum += abs(A[i] - A[i - 1]);
  // cout << i << endl;
  // for (int i = 0; i < N + 1; i++) {
  //     cout << "A[" << i << ']' <<  A[i] << endl;
  // }
  // cout << sum << endl;
  pre = 0;
  for (i = 0; i < N; i++) {
    cout << sum + abs(A[i + 1] - pre) - abs(A[i] - pre) - abs(A[i + 1] - A[i])
         << endl;
    pre = A[i];
  }
  // cin >> x;
  // cout << x * x * x << endl;
  // scanf("%u %u", &x, &y);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
typedef uint uint32_t;

int A[100002];

int main() {
  uint N;
  cin >> N;
  int sum = 0, pre = 0, i;
  for (i = 0; i < N; i++) {
    cin >> A[i];
    sum += abs(A[i] - pre);
    pre = A[i];
  }
  A[i] = 0;
  sum += abs(A[i] - A[i - 1]);
  // cout << i << endl;
  // for (int i = 0; i < N + 1; i++) {
  //     cout << "A[" << i << ']' <<  A[i] << endl;
  // }
  // cout << sum << endl;
  pre = 0;
  for (i = 0; i < N; i++) {
    cout << sum + abs(A[i + 1] - pre) - abs(A[i] - pre) - abs(A[i + 1] - A[i])
         << endl;
    pre = A[i];
  }
  // cin >> x;
  // cout << x * x * x << endl;
  // scanf("%u %u", &x, &y);
}
Problem p03401 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007;
// 最大公約数
int gcd(int s, int b) {
  if (b == 0)
    return s;
  return gcd(b, s % b);
}
signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) { cin >> a[i]; }
  int b = 0, ans = 0;
  rep(i, n) {
    ans = 0;
    b = 0;
    rep(j, n) {
      if (i != j) {
        ans += abs(b - a[j]);
        b = a[j];
      }
    }
    ans += abs(0 - b);
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007;
// 最大公約数
int gcd(int s, int b) {
  if (b == 0)
    return s;
  return gcd(b, s % b);
}
signed main() {
  int n;
  cin >> n;
  int a[n + 2];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[0] = 0;
  a[n + 1] = 0;
  int kai = 0;
  for (int i = 0; i <= n; i++) {
    kai += abs(a[i] - a[i + 1]);
  }
  int ans;
  for (int i = 1; i <= n; i++) {
    ans =
        abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i + 1] - a[i - 1]);
    cout << kai - ans << endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int N;
  int A[110000];
  int price = 0;
  std::cin >> N;
  A[0] = 0;
  A[N + 1] = 0;
  for (int i = 1; i <= N; i++) {
    std::cin >> A[i];
  }

  for (int p = 1; p <= N; p++) {
    for (int i = 0; i <= N + 1; i++) {
      if (p == i + 1) {
        int x = A[i] - A[i + 2];
        price = price + abs(x);
        i++;
      } else {
        int x = A[i] - A[i + 1];
        price = price + abs(x);
      }
    }
    std::cout << price << std::endl;
    price = 0;
  }
}


Fixed submission:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int N;
  int A[110000];
  int price = 0;
  std::cin >> N;
  A[0] = 0;
  A[N + 1] = 0;
  for (int i = 1; i <= N; i++) {
    std::cin >> A[i];
  }

  for (int i = 0; i <= N; i++) {
    price = price + abs(A[i + 1] - A[i]);
  }
  for (int i = 0; i < N; i++) {
    std::cout << price - (abs(A[i] - A[i + 1])) - (abs(A[i + 1] - A[i + 2])) +
                     abs(A[i] - A[i + 2])
              << std::endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, m, n) for (int i = m; i < (n); i++)
#define RFOR(i, m, n) for (int i = (m - 1); i >= 0; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define print(ele) cout << ele << endl
#define print10(ele) cout << fixed << setprecision(10) << ele << endl

using namespace std;
typedef long long ll;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef vector<ll> lvec;

const int mod = 1e9 + 7;
const ll INF = 1000000000000000000LL;

int main() {
  int n;
  cin >> n;
  ivec a(n + 2);
  REP(i, n) cin >> a[i + 1];

  ivec ans(n);
  FOR(j, 1, n + 1) {
    int answer = 0;
    REP(i, n + 1) {
      if (i == j)
        continue;
      if (i + 1 == j)
        answer += abs(a[i] - a[i + 2]);
      else
        answer += abs(a[i] - a[i + 1]);
    }
    print(answer);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, m, n) for (int i = m; i < (n); i++)
#define RFOR(i, m, n) for (int i = (m - 1); i >= 0; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define print(ele) cout << ele << endl
#define print10(ele) cout << fixed << setprecision(10) << ele << endl

using namespace std;
typedef long long ll;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef vector<ll> lvec;

const int mod = 1e9 + 7;
const ll INF = 1000000000000000000LL;

int main() {
  int n;
  cin >> n;
  ivec a(n + 2);
  REP(i, n) cin >> a[i + 1];

  int d = 0;
  REP(i, n + 1) { d += abs(a[i] - a[i + 1]); }
  FOR(i, 1, n + 1) {
    int ans = d - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]) +
              abs(a[i - 1] - a[i + 1]);
    print(ans);
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

int solve(vector<int> a, int i, int ans) {
  return ans - abs(a[i + 1] - a[i]) - abs(a[i] - a[i - 1]) +
         abs(a[i + 1] - a[i - 1]);
}

signed main() {
  int n;
  cin >> n;

  vector<int> a(n + 2);
  rep(i, n) cin >> a[i + 1];

  int ans = 0;
  rep(i, (int)a.size() - 1) { ans += abs(a[i + 1] - a[i]); }

  for (int i = 1; i <= n; i++) {
    cout << solve(a, i, ans) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

int solve(const vector<int> &a, int i, int ans) {
  return ans - abs(a[i + 1] - a[i]) - abs(a[i] - a[i - 1]) +
         abs(a[i + 1] - a[i - 1]);
}

signed main() {
  int n;
  cin >> n;

  vector<int> a(n + 2);
  rep(i, n) cin >> a[i + 1];

  int ans = 0;
  rep(i, (int)a.size() - 1) { ans += abs(a[i + 1] - a[i]); }

  for (int i = 1; i <= n; i++) {
    cout << solve(a, i, ans) << endl;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <ciso646>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
int main() {
  ll N;
  cin >> N;
  ll a[10010];
  for (ll i = 1; i <= N; i++) {
    cin >> a[i];
  }
  ll sum = 0;
  a[0] = 0;
  a[N + 1] = 0;
  for (ll i = 0; i <= N; i++) {
    sum += abs(a[i + 1] - a[i]);
  }
  for (ll i = 1; i <= N; i++) {
    cout << sum + abs(a[i - 1] - a[i + 1]) - abs(a[i - 1] - a[i]) -
                abs(a[i] - a[i + 1])
         << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <ciso646>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
int main() {
  ll N;
  cin >> N;
  ll a[N + 10];
  for (ll i = 1; i <= N; i++) {
    cin >> a[i];
  }
  ll sum = 0;
  a[0] = 0;
  a[N + 1] = 0;
  for (ll i = 0; i <= N; i++) {
    sum += abs(a[i + 1] - a[i]);
  }
  for (ll i = 1; i <= N; i++) {
    cout << sum + abs(a[i - 1] - a[i + 1]) - abs(a[i - 1] - a[i]) -
                abs(a[i] - a[i + 1])
         << endl;
  }
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int N, A[10001];
  long long int ans = 0, t;

  cin >> N;

  A[0] = 0;
  A[N + 1] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];

    ans += abs(A[i] - A[i - 1]);
  }
  ans += abs(A[N]);

  for (int i = 1; i <= N; i++) {
    if (abs(A[i + 1] - A[i - 1]) !=
        abs(A[i] - A[i - 1]) + abs(A[i] - A[i + 1])) {
      t = ans;
      t -= -abs(A[i + 1] - A[i - 1]) + abs(A[i] - A[i - 1]) +
           abs(A[i] - A[i + 1]);
      cout << t << endl;
    } else
      cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N, A[100001];
  long long int ans = 0, t;

  cin >> N;

  A[0] = 0;
  A[N + 1] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];

    ans += abs(A[i] - A[i - 1]);
  }
  ans += abs(A[N]);

  for (int i = 1; i <= N; i++) {
    if (abs(A[i + 1] - A[i - 1]) !=
        abs(A[i] - A[i - 1]) + abs(A[i] - A[i + 1])) {
      t = ans;
      t -= -abs(A[i + 1] - A[i - 1]) + abs(A[i] - A[i - 1]) +
           abs(A[i] - A[i + 1]);
      cout << t << endl;
    } else
      cout << ans << endl;
  }

  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>

pii a[10000 + 5];
int main() {
  std::ios::sync_with_stdio(false);

  int N, i;
  cin >> N;
  ll tot = 0;
  a[0].ss = 0;
  a[0].ff = 0;
  f(i, 1, N + 1) {
    ll k;
    cin >> k;
    a[i].ff = k;
    a[i].ss = abs(a[i - 1].ff - k);
    tot += a[i].ss;
  }

  a[0].ss = abs(a[N].ff);
  tot += a[0].ss;
  for (int i = 1; i <= N; i++) {
    if (i == N) {
      cout << tot - a[0].ss - a[N].ss + abs(a[i - 1].ff) << endl;
    } else {
      cout << tot - a[i].ss - a[i + 1].ss + abs(a[i - 1].ff - a[i + 1].ff)
           << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>

pii a[100005];
int main() {
  std::ios::sync_with_stdio(false);

  int N, i;
  cin >> N;
  ll tot = 0;
  a[0].ss = 0;
  a[0].ff = 0;
  f(i, 1, N + 1) {
    ll k;
    cin >> k;
    a[i].ff = k;
    a[i].ss = abs(a[i - 1].ff - k);
    tot += a[i].ss;
  }

  a[0].ss = abs(a[N].ff);
  tot += a[0].ss;
  for (int i = 1; i <= N; i++) {
    if (i == N) {
      cout << tot - a[0].ss - a[N].ss + abs(a[i - 1].ff) << endl;
    } else {
      cout << tot - a[i].ss - a[i + 1].ss + abs(a[i - 1].ff - a[i + 1].ff)
           << endl;
    }
  }

  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
int a[10005];
int main() {
  int n, sum;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sum += abs(a[1]);
  sum += abs(a[n]);
  for (int i = 1; i < n; i++) {
    sum += abs(a[i + 1] - a[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      cout << sum - abs(a[1]) - abs(a[2] - a[1]) + abs(a[2]) << endl;
    } else if (i == n) {
      cout << sum - abs(a[n]) - abs(a[n] - a[n - 1]) + abs(a[n - 1]) << endl;
    } else
      cout << sum - abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i]) +
                  abs(a[i + 1] - a[i - 1])
           << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
int a[100005];
int main() {
  int n, sum;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sum += abs(a[1]);
  sum += abs(a[n]);
  for (int i = 1; i < n; i++) {
    sum += abs(a[i + 1] - a[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      cout << sum - abs(a[1]) - abs(a[2] - a[1]) + abs(a[2]) << endl;
    } else if (i == n) {
      cout << sum - abs(a[n]) - abs(a[n] - a[n - 1]) + abs(a[n - 1]) << endl;
    } else
      cout << sum - abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i]) +
                  abs(a[i + 1] - a[i - 1])
           << endl;
  }
  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(10000);
  a[0] = 0;
  a[n + 1] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  int sum = 0;
  for (int i = 0; i < n + 1; i++) {
    sum = sum + abs(a[i + 1] - a[i]);
  }
  for (int i = 1; i < n + 1; i++) {
    cout << sum - abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i]) +
                abs(a[i + 1] - a[i - 1])
         << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(100000);
  a[0] = 0;
  a[n + 1] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  int sum = 0;
  for (int i = 0; i < n + 1; i++) {
    sum = sum + abs(a[i + 1] - a[i]);
  }
  for (int i = 1; i < n + 1; i++) {
    cout << sum - abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i]) +
                abs(a[i + 1] - a[i - 1])
         << endl;
  }

  return 0;
}
Problem p03401 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N + 2);
  int sum = 0;
  int tempsum = 0;
  for (int i = 0; i <= N; i++) {
    if (i == 0) {
      A[0] = 5000;
      A[N + 1] = 5000;
      continue;
    }
    cin >> A[i];
    A[i] += 5000;
    sum += abs(A[i - 1] - A[i]);
  }
  sum += abs(A[N + 1] - A[N]);
  tempsum = sum;

  for (int i = 1; i <= N; i++) {
    sum = tempsum;
    if (A[i - 1] < A[i] && A[i - 1] < A[i + 1] && A[i] > A[i + 1]) {
      sum -= abs(A[i + 1] - A[i]) * 2;
    } else if (A[i - 1] < A[i] && A[i - 1] >= A[i + i]) {
      sum -= abs(A[i - 1] - A[i]) * 2;
    } else if (A[i - 1] > A[i] && A[i - 1] > A[i + 1] && A[i + 1] > A[i]) {
      sum -= abs(A[i + 1] - A[i]) * 2;
    } else if (A[i - 1] > A[i] && A[i - 1] <= A[i + 1]) {
      sum -= abs(A[i - 1] - A[i]) * 2;
    }
    cout << sum << endl;
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N + 2);
  int sum = 0;
  int tempsum = 0;
  for (int i = 0; i <= N; i++) {
    if (i == 0) {
      A[0] = 5000;
      A[N + 1] = 5000;
      continue;
    }
    cin >> A[i];
    A[i] += 5000;
    sum += abs(A[i - 1] - A[i]);
  }
  sum += abs(A[N + 1] - A[N]);
  tempsum = sum;

  for (int i = 1; i <= N; i++) {
    sum = tempsum;
    if (A[i - 1] < A[i] && A[i - 1] < A[i + 1] && A[i] > A[i + 1]) {
      sum -= abs(A[i + 1] - A[i]) * 2;
    } else if (A[i - 1] < A[i] && A[i - 1] >= A[i + 1]) {
      sum -= abs(A[i - 1] - A[i]) * 2;
    } else if (A[i - 1] > A[i] && A[i - 1] > A[i + 1] && A[i + 1] > A[i]) {
      sum -= abs(A[i + 1] - A[i]) * 2;
    } else if (A[i - 1] > A[i] && A[i - 1] <= A[i + 1]) {
      sum -= abs(A[i - 1] - A[i]) * 2;
    }
    cout << sum << endl;
  }

  return 0;
}