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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int N;
int X[15], Y[15], P[15];
int genlis[15];
long long int ans = 2000000000000000000;
int cou = 0;
void func2(int gen) {
  for (int i = 0; i < (1 << gen); i++) {
    long long int karians = 0;
    for (int j = 0; j < N; j++) {
      int kyori = 2000000000;
      for (int k = 0; k < gen; k++) {
        if (i & (1 << k))
          kyori = min(kyori, abs(X[genlis[k]] - X[j]));
        else
          kyori = min(kyori, abs(Y[genlis[k]] - Y[j]));
      }
      kyori = min(kyori, abs(X[j]));
      kyori = min(kyori, abs(Y[j]));

      // printf("[%d]:[%lld]\\n",j,kyori);
      karians += (long long int)kyori * (long long int)P[j];
      if (ans <= karians)
        break;
    }
    ans = min(ans, karians);
  }
}
void func(int gen, int depth, int dokomade) {
  if (N - dokomade - 1 < gen - depth) {

    return;
  }
  if (gen == depth) {
    // printf("[%lld][%lld]\\n",genlis[0],genlis[1]);
    func2(gen);

    return;
  }
  for (int i = dokomade + 1; i < N; i++) {
    genlis[depth] = i;
    // depth;
    func(gen, depth + 1, i);
    // depth;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }

  // func(1,0,-1);
  // cout<<ans<<endl;

  for (int i = 0; i <= N; i++) {
    ans = 2000000000000000000;
    func(i, 0, -1);
    cout << ans << endl;
  }

  return 0;
}
/*
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<iostream>
#include<queue>
#include <time.h>
using namespace std;
int N;
int X[15],Y[15],P[15];
int genlis[15];
int abssX[16][16]={0};
int abssY[16][16]={0};
long long int ans=2000000000000000000;
int cou=0;
void func2(int gen){
    for(int i=0;i<(1<<gen);i++){
        long long int karians=0;
        for(int j=0;j<N;j++){
            int kyori=2000000000;
            for(int k=0;k<gen;k++){
                if(i&(1<<k))kyori=min(kyori,X[genlis[k]]-X[j]);
                else kyori=min(kyori,abs(Y[genlis[k]]-Y[j]));
            }
            kyori=min(kyori,abs(X[j]));
            kyori=min(kyori,abs(Y[j]));

            //printf("[%d]:[%lld]\\n",j,kyori);
            karians+=(long long int)kyori*(long long int)P[j];
        }
        ans=min(ans,karians);
    }
}
void func(int gen,int depth,int dokomade){
    if(N-dokomade-1<gen-depth){

        //return;
    }
    if(gen==depth){
        //printf("[%lld][%lld]\\n",genlis[0],genlis[1]);
        func2(gen);




        return;
    }
    for(int i=dokomade+1;i<N;i++){
        genlis[depth]=i;
        //depth;
        func(gen,depth+1,i);
        //depth;
    }
}




int main(){


    cin>>N;
    for(int i=0;i<N;i++){
        cin>>X[i]>>Y[i]>>P[i];
    }
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            abssX[i][j]=abs(X[i]-X[j]);
            abssX[i][j]=abs(X[i]-X[j]);
        }
    }

    //func(1,0,-1);
    //cout<<ans<<endl;







    for(int i=0;i<=N;i++){
        clock_t st = clock();
        ans=2000000000000000000;
        func(i,0,-1);
        cout<<ans<<endl;
        clock_t en = clock();
        printf("[%d]:[%lf]\\n",i,(double)(en-st)/CLOCKS_PER_SEC);
    }

    return 0;
}
*/


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int N;
int X[15], Y[15], P[15];
int genlis[15];
long long int ans = 2000000000000000000;
int cou = 0;
void func2(int gen) {
  for (int i = 0; i < (1 << gen); i++) {
    long long int karians = 0;
    for (int j = 0; j < N; j++) {
      int kyori = 2000000000;
      for (int k = 0; k < gen; k++) {
        if (i & (1 << k))
          kyori = min(kyori, abs(X[genlis[k]] - X[j]));
        else
          kyori = min(kyori, abs(Y[genlis[k]] - Y[j]));
      }
      kyori = min(kyori, abs(X[j]));
      kyori = min(kyori, abs(Y[j]));

      // printf("[%d]:[%lld]\\n",j,kyori);
      karians += (long long int)kyori * (long long int)P[j];
    }
    ans = min(ans, karians);
  }
}
void func(int gen, int depth, int dokomade) {
  if (gen == depth) {
    // printf("[%lld][%lld]\\n",genlis[0],genlis[1]);
    func2(gen);

    return;
  }
  for (int i = dokomade + 1; i < N; i++) {
    genlis[depth] = i;
    // depth;
    func(gen, depth + 1, i);
    // depth;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }

  // func(1,0,-1);
  // cout<<ans<<endl;

  for (int i = 0; i <= N; i++) {
    ans = 2000000000000000000;
    func(i, 0, -1);
    cout << ans << endl;
  }

  return 0;
}