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

#include <bits/stdc++.h>

using namespace std;

int n, a[20];
long long P[20], X[20], Y[20], Re[20];

void Gen(int I, int A[]) {
  if (I == n) {
    int k1 = 0, k2 = 0;
    long long Vx[17], Vy[17], Mi[17];
    Vx[0] = 0;
    Vy[0] = 0;
    for (int i = 0; i < n; i++) {
      Mi[i] = 0;
      if (a[i] == 1) {
        Vx[k1 + 1] = X[i];
        k1++;
      } else if (a[i] == 2) {
        Vy[k2 + 1] = Y[i];
        k2++;
      } else {
        Mi[i] = 2000000000000;
      }
    }
    for (int i = 0; i < n; i++) {
      if (a[i] == 0) {
        for (int y = 0; y <= k1; y++) {
          Mi[i] = min(Mi[i], abs(Vx[y] - X[i]));
        }
        for (int y = 0; y <= k2; y++) {
          Mi[i] = min(Mi[i], abs(Vy[y] - Y[i]));
        }
      }
    }
    long long At = 0;
    for (int i = 0; i < n; i++) {
      At += P[i] * Mi[i];
    }
    Re[k1 + k2] = min(Re[k1 + k2], At);
  } else {
    A[I] = 0;
    Gen(I + 1, A);
    A[I] = 1;
    Gen(I + 1, A);
    A[I] = 2;
    Gen(I + 1, A);
    A[I] = 0;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  // sort(VX,VX+n);
  // sort(VY,VY+n);
  for (int i = 0; i <= n; i++) {
    Re[i] = 2000000000000000;
    a[i] = 0;
  }
  Gen(0, a);
  for (int i = 0; i <= n; i++) {
    cout << Re[i] << "\\n";
  }
  return 0;
}
/*
15
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903
10 10 12212
12 12 92212
14 14 21855
19 13 12155
15 19 26555
135 544 78945
1561 5612 12398
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, a[20];
long long P[20], X[20], Y[20], Re[20];
pair<int, int> VX[17], VY[17];

void Gen(int I, int A[]) {
  if (I == n) {
    int k1 = 0, k2 = 0;
    long long Vx[17], Vy[17], Mi[17];
    Vx[0] = 0;
    Vy[0] = 0;
    for (int i = 0; i < n; i++) {
      Mi[i] = 2000000000000;
      if (a[i] == 1) {
        Vx[k1 + 1] = X[i];
        k1++;
      } else if (a[i] == 2) {
        Vy[k2 + 1] = Y[i];
        k2++;
      }
    }
    sort(Vx, Vx + k1 + 1);
    sort(Vy, Vy + k2 + 1);
    long long At = 0;
    int I = 0, II;
    for (int i = 0; i <= k1; i++) {
      while (VX[I].first <= Vx[i]) {
        II = VX[I].second;
        if (Vx[i] - VX[I].first < Mi[II]) {
          Mi[II] = Vx[i] - VX[I].first;
        }
        I++;
        if (I == n) {
          i = k1 + 1;
          break;
        }
      }
    }
    I = 0;
    for (int i = 0; i <= k2; i++) {
      while (VY[I].first <= Vy[i]) {
        II = VY[I].second;
        if (Vy[i] - VY[I].first < Mi[II]) {
          Mi[II] = Vy[i] - VY[I].first;
        }
        I++;
        if (I == n) {
          i = k2 + 1;
          break;
        }
      }
    }
    I = n - 1;
    for (int i = k1; i >= 0; i--) {
      while (VX[I].first >= Vx[i]) {
        II = VX[I].second;
        if (VX[I].first - Vx[i] < Mi[II]) {
          Mi[II] = VX[I].first - Vx[i];
        }
        I--;
        if (I == -1) {
          i = -1;
          break;
        }
      }
    }
    I = n - 1;
    for (int i = k2; i >= 0; i--) {
      while (VY[I].first >= Vy[i]) {
        II = VY[I].second;
        if (VY[I].first - Vy[i] < Mi[II]) {
          Mi[II] = VY[I].first - Vy[i];
        }
        I--;
        if (I == -1) {
          i = -1;
          break;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      At += Mi[i] * P[i];
    }
    Re[k1 + k2] = min(Re[k1 + k2], At);
  } else {
    A[I] = 0;
    Gen(I + 1, A);
    A[I] = 1;
    Gen(I + 1, A);
    A[I] = 2;
    Gen(I + 1, A);
    A[I] = 0;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> X[i] >> Y[i] >> P[i];
    VX[i] = {X[i], i};
    VY[i] = {Y[i], i};
  }
  sort(VX, VX + n);
  sort(VY, VY + n);
  for (int i = 0; i <= n; i++) {
    Re[i] = 2000000000000000;
    a[i] = 0;
  }
  Gen(0, a);
  for (int i = 0; i <= n; i++) {
    cout << Re[i] << "\\n";
  }
  return 0;
}
/*
15
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903
10 10 12212
12 12 92212
14 14 21855
19 13 12155
15 19 26555
135 544 78945
1561 5612 12398
*/