alexjercan/bugnet huggingface dataset:
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  int A[10];
  int atai[q][4];
  long long ans = 0;
  long long max = 0;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> atai[i][j];
    }
  }

  for (A[0] = 1; A[0] <= m; A[0]++) {
    for (A[1] = A[0]; A[1] <= m; A[1]++) {
      for (A[2] = A[1]; A[2] <= m; A[2]++) {
        for (A[3] = A[2]; A[3] <= m; A[3]++) {
          for (A[4] = A[3]; A[4] <= m; A[4]++) {
            for (A[5] = A[4]; A[5] <= m; A[5]++) {
              for (A[6] = A[5]; A[6] <= m; A[6]++) {
                for (A[7] = A[6]; A[7] <= m; A[7]++) {
                  for (A[8] = A[7]; A[8] <= m; A[8]++) {
                    for (A[9] = A[8]; A[9] <= m; A[9]++) {
                      ans = 0;
                      for (int i = 0; i < q; i++) {
                        if (A[atai[i][1] - 1] - A[atai[i][0] - 1] ==
                            atai[i][2]) {
                          ans += atai[i][3];
                        }
                      }
                      if (ans >= max)
                        max = ans;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << max << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  int A[n];
  int atai[q][4];
  long long ans;
  long long max = 0;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> atai[i][j];
    }
  }

  for (A[0] = 0; A[0] < n; A[0]++) {
    for (A[1] = A[0]; A[1] < n; A[1]++) {
      for (A[2] = A[1]; A[2] < n; A[2]++) {
        for (A[3] = A[2]; A[3] < n; A[3]++) {
          for (A[4] = A[3]; A[4] < n; A[4]++) {
            for (A[5] = A[4]; A[5] < n; A[5]++) {
              for (A[6] = A[5]; A[6] < n; A[6]++) {
                for (A[7] = A[6]; A[7] < n; A[7]++) {
                  for (A[8] = A[7]; A[8] < n; A[8]++) {
                    for (A[9] = A[8]; A[9] < n; A[9]++) {
                      ans = 0;
                      for (int i = 0; i < q; i++) {
                        if (A[atai[i][1]] - A[atai[i][0]] == atai[i][2]) {
                          ans += atai[i][3];
                        }
                      }
                      if (ans > max)
                        max = ans;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << max << endl;

  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 10;
const int maxM = 1e6 + 10;
const int inf = 0x3f3f3f3f;

ll N, M, Q;
struct node {
  ll a, c, d;
};
struct node2 {
  ll a, b, c, d;
} qq[maxn];
vector<node> adj[maxn];
ll A[maxn];
ll res = 0;
int B[maxn], tail = 0;
void dfs(int id, int last = 1) {
  if (id > N) {
    ll sum = 0;
    for (int i = 1; i <= N; i++) {
      if (adj[i].size() == 0)
        continue;
      for (auto no : adj[i]) {
        if (A[i] - A[no.a] == no.c)
          sum += no.d;
      }
    }
    res = max(res, sum);
    return;
  }
  A[id] = last;
  dfs(id + 1, last);
  for (auto no : adj[id]) {
    ll cur = A[no.a] + no.c;
    if (cur > M || cur < last)
      continue;
    else {
      A[id] = cur;
      dfs(id + 1, cur);
    }
  }
}

int main() {
  // debug;
  ios;

  cin >> N >> M >> Q;
  for (int i = 1; i <= Q; i++) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    adj[b].pb({a, c, d});
    qq[i] = {a, b, c, d};
  }
  dfs(1);
  cout << res << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

int s[20];
int a[20], b[20], c[20], d[20];

int sum = -inf;
int n, m, q;

void check() {
  int now = 0;
  for (int i = 0; i < q; i++) {
    if (s[b[i]] - s[a[i]] == c[i]) {
      now += d[i];
    }
  }
  sum = max(sum, now);
}

void dfs(int now, int val) {
  s[now] = val;
  if (now == n) {
    check();
    return;
  }
  for (int i = val; i <= m; i++) {
    dfs(now + 1, i);
  }
  return;
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  dfs(1, 1);

  cout << sum << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef struct QRY {
  int a;
  int b;
  int c;
  int d;
} qry_t;

int main(void) {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<qry_t> qry(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> qry.at(i).a >> qry.at(i).b >> qry.at(i).c >> qry.at(i).d;
  }

  vector<vector<int>> A(1, vector<int>(N)), Am(0, vector<int>(N));
  A.at(0).at(0) = 1;

  /* 数列をDFSで列挙 */
  for (int i = 1; i < N; i++) {
    Am = A;
    A.clear();

    for (auto it = Am.begin(); it != Am.end(); ++it) {
      for (int j = it->at(i - 1); j <= M; ++j) {
        vector<int> a = *it;
        a[i] = j;
        A.push_back(a);
      }
    }
  }

  int ans = 0;
  for (auto it = A.begin(); it != A.end(); ++it) {
    int tmp = 0;
    for (int i = 0; i < Q; ++i) {
      if (it->at(qry.at(i).b - 1) - it->at(qry.at(i).a - 1) == qry.at(i).c) {
        tmp += qry.at(i).d;
      }
    }
    ans = max(tmp, ans);
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef struct QRY {
  int a;
  int b;
  int c;
  int d;
} qry_t;

int main(void) {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<qry_t> qry(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> qry.at(i).a >> qry.at(i).b >> qry.at(i).c >> qry.at(i).d;
  }

  vector<vector<int>> A(1, vector<int>(M)), Am(0, vector<int>(M));
  A.at(0).at(0) = 1;

  /* 数列をDFSで列挙 */
  for (int i = 1; i < N; i++) {
    Am = A;
    A.clear();

    for (auto it = Am.begin(); it != Am.end(); ++it) {
      for (int j = it->at(i - 1); j <= M; ++j) {
        vector<int> a = *it;
        a[i] = j;
        A.push_back(a);
      }
    }
  }

  int ans = 0;
  for (auto it = A.begin(); it != A.end(); ++it) {
    int tmp = 0;
    for (int i = 0; i < Q; ++i) {
      if (it->at(qry.at(i).b - 1) - it->at(qry.at(i).a - 1) == qry.at(i).c) {
        tmp += qry.at(i).d;
      }
    }
    ans = max(tmp, ans);
  }

  cout << ans << endl;

  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

void next_A(vector<int> &A, int M) {
  if (A.size() >= 1) {
    int tmp = A.at(A.size() - 1);
    if (tmp < M) {
      A.pop_back();
      A.push_back(tmp + 1);
    } else if (tmp == M) {
      A.pop_back();
      next_A(A, M);
      A.push_back(A.at(A.size() - 1));
    }
  }
}

void print_A(vector<int> &A, int N) {
  for (int i = 0; i < N; i++) {
    cout << A.at(i) << " ";
  }
  cout << endl;
}

int64_t factorial(int64_t n) {
  if (n > 1) {
    return n * factorial(n - 1);
  } else {
    return 1;
  }
}

int64_t nCm(int64_t n, int64_t m) {
  return factorial(n) / factorial(n - m) / factorial(m);
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  }

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    A.at(i) = 1;
  }

  int point = 0;
  int maxpoint = 0;
  int64_t R = nCm(N + M - 1, M - 1);
  for (int i = 0; i < Q; i++) {
    if (A.at(b.at(i) - 1) - A.at(a.at(i) - 1) == c.at(i)) {
      maxpoint += d.at(i);
    }
  }

  for (int64_t p = 0; p < R - 1; p++) {
    next_A(A, M);
    point = 0;
    for (int i = 0; i < Q; i++) {
      if (A.at(b.at(i) - 1) - A.at(a.at(i) - 1) == c.at(i)) {
        point += d.at(i);
      }
    }
    maxpoint = max(maxpoint, point);
  }
  cout << maxpoint << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 0)


Fixed submission:

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

void next_A(vector<int> &A, int M) {
  if (A.size() >= 1) {
    int tmp = A.at(A.size() - 1);
    if (tmp < M) {
      A.pop_back();
      A.push_back(tmp + 1);
    } else if (tmp == M) {
      A.pop_back();
      next_A(A, M);
      A.push_back(A.at(A.size() - 1));
    }
  }
}

int factorial(int n) {
  if (n > 1) {
    return n * factorial(n - 1);
  } else {
    return 1;
  }
}

int nCm(int n, int m) { return factorial(n) / factorial(n - m) / factorial(m); }

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  }

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    A.at(i) = 1;
  }

  int point;
  int max = 0;
  int R = nCm(N + M - 1, M - 1);

  for (int p = 0; p < R; p++) {
    point = 0;
    for (int i = 0; i < Q; i++) {
      if (A.at(b.at(i) - 1) - A.at(a.at(i) - 1) == c.at(i)) {
        point += d.at(i);
      }
    }
    if (point > max)
      max = point;
    next_A(A, M);
  }

  cout << max << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int funcovf(int *Ax, int i, int M) {
  if (i == 0) {
    Ax[0] = 2;
    return 0;
  }

  Ax[i]++;

  if (Ax[i] > M) {
    funcovf(Ax, i - 1, M);
    Ax[i] = Ax[i - 1];
  }
  return 0;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;

  int ai[50];
  int bi[50];
  int ci[50];
  int di[50];

  for (int i = 0; i < Q; i++) {
    cin >> ai[i] >> bi[i] >> ci[i] >> di[i];
  }

  int Ax[10];
  for (int i = 0; i < 10; i++) {
    Ax[i] = 1;
  }

  int maxnum = 0;

  while (1) {
    // check
    int tempnum = 0;
    for (int i = 0; i < Q; i++) {
      if ((Ax[bi[i] - 1] - Ax[ai[i] - 1]) == ci[i]) {
        tempnum = tempnum + di[i];
      }
    }
    if (tempnum > maxnum) {
      maxnum = tempnum;
    }

    // Ax[i] change
    funcovf(Ax, N - 1, M);
    if (Ax[0] > 1) {
      break;
    }
  }
  cout << maxnum << std::endl;
}


Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int funcovf(int *Ax, int i, int M) {
  Ax[i]++;

  if (Ax[i] > M) {
    funcovf(Ax, i - 1, M);
    Ax[i] = Ax[i - 1];
  }
  return 0;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;

  int ai[50];
  int bi[50];
  int ci[50];
  int di[50];

  for (int i = 0; i < Q; i++) {
    cin >> ai[i] >> bi[i] >> ci[i] >> di[i];
  }

  int Ax[10];
  for (int i = 0; i < 10; i++) {
    Ax[i] = 1;
  }

  int maxnum = 0;

  while (1) {
    // check
    int tempnum = 0;
    for (int i = 0; i < Q; i++) {
      if ((Ax[bi[i] - 1] - Ax[ai[i] - 1]) == ci[i]) {
        tempnum = tempnum + di[i];
      }
    }
    if (tempnum > maxnum) {
      maxnum = tempnum;
    }

    // Ax[i] change
    funcovf(Ax, N - 1, M);
    if (Ax[0] > 1) {
      break;
    }
  }
  cout << maxnum << std::endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

template <typename T> bool next_combination(T first, T last, int k) {
  T subset = first + k;
  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;
    }
  }
  rotate(first, subset, last);
  return false;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> A(M + N - 1);
  REP(i, M + N - 1) { A[i] = i + 1; }

  vector<vector<int>> itr(Q, vector<int>(4));
  REP(i, Q) {
    REP(j, 4) { cin >> itr[i][j]; }
  }

  int ans = 0;

  do {
    int count = 0;
    vector<int> arr(N);
    REP(n, N) { arr[n] = A[n] - n; }
    REP(i, Q) {
      if (arr[itr[i][1] - 1] - arr[itr[i][0] - 1] == itr[i][2]) {
        count += itr[i][3];
      }
    }
    ans = max(ans, count);
  } while (next_combination(ALL(A), N));

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

template <typename T> bool next_combination(T first, T last, int k) {
  T subset = first + k;
  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;
    }
  }
  rotate(first, subset, last);
  return false;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> A(M * 2 - 1);
  REP(i, M * 2 - 1) { A[i] = i + 1; }

  vector<vector<int>> itr(Q, vector<int>(4));
  REP(i, Q) {
    REP(j, 4) { cin >> itr[i][j]; }
  }

  int ans = 0;

  do {
    int count = 0;
    REP(i, Q) {
      vector<int> arr(N);
      REP(n, N) { arr[n] = A[n] - n; }
      if (arr[itr[i][1] - 1] - arr[itr[i][0] - 1] == itr[i][2]) {
        count += itr[i][3];
      }
    }
    ans = max(ans, count);
  } while (next_combination(ALL(A), N));

  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  queue<string> qq;
  qq.push("0");
  queue<string> s;
  while (!qq.empty()) {
    string f = qq.front();
    qq.pop();
    if (f.size() == n)
      s.push(f);
    else {
      int u = f[f.size() - 1] - '0';
      for (int i = u; i < m; i++) {
        qq.push(f + to_string(i));
      }
    }
  }
  int ans = 0;
  while (!s.empty()) {
    string f = s.front();
    s.pop();
    int r = 0;
    rep(i, q) {
      int g = f[a[i] - 1] - '0', h = f[b[i] - 1] - '0';
      if (h - g == c[i])
        r += d[i];
    }
    ans = max(ans, r);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  queue<string> qq;
  qq.push("1");
  queue<string> s;
  while (!qq.empty()) {
    string f = qq.front();
    qq.pop();
    if (f.size() == n)
      s.push(f);
    else {
      int u = f[f.size() - 1] - '0';
      for (int i = u; i <= m; i++) {
        qq.push(f + to_string(i));
      }
    }
  }
  int ans = 0;
  while (!s.empty()) {
    string f = s.front();
    s.pop();
    int r = 0;
    rep(i, q) {
      int g = f[a[i] - 1] - '0', h = f[b[i] - 1] - '0';
      if (h - g == c[i])
        r += d[i];
    }
    ans = max(ans, r);
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int n, m, q;
vector<int> a, b, c, d;
int ans;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i])
        now += d[i];
    }
    ans = max(ans, now);
    return;
  }

  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {

  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int n, m, q;
vector<int> a, b, c, d;
int ans;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i])
        now += d[i];
    }
    ans = max(ans, now);
    return;
  }

  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {

  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(vector<int>(1, 1));
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<vector<int>> abcd(Q, vector<int>(4));
  for (int i = 0; i < Q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    abcd[i][0] = a - 1;
    abcd[i][1] = b - 1;
    abcd[i][2] = c;
    abcd[i][3] = d;
  }

  int summax = 0;

  for (int tmp = 0; tmp < (1 << 19); tmp++) {
    bitset<20> s(tmp);
    if (s.count() == N + M - 1)
      break;
    if (s.count() != M - 1)
      continue;

    vector<int> A(0);
    int cnt = 1;
    for (int i = 0; i < N + M - 1; i++) {
      if (s[i] == 0)
        A.push_back(cnt);
      else
        cnt++;
    }

    int sum = 0;

    for (int i = 0; i < Q; i++) {
      if (A[abcd[i][1]] - A[abcd[i][0]] == abcd[i][2])
        sum += abcd[i][3];
    }

    if (summax < sum)
      summax = sum;
  }

  cout << summax << endl;

  return 0;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<vector<int>> abcd(Q, vector<int>(4));
  for (int i = 0; i < Q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    abcd[i][0] = a - 1;
    abcd[i][1] = b - 1;
    abcd[i][2] = c;
    abcd[i][3] = d;
  }

  priority_queue<int> sumlist;

  for (int tmp = 0; tmp < (1 << 10); tmp++) {
    bitset<10> s(tmp);
    if (s.count() != N)
      continue;

    vector<int> A(0);
    for (int i = 0; i < M; i++) {
      if (s.test(i))
        A.push_back(i);
    }

    if (A.size() != N)
      continue;

    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[abcd[i][1]] - A[abcd[i][0]] == abcd[i][2])
        sum += abcd[i][3];
    }

    sumlist.push(sum);
  }

  cout << sumlist.top() << endl;

  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using lint = long long;

int arr[50][4] = {0};
int Q;

int dfs(vector<int> A, int digit, int max, int N, int M) {
  if (N < digit) {
    int sum = 0;
    for (int i = 0; i <= Q; i++) {
      if (A[arr[i][1]] - A[arr[i][0]] == arr[i][2]) {
        sum += arr[i][3];
      }
    }

    return sum;
  }
  for (int i = A[digit - 1]; i < M; i++) {
    A[digit] = i;
    int x = dfs(A, digit + 1, max, N, M);
    if (x > max) {
      max = x;
    }
  }
  return max;
}

int main(void) {
  int N, M;
  cin >> N >> M >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2] >> arr[i][3];
  }
  vector<int> A(N + 1, 0);
  cout << dfs(A, 1, 0, N, M) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using lint = long long;

int arr[50][4] = {0};
int Q;

int dfs(vector<int> A, int digit, int max, int N, int M) {
  if (A.size() < digit) {
    int sum = 0;
    for (int i = 0; i <= Q; i++) {
      if (A[arr[i][1]] - A[arr[i][0]] == arr[i][2]) {
        sum += arr[i][3];
      }
    }

    return sum;
  }
  for (int i = A[digit - 1]; i < M; i++) {
    A[digit] = i;
    int x = dfs(A, digit + 1, max, N, M);
    if (x > max) {
      max = x;
    }
  }
  return max;
}

int main(void) {
  int N, M;
  cin >> N >> M >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2] >> arr[i][3];
  }
  vector<int> A(N + 1, 0);
  cout << dfs(A, 1, 0, N, M) << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

vector<string> all;

void solve(string s, long long n, long long m) {
  int len = s.size();
  string p;
  if (len == n)
    all.push_back(s);
  else {
    for (int i = s[len - 1] - 'a'; i < m; i++) {
      p = 'a' + i;
      solve(s + p, n, m);
    }
  }
  return;
}

int main() {
  long long n, m, q, ans, rans = 0;
  string p;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  rep(i, m) {
    p = 'a' + i;
    solve(p, n, m);
  }
  int l = all.size();
  rep(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  rep(i, l) {
    ans = 0;

    rep(k, q) {
      if (all[i][b[k] - 1] - all[i][a[k] - 1] == c[k])
        ans += d[k];
    }
    rans = max(rans, ans);
  }
  cout << rans;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

vector<long long> all;

void solve(long long s, long long n, long long m) {
  if (s > pow(10, n - 1))
    all.push_back(s);
  else {
    for (int i = s % 10; i <= m; i++)
      if (i)
        solve(s * 10 + i, n, m);
  }
  return;
}

int main() {
  long long n, m, q, ans, rans = 0;
  vector<string> all2;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  solve(0, n, m);
  int l = all.size();
  rep(i, l) all2.push_back(to_string(all[i]));
  rep(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  rep(i, l) {
    ans = 0;

    rep(k, q) {
      if (all2[i][b[k] - 1] - all2[i][a[k] - 1] == c[k])
        ans += d[k];
    }
    rans = max(rans, ans);
  }
  cout << rans;
  return 0;
}