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

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <vector>
#define int long long
#define INF 1e18
using namespace std;
typedef pair<int, int> P;
signed main() {
  int n, d, ans = 0, sum[31] = {}, sum2[31] = {};
  int x[31], y[31];
  vector<P> d1, d2, dp[16];
  cin >> n >> d;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    sum[i] = x[i] + sum[i - 1];
  }
  dp[0].push_back(make_pair(0, 0));
  for (int i = 1; i <= 15; i++) {
    for (int j = 0; j < i; j++) {
      for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end(); itr++) {
        P p = *itr;
        dp[i].push_back(make_pair(p.first + x[i], p.second + y[i]));
        if (n <= 15 || p.first - x[i] + sum[15] - sum[i] > 0) {
          dp[i].push_back(make_pair(p.first - x[i], p.second - y[i]));
        }
      }
    }
    if (n <= i) {
      for (int j = 0; j <= i; j++) {
        for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end();
             itr++) {
          P p = *itr;
          if (p.first >= -d && p.first <= d) {
            ans = max(p.second, ans);
          }
          if (-1 * p.first >= -d && -1 * p.first <= d) {
            ans = max(-1 * p.second, ans);
          }
        }
      }
      cout << ans << endl;
      return 0;
    }
  }
  for (int i = 0; i <= 15; i++) {
    for (vector<P>::iterator itr = dp[i].begin(); itr != dp[i].end(); itr++) {
      P p = *itr;
      d1.push_back(make_pair(p.first, p.second));
      d1.push_back(make_pair(-p.first, -p.second));
    }
    dp[i].clear();
  }
  dp[0].push_back(make_pair(0, 0));
  for (int i = 16; i <= n; i++) {
    for (int j = 0; j < i - 15; j++) {
      for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end(); itr++) {
        dp[i - 15].push_back(make_pair(itr->first + x[i], itr->second + y[i]));
        if (itr->first - x[i] + sum[n] - sum[i] >= 0)
          dp[i - 15].push_back(
              make_pair(itr->first - x[i], itr->second - y[i]));
      }
    }
  }
  for (int j = 0; j <= 15; j++) {
    for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end(); itr++) {
      d2.push_back(make_pair(itr->first, itr->second));
      d2.push_back(make_pair(-itr->first, -itr->second));
    }
    dp[j].clear();
  }

  sort(d1.begin(), d1.end());
  sort(d2.begin(), d2.end());

  int a = 0, b = 0;
  deque<int> c;
  for (int i = d1.size() - 1; i >= 0; i--) {
    while (a < d2.size() && d1[i].first + d2[a].first <= d) {
      while (!c.empty() && d2[c.back()].second <= d2[a].second)
        c.pop_back();
      c.push_back(a);
      a++;
    }
    while (b < d2.size() && d2[b].first + d1[i].first < -d) {
      if (!c.empty() && c.front() == b)
        c.pop_front();
      b++;
    }
    if (!c.empty())
      ans = max(ans, d1[i].second + d2[c.front()].second);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define INF 1e18
using namespace std;
typedef pair<int, int> P;
signed main() {
  int n, d, ans = 0, sum[31] = {}, sum2[31] = {};
  int x[31], y[31];
  vector<P> d1, d2, dp[16];
  cin >> n >> d;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    sum[i] = x[i] + sum[i - 1];
  }
  dp[0].push_back(make_pair(0, 0));
  for (int i = 1; i <= 15; i++) {
    for (int j = 0; j < i; j++) {
      for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end(); itr++) {
        P p = *itr;
        dp[i].push_back(make_pair(p.first + x[i], p.second + y[i]));
        if (n <= 15 || p.first - x[i] + sum[15] - sum[i] > 0) {
          dp[i].push_back(make_pair(p.first - x[i], p.second - y[i]));
        }
      }
    }
    if (n <= i) {
      for (int j = 0; j <= i; j++) {
        for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end();
             itr++) {
          P p = *itr;
          if (p.first >= -d && p.first <= d) {
            ans = max(p.second, ans);
          }
          if (-1 * p.first >= -d && -1 * p.first <= d) {
            ans = max(-1 * p.second, ans);
          }
        }
      }
      cout << ans << endl;
      return 0;
    }
  }
  for (int i = 0; i <= 15; i++) {
    for (vector<P>::iterator itr = dp[i].begin(); itr != dp[i].end(); itr++) {
      P p = *itr;
      d1.push_back(make_pair(p.first, p.second));
      d1.push_back(make_pair(-p.first, -p.second));
    }
    dp[i].clear();
  }
  dp[0].push_back(make_pair(0, 0));
  for (int i = 16; i <= n; i++) {
    for (int j = 0; j < i - 15; j++) {
      for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end(); itr++) {
        dp[i - 15].push_back(make_pair(itr->first + x[i], itr->second + y[i]));
        if (itr->first - x[i] + sum[n] - sum[i] >= 0)
          dp[i - 15].push_back(
              make_pair(itr->first - x[i], itr->second - y[i]));
      }
    }
  }
  for (int j = 0; j <= 15; j++) {
    for (vector<P>::iterator itr = dp[j].begin(); itr != dp[j].end(); itr++) {
      d2.push_back(make_pair(itr->first, itr->second));
      d2.push_back(make_pair(-itr->first, -itr->second));
    }
    dp[j].clear();
  }

  sort(d1.begin(), d1.end());
  sort(d2.begin(), d2.end());

  int a = 0, b = 0, c = 0;
  for (int i = d1.size() - 1; i >= 0; i--) {
    while (a < d2.size() && d1[i].first + d2[a].first) {
      if (d2[c].second <= d2[a].second)
        c = a;
    }
    if (d2[c].first + d1[i].first < -d)
      continue;
    ans = max(ans, d1[i].second + d2[c].second);
  }
  cout << ans << endl;
  return 0;
}