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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#include <cmath>
#include <queue>
#include <stack>

using namespace std;

#define LL long long

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

LL mod(LL x, LL md) { return ((x % md) + md) % md; }

class Mint {
public:
  static LL md;
  LL x;
  Mint(LL _x = 0) { x = mod(_x, md); }
  Mint(const Mint &y) { x = mod(y.x, md); }
};

void MintMyExgcd(LL a, LL b, LL &x, LL &y) {
  // solve equation ax + by = 1 (mod m)
  LL md = Mint::md;
  if (b == 0) {
    y = 0;
    x = 1;
    return;
  }
  MintMyExgcd(b, a % b, x, y);
  LL t = y;
  y = mod(x - (a / b) * y, md);
  x = t;
}

LL MintNY(LL x) {
  if (x == 0) {
    return 1;
  }
  LL md = Mint::md;
  LL a, b;
  MintMyExgcd(x, md, a, b);
  while (a < 0) {
    a += md;
  }
  return a;
}

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

Mint operator+(Mint a, Mint b) { return Mint(mod(a.x + b.x, Mint::md)); }
Mint operator-(Mint a, Mint b) { return Mint(mod(a.x - b.x, Mint::md)); }
Mint operator*(Mint a, Mint b) { return Mint(mod(a.x * b.x, Mint::md)); }
Mint operator/(Mint a, Mint b) {
  Mint c = Mint(MintNY(b.x));
  return Mint(mod(a.x * c.x, Mint::md));
}

Mint operator-(Mint a) { return Mint(-a.x); }
bool operator==(Mint a, Mint b) { return a.x == b.x; }
bool operator!=(Mint a, Mint b) { return a.x != b.x; }
bool operator>(Mint a, Mint b) { return a.x > b.x; }
bool operator<(Mint a, Mint b) { return a.x < b.x; }
bool operator>=(Mint a, Mint b) { return a.x >= b.x; }
bool operator<=(Mint a, Mint b) { return a.x <= b.x; }

void operator+=(Mint &a, Mint b) { a = a + b; }
void operator-=(Mint &a, Mint b) { a = a - b; }
void operator*=(Mint &a, Mint b) { a = a * b; }
void operator/=(Mint &a, Mint b) { a = a / b; }

ostream &operator<<(ostream &out, Mint a) {
  out << a.x;
  return out;
}

istream &operator>>(istream &in, Mint &a) {
  in >> a.x;
  return in;
}

Mint factorial(LL n) {
  if (n == 0) {
    return Mint(1);
  }
  Mint res = 1;
  for (LL i = 1; i <= n; ++i) {
    res *= i;
  }
  return res;
}
Mint multiRange(LL s, LL t) {
  Mint res = 1;
  for (LL i = s; i <= t; ++i) {
    res *= i;
  }
  return res;
}

Mint getC(LL n, LL k) {
  if (n - k < k) {
    return getC(n, n - k);
  }
  return multiRange(n - k + 1, n) / factorial(k);
}

Mint getA(LL n, LL k) { return multiRange(n - k + 1, n); }

Mint countNonNegativeDivision(LL n, LL k) { return getC(n + k - 1, k - 1); }
Mint countPositiveDivision(LL n, LL k) {
  if (n < k) {
    return 0;
  }
  if (k <= 0) {
    return 0;
  }
  return getC(n - 1, k - 1);
}
Mint quickMod(Mint a, LL b) {
  Mint res = 1;
  while (b > 0) {
    if (b % 2) {
      res *= a;
    }
    a *= a;
    b /= 2;
  }
  return res;
}
template <class TypeName> vector<TypeName> getVector1D(int n, TypeName val) {
  return vector<TypeName>(n, val);
}
template <class TypeName>
vector<vector<TypeName>> getVector2D(int n1, int n2, TypeName val) {
  vector<vector<TypeName>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<TypeName>(n2, val);
  }
  return res;
}
template <class TypeName>
vector<vector<vector<TypeName>>> getVector3D(int n1, int n2, int n3,
                                             TypeName val) {
  vector<vector<vector<TypeName>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<TypeName>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<TypeName>(n3, val);
    }
  }
  return res;
}
template <class TypeName>
vector<vector<vector<vector<TypeName>>>> getVector4D(int n1, int n2, int n3,
                                                     int n4, TypeName val) {
  vector<vector<vector<vector<TypeName>>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<vector<TypeName>>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<vector<TypeName>>(n3);
      for (int k = 0; k < n3; ++k) {
        res[i][j][k] = vector<TypeName>(n4, val);
      }
    }
  }
  return res;
}

LL Mint::md = 1000000007;
int n;
vector<LL> x, y, p;
vector<LL> xd, yd;

vector<LL> xL, yL;
vector<LL> currXd, currYd;

vector<LL> res;
vector<vector<LL>> xdtmp, ydtmp;

int ct = 0;
void searchMin(int u) {
  if (u >= n) {
    LL curr = 0;
    for (int i = 0; i < n; ++i) {
      curr += min(xd[i], yd[i]) * p[i];
    }
    res[ct] = min(res[ct], curr);
    // for (int i = 0; i < n; ++i) {
    // 	LL curMin = 10000000;
    // 	for (int j = 0; j < xL.size(); ++j) {
    // 		curMin = min(curMin, abs(x[i] - xL[j]));
    // 	}
    // 	for (int j = 0; j < yL.size(); ++j) {
    // 		curMin = min(curMin, abs(y[i] - yL[j]));
    // 	}
    // 	curr += (p[i] * curMin);
    // }
    // res[ct] = min(res[ct], curr);
    return;
  }
  // xL.push_back(x[u]);
  ++ct;
  swap(xdtmp[u], xd);
  for (int i = 0; i < n; ++i) {
    // xdtmp[u][i] = xd[i];
    // copy(xd.begin(), xd.end(), xdtmp[u].begin());
    xd[i] = min(xdtmp[u][i], abs(x[i] - x[u]));
  }
  searchMin(u + 1);
  swap(xdtmp[u], xd);
  // for (int i = 0; i < n; ++i) {
  // 	xd[i] = xdtmp[u][i];
  // }
  --ct;
  // xL.pop_back();

  // yL.push_back(y[u]);
  swap(ydtmp[u], yd);
  for (int i = 0; i < n; ++i) {
    // ydtmp[u][i] = yd[i];
    yd[i] = min(ydtmp[u][i], abs(y[i] - y[u]));
  }
  ++ct;
  searchMin(u + 1);
  swap(ydtmp[u], yd);
  // for (int i = 0; i < n; ++i) {
  // 	yd[i] = ydtmp[u][i];
  // }
  --ct;
  // yL.pop_back();
  searchMin(u + 1);
}

vector<LL> searchForMin() {
  xd = vector<LL>(n);
  yd = vector<LL>(n);
  xdtmp = vector<vector<LL>>(n + 1);
  ydtmp = vector<vector<LL>>(n + 1);
  for (int i = 0; i < n; ++i) {
    xd[i] = abs(x[i]);
    yd[i] = abs(y[i]);
  }
  for (int i = 0; i <= n; ++i) {
    xdtmp[i] = xd;
    ydtmp[i] = yd;
  }
  LL rres = 0;
  for (int i = 0; i < n; ++i) {
    rres += p[i] * min(xd[i], yd[i]);
  }
  res = vector<LL>(n + 1, rres);
  xL.clear();
  xL.push_back(0);
  yL.clear();
  yL.push_back(0);
  searchMin(0);

  return res;
}

void countN() {
  int n = 15;
  int aa = 1;
  vector<int> cnt(16, 0);
  for (int i = 0; i < n; ++i) {
    aa *= 3;
  }
  for (int x = 0; x < aa; ++x) {
    int xx = x;
    int ct = 0;
    for (int j = 0; j < 15; ++j) {
      if (xx % 3) {
        ++ct;
      }
      xx /= 3;
    }
    cnt[ct] += 1;
  }
  for (int i = 0; i < cnt.size(); ++i) {
    cout << cnt[i] << endl;
  }
}

int main() {
  // std::ios::sync_with_stdio(false);
  // std::cin.tie(0);
  cin >> n;
  x = vector<LL>(n);
  y = vector<LL>(n);
  p = vector<LL>(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> p[i];
  }
  vector<LL> res = searchForMin();
  for (int i = 0; i <= n; ++i) {
    cout << res[i] << endl;
  }
  // cout << 0 << endl;
  // countN();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#include <cmath>
#include <queue>
#include <stack>

using namespace std;

#define LL long long

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

LL mod(LL x, LL md) { return ((x % md) + md) % md; }

class Mint {
public:
  static LL md;
  LL x;
  Mint(LL _x = 0) { x = mod(_x, md); }
  Mint(const Mint &y) { x = mod(y.x, md); }
};

void MintMyExgcd(LL a, LL b, LL &x, LL &y) {
  // solve equation ax + by = 1 (mod m)
  LL md = Mint::md;
  if (b == 0) {
    y = 0;
    x = 1;
    return;
  }
  MintMyExgcd(b, a % b, x, y);
  LL t = y;
  y = mod(x - (a / b) * y, md);
  x = t;
}

LL MintNY(LL x) {
  if (x == 0) {
    return 1;
  }
  LL md = Mint::md;
  LL a, b;
  MintMyExgcd(x, md, a, b);
  while (a < 0) {
    a += md;
  }
  return a;
}

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

Mint operator+(Mint a, Mint b) { return Mint(mod(a.x + b.x, Mint::md)); }
Mint operator-(Mint a, Mint b) { return Mint(mod(a.x - b.x, Mint::md)); }
Mint operator*(Mint a, Mint b) { return Mint(mod(a.x * b.x, Mint::md)); }
Mint operator/(Mint a, Mint b) {
  Mint c = Mint(MintNY(b.x));
  return Mint(mod(a.x * c.x, Mint::md));
}

Mint operator-(Mint a) { return Mint(-a.x); }
bool operator==(Mint a, Mint b) { return a.x == b.x; }
bool operator!=(Mint a, Mint b) { return a.x != b.x; }
bool operator>(Mint a, Mint b) { return a.x > b.x; }
bool operator<(Mint a, Mint b) { return a.x < b.x; }
bool operator>=(Mint a, Mint b) { return a.x >= b.x; }
bool operator<=(Mint a, Mint b) { return a.x <= b.x; }

void operator+=(Mint &a, Mint b) { a = a + b; }
void operator-=(Mint &a, Mint b) { a = a - b; }
void operator*=(Mint &a, Mint b) { a = a * b; }
void operator/=(Mint &a, Mint b) { a = a / b; }

ostream &operator<<(ostream &out, Mint a) {
  out << a.x;
  return out;
}

istream &operator>>(istream &in, Mint &a) {
  in >> a.x;
  return in;
}

Mint factorial(LL n) {
  if (n == 0) {
    return Mint(1);
  }
  Mint res = 1;
  for (LL i = 1; i <= n; ++i) {
    res *= i;
  }
  return res;
}
Mint multiRange(LL s, LL t) {
  Mint res = 1;
  for (LL i = s; i <= t; ++i) {
    res *= i;
  }
  return res;
}

Mint getC(LL n, LL k) {
  if (n - k < k) {
    return getC(n, n - k);
  }
  return multiRange(n - k + 1, n) / factorial(k);
}

Mint getA(LL n, LL k) { return multiRange(n - k + 1, n); }

Mint countNonNegativeDivision(LL n, LL k) { return getC(n + k - 1, k - 1); }
Mint countPositiveDivision(LL n, LL k) {
  if (n < k) {
    return 0;
  }
  if (k <= 0) {
    return 0;
  }
  return getC(n - 1, k - 1);
}
Mint quickMod(Mint a, LL b) {
  Mint res = 1;
  while (b > 0) {
    if (b % 2) {
      res *= a;
    }
    a *= a;
    b /= 2;
  }
  return res;
}
template <class TypeName> vector<TypeName> getVector1D(int n, TypeName val) {
  return vector<TypeName>(n, val);
}
template <class TypeName>
vector<vector<TypeName>> getVector2D(int n1, int n2, TypeName val) {
  vector<vector<TypeName>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<TypeName>(n2, val);
  }
  return res;
}
template <class TypeName>
vector<vector<vector<TypeName>>> getVector3D(int n1, int n2, int n3,
                                             TypeName val) {
  vector<vector<vector<TypeName>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<TypeName>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<TypeName>(n3, val);
    }
  }
  return res;
}
template <class TypeName>
vector<vector<vector<vector<TypeName>>>> getVector4D(int n1, int n2, int n3,
                                                     int n4, TypeName val) {
  vector<vector<vector<vector<TypeName>>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<vector<TypeName>>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<vector<TypeName>>(n3);
      for (int k = 0; k < n3; ++k) {
        res[i][j][k] = vector<TypeName>(n4, val);
      }
    }
  }
  return res;
}

LL Mint::md = 1000000007;
int n;
vector<LL> x, y, p;
vector<LL> xd, yd;

vector<LL> xL, yL;
vector<LL> currXd, currYd;

LL res;

void searchMin(int u, int k) {
  if (u >= n) {
    LL curr = 0;
    for (int i = 0; i < n; ++i) {
      LL curMin = 10000000;
      for (int j = 0; j < xL.size(); ++j) {
        curMin = min(curMin, abs(x[i] - xL[j]));
      }
      for (int j = 0; j < yL.size(); ++j) {
        curMin = min(curMin, abs(y[i] - yL[j]));
      }
      curr += (p[i] * curMin);
    }
    res = min(res, curr);
    return;
  }
  if (n - u < k) {
    return;
  }
  if (k > 0) {
    xL.push_back(x[u]);
    searchMin(u + 1, k - 1);
    xL.pop_back();

    yL.push_back(y[u]);
    searchMin(u + 1, k - 1);
    yL.pop_back();
  }
  searchMin(u + 1, k);
}

LL searchForMin(int k) {
  xd = vector<LL>(n);
  yd = vector<LL>(n);
  for (int i = 0; i < n; ++i) {
    xd[i] = abs(x[i]);
    yd[i] = abs(y[i]);
  }
  res = 0;
  for (int i = 0; i < n; ++i) {
    res += p[i] * min(xd[i], yd[i]);
  }
  xL.clear();
  xL.push_back(0);
  yL.clear();
  yL.push_back(0);
  searchMin(0, k);

  return res;
}

void countN() {
  int n = 15;
  int aa = 1;
  vector<int> cnt(16, 0);
  for (int i = 0; i < n; ++i) {
    aa *= 3;
  }
  for (int x = 0; x < aa; ++x) {
    int xx = x;
    int ct = 0;
    for (int j = 0; j < 15; ++j) {
      if (xx % 3) {
        ++ct;
      }
      xx /= 3;
    }
    cnt[ct] += 1;
  }
  for (int i = 0; i < cnt.size(); ++i) {
    cout << cnt[i] << endl;
  }
}

int main() {
  // std::ios::sync_with_stdio(false);
  // std::cin.tie(0);
  cin >> n;
  x = vector<LL>(n);
  y = vector<LL>(n);
  p = vector<LL>(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> p[i];
  }
  for (int i = 0; i < n; ++i) {
    cout << searchForMin(i) << endl;
  }
  cout << 0 << endl;
  // countN();
  return 0;
}