Language: C++

void dfs(int aa, int num)
{
#if 0
        for (int i = 0; i < N; ++i) {
                cout << A[i] << " ";
        }
        cout << endl;
#endif
        A[num-1] = aa;
        if (num == N) {
                long long ans = 0;
                for (int i = 0; i < Q; ++i) {
                        if ((A[b[i]-1] - A[a[i]-1]) == c[i]) {
                                ans += d[i];
                        }
                }
                if (maxv < ans) maxv = ans;
                if (ans == maxp) {
                        cout << ans << endl;
                        exit(0);
                }
                return;
        }

        for (int i = 1; i <= M; ++i) {
                dfs(i, num+1);
        }

}

int main()
{
        maxp = 0;
        cin >> N >> M >> Q;
        for (int i = 0; i < Q; ++i) {
                int t1, t2, t3, t4;
                cin >> t1 >> t2 >> t3 >> t4;
                a.push_back(t1);
                b.push_back(t2);
                c.push_back(t3);
                d.push_back(t4);
                maxp += t4;
        }

        for (int i = 0; i < N; ++i) {
                A[i] = 0;
        }
        maxv = -1;
        dfs(1, 1);
        cout << maxv << endl;


}


Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:9:9: error: 'A' was not declared in this scope
    9 |         A[num-1] = aa;
      |         ^
a.cc:10:20: error: 'N' was not declared in this scope
   10 |         if (num == N) {
      |                    ^
a.cc:12:37: error: 'Q' was not declared in this scope
   12 |                 for (int i = 0; i < Q; ++i) {
      |                                     ^
a.cc:13:32: error: 'b' was not declared in this scope
   13 |                         if ((A[b[i]-1] - A[a[i]-1]) == c[i]) {
      |                                ^
a.cc:13:44: error: 'a' was not declared in this scope
   13 |                         if ((A[b[i]-1] - A[a[i]-1]) == c[i]) {
      |                                            ^
a.cc:13:56: error: 'c' was not declared in this scope
   13 |                         if ((A[b[i]-1] - A[a[i]-1]) == c[i]) {
      |                                                        ^
a.cc:14:40: error: 'd' was not declared in this scope
   14 |                                 ans += d[i];
      |                                        ^
a.cc:17:21: error: 'maxv' was not declared in this scope
   17 |                 if (maxv < ans) maxv = ans;
      |                     ^~~~
a.cc:18:28: error: 'maxp' was not declared in this scope
   18 |                 if (ans == maxp) {
      |                            ^~~~
a.cc:19:25: error: 'cout' was not declared in this scope
   19 |                         cout << ans << endl;
      |                         ^~~~
a.cc:19:40: error: 'endl' was not declared in this scope
   19 |                         cout << ans << endl;
      |                                        ^~~~
a.cc:20:25: error: 'exit' was not declared in this scope
   20 |                         exit(0);
      |                         ^~~~
a.cc:1:1: note: 'exit' is defined in header '<cstdlib>'; this is probably fixable by adding '#include <cstdlib>'
  +++ |+#include <cstdlib>
    1 | void dfs(int aa, int num)
a.cc:25:30: error: 'M' was not declared in this scope
   25 |         for (int i = 1; i <= M; ++i) {
      |                              ^
a.cc: In function 'int main()':
a.cc:33:9: error: 'maxp' was not declared in this scope
   33 |         maxp = 0;
      |         ^~~~
a.cc:34:9: error: 'cin' was not declared in this scope
   34 |         cin >> N >> M >> Q;
      |         ^~~
a.cc:34:16: error: 'N' was not declared in this scope
   34 |         cin >> N >> M >> Q;
      |                ^
a.cc:34:21: error: 'M' was not declared in this scope
   34 |         cin >> N >> M >> Q;
      |                     ^
a.cc:34:26: error: 'Q' was not declared in this scope
   34 |         cin >> N >> M >> Q;
      |                          ^
a.cc:38:17: error: 'a' was not declared in this scope
   38 |                 a.push_back(t1);
      |                 ^
a.cc:39:17: error: 'b' was not declared in this scope
   39 |                 b.push_back(t2);
      |                 ^
a.cc:40:17: error: 'c' was not declared in this scope
   40 |                 c.push_back(t3);
      |                 ^
a.cc:41:17: error: 'd' was not declared in this scope
   41 |                 d.push_back(t4);
      |                 ^
a.cc:46:17: error: 'A' was not declared in this scope
   46 |                 A[i] = 0;
      |                 ^
a.cc:48:9: error: 'maxv' was not declared in this scope
   48 |         maxv = -1;
      |         ^~~~
a.cc:50:9: error: 'cout' was not declared in this scope
   50 |         cout << maxv << endl;
      |         ^~~~
a.cc:50:25: error: 'endl' was not declared in this scope
   50 |         cout << maxv << endl;
      |                         ^~~~