Problem p02984 (C++)

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

#define fi first
#define se second
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int n;

ll calc(vector<ll> a, ll p) {
  ll temp = p;
  rep(i, n) {
    // cout << "i:" << i << " temp:" << temp << " a[i]:" << a[i] << endl;
    if (a[i] < temp && i % 2 == 0)
      return -1;
    else if (a[i] < temp && i % 2 == 1)
      return -2;
    else
      temp = a[i] - temp;
  }
  // cout << "p:" << p << " temp:" << temp << endl;
  if (p == temp)
    return 1;
  /*else if (p < temp) {
          cout << "p:" << p << " temp:" << temp << endl;
          return -1;
  }*/
  /*else if (p > temp) {
          cout << "p:" << p << " temp:" << temp << endl;
          return -2;
  }*/
}

ll binary_search(vector<ll> a, ll max) {
  ll left = 0, right = max;
  while (right >= left) {
    int mid = (right + left) / 2;
    // cout << "left:" << left << " right: " << right << " mid:" << mid << endl;
    // cout << "calc: " << calc(a,mid) << endl;
    ll temp = calc(a, mid);
    if (temp == 1)
      return mid;
    else if (temp == -1)
      right = mid - 1;
    else if (temp == -2)
      left = mid + 1;
  }
}
int main() {
  cin >> n;
  vector<ll> a(n);
  ll max = -1e18;
  rep(i, n) {
    cin >> a[i];
    chmax(max, a[i]);
  }
  vector<ll> ans(n);

  ans[0] = binary_search(a, max);
  for (int i = 1; i < n; i++) {
    ans[i] = a[i - 1] - ans[i - 1];
  }
  rep(i, n) {
    cout << ans[i] * 2;
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;

#define fi first
#define se second
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int n;

ll calc(vector<ll> a, ll p) {
  ll temp = p;
  rep(i, n) {
    // cout << "i:" << i << " temp:" << temp << " a[i]:" << a[i] << endl;
    if (a[i] < temp && i % 2 == 0)
      return -1;
    else if (a[i] < temp && i % 2 == 1)
      return -2;
    else
      temp = a[i] - temp;
  }
  // cout << "p:" << p << " temp:" << temp << endl;
  if (p == temp)
    return 1;
  else if (p < temp) {
    // cout << "p:" << p << " temp:" << temp << endl;
    return -2;
  } else if (p > temp) {
    // cout << "p:" << p << " temp:" << temp << endl;
    return -1;
  }
}

ll binary_search(vector<ll> a, ll max) {
  ll left = 0, right = max;
  while (right >= left) {
    int mid = (right + left) / 2;
    // cout << "left:" << left << " right: " << right << " mid:" << mid << endl;
    // cout << "calc: " << calc(a,mid) << endl;
    ll temp = calc(a, mid);
    if (temp == 1)
      return mid;
    else if (temp == -1)
      right = mid - 1;
    else if (temp == -2)
      left = mid + 1;
  }
}
int main() {
  cin >> n;
  vector<ll> a(n);
  ll max = -1e18;
  rep(i, n) {
    cin >> a[i];
    chmax(max, a[i]);
  }
  vector<ll> ans(n);

  ans[0] = binary_search(a, max);
  for (int i = 1; i < n; i++) {
    ans[i] = a[i - 1] - ans[i - 1];
  }
  rep(i, n) {
    cout << ans[i] * 2;
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }

  return 0;
}