Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define SZ(a) int((a).size())
#define _REP(_1, _2, _3, _4, name, ...) name
#define _REP4(i, b, e, s)                                                      \
  for (decltype(e) _b = (b), _e = (e), i = _b + (0 < (s) ? 0 : (s));           \
       (0 < (s) ? i < _e : _e <= i); i += (s))
#define _REP3(i, b, e)                                                         \
  for (decltype(e) _b = (b), _e = (e), i = (_b < _e ? _b : _b - 1);            \
       (_b < _e ? i < _e : _e <= i); (_b < _e ? i++ : i--))
#define _REP2(i, n) for (decltype(n) i = 0, _n = (n); i < _n; i++)
#define _REP1(n) for (decltype(n) _i = 0, _n = (n); _i < _n; _i++)
#define REP(...) _REP(__VA_ARGS__, _REP4, _REP3, _REP2, _REP1)(__VA_ARGS__)
#define FOR(it, c) for (auto &&it = begin(c); it != end(c); it++)
#define ROF(it, c) for (auto &&it = rbegin(c); it != rend(c); it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define INT(n)                                                                 \
  int n;                                                                       \
  assert(0 < scanf("%d", &n))

template <typename T>
bool next_combination(const T first, const T last, int k) {
  const T subset = first + k;
  // empty container | k = 0 | k == n
  if (first == last || first == subset || last == subset) {
    return false;
  }
  T src = subset;
  while (first != src) {
    src--;
    if (*src < *(last - 1)) {
      T dest = subset;
      while (*src >= *dest) {
        dest++;
      }
      iter_swap(src, dest);
      rotate(src + 1, dest + 1, last);
      rotate(subset, subset + (last - dest) - 1, last);
      return true;
    }
  }
  // restore
  rotate(first, subset, last);
  return false;
}

int main() {
  INT(n);
  INT(m);
  INT(q);
  int ans = 0;
  vector<vector<int>> qs;
  REP(q) {
    INT(a);
    INT(b);
    INT(c);
    INT(d);
    qs.PB({a, b, c, d});
  }
  vector<int> v;
  REP(i, n + m - 1) { v.PB(i + 1); }
  do {
    vector<int> w(n + m - 1, 0), as;
    REP(i, m - 1) { w[v[i]] = 1; }
    int ai = 1;
    for (int e : w) {
      if (e == 0) {
        as.PB(ai);
      } else {
        ai++;
      }
    }
    int s = 0;
    for (vector<int> qi : qs) {
      if (as[qi[1] - 1] - as[qi[0] - 1] == qi[2]) {
        s += qi[3];
      }
    }
    ans = max(ans, s);
  } while (next_combination(v.begin(), v.end(), m - 1));
  printf("%d\n", ans);
  return 0;
}

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

#define SZ(a) int((a).size())
#define _REP(_1, _2, _3, _4, name, ...) name
#define _REP4(i, b, e, s)                                                      \
  for (decltype(e) _b = (b), _e = (e), i = _b + (0 < (s) ? 0 : (s));           \
       (0 < (s) ? i < _e : _e <= i); i += (s))
#define _REP3(i, b, e)                                                         \
  for (decltype(e) _b = (b), _e = (e), i = (_b < _e ? _b : _b - 1);            \
       (_b < _e ? i < _e : _e <= i); (_b < _e ? i++ : i--))
#define _REP2(i, n) for (decltype(n) i = 0, _n = (n); i < _n; i++)
#define _REP1(n) for (decltype(n) _i = 0, _n = (n); _i < _n; _i++)
#define REP(...) _REP(__VA_ARGS__, _REP4, _REP3, _REP2, _REP1)(__VA_ARGS__)
#define FOR(it, c) for (auto &&it = begin(c); it != end(c); it++)
#define ROF(it, c) for (auto &&it = rbegin(c); it != rend(c); it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define INT(n)                                                                 \
  int n;                                                                       \
  assert(0 < scanf("%d", &n))

template <typename T>
bool next_combination(const T first, const T last, int k) {
  const T subset = first + k;
  // empty container | k = 0 | k == n
  if (first == last || first == subset || last == subset) {
    return false;
  }
  T src = subset;
  while (first != src) {
    src--;
    if (*src < *(last - 1)) {
      T dest = subset;
      while (*src >= *dest) {
        dest++;
      }
      iter_swap(src, dest);
      rotate(src + 1, dest + 1, last);
      rotate(subset, subset + (last - dest) - 1, last);
      return true;
    }
  }
  // restore
  rotate(first, subset, last);
  return false;
}

int main() {
  INT(n);
  INT(m);
  INT(q);
  int ans = 0;
  vector<vector<int>> qs;
  REP(q) {
    INT(a);
    INT(b);
    INT(c);
    INT(d);
    qs.PB({a, b, c, d});
  }
  vector<int> v;
  REP(i, n + m - 1) { v.PB(i); }
  do {
    vector<int> w(n + m - 1, 0), as;
    REP(i, m - 1) { w[v[i]] = 1; }
    int ai = 1;
    for (int e : w) {
      if (e == 0) {
        as.PB(ai);
      } else {
        ai++;
      }
    }
    int s = 0;
    for (vector<int> qi : qs) {
      if (as[qi[1] - 1] - as[qi[0] - 1] == qi[2]) {
        s += qi[3];
      }
    }
    ans = max(ans, s);
  } while (next_combination(v.begin(), v.end(), m - 1));
  printf("%d\n", ans);
  return 0;
}