Problem:
Problem K: Manhattan Warp Machine 2
Problem
とある宇宙では、3次元の格子点上に星が存在し、宇宙人達はマンハッタンワープ装置という装置を使い、星間を移動している。
このワープ装置には、
N
個のボタンが付いており、ボタン
i
を押すと、現在いる星からのマンハッタン距離が
d
i
である任意の星にワープすることができる。
ワープ装置は改良され、コストがかからずに移動出来る。
今、(0, 0, 0)の星にいるある宇宙人がある
M
個の星を訪れたいと考えている。
M
個の星を全て訪れるのに最短で何回ワープをする必要があるか答えよ。
もし、1つでも訪れることが不可能な星がある場合は-1を出力せよ。
M
個の星は好きな順番に訪れることが出来、全て訪れた後に(0, 0, 0)に戻って来る必要は無い。
3次元上の全ての格子点には必ず星が存在する。
点(
x
1
,
y
1
,
z
1
)と点(
x
2
,
y
2
,
z
2
)間のマンハッタン距離は|
x
1
-
x
2
| + |
y
1
-
y
2
| + |
z
1
-
z
2
|で表される。
Input
N
M
d
1
...
d
N
x
1
y
1
z
1
...
x
M
y
M
z
M
入力は全て整数で与えられる。
1行目に
N
,
M
が与えられる。
2行目に移動可能なマンハッタン距離
d
i
が空白区切りで与えられる。
3行目以降の
M
行に、
M
個の訪れたい星の格子点の座標(
x
i
,
y
i
,
z
i
)が1行ずつ空白区切りで与えられる。
Constraints
入力は以下の条件を満たす。
1 ≤
N
≤ 15
1 ≤
M
≤ 15
1 ≤
d
i
≤ 3×10
5
-10
5
≤
x
i
,
y
i
,
z
i
≤ 10
5
与えられるマンハッタン距離
d
i
は全て異なる。
与えられる格子点の座標は全て異なる。
Output
M
個の星全てを訪れるのにかかる最短ワープ回数を1行に出力せよ。
訪れることが不可能な星がある場合は-1を出力せよ。
Sample Input 1
2 1
1 2
5 0 0
Sample Output 1
3
Sample Input 2
2 2
9 3
3 0 0
3 9 0
Sample Output 2
2
Sample Input 3
1 1
4
3 0 0
Sample Output 3
-1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

#define  range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; 
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

class BFS_Graph {
  const int INF = 1001001001;

  int n_;
  vector< vector< int > > G;
  vector< int > ds;

public:
  BFS_Graph(int n) : n_(n), G(n), ds(n, INF) {}

  int inf() const { return INF; }

  void add_edge(int u, int v) {
    G[u].emplace_back(v);
  }

  void build(int st) {
    queue<int> que;
    que.emplace(st);
    ds[st] = 0;

    while (not que.empty()) {
      int v = que.front();
      que.pop();
      for (auto &u : G[v]) {
        if (ds[u] != INF) continue;
        ds[u] = ds[v] + 1;
        que.emplace(u);
      }
    }
  }

  int operator[](size_t idx) const {
    return ds.at(idx);
  }
};

template < typename CostType >
class shortestHamiltonPath {
  using u32 = unsigned int;
  struct edge {
    u32 to;
    CostType cost;

    edge(u32 to_, CostType cost_) : to(to_), cost(cost_) {}
  };

  const CostType INF = numeric_limits<CostType>::max() / 2;

  u32 V;
  vector< vector<edge> > G;
  vector< CostType > ds;

  vector< vector< CostType > > dp;

  CostType dfs(u32 v, u32 bit) {
    CostType &res = dp[v][bit];
    if (bit + 1 == 1u << V) res = 0;
    if (res != -1) return res;
    res = INF;
    for (auto [u, cost] : G[v]) {
      if (bit & (1 << u)) continue;
      res = min(res, cost + dfs(u, bit | (1 << u)));
    }
    return res;
  }

  public:
  shortestHamiltonPath(u32 V_) : V(V_), G(V_) {}

  void add_edge(int from, int to, CostType cost) {
    G[from].emplace_back(to, cost);
  }

  CostType inf() {
    return INF;
  }

  CostType shortest_hamilton_path() {
    dp.assign(V, vector< CostType >(1 << V, -1));
    return dfs(0, 1);
  }
};

void solve() {
  int n = input(), m = input();
  
  int V = 6e5;
  BFS_Graph G(V);

  int max_d = -1;
  range(i, 0, n) {
    int d = input();
    chmax(max_d, d);
    range(x, d, V) {
      G.add_edge(x - d, x);
      G.add_edge(x, x - d);
    }
  }

  G.build(0);

  vector< int > xs(m + 1), ys(m + 1), zs(m + 1);
  range(i, 1, m + 1) {
    cin >> xs[i] >> ys[i] >> zs[i];
  }
  
  shortestHamiltonPath<i64> T(m + 1);
  range(u, 0, m + 1) range(v, u + 1, m + 1) {
    int d = abs(xs[u] - xs[v]) + abs(ys[u] - ys[v]) + abs(zs[u] - zs[v]);
    int cost = G[d % max_d];
    if (cost == G.inf()) continue;
    T.add_edge(u, v, cost + d / max_d);
    T.add_edge(v, u, cost + d / max_d);
  }

  i64 ans = T.shortest_hamilton_path();
  if (ans == T.inf()) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

signed main() {
  solve();
}
accuracy: 0.125
time_ms: 40
memory_kb: 38312
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

#define  range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; 
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

class BFS_Graph {
  const int INF = 1001001001;

  int n_;
  vector< vector< int > > G;
  vector< int > ds;

public:
  BFS_Graph(int n) : n_(n), G(n), ds(n, INF) {}

  int inf() const { return INF; }

  void add_edge(int u, int v) {
    G[u].emplace_back(v);
  }

  void build(int st) {
    queue<int> que;
    que.emplace(st);
    ds[st] = 0;

    while (not que.empty()) {
      int v = que.front();
      que.pop();
      for (auto &u : G[v]) {
        if (ds[u] != INF) continue;
        ds[u] = ds[v] + 1;
        que.emplace(u);
      }
    }
  }

  int operator[](size_t idx) const {
    return ds.at(idx);
  }
};

template < typename CostType >
class shortestHamiltonPath {
  using u32 = unsigned int;
  struct edge {
    u32 to;
    CostType cost;

    edge(u32 to_, CostType cost_) : to(to_), cost(cost_) {}
  };

  const CostType INF = numeric_limits<CostType>::max() / 2;

  u32 V;
  vector< vector<edge> > G;
  vector< CostType > ds;

  vector< vector< CostType > > dp;

  CostType dfs(u32 v, u32 bit) {
    CostType &res = dp[v][bit];
    if (bit + 1 == 1u << V) res = 0;
    if (res != -1) return res;
    res = INF;
    for (auto [u, cost] : G[v]) {
      if (bit & (1 << u)) continue;
      res = min(res, cost + dfs(u, bit | (1 << u)));
    }
    return res;
  }

  public:
  shortestHamiltonPath(u32 V_) : V(V_), G(V_) {}

  void add_edge(int from, int to, CostType cost) {
    G[from].emplace_back(to, cost);
  }

  CostType inf() {
    return INF;
  }

  CostType shortest_hamilton_path() {
    dp.assign(V, vector< CostType >(1 << V, -1));
    return dfs(0, 1);
  }
};

void solve() {
  int n = input(), m = input();
  
  int V = 6e5;
  BFS_Graph G(V);

  int max_d;
  range(i, 0, n) {
    int d = input();
    chmax(max_d, d);
    range(x, d, V) {
      G.add_edge(x - d, x);
      G.add_edge(x, x - d);
    }
  }

  G.build(0);

  vector< int > xs(m + 1), ys(m + 1), zs(m + 1);
  range(i, 1, m + 1) {
    cin >> xs[i] >> ys[i] >> zs[i];
  }
  
  shortestHamiltonPath<i64> T(m + 1);
  range(u, 0, m + 1) range(v, u + 1, m + 1) {
    int d = abs(xs[u] - xs[v]) + abs(ys[u] - ys[v]) + abs(zs[u] - zs[v]);
    int cost = G[d % max_d];
    if (cost == G.inf()) continue;
    T.add_edge(u, v, cost + d / max_d);
    T.add_edge(v, u, cost + d / max_d);
  }

  i64 ans = T.shortest_hamilton_path();
  if (ans == T.inf()) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

signed main() {
  solve();
}
accuracy: 0.125
time_ms: 40
memory_kb: 38312
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF=1<<29,MAX=600001;
struct P {int x,y,z;}b[15];

int D(P p, P q) {return abs(p.x-q.x)+abs(p.y-q.y)+abs(p.z-q.z);}

int d[MAX];
int main() {
  int n,m;
  cin >> n >> m;
  int a[n],e[2]={0,0};
  for(int i=0; i<n; i++) cin >> a[i];
  sort(a,a+n);
  for(int i=0; i<MAX; i++) d[i]=(i?INF:0);
  for(int i=0; i<n; i++) {
    e[(a[i]+1)%2]=a[i];
    d[a[i]]=1;
    for(int j=a[i]-e[a[i]%2]; j<=a[i]+e[a[i]%2]; j+=2) d[j]=min(d[j],2);
  }
  for(int i=0; i<=a[n-1]*2; i+=2) d[i]=min(d[i],2);
  for(int i=0; i<MAX; i++) d[i]=min(d[i],min(d[abs(i-e[0])],d[abs(i-e[1])])+1);
  for(int i=0; i<m; i++) cin >> b[i].x >> b[i].y >> b[i].z;
  int w[m][m],dp[1<<m][m],ans=INF;
  for(int i=0; i<m; i++) {
    for(int j=0; j<m; j++) w[i][j]=d[D(b[i],b[j])];
  }
  fill(dp[0],dp[1<<m],INF);
  for(int i=0; i<m; i++) dp[1<<i][i]=d[D((P){0,0,0},b[i])];
  for(int t=1; t<(1<<m); t++) {
    for(int i=0; i<m; i++) {
      for(int j=0; j<m; j++) dp[t|(1<<j)][j]=min(dp[t|(1<<j)][j],dp[t][i]+w[i][j]);
    }
  }
  for(int i=0; i<m; i++) ans=min(ans,dp[(1<<m)-1][i]);
  cout << (ans<INF?ans:-1) << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7364
final_rank: 1

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 4;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    for(int i=0; i<n; ++i)
        cin >> d[i];
    sort(d.begin(), d.end());

    vector<int> x(m), y(m), z(m);
    for(int i=0; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(600001, INF);
    for(int i=0; i<n; ++i){
        for(int j=i; j<n; ++j){
            for(int k=d[j]-d[i]; k<=d[j]+d[i]; k+=2)
                cost[k] = 2;
        }
    }
    cost[0] = 0;
    for(int i=0; i<600001; ++i){
        for(int j=0; j<n; ++j){
            int k = i + d[j];
            if(k < 600001)
                cost[k] = min(cost[k], cost[i] + 1);

            k = abs(d[j] - i);
            if(k < 600001)
                cost[k] = min(cost[k], cost[i] + 1);
        }
    }

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    for(int i=0; i<m; ++i){
        int dist = abs(x[i]) + abs(y[i]) + abs(z[i]);
        dp[1<<i][i] = cost[dist];
    }
    for(int i=0; i<(1<<m); ++i){
        for(int j=0; j<m; ++j){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 6804
final_rank: 2

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 4;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    for(int i=0; i<n; ++i)
        cin >> d[i];
    sort(d.begin(), d.end());

    vector<int> x(m), y(m), z(m);
    for(int i=0; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(600001, INF);
    for(int i=0; i<n; ++i){
        for(int j=i; j<n; ++j){
            for(int k=d[j]-d[i]; k<=d[j]+d[i]; k+=2)
                cost[k] = 2;
        }
    }
    cost[0] = 0;
    for(int i=0; i<600001; ++i){
        for(int j=0; j<n; ++j){
            int k = abs(i - d[j]);
            cost[i] = min(cost[i], cost[k] + 1);
        }
    }

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    for(int i=0; i<m; ++i){
        int dist = abs(x[i]) + abs(y[i]) + abs(z[i]);
        dp[1<<i][i] = cost[dist];
    }
    for(int j=0; j<m; ++j){
        for(int i=0; i<(1<<m); ++i){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 0.6
time_ms: 30
memory_kb: 6804
final_rank: 4

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 4;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    for(int i=0; i<n; ++i)
        cin >> d[i];

    vector<int> x(m), y(m), z(m);
    for(int i=0; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(600001, INF);
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            cost[d[i]+d[j]] = 2;
        }
    }
    for(int i=0; i<600001; ++i){
        for(int j=0; j<n; ++j){
            int k = i + d[j];
            if(k < 600001)
                cost[k] = min(cost[k], cost[i] + 1);
        }
    }
    for(int i=600000; i>2; --i)
        cost[i-2] = min(cost[i-2], cost[i]);
    for(int i=0; i<n; ++i)
        cost[d[i]] = 1;
    cost[0] = 0;

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    for(int i=0; i<m; ++i){
        int dist = abs(x[i]) + abs(y[i]) + abs(z[i]);
        dp[1<<i][i] = cost[dist];
    }
    for(int i=0; i<(1<<m); ++i){
        for(int j=0; j<m; ++j){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 0.45
time_ms: 10
memory_kb: 3568
final_rank: 5

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 4;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    for(int i=0; i<n; ++i)
        cin >> d[i];

    ++ m;
    vector<int> x(m), y(m), z(m);
    x[0] = y[0] = z[0] = 0;
    for(int i=1; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(1000000, INF);
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            cost[d[i]+d[j]] = 2;
        }
    }
    for(int i=0; i<1000000; ++i){
        for(int j=0; j<n; ++j){
            int k = i + d[j];
            if(k < 1000000)
                cost[k] = min(cost[k], cost[i] + 1);
        }
    }
    for(int i=1000000-1; i>2; --i)
        cost[i-2] = min(cost[i-2], cost[i]);
    for(int i=0; i<n; ++i)
        cost[d[i]] = 1;
    cost[0] = 0;

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    dp[1][0] = 0;
    for(int i=0; i<(1<<m); ++i){
        for(int j=0; j<m; ++j){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 0.45
time_ms: 10
memory_kb: 5152
final_rank: 6

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 4;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    for(int i=0; i<n; ++i)
        cin >> d[i];

    ++ m;
    vector<int> x(m), y(m), z(m);
    x[0] = y[0] = z[0] = 0;
    for(int i=1; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(1000000, INF);
    cost[0] = 0;
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            cost[d[i]+d[j]] = 2;
        }
    }
    for(int i=0; i<1000000; ++i){
        for(int j=0; j<n; ++j){
            int k = i + d[j];
            if(k < 1000000)
                cost[k] = min(cost[k], cost[i] + 1);
        }
    }
    for(int i=1000000-1; i>2; --i)
        cost[i-2] = min(cost[i-2], cost[i]);
    for(int i=0; i<n; ++i)
        cost[d[i]] = 1;

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    dp[1][0] = 0;
    for(int i=0; i<(1<<m); ++i){
        for(int j=0; j<m; ++j){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 0.075
time_ms: 10
memory_kb: 4952
final_rank: 17

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 2;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    for(int i=0; i<n; ++i)
        cin >> d[i];

    ++ m;
    vector<int> x(m), y(m), z(m);
    x[0] = y[0] = z[0] = 0;
    for(int i=1; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(1000000, INF);
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            cost[d[i]+d[j]] = 2;
        }
    }
    for(int i=0; i<1000000; ++i){
        for(int j=0; j<n; ++j){
            int k = i + d[j];
            if(k < 1000000)
                cost[k] = min(cost[k], cost[i] + 1);
        }
    }
    for(int i=1000000-1; i>2; --i)
        cost[i-2] = min(cost[i-2], max(2, cost[i]));
    cost[0] = 0;
    for(int i=0; i<n; ++i)
        cost[d[i]] = 1;

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    dp[1][0] = 0;
    for(int i=0; i<(1<<m); ++i){
        for(int j=0; j<m; ++j){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 0.45
time_ms: 10
memory_kb: 5152
final_rank: 6

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 2;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    for(int i=0; i<n; ++i)
        cin >> d[i];

    ++ m;
    vector<int> x(m), y(m), z(m);
    x[0] = y[0] = z[0] = 0;
    for(int i=1; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(1000000, INF);
    cost[0] = 0;
    for(int i=0; i<1000000; ++i){
        for(int j=0; j<n; ++j){
            int k = i + d[j];
            if(k < 1000000)
                cost[k] = min(cost[k], cost[i] + 1);
        }
    }
    for(int i=1000000-1; i>=2; --i)
        cost[i-2] = min(cost[i-2], cost[i]);

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    dp[1][0] = 0;
    for(int i=0; i<(1<<m); ++i){
        for(int j=0; j<m; ++j){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 0.075
time_ms: 10
memory_kb: 4892
final_rank: 16

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 2;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> d(n);
    int evenMax = -1;
    int oddMax = -1;
    for(int i=0; i<n; ++i){
        cin >> d[i];
        if(d[i] % 2 == 0)
            evenMax = max(evenMax, d[i]);
        else
            oddMax = max(oddMax, d[i]);
    }

    ++ m;
    vector<int> x(m), y(m), z(m);
    for(int i=1; i<m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    vector<int> cost(1000000, INF);
    cost[0] = 0;
    for(int i=0; i<1000000; ++i){
        for(int j=0; j<n; ++j){
            if(i + d[j] < 1000000)
                cost[i+d[j]] = min(cost[i+d[j]], cost[i] + 1);
        }
    }
    for(int i=1000000-1; i>=2; --i)
        cost[i-2] = min(cost[i-2], max(2, cost[i]));

    vector<vector<int> > dp(1<<m, vector<int>(m, INF));
    dp[1][0] = 0;
    for(int i=0; i<(1<<m); ++i){
        for(int j=0; j<m; ++j){
            for(int k=0; k<m; ++k){
                int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);
                dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);
            }
        }
    }

    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}
accuracy: 0.225
time_ms: 10
memory_kb: 5152
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+9*1e+5;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d[16],c;
int BIT[SIZE];
void add(int x,int p){
	while(x<=SIZE){
		BIT[x]+=p;
		x+=x&-x;
	}
}
int sum(int x){
	int sum=0;
	while(x>0){
		sum+=BIT[x];
		x-=x&-x;
	}
	return sum;
}
int calc(int x){
    if(x>=SIZE) return INF;
    if(isUselly[x] && x%2==0) return 2;
    if(DP[x]) return DP[x];
    return DP[x]=min(max(3,dist[x]),calc(x+2)); 
}
void calc(){
    REP(i,SIZE){
        int k=SIZE-i-1;
        calc(k);
    }
}
int distances(int a,int b){
    int dif=0;
    REP(i,3)
        dif+=abs(point[b][i]-point[a][i]);
    int minium=min(dist[dif],calc(dif));
    if(sum(dif)>0 && dif%2)
    	minium=min(minium,2);
    //cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
    return minium;
}
LL sells(int now,int need){
    if(need==0) return 0;
    if(SDP[now][need]) return SDP[now][need];
    LL minium=INF;
    REP(i,M)
        if(need&(1<<i))
            minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
    return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    REP(i,N){
        cin >> d[i];
        edge.PB(pr(d[i],1));
        edge.PB(pr(-d[i],1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(next>0 && next<SIZE){
                if(nd==2)
                    isUselly[next]=true;
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }

    REP(i,N)
    	REP(j,N)
    		if((d[i]^d[j])&1){
    			add(abs(d[i]-d[j]),1);
    			add(abs(d[i]+d[j]+1),-1);
    		}
    REP(i,M){
        REP(j,3)
            cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
        REP(j,M+1)
            cost[i][j]=distances(i,j);
 
    LL ans=sells(M,(1<<M)-1);
    if(ans==INF)
        cout << -1 << endl;
    else
        cout << ans << endl;
}
accuracy: 1
time_ms: 180
memory_kb: 24008
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+2*1e+6;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d[16],c;
int themin=INF,themax=0;
int BIT[SIZE];
void add(int x,int p){
	while(x<=SIZE){
		BIT[x]+=p;
		x+=x&-x;
	}
}
int sum(int x){
	int sum=0;
	while(x>0){
		sum+=BIT[x];
		x-=x&-x;
	}
	return sum;
}
int calc(int x){
	if(x>SIZE) return INF;
	if(DP[x]) return DP[x];
	if(x%2)
	return DP[x]=min(max(3,dist[x]),calc(x+2)); 
	return DP[x]=min(max(2,dist[x]),calc(x+2));
}
void calc(){
	REP(i,SIZE){
		int k=SIZE-i-1;
		calc(k);
	}
}
int distances(int a,int b){
	int dif=0;
	REP(i,3)
		dif+=abs(point[b][i]-point[a][i]);
	int minium=min(dist[dif],calc(dif));
	if(sum(dif) && dif%2) minium=min(minium,2);
	//cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
	return minium;
}
LL sells(int now,int need){
	if(need==0)	return 0;
	if(SDP[now][need]) return SDP[now][need];
	LL minium=INF;
	REP(i,M)
		if(need&(1<<i))
			minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
	return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    if(M>15) return -1;
    if(N>15) return -1;
    REP(i,N){
        cin >> d[i];
        edge.PB(pr(d[i],1));
        edge.PB(pr(-d[i],1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(nd==2)
            	isUselly[next]=true;
            if(next>=0 && next<SIZE){
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }
    REP(i,N)
    	REP(j,N)
    		if((d[i]^d[j])&1){
    			add(abs(d[i]-d[j]),1);
    			add(abs(d[i]+d[j]+1),-1);
    		}
    REP(i,M){
    	REP(j,3)
    		cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
    	REP(j,M+1)
    		cost[i][j]=distances(i,j);

    LL ans=sells(M,(1<<M)-1);
    if(ans>=INF)
    	cout << -1 << endl;
    else
	    cout << ans << endl;
	return 0;
}
accuracy: 0.075
time_ms: 60
memory_kb: 26352
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+9*1e+5;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d[16],c;
int BIT[SIZE];
void add(int x,int p){
	while(x<=SIZE){
		BIT[x]+=p;
		x+=x&-x;
	}
}
int sum(int x){
	int sum=0;
	while(x>0){
		sum+=BIT[x];
		x-=x&-x;
	}
	return sum;
}
int calc(int x){
    if(x>SIZE) return INF;
    if(isUselly[x] && x%2==0) return 2;
    if(DP[x]) return DP[x];
    return DP[x]=min(max(3,dist[x]),calc(x+2)); 
}
void calc(){
    REP(i,SIZE){
        int k=SIZE-i-1;
        calc(k);
    }
}
int distances(int a,int b){
    int dif=0;
    REP(i,3)
        dif+=abs(point[b][i]-point[a][i]);
    int minium=min(dist[dif],calc(dif));
    if(sum(dif)>0 && dif%2)
    	minium=min(minium,2);
    //cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
    return minium;
}
LL sells(int now,int need){
    if(need==0) return 0;
    if(SDP[now][need]) return SDP[now][need];
    LL minium=INF;
    REP(i,M)
        if(need&(1<<i))
            minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
    return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    REP(i,N){
        cin >> d[i];
        edge.PB(pr(d[i],1));
        edge.PB(pr(-d[i],1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(next>0 && next<SIZE){
                if(nd==2)
                    isUselly[next]=true;
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }

    REP(i,N)
    	REP(j,N)
    		if((d[i]^d[j])&1){
    			add(abs(d[i]-d[j]),1);
    			add(abs(d[i]+d[j]+1),-1);
    		}
    REP(i,M){
        REP(j,3)
            cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
        REP(j,M+1)
            cost[i][j]=distances(i,j);
 
    LL ans=sells(M,(1<<M)-1);
    if(ans==INF)
        cout << -1 << endl;
    else
        cout << ans << endl;
}
accuracy: 0.15
time_ms: 30
memory_kb: 15308
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+2*1e+6;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d[16],c;
int themin=INF,themax=0;
int BIT[SIZE];
void add(int x,int p){
	while(x<=SIZE){
		BIT[x]+=p;
		x+=x&-x;
	}
}
int sum(int x){
	int sum=0;
	while(x>0){
		sum+=BIT[x];
		x-=x&-x;
	}
	return sum;
}
int calc(int x){
	if(x>SIZE) return INF;
	if(DP[x]) return DP[x];
	if(x%2)
	return DP[x]=min(max(3,dist[x]),calc(x+2)); 
	return DP[x]=min(max(2,dist[x]),calc(x+2));
}
void calc(){
	REP(i,SIZE){
		int k=SIZE-i-1;
		calc(k);
	}
}
int distances(int a,int b){
	int dif=0;
	REP(i,3)
		dif+=abs(point[b][i]-point[a][i]);
	int minium=min(dist[dif],calc(dif));
	if(sum(dif) && dif%2) minium=min(minium,2);
	//cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
	return minium;
}
LL sells(int now,int need){
	if(need==0)	return 0;
	if(SDP[now][need]) return SDP[now][need];
	LL minium=INF;
	REP(i,M)
		if(need&(1<<i))
			minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
	return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    if(M>15) return -1;
    if(N>15) return -1;
    REP(i,N){
        cin >> d[i];
        edge.PB(pr(d[i],1));
        edge.PB(pr(-d[i],1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(nd==2)
            	isUselly[next]=true;
            if(next>=0 && next<SIZE){
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }
    REP(i,N)
    	REP(j,M)
    		if((d[i]^d[j])&1){
    			add(abs(d[i]-d[j]),1);
    			add(abs(d[i]+d[j]+1),-1);
    		}
    REP(i,M){
    	REP(j,3)
    		cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
    	REP(j,M+1)
    		cost[i][j]=distances(i,j);

    LL ans=sells(M,(1<<M)-1);
    if(ans>=INF)
    	cout << -1 << endl;
    else
	    cout << ans << endl;
	return 0;
}
accuracy: 0.075
time_ms: 60
memory_kb: 26352
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+2*1e+6;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d[16],c;
int themin=INF,themax=0;
int BIT[SIZE];
void add(int x,int p){
	while(x<=SIZE){
		BIT[x]+=p;
		x+=x&-x;
	}
}
int sum(int x){
	int sum=0;
	while(x>0){
		sum+=BIT[x];
		x-=x&-x;
	}
	return sum;
}
int calc(int x){
	if(x>SIZE) return INF;
	if(DP[x]) return DP[x];
	if(x%2)
	return DP[x]=min(max(3,dist[x]),calc(x+2)); 
	return DP[x]=min(max(2,dist[x]),calc(x+2));
}
void calc(){
	REP(i,SIZE){
		int k=SIZE-i-1;
		calc(k);
	}
}
int distances(int a,int b){
	int dif=0;
	REP(i,3)
		dif+=abs(point[b][i]-point[a][i]);
	int minium=min(dist[dif],calc(dif));
	if(sum(dif)) minium=min(minium,2);
	//cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
	return minium;
}
LL sells(int now,int need){
	if(need==0)	return 0;
	if(SDP[now][need]) return SDP[now][need];
	LL minium=INF;
	REP(i,M)
		if(need&(1<<i))
			minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
	return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    if(M>15) return -1;
    if(N>15) return -1;
    REP(i,N){
        cin >> d[i];
        edge.PB(pr(d[i],1));
        edge.PB(pr(-d[i],1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(nd==2)
            	isUselly[next]=true;
            if(next>=0 && next<SIZE){
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }
    REP(i,N)
    	REP(j,M)
    		if((d[i]^d[j])&1){
    			add(abs(d[i]-d[j]),1);
    			add(abs(d[i]+d[j]+1),-1);
    		}
    REP(i,M){
    	REP(j,3)
    		cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
    	REP(j,M+1)
    		cost[i][j]=distances(i,j);

    LL ans=sells(M,(1<<M)-1);
    if(ans>=INF)
    	cout << -1 << endl;
    else
	    cout << ans << endl;
	return 0;
}
accuracy: 0.075
time_ms: 60
memory_kb: 26352
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+2*1e+6;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d,c;
int calc(int x){
	if(x>SIZE) return INF;
	if(isUselly[x]) return 2;
	if(DP[x]) return DP[x];
	return DP[x]=min(max(3,dist[x]),calc(x+2)); 
}
void calc(){
	REP(i,SIZE){
		int k=SIZE-i-1;
		calc(k);
	}
}
int distances(int a,int b){
	int dif=0;
	REP(i,3)
		dif+=abs(point[b][i]-point[a][i]);
	int minium=min(dist[dif],calc(dif));
	//cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
	return minium;
}
LL sells(int now,int need){
	if(need==0)	return 0;
	if(SDP[now][need]) return SDP[now][need];
	LL minium=INF;
	REP(i,M)
		if(need&(1<<i))
			minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
	return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    if(M>15) return -1;
    if(N>15) return -1;
    REP(i,N){
        cin >> d;
        edge.PB(pr(d,1));
        edge.PB(pr(-d,1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(next>=0 && next<SIZE){
            	if(nd==2)
            		isUselly[next]=true;
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }
    REP(i,M){
    	REP(j,3)
    		cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
    	REP(j,M+1)
    		cost[i][j]=distances(i,j);

    LL ans=sells(M,(1<<M)-1);
    if(ans>=INF)
    	cout << -1 << endl;
    else
	    cout << ans << endl;
	return 0;
}
accuracy: 0.15
time_ms: 60
memory_kb: 24652
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+2*1e+6;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d,c;
int calc(int x){
	if(x>SIZE) return INF;
	if(isUselly[x]) return 2;
	if(DP[x]) return DP[x];
	return DP[x]=min(max(3,dist[x]),calc(x+2)); 
}
void calc(){
	REP(i,SIZE){
		int k=SIZE-i-1;
		calc(k);
	}
}
int distances(int a,int b){
	int dif=0;
	REP(i,3)
		dif+=abs(point[b][i]-point[a][i]);
	int minium=min(dist[dif],calc(dif));
	//cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
	return minium;
}
LL sells(int now,int need){
	if(need==0)	return 0;
	if(SDP[now][need]) return SDP[now][need];
	LL minium=INF;
	REP(i,M)
		if(need&(1<<i))
			minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
	return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    if(M>15) return -1;
    REP(i,N){
        cin >> d;
        edge.PB(pr(d,1));
        edge.PB(pr(-d,1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(next>=0 && next<SIZE){
            	if(nd==2)
            		isUselly[next]=true;
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }
    REP(i,M){
    	REP(j,3)
    		cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
    	REP(j,M+1)
    		cost[i][j]=distances(i,j);

    LL ans=sells(M,(1<<M)-1);
    if(ans>=INF)
    	cout << -1 << endl;
    else
	    cout << ans << endl;
}
accuracy: 0.15
time_ms: 60
memory_kb: 24652
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+2*1e+6;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d,c;
int calc(int x){
	if(x>SIZE) return INF;
	if(isUselly[x]) return 2;
	if(DP[x]) return DP[x];
	return DP[x]=min(max(3,dist[x]),calc(x+2)); 
}
void calc(){
	REP(i,SIZE){
		int k=SIZE-i-1;
		calc(k);
	}
}
int distances(int a,int b){
	int dif=0;
	REP(i,3)
		dif+=abs(point[b][i]-point[a][i]);
	int minium=min(dist[dif],calc(dif));
	//cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
	return minium;
}
LL sells(int now,int need){
	if(need==0)	return 0;
	if(SDP[now][need]) return SDP[now][need];
	LL minium=INF;
	REP(i,M)
		if(need&(1<<i))
			minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
	return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    if(M>15) return -1;
    REP(i,N){
        cin >> d;
        edge.PB(pr(d,1));
        edge.PB(pr(-d,1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(next>0 && next<SIZE){
            	if(nd==2)
            		isUselly[next]=true;
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }
    REP(i,M){
    	REP(j,3)
    		cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
    	REP(j,M+1)
    		cost[i][j]=distances(i,j);

    LL ans=sells(M,(1<<M)-1);
    if(ans>=INF)
    	cout << -1 << endl;
    else
	    cout << ans << endl;
}
accuracy: 0.15
time_ms: 60
memory_kb: 24652
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define F first
#define S second
#define X real()
#define Y imag()
#define PB push_back 
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef complex<int> cld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut,pr> ppr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
typedef vector<ppr> Vppr;
const int SIZE=100+2*1e+6;
const int INF=1<<30;
int dist[SIZE];
Vpr edge;
PQ qu;
int point[16][3];
LL cost[16][16];
int isUselly[SIZE];
int DP[SIZE];
int SDP[16][1<<16];
int N,M,d,c;
int calc(int x){
	if(x>SIZE) return INF;
	if(isUselly[x]) return 2;
	if(DP[x]) return DP[x];
	return DP[x]=min(max(3,dist[x]),calc(x+2)); 
}
void calc(){
	REP(i,SIZE){
		int k=SIZE-i-1;
		calc(k);
	}
}
int distances(int a,int b){
	int dif=0;
	REP(i,3)
		dif+=abs(point[b][i]-point[a][i]);
	int minium=min(dist[dif],calc(dif));
	//cout << dif <<" " <<a<< " " <<b <<" " << minium << endl;
	return minium;
}
LL sells(int now,int need){
	if(need==0)	return 0;
	if(SDP[now][need]) return SDP[now][need];
	LL minium=INF;
	REP(i,M)
		if(need&(1<<i))
			minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);
	return SDP[now][need]=minium;
}
int main(){
    FOR(i,1,SIZE)
        dist[i]=INF;
    cin >> N>> M;
    REP(i,N){
        cin >> d;
        edge.PB(pr(d,1));
        edge.PB(pr(-d,1));
    }
    qu.push(pr(0,0));
    while(!qu.empty()){
        int now=qu.top().S;
        int d=qu.top().F;
        qu.pop();
        if(d!=dist[now]) continue;
        REP(i,N*2){
            int next=now+edge[i].F;
            int nd=d+edge[i].S;
            if(next>0 && next<SIZE){
            	if(nd==2)
            		isUselly[next]=true;
                if(dist[next]>nd){
                    dist[next]=nd;
                    qu.push(pr(nd,next));
                }
            }
        }
    }
    REP(i,M){
    	REP(j,3)
    		cin >> point[i][j];
    }
    calc();
    REP(i,M+1)
    	REP(j,M+1)
    		cost[i][j]=distances(i,j);

    LL ans=sells(M,(1<<M)-1);
    if(ans==INF)
    	cout << -1 << endl;
    else
	    cout << ans << endl;
}
accuracy: 0.15
time_ms: 60
memory_kb: 24648
final_rank: 10