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

#define _USE_MATH_DEFINES

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

#define int long long
// region TEMPLATE
////@formatter:off
#define double long double
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void __read(T &a) { cin >> a; }
template <typename T, typename... Args> void __read(T &a, Args &...args) {
  cin >> a;
  __read(args...);
}
#define make(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  __read(__VA_ARGS__)
#define makev(a, n)                                                            \\
  VI a(n);                                                                     \\
  FOR(n) cin >> a[i]
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define FORIT(p) for (auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for (signed i = 0; i < (b); i++)
#define FORE(i, l, r) for (int i = (l); i < (r); i++)
#define NFOR(i, n) for (int i = (((int)n) - 1); i >= 0; i--)
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PPII;
constexpr ll M7 = 1000000007ll;
constexpr ll M9 = 1000000009ll;
constexpr ll MFFT = 998244353ll;
template <class T> void outv(T &a) {
  for (auto &x : a)
    cout << x << ' ';
}
static mt19937
    rnd(static_cast<unsigned>(chrono::steady_clock::now()
                                  .time_since_epoch()
                                  .count())); // NOLINT(cert-err58-cpp)
auto __fast_io__ =
    (ios_base::sync_with_stdio(false),
     cin.tie(
         nullptr)); // NOLINT(cert-err58-cpp,cppcoreguidelines-interfaces-global-init)
////@formatter:on
// endregion

int32_t main() {
  make(int, n, m, q);
  vector<tuple<int, int, int, int>> ar(q);
  for (auto &[a, b, c, d] : ar) {
    cin >> a >> b >> c >> d;
    a--, b--;
  }
  VI val(n, 1);
  int ans = 0;
  while (val[0] == 1) {
    int sm = 0;
    for (auto &[a, b, c, d] : ar) {
      if (val[b] - val[a] == c) {
        sm += d;
      }
    }
    ans = max(ans, sm);
    int i = n - 1;
    while (i >= 0 && val[i] == m) {
      i--;
    }
    if (i < 0) {
      break;
    }
    val[i]++;
    while (i + 1 < n) {
      val[i + 1] = val[i];
      i++;
    }
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#define _USE_MATH_DEFINES

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

#define int long long
// region TEMPLATE
////@formatter:off
#define double long double
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void __read(T &a) { cin >> a; }
template <typename T, typename... Args> void __read(T &a, Args &...args) {
  cin >> a;
  __read(args...);
}
#define make(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  __read(__VA_ARGS__)
#define makev(a, n)                                                            \\
  VI a(n);                                                                     \\
  FOR(n) cin >> a[i]
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define FORIT(p) for (auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for (signed i = 0; i < (b); i++)
#define FORE(i, l, r) for (int i = (l); i < (r); i++)
#define NFOR(i, n) for (int i = (((int)n) - 1); i >= 0; i--)
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PPII;
constexpr ll M7 = 1000000007ll;
constexpr ll M9 = 1000000009ll;
constexpr ll MFFT = 998244353ll;
template <class T> void outv(T &a) {
  for (auto &x : a)
    cout << x << ' ';
}
static mt19937
    rnd(static_cast<unsigned>(chrono::steady_clock::now()
                                  .time_since_epoch()
                                  .count())); // NOLINT(cert-err58-cpp)
auto __fast_io__ =
    (ios_base::sync_with_stdio(false),
     cin.tie(
         nullptr)); // NOLINT(cert-err58-cpp,cppcoreguidelines-interfaces-global-init)
////@formatter:on
// endregion

int32_t main() {
  make(int, n, m, q);
  vector<tuple<int, int, int, int>> ar(q);
  for (auto &[a, b, c, d] : ar) {
    cin >> a >> b >> c >> d;
    a--, b--;
  }
  VI val(n, 1);
  int ans = 0;
  while (val[0] == 1) {
    int sm = 0;
    for (auto &[a, b, c, d] : ar) {
      if (val[b] - val[a] == c) {
        sm += d;
      }
    }
    ans = max(ans, sm);
    int i = n - 1;
    while (val[i] == m) {
      i--;
    }
    val[i]++;
    while (i + 1 < n) {
      val[i + 1] = val[i];
      i++;
    }
  }
  cout << ans;
  return 0;
}