alexjercan/bugnet huggingface dataset:
Problem p01754 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

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

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, P, Q;
int C[500010], D[500010];

signed main() {
  cin >> N >> P >> Q;
  rep(i, 0, N) {
    cin >> C[i];
    D[i] = P * i + C[i];
  }
  sort(D, D + N);

  int sum = accumulate(C, C + N, 0LL), ans = -INF;
  repq(i, 0, N) {
    chmax(ans, sum + P * Q * i + P * i * (i - 1));
    if (i < N)
      sum -= D[i];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

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

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, P, Q, C[500010];

// dp[i][j] := i ??\\????????§??§????????? j
// ???????????¨???????????????????????§???
int dp[2][500010];

signed main() {
  cin >> N >> P >> Q;
  rep(i, 0, N) cin >> C[i];
  rep(i, 0, 2) repq(j, 0, N) dp[i][j] = -INF;
  dp[0][0] = 0;

  int ans = -INF;
  rep(i, 0, N) repq(j, 0, i) {
    int v = i % 2;
    if (dp[v][j] == -INF)
      continue;
    // ???????????????
    chmax(dp[v ^ 1][j], dp[v][j] + C[i]);

    // ????????????
    int S = Q + j - (i - j);
    chmax(dp[v ^ 1][j + 1], dp[v][j] + P * S);
    dp[v][j] = -INF;
  }
  repq(i, 0, N) chmax(ans, dp[N % 2][i]);
  cout << ans << endl;
  return 0;
}
Problem p01754 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

long long int N, P, Q;

int main() {
  cin >> N >> P >> Q;
  vector<long long int> cs(N);
  for (int i = 0; i < N; ++i) {
    cin >> cs[i];
  }
  int amin = 0;
  int amax = N + 1;
  vector<long long int> totals(cs);
  for (int i = 0; i < cs.size(); ++i) {
    totals[i] = -totals[i] - i * P;
  }
  sort(totals.begin(), totals.end(), greater<long long int>());
  long long int ans = accumulate(cs.begin(), cs.end(), 0ll);
  long long int now = ans;
  for (int i = 0; i < totals.size(); ++i) {
    long long int plus = totals[i] + (Q + i * 2) * P;
    now += plus;
    ans = max(ans, now);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

long long int N, P, Q;
long long int getans(const int self, const vector<long long int> &vs) {
  vector<long long int> totals(vs);
  for (int i = 0; i < vs.size(); ++i) {
    totals[i] = -totals[i] - i * P;
  }
  sort(totals.begin(), totals.end(), greater<long long int>());
  long long int ans = 0;
  for (int i = 0; i < self; ++i) {
    ans += totals[i];
  }
  ans += self * (Q + (Q + 2 * (self - 1))) * P / 2;
  return ans;
}

int main() {
  cin >> N >> P >> Q;
  vector<long long int> cs(N);
  for (int i = 0; i < N; ++i) {
    cin >> cs[i];
  }
  int amin = 0;
  int amax = N + 1;
  while (amin + 2 <= amax) {
    const int amid1 = (amin * 2 + amax) / 3;
    const int amid2 = (amin + amax * 2) / 3;
    const long long int a1 = getans(amid1, cs);
    const long long int a2 = getans(amid2, cs);
    if (a1 < a2) {
      amin = amid1;
    } else {
      amax = amid2;
    }
  }
  long long int ans = 0;
  for (int s = amin; s < amax; ++s) {
    ans = max(ans, getans(s, cs));
  }
  ans += accumulate(cs.begin(), cs.end(), 0ll);
  cout << ans << endl;
  return 0;
}
Problem p01754 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int N, P, Q;
  scanf("%d%d%d", &N, &P, &Q);
  vector<long long> c(N), a(N + 1);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &c[i]);
    c[i] += (long long)P * i;
  }
  for (int i = 1; i <= N; i++) {
    a[i] = ((long long)Q + (i - 1) * 2) * P + a[i - 1];
  }
  sort(c.begin(), c.end());
  long long s = 0, ma = a[N];
  for (int i = N - 1; i >= 0; i--) {
    s += c[i];
    ma = max(ma, s + a[i]);
  }
  printf("%lld\\n", ma - (long long)P * N * (N - 1) / 2);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int N, P, Q;
  scanf("%d%d%d", &N, &P, &Q);
  vector<long long> c(N), a(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &c[i]);
    c[i] += P * i;
  }
  for (int i = 1; i <= N; i++) {
    a[i] = ((long long)Q + (i - 1) * 2) * P + a[i - 1];
  }
  sort(c.begin(), c.end());
  long long s = 0, ma = a[N];
  for (int i = N - 1; i >= 0; i--) {
    s += c[i];
    ma = max(ma, s + a[i]);
  }
  printf("%lld\\n", ma - (long long)P * N * (N - 1) / 2);
}
Problem p01754 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
using namespace std;

int main() {
  long long N, P, Q;
  scanf("%lld %lld %lld", &N, &P, &Q);
  vector<long long> C(N);
  for (int i = 0; i < N; ++i)
    scanf("%lld", &C[i]);
  long long s = 0;
  for (int i = 0; i < N; ++i) {
    s += C[i];
    C[i] = (Q - i) * P - C[i];
  }
  sort(C.begin(), C.end(), greater<>());
  long long ans = s;
  for (int i = 0; i < N; ++i) {
    s += C[i] + 2 * i * P;
    ans = max(ans, s);
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
using namespace std;
// long long dp[(int)5e2+1][(int)5e2+1], C[(int)5e5];
long long C[(int)5e5];
map<pair<int, int>, long long> dp;
int N, P, Q;

long long int solve(int d, int k) {
  // cout << d << " " << k << endl;
  if (d == N)
    return 0;
  // if(dp[d][k] > 0) return dp[d][k];
  if (dp[make_pair(d, k)] > 0)
    return dp[make_pair(d, k)];
  long long q = Q + 2 * k - d;
  if (C[d] <= P * q) {
    dp[make_pair(d, k)] = P * q + solve(d + 1, k + 1);
  } else {
    dp[make_pair(d, k)] =
        max(C[d] + solve(d + 1, k), P * q + solve(d + 1, k + 1));
  }
  return dp[make_pair(d, k)];
}

int main() {
  cin >> N >> P >> Q;
  for (int i = 0; i < N; ++i)
    cin >> C[i];
  cout << solve(0, 0) << endl;
  return 0;
}
Problem p01754 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long
typedef long long ll;

const ll inf = 1e18;

ll S = 0;
ll N, P, Q;
vector<ll> C;
vector<ll> v;
ll f(ll t) {
  ll res = (t * (t - 1) / 2) * 2 * P + S;
  for (int i = 0; i < t; ++i) {
    res += v[i];
  }
  return res;
}
signed main() {
  cin >> N >> P >> Q;
  C.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> C[i];
  }

  S = 0;
  for (int i = 0; i < N; ++i) {
    S += C[i];
  }
  for (int i = 0; i < N; ++i) {
    v.push_back((Q - i) * P - C[i]);
  }
  sort(v.begin(), v.end(), greater<ll>());

  ll now = S;
  ll ans = now;
  for (int i = 0; i < N; ++i) {
    now += i * 2 * P;
    now += v[i];
    ans = max(ans, now);
  }
  cout << ans << endl;

  /*
  ll l = 0, r = N;
  for (int t = 0; t < 100; ++t) {
          ll m1 = l+(r-l)/3, m2 = l+(r-l)*2/3;
          if ( f(m1) < f(m2) ) {
                  l = m1;
          }
          else if ( f(m1) > f(m2) ) {
                  r = m2;
          }
          else if ( f(l) < f(m1) ) {
                  l = m1;
          }
          else {
                  r = m2;
          }
  }
  assert(m2-m1 < 3);
  */
  /*
  ll ans = l;
  for (ll i = l; i <= r; ++i) {
          if ( f(i) > f(ans) ) ans = i;
  }
  */
  //	cout << f(1) << endl;
  //	cout << ans << " " << f(ans) << endl;
  //	cout << f(ans) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll N, P, Q;
vector<ll> C;
ll f(ll t) {
  ll res = t * (t - 1) / 2 * 2 * P;
  for (int i = 0; i < N; ++i) {
    res += C[i];
  }
  vector<ll> v;
  for (int i = 0; i < N; ++i) {
    v.push_back((Q - i) * P - C[i]);
  }
  sort(v.begin(), v.end(), greater<ll>());
  for (int i = 0; i < t; ++i) {
    res += v[i];
  }
  return res;
}
int main() {
  cin >> N >> P >> Q;
  C.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> C[i];
  }
  ll l = 0, r = N;
  for (int t = 0; t < 100; ++t) {
    ll m1 = l + (r - l) / 3, m2 = l + (r - l) * 2 / 3;
    if (f(m1) < f(m2)) {
      l = m1;
    } else {
      r = m2;
    }
  }
  ll ans = l;
  for (ll i = l; i <= r; ++i) {
    if (f(i) > f(ans))
      ans = i;
  }
  //	cout << f(1) << endl;
  //	cout << ans << " " << f(ans) << endl;
  cout << f(ans) << endl;
}
Problem p01754 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

// n????°?????????´??????DP[i][j]=i??\\?????????????????§j?????????
// n?????§????????´???????????§????????¨?????¢????????????????????????????????§?????????????????°m?????????????£????????????????????????????????????????C[i]+(i-1)P?????§????????????????????§???C[i]+(i-1)P????°????????????????????????????
//??????????????????????????????????§£??????

int main(void) {
  ll N, P, Q;
  scanf("%lld%lld%lld", &N, &P, &Q);
  ll C[500002], D[500002];
  ll C_sum = 0;
  rep(i, N) {
    scanf("%lld", &C[i]);
    C_sum += C[i];
    C[i] += i * P;
  }
  sort(C, C + N); //??????????????????????????¨???????????¨??????????????????
  D[0] = 0;       //???????´???????????±?????????????????????¨TLE??????
  rep(i, N) { D[i + 1] = D[i] + C[i]; }
  ll sum;
  ll ans = LLONG_MIN;
  rep(m, N + 1) { // m?????????
    sum = C_sum;  //?????\\?£?????????????
    sum -= D[m];  //?£??????????????????????????????¨??????????????????
    sum += (m - 1 + Q) * P * m; //???????????????????£???£???
    if (ans < sum)
      ans = sum;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

// n????°?????????´??????DP[i][j]=i??\\?????????????????§j?????????
// n?????§????????´???????????§????????¨?????¢????????????????????????????????§?????????????????°m?????????????£????????????????????????????????????????C[i]+(i-1)P?????§????????????????????§???C[i]+(i-1)P????°????????????????????????????
//??????????????????????????????????§£??????

int main(void) {
  ll N, P, Q;
  scanf("%lld%lld%lld", &N, &P, &Q);
  ll C[500002];
  ll C_sum = 0;
  rep(i, N) {
    scanf("%lld", &C[i]);
    C_sum += C[i];
    C[i] += i * P;
  }
  sort(C, C + N); //??????????????????????????¨???????????¨??????????????????
  ll sum;
  ll ans = LLONG_MIN;
  rep(m, N + 1) {          // m?????????
    sum = C_sum;           //?????\\?£?????????????
    rep(i, m) sum -= C[i]; //?£??????????????????????????????¨??????????????????
    sum += (m - 1 + Q) * P * m; //???????????????????£???£???
    if (ans < sum)
      ans = sum;
  }
  cout << ans << endl;
  return 0;
}
Problem p01754 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;

ll N, P, Q;
ll C[500000];
ll d[500000];

int main() {
  scanf("%lld%lld%lld", &N, &P, &Q);
  ll init = 0;
  for (ll i = 0; i < N; i++) {
    scanf("%lld", &C[i]);
    d[i] = i * P + C[i];
    init += C[i];
  }
  sort(d, d + N);
  ll sum = init, ans = init;
  for (ll i = 0; i < N; i++) {
    sum += 2 * P * i + P * Q - d[i];
    ans = max(ans, sum);
  }
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;

int N, P, Q;
ll C[500000];
ll d[500000];

int main() {
  while (1) {
    scanf("%d%d%d", &N, &P, &Q);
    if (N == 0)
      break;
    ll init = 0;
    for (int i = 0; i < N; i++) {
      scanf("%lld", &C[i]);
      d[i] = i * P + C[i];
      init += C[i];
    }
    sort(d, d + N);
    ll sum = init, ans = init;
    for (ll i = 0; i < N; i++) {
      sum += 2 * P * i + P * Q - d[i];
      ans = max(ans, sum);
    }
    printf("%lld\\n", ans);
  }
}
Problem p01754 (C++) — buggy submission, verdict: Time Limit Exceeded

#if 0
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2642
17:05-

#endif

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <stdint.h>
using namespace std;

// pair<int, int> c[500010];
long long c[500010];

#define REP(var, start, count) for (long long var = start; var < count; var++)

int main(void) {

  int n, p, q;

  cin >> n >> p >> q;

  long long zisui = (long long)p * q;
  long long ztotal = 0;

  REP(i, 0, n) {
    int ccc;
    cin >> ccc;
    c[i] = ccc - zisui; //????????????????????¨??§????????????????????????

    ztotal += zisui;
    zisui -= p;
    // if ( i%100 == 0 ) { cerr << "i " << i << " c[i] " << c[i] << " ztotal "
    // << ztotal << endl; }
  }
  // cerr << "ztotal " << ztotal << endl;

  sort(c, c + n);
  reverse(c, c + n);

  long long max = LLONG_MIN;

  long long sum = 0;

  for (long long day = 0; day <= n; day++) { //?????\\????£???????????????????

    if (day != 0) {
      sum += c[day - 1];
    }

    long long zisuiday = n - day;
    long long bonus = p * (zisuiday) * (zisuiday - 1);
    if (zisuiday <= 1) {
      bonus = 0;
    }

    long long ret = sum + bonus + ztotal;
    // if(zisuiday<10 || day<10 ||day%100==0) { cerr << "day " << day << " bonus
    // " << bonus << " sum " << sum << " ret " << ret << endl; }

    if (ret > max) {
      max = ret;
    }
  }

  cout << max << endl;

  return 0;
}

Error: TLE

Fixed submission:

#if 0
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2642
17:05-

#endif

#include <algorithm>
#include <iostream>
#include <limits.h>
using namespace std;

// pair<int, int> c[500010];
int c[500010];

#define REP(var, start, count) for (int var = start; var < count; var++)

int main(void) {

  int n, p, q;

  cin >> n >> p >> q;

  long long zisui = p * q;
  long long ztotal = 0;

  REP(i, 0, n) {
    int ccc;
    cin >> ccc;
    c[i] = ccc - zisui; //????????????????????¨??§????????????????????????

    ztotal += zisui;
    zisui -= p;
  }
  // cerr << "ztotal " << ztotal << endl;

  sort(c, c + n);
  reverse(c, c + n);

  long long max = -9223372036854775800LL;

  REP(day, 0, n + 1) { //?????\\????????????????????????

    long long sum = 0;
    REP(i, 0, n - day) { sum += c[i]; }

    long long bonus = p * (day) * (day - 1);
    if (day <= 1) {
      bonus = 0;
    }
    // cerr << "day " << day << " bonus " << bonus << " sum " << sum << endl;

    sum += bonus;
    sum += ztotal;

    if (sum > max) {
      max = sum;
    }
  }

  cout << max << endl;

  return 0;
}
Problem p01756 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 100001
using namespace std;
typedef pair<string, int> P;
typedef set<P>::iterator It;

void solve(string str, set<P> A, vector<int> &L) {
  for (int i = 0; i < str.size(); i++) {
    string t;
    t += str[i];
    It it;
    while ((it = A.lower_bound(P(t, -1))) != A.end()) {
      if (it->first.substr(0, t.size()) != t)
        break;
      while (it != A.end() && it->first == t)
        L[it->second] = i, A.erase(it++);
      if (i + t.size() >= str.size())
        break;
      t += str[i + t.size()];
    }
  }
}

int main() {
  string str;
  cin >> str;
  int len = str.size(), n;
  cin >> n;
  set<P> A, B;
  string a[N], b[N];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    A.insert(P(a[i], i));
    reverse(b[i].begin(), b[i].end());
    B.insert(P(b[i], i));
  }
  vector<int> L(N, -1), R(N, -1);
  solve(str, A, L);
  reverse(str.begin(), str.end());
  solve(str, B, R);

  for (int i = 0; i < n; i++) {
    int cnt = 0, alen = a[i].size(), blen = b[i].size();
    while (cnt < min(alen, blen) && a[i][alen - cnt] == b[i][cnt])
      cnt++;
    if (L[i] == -1 || R[i] == -1 || len - R[i] < L[i] + alen - cnt)
      printf("0\\n");
    else
      printf("%d\\n", len - R[i] - L[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 100001
using namespace std;
typedef pair<string, int> P;
typedef set<P>::iterator It;

void solve(string str, set<P> A, vector<int> &L) {
  for (int i = 0; i < str.size(); i++) {
    string t;
    t += str[i];
    It it;
    while ((it = A.lower_bound(P(t, -1))) != A.end()) {
      while (it != A.end() && it->first == t)
        L[it->second] = i, A.erase(it++);
      if (i + t.size() >= str.size())
        break;
      t += str[i + t.size()];
    }
  }
}

int main() {
  string str;
  cin >> str;
  int len = str.size(), n;
  cin >> n;
  set<P> A, B;
  string a[N], b[N];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    A.insert(P(a[i], i));
    reverse(b[i].begin(), b[i].end());
    B.insert(P(b[i], i));
  }
  vector<int> L(N, -1), R(N, -1);
  solve(str, A, L);
  reverse(str.begin(), str.end());
  solve(str, B, R);

  for (int i = 0; i < n; i++) {
    int cnt = 0, alen = a[i].size(), blen = b[i].size();
    while (cnt < min(alen, blen) && a[i][alen - cnt] == b[i][cnt])
      cnt++;
    if (L[i] == -1 || R[i] == -1 || len - R[i] < L[i] + alen - cnt)
      printf("0\\n");
    else
      printf("%d\\n", len - R[i] - L[i]);
  }
  return 0;
}
Problem p01757 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = signed;
using ll = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n, m;
  cin >> n >> m;

  m++;
  vector<Int> a(m), b(m);
  for (Int i = 0; i < m; i++)
    cin >> a[i];
  for (Int i = 1; i < m; i++)
    cin >> b[i];

  vector<ll> hs;
  function<void(Int, Int)> predfs = [&](Int l, Int r) {
    Int x = lower_bound(a.begin(), a.end(), r) - a.begin();
    if (a[x - 1] <= l && r <= a[x])
      return;

    hs.emplace_back(((ll)l << 31) | r);
    Int m = (l + r) >> 1;
    predfs(l, m);
    predfs(m, r);
  };

  predfs(0, (1 << n));
  sort(hs.begin(), hs.end());
  hs.erase(unique(hs.begin(), hs.end()), hs.end());
  map<ll, Int> idx;
  for (Int i = 0; i < (Int)hs.size(); i++)
    idx[hs[i]] = i;
  // cout<<hs.size()<<endl;

  vector<vector<Int>> dp(n + 1, vector<Int>(hs.size(), -1));

  function<Int(Int, Int, Int, Int)> dfs = [&](Int l, Int r, Int d,
                                              Int k) -> Int {
    // cout<<l<<" "<<r<<" "<<d<<" "<<k<<endl;
    Int x = lower_bound(a.begin(), a.end(), r) - a.begin();
    if (a[x - 1] <= l && r <= a[x]) {
      Int v = b[x], tmp = 0;
      tmp += (k == v);
      tmp += (r - l) >> (n - v + 1);
      return tmp;
    }

    if (~dp[k][idx[((ll)l << 31) | r]])
      return dp[k][idx[((ll)l << 31) | r]];
    Int &res = (dp[k][idx[((ll)l << 31) | r]] = 0);

    Int m = (l + r) >> 1;
    res = max(res, dfs(l, m, d + 1, d) + dfs(m, r, d + 1, k));
    res = max(res, dfs(l, m, d + 1, k) + dfs(m, r, d + 1, d));
    return res;
  };

  cout << (1 << n) - dfs(0, 1 << n, 1, 0) << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = signed;
using ll = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n, m;
  cin >> n >> m;

  m++;
  vector<Int> a(m), b(m);
  for (Int i = 0; i < m; i++)
    cin >> a[i];
  for (Int i = 1; i < m; i++)
    cin >> b[i];

  // using P = pair<Int, Int>;
  vector<unordered_map<ll, Int>> dp(n + 1);
  for (Int i = 0; i <= n; i++)
    dp[i].reserve(1 << 15);
  function<Int(Int, Int, Int, Int)> dfs = [&](Int l, Int r, Int d,
                                              Int k) -> Int {
    // cout<<l<<" "<<r<<" "<<d<<" "<<k<<endl;
    Int x = lower_bound(a.begin(), a.end(), r) - a.begin();
    if (a[x - 1] <= l && r <= a[x]) {
      Int v = b[x], tmp = 0;
      tmp += (k == v);
      tmp += (r - l) >> (n - v + 1);
      return tmp;
    }

    if (dp[k].count(((ll)l << 31) | r))
      return dp[k][((ll)l << 31) | r];
    Int &res = (dp[k][((ll)l << 31) | r] = 0);

    Int m = (l + r) >> 1;
    res = max(res, dfs(l, m, d + 1, d) + dfs(m, r, d + 1, k));
    res = max(res, dfs(l, m, d + 1, k) + dfs(m, r, d + 1, d));
    return res;
  };

  cout << (1 << n) - dfs(0, 1 << n, 1, 0) << endl;
  return 0;
}