Problem p02695 (C++)

Buggy submission (status: Runtime Error):
// raja1999

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
// setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define int ll

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// std::ios::sync_with_stdio(false);
int n, m, q;
int res = 0;
int a[55], b[55], c[55], d[55];
int gen(int pos, vi v, int x) {
  int j, i;
  if (pos == n) {
    int ans = 0;
    rep(i, q) {
      if (v[b[i]] - v[a[i]] == c[i]) {
        ans += d[i];
      }
    }
    res = max(res, ans);
    return 0;
  }
  vi v1;
  f(j, x, m + 1) {
    v1 = v;
    v1.pb(j);
    gen(pos + 1, v1, j);
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int i;
  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  vi vec;
  gen(0, vec, 1);
  cout << res << endl;
  return 0;
}


Fixed submission:
// raja1999

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
// setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define int ll

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// std::ios::sync_with_stdio(false);
int n, m, q;
int res = 0;
int a[55], b[55], c[55], d[55];
int gen(int pos, vi v, int x) {
  int j, i;
  if (pos == n) {
    int ans = 0;
    rep(i, q) {
      if (v[b[i]] - v[a[i]] == c[i]) {
        ans += d[i];
      }
    }
    res = max(res, ans);
    return 0;
  }
  vi v1;
  f(j, x, m + 1) {
    v1 = v;
    v1.pb(j);
    gen(pos + 1, v1, j);
  }
  return 0;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int i;
  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  vi vec;
  gen(0, vec, 1);
  cout << res << endl;
  return 0;
}