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

#pragma GCC optimize("O3")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < (int)(n); i++)
#define N 20

int main() {
  int a[N];
  int b[N];
  int c[N];
  int d[N];
  long long ans[N];
  vector<int> aa;
  vector<int> bb;
  f(i, N) ans[i] = 1000000000000000000;
  int n, k;
  int x, y, z;
  int l, r, m;
  int cnt;
  vector<pair<int, int>> aaa;
  vector<pair<int, int>> bbb;
  int aaaa[N];
  int bbbb[N];
  int aaaaa[100000][N];
  int bbbbb[100000][N];
  int aaaaaa;
  int bbbbbb;
  int xa, xb, sz;
  long long s, xx, yy;
  scanf("%d", &n);
  k = 1;
  f(i, n) k = k * 3;
  f(i, n) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    aaa.push_back(make_pair(a[i], i));
    bbb.push_back(make_pair(b[i], i));
  }
  sort(aaa.begin(), aaa.end());
  sort(bbb.begin(), bbb.end());
  f(i, k) {
    x = i;
    cnt = 0;
    aa.clear();
    bb.clear();
    aa.push_back(0);
    bb.push_back(0);
    aaaaaa = 0;
    bbbbbb = 0;
    f(j, n) {
      d[j] = x % 3;
      if (d[j] > 0)
        cnt++;
      x /= 3;
      aaaaaa *= 2;
      bbbbbb *= 2;
      if (d[j] == 1) {
        aa.push_back(a[j]);
        aaaaaa++;
      }
      if (d[j] == 2) {
        bb.push_back(b[j]);
        bbbbbb++;
      }
    }
    sort(aa.begin(), aa.end());
    sort(bb.begin(), bb.end());
    s = 0;
    if (aaaaa[aaaaaa][N - 1] == 0) {
      xa = 0;
      sz = aa.size();
      f(j, n) {
        while (xa < sz) {
          if (aaa[j].first > aa[xa])
            xa++;
          else
            break;
        }
        x = abs(aaa[j].first - aa[xa - 1]);
        if (xa < sz)
          x = min(x, abs(aaa[j].first - aa[xa]));
        aaaa[aaa[j].second] = x;
        aaaaa[aaaaaa][aaa[j].second] = x;
      }
      aaaaa[aaaaaa][N - 1] = 1;
    }

    if (bbbbb[bbbbbb][N - 1] == 0) {
      xb = 0;
      sz = bb.size();
      f(j, n) {
        while (xb < sz) {
          if (bbb[j].first > bb[xb])
            xb++;
          else
            break;
        }
        x = abs(bbb[j].first - bb[xb - 1]);
        if (xb < sz)
          x = min(x, abs(bbb[j].first - bb[xb]));
        bbbb[bbb[j].second] = x;
        bbbbb[bbbbbb][bbb[j].second] = x;
      }
      bbbbb[bbbbbb][N - 1] = 1;
    }
    f(j, n) {
      x = min(aaaaa[aaaaaa][j], bbbbb[bbbbbb][j]);
      xx = (long long)x;
      yy = (long long)c[j];
      s += (xx * yy);
    }
    ans[cnt] = min(s, ans[cnt]);
  }
  f(i, n + 1) printf("%lld\\n", ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < (int)(n); i++)
#define N 20

int main() {
  int a[N];
  int b[N];
  int c[N];
  int d[N];
  long long ans[N];
  vector<int> aa;
  vector<int> bb;
  f(i, N) ans[i] = 1000000000000000000;
  int n, k;
  int x, y, z;
  int l, r, m;
  int cnt;
  vector<pair<int, int>> aaa;
  vector<pair<int, int>> bbb;
  int aaaa[N];
  int bbbb[N];
  int xa, xb, sz;
  long long s, xx, yy;
  scanf("%d", &n);
  k = 1;
  f(i, n) k = k * 3;
  f(i, n) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    aaa.push_back(make_pair(a[i], i));
    bbb.push_back(make_pair(b[i], i));
  }
  sort(aaa.begin(), aaa.end());
  sort(bbb.begin(), bbb.end());
  f(i, k) {
    x = i;
    cnt = 0;
    aa.clear();
    bb.clear();
    aa.push_back(0);
    bb.push_back(0);
    f(j, n) {
      d[j] = x % 3;
      if (d[j] > 0)
        cnt++;
      x /= 3;
      if (d[j] == 1)
        aa.push_back(a[j]);
      if (d[j] == 2)
        bb.push_back(b[j]);
    }
    sort(aa.begin(), aa.end());
    sort(bb.begin(), bb.end());
    s = 0;
    xa = 0;
    sz = aa.size();
    f(j, n) {
      while (xa < sz) {
        if (aaa[j].first > aa[xa])
          xa++;
        else
          break;
      }
      x = abs(aaa[j].first - aa[xa - 1]);
      if (xa < sz)
        x = min(x, abs(aaa[j].first - aa[xa]));
      aaaa[aaa[j].second] = x;
    }
    xb = 0;
    sz = bb.size();
    f(j, n) {
      while (xb < sz) {
        if (bbb[j].first > bb[xb])
          xb++;
        else
          break;
      }
      x = abs(bbb[j].first - bb[xb - 1]);
      if (xb < sz)
        x = min(x, abs(bbb[j].first - bb[xb]));
      bbbb[bbb[j].second] = x;
    }
    f(j, n) {
      x = min(aaaa[j], bbbb[j]);
      xx = (long long)x;
      yy = (long long)c[j];
      s += (xx * yy);
    }
    ans[cnt] = min(s, ans[cnt]);
  }
  f(i, n + 1) printf("%lld\\n", ans[i]);

  return 0;
}