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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

const double PI = acos(-1);
const double EPS = 1e-9; // うまくいかなかったらゆるめる

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

vi tate, yoko;
vi ans;
vi x, y, p;
int n;

void dfs(int i, int cnt) {
  // ここで計算する
  if (i == n) {
    int cnt = tate.size() + yoko.size() - 2;
    int sum = 0;
    rep(i, n) {
      int d = LINF;
      rep(j, tate.size()) d = min(d, abs(tate[j] - x[i]));
      rep(j, yoko.size()) d = min(d, abs(yoko[j] - y[i]));
      sum += p[i] * d;
    }
    ans[cnt] = min(ans[cnt], sum);
    return;
  }

  dfs(i + 1, cnt);

  tate.push_back(x[i]);
  dfs(i + 1, cnt + 1);
  tate.pop_back();

  yoko.push_back(y[i]);
  dfs(i + 1, cnt + 1);
  yoko.pop_back();
}

signed main(void) {
  cin >> n;
  x = y = p = vi(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  ans = vi(n + 1, LINF);
  int ma = 1;
  rep(i, n) ma *= 3;
  tate.push_back(0);
  yoko.push_back(0);
  dfs(0, 0);
  rep(i, n + 1) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

const double PI = acos(-1);
const double EPS = 1e-9; // うまくいかなかったらゆるめる

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

signed main(void) {
  int n;
  cin >> n;
  vi x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vi ans(n + 1, LINF);
  int ma = 1;
  rep(i, n) ma *= 3;
  vi tate, yoko;
  tate.reserve(15);
  yoko.reserve(15);
  rep(bit, ma) {
    tate.clear();
    yoko.clear();
    tate.push_back(0);
    yoko.push_back(0);
    int tmp = bit;
    rep(i, n) {
      if (tmp % 3 == 1) {
        tate.push_back(x[i]);
      }
      if (tmp % 3 == 2) {
        yoko.push_back(y[i]);
      }
      tmp /= 3;
    }
    /*
    sort(all(tate));
    sort(all(yoko));
    */
    int cnt = tate.size() + yoko.size() - 2;
    int sum = 0;
    rep(i, n) {
      int d = LINF;
      rep(j, tate.size()) d = min(d, abs(tate[j] - x[i]));
      rep(j, yoko.size()) d = min(d, abs(yoko[j] - y[i]));
      sum += p[i] * d;
    }
    ans[cnt] = min(ans[cnt], sum);
  }
  rep(i, n + 1) cout << ans[i] << endl;
}