Problem:
Elevator
Time Limit: 8 sec / Memory Limit: 64 MB
E: エレベータ
あなたの友人は，キリンのマークの引越業者でアルバイトをしている．
彼の仕事は，建物から荷物を運び出すことである．
今回，彼はある
n
階建てのビルからいくつかの荷物を運び出すことになった．
幸い，このビルにはエレベータが1つ備え付けられているため，
エレベータで荷物を運び出すことにした．
運び出すべき荷物は，各階に複数個あり，荷物毎に重さが異なる．
エレベータには，重量制限が設定されており，重さ
W
を超える荷物を載せることはできない．
エレベータは十分に広く，重量制限を満たすならば，どれだけ多くの荷物でも載せることができる．
エレベータは停止した階で荷物の搬入と搬出を行うことができる．
ベテランアルバイターである彼は，エレベータに乗らずに，階段で移動する．
エレベータに荷物を積み込んだ後，階段で次のエレベータの停止フロアに先回りし，荷物の搬入と搬出を行うのだ．
ベテランアルバイターの彼は，荷物の搬入と搬出を非常に高速に行うことができるので，各フロアでのエレベータの停止時間は，無視することができる．
よって，全ての荷物を1階に移動させるのに必要な時間は，エレベータの移動距離から容易に算出できる．
彼は，荷物をできるだけ早く1階に移動させたい．
彼はベテランアルバイターではあるが，どのような手順で各階での荷物の出し入れを行えばよいのか分からない．
そこで，彼は，友人であるあなたに助けを求めてきた．
あなたの仕事は，彼が全ての荷物を1階に移動させるために必要なエレベータの最小の移動距離を求めることである．
Input
入力は以下の形式で与えられる．
n m W
1階の荷物の情報
...
i
階の荷物の情報
...
n
階の荷物の情報
i
階の荷物の情報は，以下の形式で与えられる．(1 <=
i
<=
n
)
k
i
w
1
w
2
, ...,
w
j
, ...,
w
k
i
入力の形式の各変数の意味は次の通りである．
n
はフロア数(1 <=
n
<= 10000)
m
はエレベータの初期位置(1 <=
m
<= n)
W
はエレベータの重量制限(1 <=
W
<= 10000)
k
i
は荷物の数(0 <=
k
i
<= 15, 1 <=
k
1
+
k
2
+ ... +
k
n
<= 15)
w
j
は荷物
j
の重さ(1 <=
w
j
<=
W
)
Output
最小の移動距離を1行で出力せよ．
Sample Input 1
3 3 2
1 1
2 1 1
3 1 1 1
Sample Output 1
8
Sample Input 2
3 3 10
0
2 4 5
3 3 3 5
Sample Output 2
6
Sample Input 3
3 3 100
5 3 4 5 6 7
0
0
Sample Output 3
0

Submitted solutions:

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, W;
    cin >> n >> m >> W;
    vector<vector<int>> v(n);
    for (int i = 0; i < n; i++) {
        int k;
        cin >> k;
        for (int j = 0; j < k; j++) {
            int w;
            cin >> w;
            v[i].push_back(w);
        }
    }
    int sta = 0;
    for (int i = 0; i < n; i++) {
        if (v[i].size()) sta = i;
    }
    if (sta == 0) {
        cout << 0 << endl;
        return 0;
    }
    int res = abs(sta + 1 - m);
    vector<int> u;
    for (int i = sta; i >= 1; i--) {
        for (int j : v[i]) {
            u.push_back(j);
        }
        int k = u.size();
        vector<int> dp(1 << k, 1e9);
        vector<int> w(1 << k);
        for (int j = 0; j < (1 << k); j++) {
            for (int l = 0; l < k; l++) {
                if ((1 << l) & j) w[j] += u[l];
            }
        }
        dp[0] = 0;
        for (int S = 0; S < (1 << k); S++) {
            for (uint T = S; T; T = (T - 1) & S) {
                if (w[T] <= W) {
                    dp[S] = min(dp[S], dp[S ^ T] + 1);
                }
            }
        }
        res += dp.back() * 2 - 1;
    }

    cout << res << endl;
}
accuracy: 1
time_ms: 1780
memory_kb: 3568
final_rank: 10

code_snippet: #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif
#define foreach(it, c)                                                         \
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))
#define rep(i, n) for (int i = 0; i < n; ++i)
template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }
typedef long long ll;
typedef pair<int, int> pint;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}
int main() {
  ios::sync_with_stdio(false);
  int n, m, W, w[16], K = 0;
  cin >> n >> m >> W;
  --m;
  int f[12345], top;
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    int s = 0;
    for (int j = 0; j < k; ++j, ++K) {
      cin >> w[K];
      s |= 1 << K;
    }
    f[i] = s;
    if (s)
      top = i;
  }
  if (f[0] == (1 << K) - 1) {
    cout << 0 << endl;
    return 0;
  }
  int sum_w[1 << 15];
  for (int i = 0; i < 1 << K; ++i) {
    int s = 0;
    for (int j = 0; j < K; ++j)
      if (i >> j & 1)
        s += w[j];
    sum_w[i] = s;
  }
  int dp[1 << 15];
  dp[0] = 0;
  for (int S = 1; S < 1 << K; ++S) {
    dp[S] = 1 << 27;
    for (int T = S; T > 0; T = --T & S) {
      if (sum_w[T] <= W)
        min_swap(dp[S], dp[S ^ T] + 1);
    }
  }
  int rider = 0;
  int res = abs(m - top);
  for (int i = n - 1; i > 0; --i) {
    rider |= f[i];
    if (rider)
      res += dp[rider] * 2 - 1;
  }
  cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3436
final_rank: 6

code_snippet: #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif
#define foreach(it, c)                                                         \
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))
#define rep(i, n) for (int i = 0; i < n; ++i)
template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }
typedef long long ll;
typedef pair<int, int> pint;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}
int main() {
  ios::sync_with_stdio(false);
  int n, m, W, w[16], K = 0;
  cin >> n >> m >> W;
  --m;
  int f[12345], top;
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    int s = 0;
    for (int j = 0; j < k; ++j, ++K) {
      cin >> w[K];
      s |= 1 << K;
    }
    f[i] = s;
    if (s)
      top = i;
  }
  if (f[0] == (1 << K) - 1) {
    cout << 0 << endl;
    return 0;
  }
  int sum_w[1 << 15];
  for (int i = 0; i < 1 << K; ++i) {
    int s = 0;
    for (int j = 0; j < K; ++j)
      if (i >> j & 1)
        s += w[j];
    sum_w[i] = s;
  }
  int dp[1 << 15];
  dp[0] = 0;
  for (int S = 1; S < 1 << K; ++S) {
    dp[S] = 1 << 27;
    for (int T = S; T > 0; T = --T & S) {
      if (sum_w[T] <= W)
        min_swap(dp[S], dp[S ^ T] + 1);
    }
  }
  int rider = 0;
  int res = abs(m - top);
  for (int i = top; i > 0; --i) {
    rider |= f[i];
    res += dp[rider] * 2 - 1;
  }
  cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3472
final_rank: 7

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

int N, M, W;
int memo[15000000];

int get_id(vector<int>&v) {
	int num=0;
	for (int i = 0; i < v.size(); ++i) {
		num*=3;
		num+=v[i];
	}
	return num;
}

int solve(vector<int>status,const vector<pair<int,int>>&items) {
	const int now_id(get_id(status));
	if(memo[now_id]!=-1)return memo[now_id];
	if (all_of(status.begin(), status.end(), [](const int a) {return a == 1; })) {
		memo[now_id]=0;
		return memo[now_id];
	}

	int now_weight=0;
	int max_height=0;
	for (int i = 0; i < status.size(); ++i) {
		if (status[i] == 2) {
			now_weight += items[i].second;
			max_height = max(max_height, items[i].first);
		}
	}
	if(now_weight>W)memo[now_id]=1e9;
	else {
		int ans =1e9;
		for (int i = 0; i < status.size(); ++i) {
			if (status[i] == 0) {
				status[i] = 2;
				ans=min(ans,solve(status,items));
				status[i]=0;
			}
		}
		if(now_weight){
			for (int i = 0; i < status.size(); ++i) {
				if(status[i]==2)status[i]=1;
			}
			ans = min(ans, 2 * max_height + solve(status, items));
		}
		memo[now_id]=ans;
		
	}
	return memo[now_id];
}

int main() {
	for (int i = 0; i< 15000000; ++i) {
		memo[i]=-1;
	}
	cin>>N>>M>>W;
	M--;
	vector<pair<int,int>>items;
	for (int i = 0; i < N; ++i) {
		int k;cin>>k;
		for (int j = 0; j < k; ++j) {
			int w;cin>>w;
			items.push_back(make_pair(i,w));
		}
	}
	if (all_of(items.begin(), items.end(), [](const pair<int, int>&p) {
		return p.first == 0;
	})) {
		cout<<0<<endl;
	}
	else {
		N = items.size();
		vector<int>status(N, 0);
		int ans = solve(status, items);

		int mh = 0;
		for (auto item : items) {
			mh = max(mh, item.first);
		}
		if (mh<M)ans = ans - mh + (M - mh);
		else ans = ans - M;

		cout << ans << endl;
	}
	
	return 0;
}
accuracy: 0.35294117647058826
time_ms: 730
memory_kb: 61756
final_rank: 19

code_snippet: #include <stdio.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(){
		loc = weight = 0;
	}
	Info(int arg_loc,int arg_weight){
		loc = arg_loc;
		weight = arg_weight;
	}
	int loc,weight;
};

struct Data{
	Data(int arg_state,int arg_total_cost){
		state = arg_state;
		total_cost = arg_total_cost;
	}
	bool operator<(const struct Data &arg) const{
		return total_cost > arg.total_cost;
	}
	int state,total_cost;
};


int main(){

	int POW[16];
	for(int i = 0; i < 16; i++)POW[i] = pow(2,i);

	int N,init_pos,W;
	scanf("%d %d %d",&N,&init_pos,&W);

	vector<int> V[N+1];
	Info info[15];
	int index = 0,tmp,max_h;

	for(int h = 1; h <= N; h++){
		scanf("%d",&tmp);
		for(int i = 0; i < tmp; i++){
			scanf("%d",&info[index].weight);
			if(h == 1)continue;
			max_h = h;
			info[index].loc = h;
			V[h].push_back(index);
			index++;
		}
	}

	if(index == 0){
		printf("0\n");
		return 0;
	}

	int* min_cost = new int[POW[index]];
	int pre_cost,ans = abs(init_pos-max_h),state = 0,item_num = 0,item_id[index],sum_weight;
	int rest_num,rest_id[index],add_state,not_carry_min_weight,add_cost;
	priority_queue<Data> Q;

	for(int floor = max_h; floor >= 2; floor--){

		if(V[floor].size() == 0){
			ans += pre_cost;
			continue;
		}
		for(int i = 0; i < V[floor].size(); i++){
			item_id[item_num++] = V[floor][i];
		}
		state = POW[item_num];

		for(int i = 0; i < state; i++)min_cost[i] = BIG_NUM;

		Q.push(Data(0,0));

		while(!Q.empty()){

			if(Q.top().state == state-1){
				Q.pop();
			}else if(Q.top().total_cost > min_cost[Q.top().state]){
				Q.pop();
			}else{

				rest_num = 0;
				for(int loop = 0; loop < item_num; loop++){
					if(Q.top().state & (1 << loop)){
						//do nothing
					}else{
						rest_id[rest_num++] = loop;
					}
				}

				for(int state = 1; state < POW[rest_num]; state++){
					sum_weight = 0;
					not_carry_min_weight = BIG_NUM;
					add_state = 0;
					for(int loop = 0; loop < rest_num; loop++){
						if(state & (1 << loop)){
							sum_weight += info[item_id[rest_id[loop]]].weight;
							add_state += POW[rest_id[loop]];
						}else{
							not_carry_min_weight = min(not_carry_min_weight,info[item_id[rest_id[loop]]].weight);
						}
					}

					if(sum_weight > W || sum_weight+not_carry_min_weight <= W)continue;

					if(Q.top().state == 0){
						add_cost = 1;
					}else{
						add_cost = 2;
					}

					if(min_cost[Q.top().state+add_state] > Q.top().total_cost+add_cost){
						min_cost[Q.top().state+add_state] = Q.top().total_cost+add_cost;
						Q.push(Data(Q.top().state+add_state,min_cost[Q.top().state+add_state]));
					}
				}
				Q.pop();
			}
		}
		ans += min_cost[state-1];
		pre_cost = min_cost[state-1];
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3600
final_rank: 8

code_snippet: #include <stdio.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(){
		loc = weight = 0;
	}
	Info(int arg_loc,int arg_weight){
		loc = arg_loc;
		weight = arg_weight;
	}
	int loc,weight;
};

struct Data{
	Data(int arg_state,int arg_total_cost){
		state = arg_state;
		total_cost = arg_total_cost;
	}
	bool operator<(const struct Data &arg) const{
		return total_cost > arg.total_cost;
	}
	int state,total_cost;
};

int main(){

	int POW[16];
	for(int i = 0; i < 16; i++)POW[i] = pow(2,i);

	int N,init_pos,W;
	scanf("%d %d %d",&N,&init_pos,&W);

	Info info[15];
	int index = 0,tmp;

	for(int h = 1; h <= N; h++){
		scanf("%d",&tmp);
		for(int i = 0; i < tmp; i++){
			scanf("%d",&info[index].weight);
			if(h == 1)continue;
			info[index].loc = h;
			index++;
		}
	}

	if(index == 0){
		printf("0\n");
		return 0;
	}

	int* min_cost = new int[POW[index]];

	for(int state = 0; state < POW[index]; state++)min_cost[state] = BIG_NUM;


	int max_height,sum_weight;

	priority_queue<Data> Q;
	vector<int> NOT_CARRY;

	bool FLG;

	for(int state = 1; state < POW[index]; state++){
		max_height = 0,sum_weight = 0;
		NOT_CARRY.clear();
		for(int loop = 0; loop < index; loop++){
			if(state & (1 << loop)){ //??????loop?????????
				max_height = max(max_height,info[loop].loc);
				sum_weight += info[loop].weight;
			}else{
				NOT_CARRY.push_back(loop);
			}
		}
		if(sum_weight > W)continue;
		FLG = true;
		for(int k = 0; k < NOT_CARRY.size(); k++){
			if(info[NOT_CARRY[k]].loc <= max_height && info[NOT_CARRY[k]].weight+sum_weight <= W){
				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		if(max_height <= init_pos){
			min_cost[state] = init_pos-1;
			Q.push(Data(state,init_pos-1));
		}else{
			min_cost[state] = (max_height-init_pos)+(max_height-1);
			Q.push(Data(state,(max_height-init_pos)+(max_height-1)));
		}
	}

	int rest_num,rest_id[index],add_state;

	while(!Q.empty()){

		if(Q.top().state == POW[index]-1){
			Q.pop();
		}else if(Q.top().total_cost > min_cost[Q.top().state]){
			Q.pop();
		}else{

			rest_num = 0;
			for(int loop = 0; loop < index; loop++){
				if(Q.top().state & (1 << loop)){
					//do nothing
				}else{
					rest_id[rest_num++] = loop;
				}
			}

			for(int state = 1; state < POW[rest_num]; state++){
				max_height = 0,sum_weight = 0;
				NOT_CARRY.clear();
				add_state = 0;
				for(int loop = 0; loop < rest_num; loop++){
					if(state & (1 << loop)){
						sum_weight += info[rest_id[loop]].weight;
						max_height = max(max_height,info[rest_id[loop]].loc);
						add_state += POW[rest_id[loop]];
					}else{
						NOT_CARRY.push_back(rest_id[loop]);
					}
				}

				if(sum_weight > W)continue;
				FLG = true;
				for(int k = 0; k < NOT_CARRY.size(); k++){
					if(info[NOT_CARRY[k]].loc <= max_height && info[NOT_CARRY[k]].weight+sum_weight <= W){
						FLG = false;
						break;
					}
				}
				if(!FLG)continue;

				if(min_cost[Q.top().state+add_state] > Q.top().total_cost+2*(max_height-1)){
					min_cost[Q.top().state+add_state] = Q.top().total_cost+2*(max_height-1);
					Q.push(Data(Q.top().state+add_state,min_cost[Q.top().state+add_state]));
				}
			}
			Q.pop();
		}
	}

	printf("%d\n",min_cost[POW[index]-1]);

	return 0;
}
accuracy: 0.35294117647058826
time_ms: 10
memory_kb: 3428
final_rank: 15

code_snippet: #include <bits/stdc++.h>
 
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
 
const int inf=1<<28;

vector<int> weight;
int floors[10010];

int sum[1<<15];
int dp[1<<15];
 
int main(){
    int n,m,limit;
    cin >> n >> m >> limit;
    
    rep(i,n){
        int k;
        cin >> k;
        floors[i]=0;
        rep(j,k){
            int w;
            cin >> w;
            floors[i]|=(1<<int(weight.size()));
            weight.push_back(w);
        }
    }

    int k=weight.size();
    const int all=(1<<k)-1;
 
    if(floors[0]==all){
        puts("0");
        return 0;
    }
    
    rep(mask,1<<k) dp[mask]=inf;
    rep(mask,1<<k)rep(i,k)if(mask&(1<<i)) sum[mask]+=weight[i];
    
    dp[0]=0;
    rep(mask,1,1<<k){
        for(int smask=mask;smask>0;smask=(smask-1)&mask){
            int tmask=mask^smask;
            if(sum[smask]<=limit) chmin(dp[mask],dp[tmask]+1); 
        }
    }

    int ans=0,cmax=m;
    rep(i,n) if(floors[i]!=0) chmax(cmax,i+1);
    
    ans+=cmax-m;
    rrep(i,cmax,1){
        ans+=2*dp[floors[i]]-1,floors[i-1]|=floors[i];
    }
    cout << ans << endl; 
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3424
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
const static int INF = 1 << 30;

int main()
{
  int n, m, W;
  int weight[15], ptr = 0;
  int bit[10000] = {};
  int sum[1 << 15] = {}, dp[1 << 15];
  int highest = 0;
  fill_n(dp, 1 << 15, INF);

  cin >> n >> m >> W;
  for(int i = 0; i < n; i++) {
    int k;
    cin >> k;
    if(k > 0) highest = i;
    for(int j = 0; j < k; j++) {
      int w;
      cin >> w;
      if(i > 0) {
        weight[ptr] = w;
        bit[i] |= 1 << ptr;
        ++ptr;
      }
    }
  }
  if(ptr == 0) {
    puts("0");
  } else {
    for(int i = 1; i < 1 << ptr; i++) {
      for(int j = 0; j < ptr; j++) {
        if((i >> j) & 1) sum[i] += weight[j];
      }
    } 

    dp[0] = 0;
    for(int i = 1; i < 1 << ptr; i++) {
      for(int j = i; j > 0; j = j - 1 & i) {
        if(sum[j] <= W) dp[i] = min(dp[i], dp[i - j] + 1);
      }
    }
    int ret = 0;
    for(int i = highest, bits = 0; i > 0; i--) {
      ret += dp[bits |= bit[i]] * 2 - 1;
    }
    cout << ret + fabs(highest - m + 1) << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 1540
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair< int, int > Pi;
typedef pair< int, Pi > Pii;

const static int INF = 1 << 30;

int flor[15], w[15];
int ptr;
int n, m, W;
int dp[1 << 15];
map< Pii, int > memo;

int rec(int bits, int weight, int height)
{
  if(bits == 0) {
    return(height * 2);
  }
  if(memo.find(make_pair(bits, make_pair(weight, height))) != memo.end()) {
    return(memo[make_pair(bits, make_pair(weight, height))]);
  }
  int ret = INF;
  for(int i = 0; i < ptr; i++) {
    if((bits >> i) & 1) {
      if(weight + w[i] <= W) {
        ret = min(ret, rec(bits&~(1 << i), weight + w[i], max(flor[i], height)));
      } else {
        ret = min(ret, rec(bits&~(1 << i), w[i], flor[i]) + height * 2);
      }
    }
  }
  return(memo[make_pair(bits, make_pair(weight, height))] = ret);
}
int main()
{
  cin >> n >> m >> W;
  for(int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for(int j = 0; j < k; j++) {
      int wid;
      cin >> wid;
      if(i > 0) {
        w[ptr] = wid;
        flor[ptr++] = i;
      }
    }
  }
  // (weight[i] + W - 1) / W * idx[i] これは嘘
  // これDPしないとわかんないよ
  //  dp[S][i]: i番目まで見たときの最大の個数
  dp[0] = 0;
  for(int i = 1; i < 1 << ptr; i++) {
    int ret = 0;
    for(int j = 0; j < ptr; j++) {
      if((i >> j) & 1) ret = max(ret, flor[j]);
    }
    dp[i] = max(0, ret - m + 1) + rec(i, 0, 0) - ret;
  }
  cout << dp[(1 << ptr) - 1] << endl;
}
accuracy: 0.35294117647058826
time_ms: 140
memory_kb: 9932
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair< int, int > Pi;
typedef pair< int, Pi > Pii;

const static long long INF = 1LL << 55;

long long dp[1 << 15];
int flor[15], w[15];
int ptr;
int n, m, W;
map< Pii, long long > memo;
long long cost[1 << 15];


long long rec(int bits, int weight, int height)
{
  if(bits == 0) {
    return(height * 2);
  }
  if(memo.find(make_pair(bits, make_pair(weight, height))) != memo.end()) {
    return(memo[make_pair(bits, make_pair(weight, height))]);
  }
  long long ret = INF;
  for(int i = 0; i < ptr; i++) {
    if((bits >> i) & 1) {
      if(weight + w[i] <= W) {
        ret = min(ret, rec(bits&~(1 << i), weight + w[i], max(flor[i], height)));
      } else {
        ret = min(ret, rec(bits&~(1 << i), w[i], flor[i]) + height * 2);
      }
    }
  }
  return(memo[make_pair(bits, make_pair(weight, height))] = ret);
}

int main()
{
  cin >> n >> m >> W;
  for(int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for(int j = 0; j < k; j++) {
      int wid;
      cin >> wid;
      if(i > 0) {
        w[ptr] = wid;
        flor[ptr++] = i;
      }
    }
  }

  // (weight[i] + W - 1) / W * idx[i] これは嘘
  // これDPしないとわかんないよ
  //  dp[S][i]: i番目まで見たときの最大の個数
  dp[0] = 0;
  for(int i = 1; i < 1 << ptr; i++) {
    int ret = 0;
    for(int j = 0; j < ptr; j++) {
      if((i >> j) & 1) ret = max(ret, flor[j]);
    }
    dp[i] = max(0, ret - m + 1) + (cost[i] = rec(i, 0, 0)) - ret;
  }
  
  //エレベータは 1階にあることを仮定して DP
  for(int i = 1; i < 1 << ptr; i++) {
    for(int j = 0; j < 1 << ptr; j++) {
      dp[i|j] = min(dp[i|j], dp[i] + cost[j]);
    }
  }
  cout << dp[(1 << ptr) - 1] << endl;
}
accuracy: 0.35294117647058826
time_ms: 240
memory_kb: 10024
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair< int, int > Pi;
typedef pair< int, Pi > Pii;

const static int INF = 1 << 30;

int dp[1 << 15];
int flor[15], w[15];
int ptr;
int n, m, W;
map< Pii, int > memo;
int cost[1 << 15];


int rec(int bits, int weight, int height)
{
  if(bits == 0) {
    return(height * 2);
  }
  if(memo.find(make_pair(bits, make_pair(weight, height))) != memo.end()) {
    return(memo[make_pair(bits, make_pair(weight, height))]);
  }
  int ret = INF;
  for(int i = 0; i < ptr; i++) {
    if((bits >> i) & 1) {
      if(weight + w[i] <= W) {
        ret = min(ret, rec(bits&~(1 << i), weight + w[i], max(flor[i], height)));
      } else {
        ret = min(ret, rec(bits&~(1 << i), w[i], flor[i]) + height * 2);
      }
    }
  }
  return(memo[make_pair(bits, make_pair(weight, height))] = ret);
}

int main()
{
  cin >> n >> m >> W;
  for(int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for(int j = 0; j < k; j++) {
      int wid;
      cin >> wid;
      if(i > 0) {
        w[ptr] = wid;
        flor[ptr++] = i;
      }
    }
  }

  // (weight[i] + W - 1) / W * idx[i] これは嘘
  // これDPしないとわかんないよ
  //  dp[S][i]: i番目まで見たときの最大の個数
  dp[0] = 0;
  for(int i = 1; i < 1 << ptr; i++) {
    int ret = 0;
    for(int j = 0; j < ptr; j++) {
      if((i >> j) & 1) ret = max(ret, flor[j]);
    }
    dp[i] = max(0, ret - m + 1) + (cost[i] = rec(i, 0, 0)) - ret;
  }
  
  //エレベータは 1階にあることを仮定して DP
  for(int i = 1; i < 1 << ptr; i++) {
    for(int j = 0; j < 1 << ptr; j++) {
      dp[i|j] = min(dp[i|j], dp[i] + cost[j]);
    }
  }
  cout << dp[(1 << ptr) - 1] << endl;
}
accuracy: 0.35294117647058826
time_ms: 230
memory_kb: 9964
final_rank: 17

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

int idx[1 << 15], weight[1 << 15];
int dp[1 << 15];
int flor[15], w[15];

int main()
{
  int n, m, W;
  cin >> n >> m >> W;
  int ptr = 0;
  for(int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for(int j = 0; j < k; j++) {
      int wid;
      cin >> wid;
      if(i > 0) {
        w[ptr] = wid;
        flor[ptr++] = i;
      }
    }
  }
  for(int i = 0; i < 1 << ptr; i++) {
    for(int j = 0; j < ptr; j++) {
      if((i >> j) & 1) {
        weight[i] += w[j];
        idx[i] = flor[j];
      }
    }
  }
  
  dp[0] = 0;
  for(int i = 1; i < 1 << ptr; i++) {
    dp[i] = max(0, idx[i] - m + 1) + ((weight[i] + W - 1) / W * idx[i] * 2) - idx[i];
  }
  
  //エレベータは 1階にあることを仮定して DP
  for(int i = 1; i < 1 << ptr; i++) {
    for(int j = 0; j < 1 << ptr; j++) {
      dp[i|j] = min(dp[i|j], dp[i] + (weight[j] + W - 1) / W * idx[j] * 2);
    }
  }
  cout << dp[(1 << ptr) - 1] << endl;
}
accuracy: 0.23529411764705882
time_ms: 250
memory_kb: 1264
final_rank: 20

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

const int INF = 1<<28;
const int MAXN = 10001;
const int MAXK = 15;
int n, m, W, K;
int B[MAXN], weight[MAXN], sum[1<<MAXK], dp[1<<MAXK];

int main() {
  while(cin >> n >> m >> W) {
    --m;
    K = 0;
    int top = 0;
    for(int i = 0; i < n; ++i) {
      int k; cin >> k;
      if(k) top = i;
      for(int j = 0; j < k; ++j) {
        int w; cin >> w;
        if(i) {
          B[i] |= 1 << K;
          weight[K++] = w;
        }
      }
    }

    if(K == 0) {
      cout << 0 << endl;
      continue;
    }

    for(int i = 0; i < (1<<K); ++i) {
      sum[i] = 0;
      for(int j = 0; j < K; ++j) {
        if(i >> j & 1) sum[i] += weight[j];
      }
    }

    fill(dp, dp+(1<<MAXK), INF);
    dp[0] = 0;
    for(int i = 1; i < (1<<K); ++i) {
      for(int j = i; j > 0; j = j - 1 & i) {
        if(sum[j] <= W) dp[i] = min(dp[i], dp[i-j] + 1);
      }
    }

    int res = abs(m - top);
    for(int i = top, j = 0; i > 0; --i) {
      j |= B[i];
      res += dp[j] * 2 - 1;
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1424
final_rank: 1

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

const int MAXN = 16;
const int INF = 1<<28;

int n, m, W;
vector<vector<int> > w;
int N;
vector<int> weight;
int sum[1<<MAXN], mem[1<<MAXN];

int rec(int b) {
  int &res = mem[b];
  if(res != -1) return res;
  if(b == 0) return res = 0;
  res = INF;
  for(int k = b; k > 0; k = k-1&b) {
    if(sum[k] <= W) res = min(res, rec(b - k) + 1);
  }
  return res;
}

int main() {
  while(cin >> n >> m >> W) {
    int top = 0;
    --m;
    weight.clear();
    w = vector<vector<int> >(n);
    for(int i = 0; i < n; ++i) {
      int k; cin >> k;
      w[i].resize(k);
      if(k) top = i;
      for(int j = 0; j < k; ++j) {
        cin >> w[i][j];
        weight.push_back(w[i][j]);
        w[i][j] = weight.size()-1;
      }
    }
    N = weight.size();

    if(top == 0) {
      cout << 0 << endl;
    } else {
      for(int b = 0; b < (1<<N); ++b) {
        sum[b] = 0;
        for(int j = 0; j < N; ++j) {
          if(b >> j & 1) sum[b] += weight[j];
        }
      }
      memset(mem, -1, sizeof(mem));
      int res = abs(top - m);
      for(int i = top, b = 0; i > 0; --i) {
        for(int j = 0; j < w[i].size(); ++j) {
          b |= 1 << w[i][j];
        }
        res += rec(b) * 2 - 1;
      }
      cout << res << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1608
final_rank: 2

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

const int MAXN = 16;
const int INF = 1<<28;

int n, m, W;
vector<vector<int> > w;
int N;
vector<int> weight, height;
int mem[1<<MAXN];

int rec(int b) {
  int &res = mem[b];
  if(res != -1) return res;
  if(b == 0) return res = 0;
  res = INF;

  int sum = 0;
  for(int i = 0; i < N; ++i) {
    if(b >> i & 1) sum += weight[i];
  }
  if(sum <= W) return res = 1;
  for(int k = b; k > 0; k = (k-1)&b) {
    if(k == b) continue;
    res = min(res, rec(k) + rec(b & ~k));
  }
  return res;
}

int main() {
  while(cin >> n >> m >> W) {
    int top = 0;
    --m;
    weight.clear();
    height.clear();
    w = vector<vector<int> >(n);
    for(int i = 0; i < n; ++i) {
      int k; cin >> k;
      w[i].resize(k);
      if(k) top = i;
      for(int j = 0; j < k; ++j) cin >> w[i][j];
      for(int j = 0; j < k; ++j) {
        weight.push_back(w[i][j]);
        w[i][j] = height.size();
        height.push_back(i);
      }
    }
    N = weight.size();
    memset(mem, -1, sizeof(mem));
    int res = abs(top - m);
    for(int i = n-1, b = 0; i >= 1; --i) {
      for(int j = 0; j < w[i].size(); ++j) {
        b |= 1 << w[i][j];
      }
      res += rec(b) * 2 - 1;
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 0.6764705882352942
time_ms: 20
memory_kb: 1532
final_rank: 11

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

const int MAXN = 15;
const int INF = 1<<28;

int n, m, W;
vector<vector<int> > w;
int N;
vector<int> weight, height;
int mem[1<<MAXN];

int rec(int b) {
  int &res = mem[b];
  if(res != -1) return res;
  if(b == 0) return res = 0;
  res = INF;

  int sum = 0;
  for(int i = 0; i < N; ++i) {
    if(b >> i & 1) sum += weight[i];
  }
  if(sum <= W) return res = 1;
  for(int k = 1; k < b; ++k) {
    if((b & k) != k) continue;
    res = min(res, rec(k) + rec(b & ~k));
  }
  return res;
}

int main() {
  while(cin >> n >> m >> W) {
    int top = 0;
    --m;
    weight.clear();
    height.clear();
    w = vector<vector<int> >(n);
    for(int i = 0; i < n; ++i) {
      int k; cin >> k;
      w[i].resize(k);
      if(k) top = i;
      for(int j = 0; j < k; ++j) cin >> w[i][j];
      for(int j = 0; j < k; ++j) {
        weight.push_back(w[i][j]);
        w[i][j] = height.size();
        height.push_back(i);
      }
    }
    N = weight.size();
    memset(mem, -1, sizeof(mem));
    int res = abs(top - m);
    for(int i = n-1, b = 0; i >= 1; --i) {
      for(int j = 0; j < w[i].size(); ++j) {
        b |= 1 << w[i][j];
      }
      res += rec(b) * 2 - 1;
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 0.6764705882352942
time_ms: 170
memory_kb: 1356
final_rank: 12

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

const int MAXN = 15;
const int INF = 1<<28;

int n, m, W;
vector<vector<int> > w;
int N;
vector<int> weight, height;
vector<pair<int, int> > E;
int dp[1<<MAXN];

int main() {
  while(cin >> n >> m >> W) {
    int top = 0;
    --m;
    weight.clear();
    height.clear();
    w = vector<vector<int> >(n);
    for(int i = 0; i < n; ++i) {
      int k; cin >> k;
      w[i].resize(k);
      if(k) top = i;
      for(int j = 0; j < k; ++j) cin >> w[i][j];
      for(int j = 0; j < k; ++j) {
        weight.push_back(w[i][j]);
        w[i][j] = height.size();
        height.push_back(i);
      }
    }
    E.clear();
    N = weight.size();

    fill(dp, dp+(1<<MAXN), INF);
    dp[0] = 0;
    for(int b = 1; b < (1<<N); ++b) {
      int sum = 0;
      for(int j = 0; j < N; ++j) {
        if(b >> j & 1) sum += weight[j];
      }
      if(sum <= W) {
        dp[b] = 1;
      } else {
        for(int k = 1; k < b; ++k) {
          if((k & b) != k) continue;
          dp[b] = min(dp[b], dp[k] + dp[b & ~k]);
        }
      }
    }

    int res = 0;
    for(int i = n-1, b = 0; i >= 1; --i) {
      for(int j = 0; j < w[i].size(); ++j) {
        b |= 1<<w[i][j];
      }
      res += dp[b] * 2 - 1;
    }
    if(top == 0) res = 0;
    else res += abs(top - m);
    cout << res << endl;
    continue;

    for(int b = 1; b < (1<<N); ++b) {
      int sum = 0, c = 0;
      for(int j = 0; j < N; ++j) {
        if(b >> j & 1) {
          sum += weight[j];
          c = max(c, height[j]);
        }
      }
      if(sum <= W) {
        E.push_back(make_pair(b, c));
      }
    }
    sort(E.begin(), E.end());
    fill(dp, dp+(1<<MAXN), INF);
    int src = 0;
    for(int i = 0; i < N; ++i) {
      if(height[i] == 0) src |= 1<<i;
    }
    dp[src] = 0;
    for(int i = src; i < (1<<N)-1; ++i) {
      if(dp[i] == INF) continue;
      int h = 0;
      for(int j = 0; j < N; ++j) {
        if(i >> j & 1); else h = j;
      }
      for(int k = lower_bound(E.begin(), E.end(), make_pair(1<<h,0)) - E.begin();
          k < E.size(); ++k) {
        if(i & E[k].first) continue;
        int ni = i | E[k].first;
        int cost;
        if(i == src) {
          if(E[k].second <= m) cost = m;
          else cost = E[k].second * 2 - m;
        } else {
          cost = E[k].second * 2;
        }
        dp[ni] = min(dp[ni], dp[i] + cost);
      }
    }
    cout << dp[(1<<N)-1] << endl;
  }
  return 0;
}
accuracy: 0.6764705882352942
time_ms: 330
memory_kb: 1348
final_rank: 13

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int p[20];
int q[20];
int cost[1<<15];
int dp[1<<15];
int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	b--;
	int sz=0;
	int h=0;
	for(int i=0;i<a;i++){
		int d,e;
		scanf("%d",&d);
		for(int j=0;j<d;j++){
			h=i;
			scanf("%d",&e);
			p[sz]=i;
			q[sz]=e;
			sz++;
		}
	}
	bool jimei=true;
	for(int i=0;i<sz;i++)if(p[i])jimei=false;
	if(jimei){
		printf("0\n");return 0;
	}
	for(int i=0;i<(1<<sz);i++)dp[i]=999999999;
	dp[0]=0;
	for(int i=0;i<(1<<sz);i++){
		int w=0;
		for(int j=0;j<sz;j++)if(i&(1<<j))w+=q[j];
		if(w<=c)cost[i]=1;
		else cost[i]=999999999;
	}
	for(int i=0;i<(1<<sz);i++){
		for(int j=(1<<sz)-1-i;j;j=((j-1)&((1<<sz)-1-i))){
			dp[i+j]=min(dp[i+j],dp[i]+cost[j]);
		}
	}
	int ret=0;
	int now=0;
	for(int i=a-1;i;i--){
		for(int j=0;j<sz;j++){
			if(p[j]==i)now+=(1<<j);
		}
		ret+=dp[now]*2;
	}
	if(h>=b)ret-=b;
	else ret+=b-h-h;
	printf("%d\n",ret);
}
accuracy: 1
time_ms: 40
memory_kb: 1284
final_rank: 4

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int p[20];
int q[20];
int cost[1<<15];
int dp[1<<15];
int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	b--;
	int sz=0;
	for(int i=0;i<a;i++){
		int d,e;
		scanf("%d",&d);
		for(int j=0;j<d;j++){
			scanf("%d",&e);
			p[sz]=i;
			q[sz]=e;
			sz++;
		}
	}
	bool jimei=true;
	for(int i=0;i<sz;i++)if(p[i])jimei=false;
	if(jimei){
		printf("0\n");return 0;
	}
	for(int i=0;i<(1<<sz);i++){
		dp[i]=999999999;
		int w=0;
		int h=0;
		for(int j=0;j<sz;j++){
			if(i&(1<<j)){
				w+=q[j];
				h=max(h,p[j]);
			}
		}
		if(w<=c){
			dp[i]=(max(b,h)-b)*2+b;
		}
	}
	for(int i=0;i<(1<<sz);i++){
		int w=0;
		int h=0;
		for(int j=0;j<sz;j++){
			if(i&(1<<j)){
				w+=q[j];
				h=max(h,p[j]);
			}
		}
		if(w<=c)cost[i]=h*2;
		else cost[i]=999999999;
	}
	for(int i=0;i<(1<<sz);i++){
		for(int j=(1<<sz)-1-i;j;j=((j-1)&((1<<sz)-1-i))){
			dp[i+j]=min(dp[i+j],dp[i]+cost[j]);
		
		}
	}
	printf("%d\n",dp[(1<<sz)-1]);
}
accuracy: 0.35294117647058826
time_ms: 30
memory_kb: 1280
final_rank: 14

code_snippet: #include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cassert>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)

using namespace std;

struct P
{
  int weight,index;
  P(int weight=inf,int index=inf):weight(weight),index(index){}
  bool operator < (const P& a)const
  {
    return weight < a.weight;
  }
};

int n,m,W;

int main()
{
  int k;
  scanf("%d %d %d",&n,&m,&W);
  
  int idx = 0;
  vector<vector<P> > vec;
  vector<int> FLOOR;

  rep(i,n)
    {
      scanf("%d",&k);
      if(k == 0)
	{
	  if(i == 0)
	    {
	      FLOOR.push_back(i);
	      vec.push_back(vector<P>());
	    }
	  continue;
	}
      int cur = vec.size();
      vec.push_back(vector<P>(k));
      FLOOR.push_back(i);
      rep(j,k)
	{
	  scanf("%d",&vec[cur][j].weight);
	}
    }

  if(FLOOR[FLOOR.size()-1] == 0)
    {
      puts("0");
      return 0;
    }

  int ans = FLOOR[FLOOR.size()-1]+1-m;
  for(int i=FLOOR.size()-1;i>=1;i--)
    {
      //cout << "cost = " << FLOOR[i] << " - " << FLOOR[i-1] << endl;
      int cost = FLOOR[i] - FLOOR[i-1];
      assert(cost >= 0);
      int SIZE = vec[i].size();
      //cout << "SIZE = " << SIZE << endl;
      int dp[(1<<SIZE)];
      rep(j,(1<<SIZE))dp[j] = inf;
      dp[0] = 0;
      rep(state,(1<<SIZE))//今のフロアの状況 0 -> まだ移動してない, 1 -> 移動したたそ〜
	{
	  if(dp[state] == inf)continue;
	  vector<P> tmp;//まだこのフロアにあるものたそ〜
	  rep(j,SIZE)
	    {
	      if((state>>j) & 1)continue;
	      tmp.push_back(P(vec[i][j].weight,j));
	    }

	  //sort(tmp.begin(),tmp.end());
	  int limit = tmp.size();
	  rep(j,(1<<limit))//次に移動するもの
	    {
	      int sum = 0;
	      int tstate = 0;
	      rep(k,limit)
		{
		  if((j>>k) & 1)
		    {
		      tstate |= (1<<tmp[k].index);
		      sum += tmp[k].weight;
		    }
		}
	      assert(sum >= 0);
	      if(sum > W)continue;
	      assert((state ^ tstate) == (state | tstate));
	      int nstate = state | tstate;
	      dp[nstate] = min(dp[nstate],
			       dp[state]+2);
	    }
	}
      rep(j,vec[i].size())vec[i-1].push_back(vec[i][j]);
      assert((dp[(1<<SIZE)-1]-1)*cost >= 0);
      ans += (dp[(1<<SIZE)-1]-1)*cost;//新たな値を得るたそ〜
      assert(ans >= 0);
      //cout << "ans = " << ans << " dp = " << dp[(1<<SIZE)-1] << endl;
    }

  printf("%d\n",ans);//えるたそ〜

  return 0;
}
accuracy: 1
time_ms: 620
memory_kb: 1336
final_rank: 9