Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef long double ld;

#define imx INT_MAX
#define imn INT_MIN
#define llmx LLONG_MAX
#define llmn LLONG_MIN

#define fi first
#define se second
#define pb push_back
#define f_s(a) sort(a.begin(), a.end())
#define b_s(a) sort(a.rbegin(), a.rend())
#define p(a) cout << a << "\\n"
// #define l(i, )
// #define rl(i, a, b) for(int i = a; i < b; i++)

/////////////////////GCD//////////////////////
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

////////////////////SIEVE OF ERATOSTHENES//////////////////////
vector<int> sieveoferatosthenes(int n) {
  vector<bool> prime(n + 1, true);
  for (int i = 2; i <= sqrt(n); i++) {
    if (prime[i]) {
      for (int j = i * i; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }
  vector<int> all_primes;
  for (int i = 2; i <= n; i++) {
    if (prime[i])
      all_primes.pb(i);
  }
  return all_primes;
}

int rec(int level, int n, int a) {
  if (level == n) {
    return a;
  }

  rec(level + 1, n, a);
  for (int i = a + 1; i <= n; i++) {
    rec(level + 1, n, a + 1);
  }
  // ans.pb()
  // rec(level+1, n, a+1);
}

int best = imn;
int q, m, n;
void rec_fn(int i, int pivot, vector<int> &arr, vector<vector<int>> &mat) {
  // cout<<i<<endl;

  // for(auto i: arr) cout<<i<<" ";
  // cout<<endl;
  if (i == n + 1) {
    int sum = 0;
    for (int k = 0; k < q; k++) {
      if (arr[mat[k][1] - 1] - arr[mat[k][0] - 1] == mat[k][2])
        sum += mat[k][3];
    }
    // cout<<sum<<endl;
    best = max(best, sum);
    return;
    // return best;
  }

  for (int j = pivot; j <= m; j++) {
    arr[i] = j;
    rec_fn(i + 1, j, arr, mat);
    // best = max(rec_fn(i+1, j, arr, mat), best);
    // cout<<" best "<<best<<" j "<<j<<endl;
    // return best;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // ll x;
  // cin>>x;
  // ll start =100;
  // int i = 0;
  // while(start < x)
  // {
  // 	start= (start+(start*1)/100);
  // 	i++;
  // }
  // cout<<i<<endl;
  // int a, b, c, d;
  ll sum = 0, maxim = 0;
  cin >> n >> m >> q;
  // for(int vector<)
  vector<vector<int>> mat(q + 1, vector<int>(4));
  vector<int> ans(n + 1, imx);
  for (int i = 0; i < q; i++)
    for (int j = 0; j < 4; j++)
      cin >> mat[i][j];

  // for(int i = 0; i < n; i++)
  // {
  // 	for(int j = 0; j < 4; j++)
  // 	{
  // 		cout<<mat[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }
  rec_fn(0, 1, ans, mat);
  p(best);
  // rec_fn(0, 1, ans, mat);
  // p(best);
  // rec(0, n, 1);
  // while(q--)
  // {
  // cin>>a>>b>>c>>d;

  // }
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;

#define imx INT_MAX
#define imn INT_MIN
#define llmx LLONG_MAX
#define llmn LLONG_MIN

#define fi first
#define se second
#define pb push_back
#define f_s(a) sort(a.begin(), a.end())
#define b_s(a) sort(a.rbegin(), a.rend())
#define p(a) cout << a << "\\n"
// #define l(i, )
// #define rl(i, a, b) for(int i = a; i < b; i++)

/////////////////////GCD//////////////////////
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

////////////////////SIEVE OF ERATOSTHENES//////////////////////
vector<int> sieveoferatosthenes(int n) {
  vector<bool> prime(n + 1, true);
  for (int i = 2; i <= sqrt(n); i++) {
    if (prime[i]) {
      for (int j = i * i; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }
  vector<int> all_primes;
  for (int i = 2; i <= n; i++) {
    if (prime[i])
      all_primes.pb(i);
  }
  return all_primes;
}

int rec(int level, int n, int a) {
  if (level == n) {
    return a;
  }

  rec(level + 1, n, a);
  for (int i = a + 1; i <= n; i++) {
    rec(level + 1, n, a + 1);
  }
  // ans.pb()
  // rec(level+1, n, a+1);
}

int best = imn;
int q, m, n;
int rec_fn(int i, int pivot, vector<int> &arr, vector<vector<int>> &mat) {
  // cout<<i<<endl;

  // for(auto i: arr) cout<<i<<" ";
  // cout<<endl;
  if (i == n + 1) {
    int sum = 0;
    for (int k = 0; k < q; k++) {
      if (arr[mat[k][1] - 1] - arr[mat[k][0] - 1] == mat[k][2])
        sum += mat[k][3];
    }
    // cout<<sum<<endl;
    best = max(best, sum);
    // return
    return best;
  }

  for (int j = pivot; j <= m; j++) {
    arr[i] = j;
    rec_fn(i + 1, j, arr, mat);
    // best = max(rec_fn(i+1, j, arr, mat), best);
    // cout<<" best "<<best<<" j "<<j<<endl;
    // return best;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // ll x;
  // cin>>x;
  // ll start =100;
  // int i = 0;
  // while(start < x)
  // {
  // 	start= (start+(start*1)/100);
  // 	i++;
  // }
  // cout<<i<<endl;
  // int a, b, c, d;
  ll sum = 0, maxim = 0;
  cin >> n >> m >> q;
  // for(int vector<)
  vector<vector<int>> mat(q + 1, vector<int>(4));
  vector<int> ans(n + 1, imx);
  for (int i = 0; i < q; i++)
    for (int j = 0; j < 4; j++)
      cin >> mat[i][j];

  // for(int i = 0; i < n; i++)
  // {
  // 	for(int j = 0; j < 4; j++)
  // 	{
  // 		cout<<mat[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }
  rec_fn(0, 1, ans, mat);
  p(best);
  // rec_fn(0, 1, ans, mat);
  // p(best);
  // rec(0, n, 1);
  // while(q--)
  // {
  // cin>>a>>b>>c>>d;

  // }
}