Problem p03808 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int xcomp(vector<LL> &a, vector<LL> &b) { return a[0] < b[0]; }

#define XSORT(c) sort((c).begin(), (c).end(), xcomp)

#define ALL(x) (x).begin(), (x).end()
LL mod = 1000000007;

int main() {
  LL N;
  cin >> N;
  LL sum = 0;
  LL waru = N * (N + 1) / 2;
  vector<LL> A(N, 0);
  LL kaisutasu = 0;
  REP(i, N) {
    cin >> A[i];
    sum += A[i];
    if (sum >= waru) {
      kaisutasu += sum / waru;
      sum -= sum / waru * waru;
    }
  }
  if (sum % waru == 0) {
    LL kaisu = sum / waru + kaisutasu;
    REP(i, N) { A[i] -= (i + 1) * kaisu; }
    LL checker = 1;
    while (true) {
      while (A[checker] == A[checker - 1]) {
        checker++;
        if (checker >= N) {
          break;
        }
      }
      if (checker >= N) {
        break;
      }
      if (A[checker] > A[checker - 1]) {
        break;
      }
      if (checker == 0) {
        break;
      }
      /*
      LL k = (-A[checker] - 1) / checker + 1;
      //LL add = min(kuso + k - 1, kaisu) - kuso + 1;
      //kuso = min(kuso + add - 1, kaisu);
      LL add = k;
      */
      LL add = (A[checker - 1] - A[checker]) / N;
      REP(i, checker) { A[i] -= (N - checker) * add; }
      FOR(i, checker, N) { A[i] += (checker)*add; }
    }
    LL flag = true;
    REP(i, N) {
      if (A[i] != 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  } else {
    cout << "NO" << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int xcomp(vector<LL> &a, vector<LL> &b) { return a[0] < b[0]; }

#define XSORT(c) sort((c).begin(), (c).end(), xcomp)

#define ALL(x) (x).begin(), (x).end()
LL mod = 1000000007;

int main() {
  LL N;
  cin >> N;
  LL sum = 0;
  LL waru = N * (N + 1) / 2;
  vector<LL> A(N, 0);
  LL kaisutasu = 0;
  REP(i, N) {
    cin >> A[i];
    sum += A[i];
    if (sum >= waru) {
      kaisutasu += sum / waru;
      sum -= sum / waru * waru;
    }
  }
  if (sum % waru == 0) {
    LL kaisu = sum / waru + kaisutasu;
    REP(i, N) { A[i] -= (i + 1) * kaisu; }
    LL checker = 1;
    while (true) {
      while (A[checker] == A[checker - 1]) {
        checker++;
        if (checker >= N) {
          break;
        }
      }
      if (checker >= N) {
        break;
      }
      if (A[checker] > A[checker - 1]) {
        break;
      }
      if (checker == 0) {
        break;
      }
      /*
      LL k = (-A[checker] - 1) / checker + 1;
      //LL add = min(kuso + k - 1, kaisu) - kuso + 1;
      //kuso = min(kuso + add - 1, kaisu);
      LL add = k;
      */
      LL add = (A[checker - 1] - A[checker]) / N;
      if (add == 0) {
        break;
      }
      REP(i, checker) { A[i] -= (N - checker) * add; }
      FOR(i, checker, N) { A[i] += (checker)*add; }
    }
    LL flag = true;
    REP(i, N) {
      if (A[i] != 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  } else {
    cout << "NO" << endl;
  }
}