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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int n, x, m;
bool isans = false;
int sum = -1;
vector<int> l;
vector<int> r;
vector<int> s;
vector<int> ans;

void saiki(int n1, vector<int> ar) {
  if (n1 >= n) {
    REP(i, n) { ar[i + 1] += ar[i]; }
    bool flg = true;
    REP(i, m) {
      if (ar[r[i]] - ar[l[i] - 1] != s[i]) {
        flg = false;
      }
    }
    if (!flg) {
      return;
    } else
      isans = true;

    if (sum < ar[ar.size() - 1]) {
      sum = ar[ar.size() - 1];
      sum = 0;
      ans = vector<int>(n);
      REP(i, ar.size() - 1) {
        ans[i] = ar[i + 1] - ar[i];
        ;
      }
    }
    return;
  }

  REP(i, x + 1) {
    ar[n1 + 1] = i;
    saiki(n1 + 1, ar);
  }
}

int main() {
  cin >> n >> x >> m;
  vector<int> ar(n + 1, x);
  ll six = pow(x, n);
  REP(i, m) {
    int a, b, c;
    cin >> a;
    if (cin.eof())
      break;
    cin >> b >> c;
    l.PB(a);
    r.PB(b);
    s.PB(c);
  }

  sum = -1;
  ar[0] = 0;
  saiki(0, ar);
  if (isans) {
    cout << ans[0];
    FOR(i, 1, ans.size()) { cout << " " << ans[i]; }
    cout << endl;
  } else
    cout << -1 << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int n, x, m;
bool isans = false;
pair<int, ll> sum;
vector<int> l;
vector<int> r;
vector<int> s;
vector<int> ans;

void saiki(int n1, vector<int> ar) {
  if (n1 > n) {
    REP(i, n) { ar[i + 1] += ar[i]; }
    bool flg = true;
    REP(i, m) {
      if (ar[r[i]] - ar[l[i] - 1] != s[i]) {
        flg = false;
      }
    }
    if (!flg) {
      return;
    } else
      isans = true;

    if (sum.first < ar[ar.size() - 1]) {
      sum.first = ar[ar.size() - 1];
      sum.second = 0;
      ans = vector<int>(n);
      REP(i, ar.size() - 1) {
        ans[i] = ar[i + 1] - ar[i];
        ;
      }
    }
    return;
  }

  REP(i, x + 1) {
    ar[n1 + 1] = i;
    saiki(n1 + 1, ar);
  }
}

int main() {
  cin >> n >> x >> m;
  vector<int> ar(n + 1, x);
  ll six = pow(x, n);
  REP(i, m) {
    int a, b, c;
    cin >> a;
    if (cin.eof())
      break;
    cin >> b >> c;
    l.PB(a);
    r.PB(b);
    s.PB(c);
  }

  sum.first = 0;
  sum.second = 0;
  ar[0] = 0;
  saiki(0, ar);
  if (isans) {
    cout << ans[0];
    FOR(i, 1, n) { cout << " " << ans[i]; }
    cout << endl;
  } else
    cout << -1 << endl;

  return 0;
}