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

/*raghav0307 - Raghav Gupta*/

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

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define ff first
#define ss second
#define pb push_back
#define fast_io()                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int ll

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int MAXN = 1e5 + 5;

int n;
int leaves[MAXN];
int maxNodes[MAXN];

signed main() {
  fast_io();

  cin >> n;
  for (int i = 0; i <= n; i++) {
    cin >> leaves[i];
  }

  if (n == 0 and leaves[0] != 1) {
    cout << "-1\\n";
    return 0;
  }

  maxNodes[0] = 1;
  for (int i = 1; i <= n; i++) {
    maxNodes[i] = min((ll)1e18, maxNodes[i - 1] * 2);
  }

  int l[n + 1];
  l[n] = leaves[n];
  int r[n + 1];
  r[n] = leaves[n];

  // cout << "level: " << n << " l: " << l << " r: " << r << "\\n";

  for (int i = n - 1; i >= 0; i--) {

    l[i] = leaves[i] + (l[i + 1] + 1) / 2;
    r[i] = min(r[i + 1] + leaves[i], maxNodes[i]);

    // if(l[i] > r[i]){
    // 	cout << "-1\\n";
    // 	return 0;
    // }
    // cout << "level: " << i << " l: " << l << " r: " << r << "\\n";
  }

  int ans = 1;
  for (int i = 1; i <= n; i++) {
    r[i] = min(r[i], 2 * (r[i - 1] - leaves[i - 1]));
    l[i] = max(l[i], l[i - 1] - leaves[i - 1]);
    ans += r[i];
    // if(l[i] > r[i]){
    // 	cout << "-1\\n";
    // 	return 0;
    // }
    // if(r[i] <= 0){
    // 	cout << "-1\\n";
    // 	return 0;
    // }
    if (r[i] < leaves[i]) {
      cout << "-1\\n";
      return 0;
    }
    // cout << "level: " << i << " l: " << l << " r: " << r << "\\n";
  }

  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

/*raghav0307 - Raghav Gupta*/

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

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define ff first
#define ss second
#define pb push_back
#define fast_io()                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int ll

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int MAXN = 1e5 + 5;

map<pair<int, int>, int> memo;
int n;
int leaves[MAXN];

int dp(int level, int nTrees) {

  if (level > n and nTrees) {
    return -1;
  } else if (level > n) {
    return 0;
  }

  int availTrees = nTrees - leaves[level];

  if (availTrees < 0) {
    return -1;
  }

  if (memo.count({level, nTrees})) {
    return memo[{level, nTrees}];
  }

  int ret = -1;

  for (int i = availTrees; i <= 2 * availTrees; i++) {
    int temp = dp(level + 1, i);
    if (temp != -1)
      ret = max(ret, nTrees + temp);
  }

  memo[{level, nTrees}] = ret;

  return ret;
}

signed main() {
  fast_io();

  cin >> n;
  for (int i = 0; i <= n; i++) {
    cin >> leaves[i];
  }

  cout << dp(0, 1) << "\\n";

  return 0;
}