Language: Java

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

class s847387793 {

    static class Solver {
        final FastScanner sc;
        final PrintWriter writer;

        Solver(final FastScanner sc, final PrintWriter writer) {
            this.sc = sc;
            this.writer = writer;
        }

        int n, m, q;
        int max = 0;
        int[] a, b, c, d;

        void run() {
            n = sc.nextInt();
            m = sc.nextInt();
            q = sc.nextInt();
            a = new int[q];
            b = new int[q];
            c = new int[q];
            d = new int[q];
            for (int i = 0; i < q; i++) {
                a[i] = sc.nextInt();
                b[i] = sc.nextInt();
                c[i] = sc.nextInt();
                d[i] = sc.nextInt();
            }
            dfs(new int[] {1});
            writer.println(max);
        }

        private void dfs(int[] array) {
            if (array.length > n) {
                int score = 0;
                for (int i = 0; i < q; i++) {
                    if (array[b[i]] - array[a[i]] == c[i]) {
                        score += d[i];
                    }
                }
                max = Math.max(max, score);
                return;
            }

            int[] newArray = new int[array.length + 1];
            System.arraycopy(array, 0, newArray, 0, array.length);
            System.arraycopy(new int[] {array[array.length - 1]}, 0, newArray, array.length, 1);

            while (newArray[newArray.length - 1] <= m) {
                dfs(newArray);
                newArray[newArray.length - 1] += 1;
            }
        }
    }

    public static void main(final String[] args) {
        final FastScanner sc = new FastScanner();
        try (final PrintWriter w = new PrintWriter(System.out)) {
            new Solver(sc, w).run();
            w.flush();
        }
    }

    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        public FastScanner() {}

        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (final IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) {
                    return false;
                }
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte()) return buffer[ptr++];
            else return -1;
        }

        private boolean isPrintableChar(final int c) {
            return 33 <= c && c <= 126;
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
            return hasNextByte();
        }

        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            final StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public long nextLong() {
            if (!hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

        public int nextInt() {
            final long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(final int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(final int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
    }
}


Example test case(s):
input: 3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

output: 110