Language: C++

/*
  AUTHOR:SOURABH
  CREATED:01:05:20
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<pll , null_type, less< pll >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define W while
#define fn(i,x) for(i=0;i<x;i++)
#define fs(i,s,x) for(i=s;i<x;i++)
#define fr(i,x) for(i=x;i>=0;i--)
#define fit(it,s) for(it=s.begin();it!=s.end();it++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define mod 1000000007
#define MAX 100005
#define M 32

using namespace std;
using namespace __gnu_pbds;

ll ans,n,m,q;
vector<pair<pll,pll> > qq;
vll A(10);

void solve(ll id,ll val)
{
    if(id==n)
    {
        ll i,sum=0;
        fn(i,q)
        {
            if(A[qq[i].fi.se]-A[qq[i].fi.fi]==qq[i].se.fi)
                sum+=qq[i].se.se;
        }
        ans=max(ans,sum);
        return;
    }
    ll i;
    fs(i,val,m+1)
    {
        A[id]=i;
        solve(id+1,i);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ll t,a,b,c,d,i;
    t=1;
    //cin>>t;
    W(t--)
    {
        cin>>n>>m>>q;
        fn(i,q)
        {
            cin>>a>>b>>c>>d;
            a--,b--;
            qq.pb({{a,b},{c,d}});
        }
        ans=0;
        solve(0,1);
        cout<<ans;
    }
    return 0;
}












/*
  AUTHOR:SOURABH
  CREATED:01:05:20
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<pll , null_type, less< pll >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define W while
#define fn(i,x) for(i=0;i<x;i++)
#define fs(i,s,x) for(i=s;i<x;i++)
#define fr(i,x) for(i=x;i>=0;i--)
#define fit(it,s) for(it=s.begin();it!=s.end();it++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define mod 1000000007
#define MAX 100005
#define M 32

using namespace std;
using namespace __gnu_pbds;

ll ans,n,m,q;
vector<pair<pll,pll> > qq;
vll A(10);

void solve(ll id,ll val)
{
    if(id==n)
    {
        ll i,sum=0;
        fn(i,q)
        {
            if(A[qq[i].fi.se]-A[qq[i].fi.fi]==qq[i].se.fi)
                sum+=qq[i].se.se;
        }
        ans=max(ans,sum);
        return;
    }
    ll i;
    fs(i,val,m+1)
    {
        A[id]=i;
        solve(id+1,i);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ll t,a,b,c,d,i;
    t=1;
    //cin>>t;
    W(t--)
    {
        cin>>n>>m>>q;
        fn(i,q)
        {
            cin>>a>>b>>c>>d;
            a--,b--;
            qq.pb({{a,b},{c,d}});
        }
        ans=0;
        solve(0,1);
        cout<<ans;
    }
    return 0;
}














Compiler output:
a.cc:133:4: error: redefinition of 'long long int ans'
  133 | ll ans,n,m,q;
      |    ^~~
a.cc:35:4: note: 'long long int ans' previously declared here
   35 | ll ans,n,m,q;
      |    ^~~
a.cc:133:8: error: redefinition of 'long long int n'
  133 | ll ans,n,m,q;
      |        ^
a.cc:35:8: note: 'long long int n' previously declared here
   35 | ll ans,n,m,q;
      |        ^
a.cc:133:10: error: redefinition of 'long long int m'
  133 | ll ans,n,m,q;
      |          ^
a.cc:35:10: note: 'long long int m' previously declared here
   35 | ll ans,n,m,q;
      |          ^
a.cc:133:12: error: redefinition of 'long long int q'
  133 | ll ans,n,m,q;
      |            ^
a.cc:35:12: note: 'long long int q' previously declared here
   35 | ll ans,n,m,q;
      |            ^
a.cc:134:24: error: redefinition of 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > qq'
  134 | vector<pair<pll,pll> > qq;
      |                        ^~
a.cc:36:24: note: 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > qq' previously declared here
   36 | vector<pair<pll,pll> > qq;
      |                        ^~
a.cc:135:5: error: redefinition of 'std::vector<long long int> A'
  135 | vll A(10);
      |     ^
a.cc:37:5: note: 'std::vector<long long int> A' previously declared here
   37 | vll A(10);
      |     ^
a.cc:137:6: error: redefinition of 'void solve(long long int, long long int)'
  137 | void solve(ll id,ll val)
      |      ^~~~~
a.cc:39:6: note: 'void solve(long long int, long long int)' previously defined here
   39 | void solve(ll id,ll val)
      |      ^~~~~
a.cc:158:5: error: redefinition of 'int main()'
  158 | int main()
      |     ^~~~
a.cc:60:5: note: 'int main()' previously defined here
   60 | int main()
      |     ^~~~