Problem:
Problem E:
Mobile Computing
There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful
stones on the planet, and the Yaen people love to collect them. They bring the stones back home
and make nice mobile arts of them to decorate their 2-dimensional living rooms.
In their 2-dimensional world, a mobile is defined recursively as follows:
a stone hung by a string, or
a rod of length 1 with two sub-mobiles at both ends; the rod
  is hung by a string at the center of gravity of sub-mobiles.
  When the weights of the sub-mobiles are
n
and
m
, and their
  distances from the center of gravity are a and b respectively,
  the equation
n
×
a
=
m
×
b
holds.
For example, if you got three stones with weights 1, 1, and 2, here are some possible mobiles
and their widths:
Given the weights of stones and the width of the room, your task is to design the widest possible
mobile satisfying both of the following conditions.
It uses all the stones.
Its width is less than the width of the room.
You should ignore the widths of stones.
In some cases two sub-mobiles hung from both ends of a rod might
overlap (see the figure on the below). Such mobiles are acceptable.
The width of the example is (1/3) + 1 + (1/4).
Input
The first line of the input gives the number of datasets. Then the specified number of datasets
follow. A dataset has the following format.
r
s
w
1
.
       .
       .
w
s
r
is a decimal fraction representing the width of the room, which satisfies 0 <
r
< 10.
s
is the
number of the stones. You may assume 1 ≤
s
≤ 6.
w
i
is the weight of the
i
-th stone, which is
an integer. You may assume 1 ≤
w
i
≤ 1000.
You can assume that no mobiles whose widths are between
r
- 0.00001 and
r
+ 0.00001 can be made of given stones.
Output
For each dataset in the input, one line containing a decimal fraction should be output. The
decimal fraction should give the width of the widest possible mobile as defined above. An
output line should not contain extra characters such as spaces.
In case there is no mobile which satisfies the requirement, answer -1 instead.
The answer should not have an error greater than 0.00000001. You may output any number of
digits after the decimal point, provided that the above accuracy condition is satisfied.
Sample Input
5
1.3
3
1
2
1
1.4
3
1
2
1
2.0
3
1
2
1
1.59
4
2
1
1
3
1.7143
4
1
2
3
5
Output for the Sample Input
-1
1.3333333333333335
1.6666666666666667
1.5833333333333335
1.7142857142857142

Submitted solutions:

code_snippet: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

const int N = 7, M = 1 << N;
int tr[M];          //-1: root, 0: no use, else:left value
int value[M];
int sumvalue[M];
double _left[M], _right[M], cur[M];   //具体的位置
bool st[N];
double r, ans;   int s;
double _r;
int q[N];
vector<double> que;

void dfs_value(int u){      //dfs求得左右子树的value
    if(tr[u] != -1){
        value[u] = tr[u];
        return;
    }
    dfs_value(u * 2);
    dfs_value(u * 2 + 1);
    value[u] = value[u * 2] + value[u * 2 + 1];
}


void update(){              //dfs进行更新ans
    int cnt = 1;
    int mark = 1;



    memcpy(value, tr, sizeof value);
    dfs_value(1);

    int u;
    double res = 0.0, t = 0.0;

    // 一路向左 加 一路向右
    double l = 0, r = 0;
    cur[1] = 0;
    for(int i = 1; i <= M; i++){
        if(tr[i] == -1){
            int x = 2 * i, y = 2 * i + 1;
            _left[i] = double(value[y]) / (value[x] + value[y]);
            _right[i] = double(value[x]) / (value[x] + value[y]);
            cur[x] = cur[i] - _left[i];
            cur[y] = cur[i] + _right[i];
            l = min(cur[x], l);
            r = max(cur[y], r);
        }
    }


    res = (r - l);
    if(res <= _r + 0.00000001){
        ans = max(ans, res);
    }

    cnt = 1;
    mark = 1;

    //cout<<"RES : "<<res<<endl;
    //que.push_back(res);
    /*printf("T  : %0.16f\n", r - l);
    printf("RES: %0.16f\n", res);
    cout<<"*************************\n";
    for(int i = 1; i < (2 << s); i++){
        printf("%d ", tr[i]);
        if(mark == i){
            cnt *= 2;
            mark += cnt;
            cout<<endl;
        }
    }
    cout<<"*************************\n";
    cnt = 1;
    mark = 1;
    cout<<"*************************\n";
    for(int i = 1; i < (2 << s); i++){
        printf("%d ", value[i]);
        if(mark == i){
            cnt *= 2;
            mark += cnt;
            cout<<endl;
        }
    }
    cout<<"*************************\n";*/

}

// 二叉树的线性便利
void dfs(int u, int surp, int need){    //need: 是现在需要的， surp 是剩余的
    if(need < surp) return;
    //if(u >= (2 << s))   return;
    //printf("U:%d, SURP:%d, NEED:%d\n", u, surp, need);
    if(need == 0){      //递归结束，填坑完毕
        //调用更新函数
        update();
        //cout<<"*************UPDATE*************"<<endl;
        return;
    }

    if(tr[u / 2] != -1){    //这个点不可以，下一个
        dfs(u + 1, surp, need);
        return;
    }


    if(surp < need){    //可以加一个杠
        int tmp = tr[u];
        tr[u] = -1;
        dfs(u + 1, surp + 1, need);
        tr[u] = tmp;
    }

    if(surp == 1 && need > 1)   return;     //一定要注意这一个，只剩一下一个位置，不可以放东西了,只允许加杠

    //不加杠
    for(int i = 1; i <= s; i++){
        if(st[i])   continue;
        st[i] = true;
        int tmp = tr[u];
        tr[u] = q[i];
        dfs(u + 1, surp - 1, need - 1);
        st[i] = false;
        tr[u] = tmp;
    }
    return;
}

int main()
{
    int T;  cin>>T;

    while(T--){
        cin>>r>>s;
        _r = r;
        for(int i = 1; i <= s; i++) scanf("%d", &q[i]);
        if(s == 1){
            if(0.0 <= r + 0.0000001){
                printf("%0.16f\n", 0.0);
            }else{
                cout<<-1<<endl;
            }
            continue;
        }

        memset(tr, 0, sizeof tr);   memset(st, false, sizeof st);
        ans = -1;
        tr[1] = -1;
        dfs(2, 2, s);

        /*for(int i = 0; i < que.size(); i++){
            printf("%0.6f\n", que[i]);
        }*/
        //printf("Ans: %0.6lf\n", ans);


        if(ans >= -0.001){
            printf("%0.16f\n", ans);
        }else{
            cout<<-1<<endl;
        }

    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3276
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 105;
const double eps = 1e-5;
int s,w[maxn],t[maxn];
double r;
bool vis[maxn];
double ans;
double li[maxn],ri[maxn],val[maxn];
void init() {
	memset(vis,false,sizeof(vis));
	memset(w,0,sizeof(w));
	memset(t,0,sizeof(t));
}
void cal(int num) {
	memset(li,0,sizeof(li));
	memset(ri,0,sizeof(ri));
	memset(val,0,sizeof(val));
	for(int i = num - 1;i >= 1;i--) {
		if(t[i] == -1) {
			val[i] = val[2*i] + val[2*i+1];
			double L = val[2*i+1]/val[i],R = val[2*i]/val[i];
			li[i] = min(li[2*i]-L,li[2*i+1]+R);
			ri[i] = max(ri[2*i+1]+R,ri[2*i]-L);
		}
		else if(t[i]) val[i] = w[t[i]];
	}
	double temp = ri[1] - li[1];
	if(temp - r < eps && temp > ans) ans = temp;
}
void dfs(int cur,int cnt,int now) {
	if(cnt == s) {
		cal(cur);
		return;	
	}
	if(t[cur/2] != -1) dfs(cur+1,cnt,now);
	else {
		if(s - cnt > now) {
			t[cur] = -1;
			dfs(cur+1,cnt,now+1);
			t[cur] = 0;
		}
		if(now == 1 && s - cnt > now) return; 
		for(int i = 1;i <= s;i++) {
			if(!vis[i]) {
				vis[i] = true;
				t[cur] = i;
				dfs(cur+1,cnt+1,now-1);
				vis[i] = false;
				t[cur] = 0;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%lf %d",&r,&s);
		init();
		for(int i = 1;i <= s;i++) scanf("%d",&w[i]);
		if(s == 1) {printf("0.00\n");continue;}
		ans = -1;
		t[1] = -1;
		dfs(2,0,2);
		if(ans == -1) printf("-1\n");
		else printf("%.10lf\n",ans);
	}
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3200
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 105;
const double eps = 1e-5;
int s,t[maxn];
double r,w[maxn];
bool vis[maxn];
double ans;
double li[maxn],ri[maxn],val[maxn];
void init() {
	memset(vis,false,sizeof(vis));
	memset(w,0,sizeof(w));
	memset(t,0,sizeof(t));
}
void cal(int num) {
	memset(li,0,sizeof(li));
	memset(ri,0,sizeof(ri));
	memset(val,0,sizeof(val));
	for(int i = num - 1;i;i--) {
		if(t[i] == -1) {
			val[i] = val[2*i] + val[2*i+1];
			double L = val[2*i+1]/val[i],R = val[2*i]/val[i];
			li[i] = min(li[2*i]-L,li[2*i+1]+R);
			ri[i] = max(ri[2*i+1]+R,ri[2*i]-L);
		}
		else if(t[i]) val[i] = w[t[i]];
	}
	double temp = ri[1] - li[1];
	if(temp - r < eps && temp > ans) ans = temp;
}
void dfs(int cur,int cnt,int now) {
	if(cnt == 0) {
		cal(cur);
		return;	
	}
	if(t[cur/2] != -1) dfs(cur+1,cnt,now);
	else {
		if(cnt > now) {
			t[cur] = -1;
			dfs(cur+1,cnt,now+1);
			t[cur] = 0;
		}
		if(now == 1 && cnt > 1) return; 
		for(int i = 1;i <= s;i++) {
			if(!vis[i]) {
				vis[i] = true;
				t[cur] = i;
				dfs(cur+1,cnt-1,now-1);
				vis[i] = false;
				t[cur] = 0;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%lf %d",&r,&s);
		init();
		for(int i = 1;i <= s;i++) scanf("%lf",&w[i]);
		if(s == 1)  {printf("%.10lf\n", 0.0);continue;}
		ans = -1;
		t[1] = -1;
		dfs(2,s,2);
		if(ans == -1) printf("-1\n");
		else printf("%.10lf\n",ans);
	}
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3228
final_rank: 10

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
 
using namespace std;
const int N = 10;
const int M = 105;
const double INF = 0x3f3f3f3f;
 
int n, v[N], t[M];
double R, w[N], ans, l[M], r[M], val[M];
 
void init () {
	memset(t, 0, sizeof(t));
	memset(v, 0, sizeof(v));
	t[1] = -1; ans = -1;
 
	scanf("%lf%d", &R, &n);
	for (int i = 1; i <= n; i++) scanf("%lf", &w[i]);
}
 
void judge(int u) {
	memset(l, 0, sizeof(l));
	memset(r, 0, sizeof(r));
	memset(val, 0, sizeof(val));
 
	for (int i = u; i; i--) {
		if (t[i] == -1) {
			int x = i*2, y = i*2+1;
			val[i] = val[x] + val[y];
			double Li = val[y] / val[i];
			double Ri = val[x] / val[i];
 
			l[i] = min(-Li+l[x], Ri+l[y]);
			r[i] = max(-Li+r[x], Ri+r[y]);
		} else if (t[i]) {
			val[i] = w[t[i]];
		}
	}
 
	double tmp = r[1] - l[1];
	if (tmp - R < 1e-5 && tmp > ans) ans = tmp;
}
 
void dfs(int u, int m, int use) {
 
	if (use == 0) {
		judge(u-1);
		return;
	}
 
	if (t[u/2] != -1)  {
		dfs(u+1, m, use);
	} else {
 
		if (use > m) {
			t[u] = -1;
			dfs(u+1, m+1, use);
			t[u] = 0;
		}
 
		if (m == 1 && use > 1) return;
 
		for (int i = 1; i <= n; i++) if (!v[i]) {
			v[i] = 1; t[u] = i;
			dfs(u+1, m-1, use-1);
			v[i] = 0; t[u] = 0;
		}
	}
}
 
int main () {
	int cas;
	scanf("%d", &cas);
	while (cas--) {
		init();
		if (n == 1) printf("%.10lf\n", 0.0);	
		else {
			dfs(2, 2, n);
			if (ans == -1) printf("-1\n");
			else printf("%.10lf\n", ans);
		}
	}
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 2556
final_rank: 5

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

template<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};

signed main(){
  Int T;
  cin>>T;

  cout<<fixed<<setprecision(12);
  while(T--){
    double width;
    Int n;
    cin>>width>>n;
    vector<double> w(n);
    for(Int i=0;i<n;i++) cin>>w[i];

    using P = pair<double, double>;
    Int s=1<<n;
    vector<double> sum(s,0);
    for(Int b=0;b<s;b++)
      for(Int i=0;i<n;i++)
	if((b>>i)&1) sum[b]+=w[i];

    function<vector<P>(Int, double)> dfs=[&](Int b,double pos){
      vector<P> res;      
      if(__builtin_popcount(b)==1){
	res.emplace_back(pos,pos);
	return res;
      }
      for(Int nb=b;nb;nb=(nb-1)&b){
	if(nb==b) continue;
	assert(nb&&(nb^b));
	double x=sum[nb],y=sum[nb^b];
	double aa=1.0/(1.0+x/y);
	double bb=1.0-aa;
	auto l=dfs(nb,pos-aa);
	auto r=dfs(nb^b,pos+bb);
	for(auto p:l) 
	  for(auto q:r)
	    res.emplace_back(min({p.first,q.first,p.second,q.second}),
			     max({p.first,q.first,p.second,q.second}));
      }
      return res;
    };
    
    double ans=-1; 
    auto v=dfs(s-1,0);
    for(auto p:v) if(p.second-p.first<width) chmax(ans,p.second-p.first);
    
    if(ans<0) cout<<"-1"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3988
final_rank: 13

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

int L[15], R[15];
int weight[15];

double W;

int N, w[6];
int TS;

int make_tree(int x, vector<int> A, vector<int> B){
  
  if( (int)B.size() == 1 ) return weight[x] = w[B[0]];
  
  int idx = 0, maxA = -1;
  
  for(int i=0;i<(int)A.size();i++){
    
    if( maxA < A[i] ){
      
      maxA = A[i];
      
      idx = i;
      
    }
    
  }
  
  assert( (int)A.size() + 1 == (int)B.size() );
  
  int sum = 0;
  
  vector<int> xLA, xLB;
  
  for(int i=0;i<idx;i++) xLA.push_back(A[i]);
  
  for(int i=0;i<=idx;i++) xLB.push_back(B[i]);
  
  TS++;
  
  L[x] = TS;
  
  sum += make_tree( TS, xLA, xLB );
  
  vector<int> xRA, xRB;
  
  for(int i=idx+1;i<(int)A.size();i++) xRA.push_back(A[i]);
  
  for(int i=idx+1;i<(int)B.size();i++) xRB.push_back(B[i]);
  
  TS++;
  
  R[x] = TS;
  
  sum += make_tree( TS, xRA, xRB );
  
  return weight[x] = sum;
}

double lmin, rmax;

void dfs(int x, double mid){
  
  lmin = min( lmin, mid );
  rmax = max( rmax, mid );
  
  if( L[x] == -1 && R[x] == -1 ) return;
  
  double l = weight[L[x]], r = weight[R[x]];
  
  double a = r / ( l + r ), b = l / ( l + r );
  
  dfs( L[x], mid - a );
  
  dfs( R[x], mid + b );
  
}

signed main(){
  
  int cnt;
  cin>>cnt;
  
  while(cnt--){
    
    cin>>W>>N;
    
    for(int i=0;i<N;i++) cin>>w[i];
    
    if( N == 1 ){
      cout<<0<<endl;
      continue;
    }
    
    double ans = -1;
    
    vector<int> A;
    
    for(int i=0;i<N-1;i++) A.push_back(i);
    
    do{
      
      vector<int> B;
      
      for(int i=0;i<N;i++) B.push_back(i);
      
      do{
	
	memset( L, -1, sizeof(L) );
	memset( R, -1, sizeof(R) );
	
	TS = 0;
	
	make_tree( TS, A, B );
	
	lmin = 1e9, rmax = -1e9;
	
	dfs( 0, 0.0 );
	
	if( rmax - lmin < W ) ans = max( ans, rmax - lmin );
	
      }
      while( next_permutation( B.begin(), B.end() ) );
      
    }
    while( next_permutation( A.begin(), A.end() ) );
        
    printf("%.8f\n", ans );
    
  }
  
  return 0;
}
accuracy: 1
time_ms: 2300
memory_kb: 3288
final_rank: 17

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

int L[15], R[15];
int weight[15];

double W;

int N, w[6];
int TS;

int make_tree(int x, vector<int> A, vector<int> B){
  
  if( (int)B.size() == 1 ) return weight[x] = w[B[0]];
  
  int idx = 0, maxA = -1;
  
  for(int i=0;i<(int)A.size();i++){
    
    if( maxA < A[i] ){
      
      maxA = A[i];
      
      idx = i;
      
    }
    
  }
  
  assert( (int)A.size() + 1 == (int)B.size() );
  
  int sum = 0;
  
  vector<int> xLA, xLB;
  
  for(int i=0;i<idx;i++) xLA.push_back(A[i]);
  
  for(int i=0;i<=idx;i++) xLB.push_back(B[i]);
  
  TS++;
  
  L[x] = TS;
  
  sum += make_tree( TS, xLA, xLB );
  
  vector<int> xRA, xRB;
  
  for(int i=idx+1;i<(int)A.size();i++) xRA.push_back(A[i]);
  
  for(int i=idx+1;i<(int)B.size();i++) xRB.push_back(B[i]);
  
  TS++;
  
  R[x] = TS;
  
  sum += make_tree( TS, xRA, xRB );
  
  return weight[x] = sum;
}

double lmin, rmax;

void dfs(int x, double mid){
  
  lmin = min( lmin, mid );
  rmax = max( rmax, mid );
  
  if( L[x] == -1 && R[x] == -1 ) return;
  
  double l = weight[L[x]], r = weight[R[x]];
  
  double a = r / ( l + r ), b = l / ( l + r );
  
  dfs( L[x], mid - a );
  
  dfs( R[x], mid + b );
  
}

signed main(){
  
  int cnt;
  cin>>cnt;
  
  while(cnt--){
    
    cin>>W>>N;
    
    for(int i=0;i<N;i++) cin>>w[i];
    
    if( N == 1 ){
      cout<<0<<endl;
      continue;
    }
    
    double ans = -1;
    
    vector<int> A;
    
    for(int i=0;i<N-1;i++) A.push_back(i);
    
    do{
      
      vector<int> B;
      
      for(int i=0;i<N;i++) B.push_back(i);
      
      do{
	
	memset( L, -1, sizeof(L) );
	memset( R, -1, sizeof(R) );
	
	TS = 0;
	
	make_tree( TS, A, B );
	
	lmin = 1e9, rmax = -1e9;
	
	dfs( 0, 0.0 );
	
	if( rmax - lmin < W ) ans = max( ans, rmax - lmin );
	
      }
      while( next_permutation( B.begin(), B.end() ) );
      
    }
    while( next_permutation( A.begin(), A.end() ) );
        
    printf("%.8f\n", ans );
    
  }
  
  return 0;
}
accuracy: 1
time_ms: 2300
memory_kb: 3200
final_rank: 16

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

template<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};

signed main(){
  Int T;
  cin>>T;

  cout<<fixed<<setprecision(12);
  while(T--){
    double width;
    Int n;
    cin>>width>>n;
    vector<double> w(n);
    for(Int i=0;i<n;i++) cin>>w[i];

    using P = pair<double, double>;
    Int s=1<<n;
    vector<double> sum(s,0);
    for(Int b=0;b<s;b++)
      for(Int i=0;i<n;i++)
	if((b>>i)&1) sum[b]+=w[i];

    function<vector<P>(Int, double)> dfs=[&](Int b,double pos){
      vector<P> res;      
      if(__builtin_popcount(b)==1){
	res.emplace_back(pos,pos);
	return res;
      }
      for(Int nb=b;nb;nb=(nb-1)&b){
	if(nb==b) continue;
	assert(nb&&(nb^b));
	double x=sum[nb],y=sum[nb^b];
	double aa=1.0/(1.0+x/y);
	double bb=1.0-aa;
	auto l=dfs(nb,pos-aa);
	auto r=dfs(nb^b,pos+bb);
	for(auto p:l) 
	  for(auto q:r)
	    res.emplace_back(min({p.first,q.first,p.second,q.second}),
			     max({p.first,q.first,p.second,q.second}));
      }
      return res;
    };
    
    double ans=-1; 
    auto v=dfs(s-1,0);
    for(auto p:v) if(p.second-p.first<width) chmax(ans,p.second-p.first);
    
    if(ans<0) cout<<"-1"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3988
final_rank: 14

code_snippet: #include <stdio.h>
#include <cmath>
#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;


enum Type{
	Stone,
	Mobile,
};

struct Info{
	Type type;
	int index_left,index_right;
	double weight,left,right,most_left_loc,most_right_loc;
};

Info info[12];
int num_stone,eq_index;
int table[7][720][6],table_index[7],eq_num[7];
double room_width;
char equation[7][100000][12];
priority_queue<double> Q;

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

void makeEquation(int array[6],int array_index,char buf[12],int buf_index,int value,int rest_mult,int length){

	if(rest_mult == 0){
		buf[buf_index] = '\0';
		strcpy(equation[length][eq_num[length]++],buf);
		return;
	}

	if(array_index < length){
		char next_buf[12];
		for(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];
		next_buf[buf_index] = array[array_index]+'0';
		makeEquation(array,array_index+1,next_buf,buf_index+1,value+1,rest_mult,length);
	}

	if(value >= 2){
		char next_buf[12];
		for(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];
		next_buf[buf_index] = '*';
		makeEquation(array,array_index,next_buf,buf_index+1,value-1,rest_mult-1,length);
	}
}

void calc_mobile(int tmp_index,int buf_index,Info tmp_info[12],int info_index,stack<int> S){

	if(equation[num_stone][tmp_index][buf_index] == '\0'){

		Q.push(tmp_info[info_index-1].most_left_loc+tmp_info[info_index-1].most_right_loc);

		return;
	}

	int left,right;
	double left_weight,right_weight;
	char tmp_ch;
	stack<int> WORK,NEXT;

	while(!S.empty()){
		WORK.push(S.top());
		S.pop();
	}

	while(!WORK.empty()){
		NEXT.push(WORK.top());
		WORK.pop();
	}

	tmp_ch = equation[num_stone][tmp_index][buf_index];

	if(tmp_ch >= '0' && tmp_ch <= '5'){

		NEXT.push(tmp_ch-'0');

		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index,NEXT);

	}else{ //tmp_ch == '*'

		left = NEXT.top();
		NEXT.pop();
		right = NEXT.top();
		NEXT.pop();

		left_weight = tmp_info[left].weight;
		right_weight = tmp_info[right].weight;

		tmp_info[info_index].left = (right_weight)/(left_weight+right_weight);
		tmp_info[info_index].right = 1.0-tmp_info[info_index].left;
		tmp_info[info_index].type = Mobile;
		tmp_info[info_index].index_left = left;
		tmp_info[info_index].index_right = right;
		tmp_info[info_index].weight = left_weight+right_weight;
		tmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);
		tmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);

		NEXT.push(info_index);
		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);

	}
}

void calc(int tmp_index){

	Info tmp_info[12];
	for(int i = 0; i < num_stone; i++)tmp_info[i] = info[i];
	for(int i = num_stone; i < 12; i++){
		tmp_info[i].index_left = -1;
		tmp_info[i].index_right = -1;
		tmp_info[i].most_left_loc = 0.0;
		tmp_info[i].most_right_loc = 0.0;
	}

	stack<int> S;

	calc_mobile(tmp_index,0,tmp_info,num_stone,S);
}

void func(){

	while(!Q.empty())Q.pop();

	scanf("%lf",&room_width);

	scanf("%d",&num_stone);

	for(int i = 0; i < num_stone; i++){
		scanf("%lf",&info[i].weight);
		info[i].type = Stone;
		info[i].index_left = -1;
		info[i].index_right = -1;
	}

	if(num_stone == 1){
		printf("0.00000000\n");
		return;
	}

	eq_index = eq_num[num_stone];

	for(int i = 0; i < eq_index; i++){
		calc(i);
	}

	while(!Q.empty()){

		if(Q.top() < room_width){
			printf("%.16lf\n",Q.top());
			return;
		}
		Q.pop();
	}

	printf("-1\n");
}

void recursive(int array[6],int array_index,int length){

	if(array_index == length){

		for(int i = 0; i < length; i++){
			table[length][table_index[length]][i] = array[i];
		}
		table_index[length]++;
		return;
	}

	bool check[length];
	for(int i = 0; i < length; i++)check[i] = false;

	for(int i = 0; i < array_index; i++){
		check[array[i]] = true;
	}

	for(int i = 0; i < length; i++){
		if(check[i] == false){
			int next_array[6];

			for(int k = 0; k < array_index; k++)next_array[k] = array[k];
			next_array[array_index] = i;
			recursive(next_array,array_index+1,length);
		}
	}
}

void makeTable(){

	for(int length = 2; length <= 6; length++){
		table_index[length] = 0;

		int first_array[6];

		recursive(first_array,0,length);
	}
}

int main(){

	makeTable();

	for(int length = 2; length <= 6; length++){
		char first_buf[12];
		eq_num[length] = 0;

		for(int i = 0; i < table_index[length]; i++){
			makeEquation(table[length][i],0,first_buf,0,0,length-1,length);
		}
	}

	int case_num;
	scanf("%d",&case_num);

	for(int i = 0; i < case_num; i++)func();

	return 0;
}
accuracy: 1
time_ms: 2520
memory_kb: 3736
final_rank: 18

code_snippet: #include <stdio.h>
#include <cmath>
#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;


enum Type{
	Stone,
	Mobile,
};

struct Info{
	Type type;
	int index_left,index_right;
	double weight,left,right,most_left_loc,most_right_loc;
};

Info info[12];
int num_stone,eq_index;
int table[7][720][6],table_index[7],eq_num[7];
double room_width;
char equation[7][100000][12];
priority_queue<double> Q;

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

void makeEquation(int array[6],int array_index,char buf[12],int buf_index,int value,int rest_mult,int length){

	if(rest_mult == 0){
		buf[buf_index] = '\0';
		strcpy(equation[length][eq_num[length]++],buf);
		return;
	}

	if(array_index < length){
		char next_buf[12];
		for(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];
		next_buf[buf_index] = array[array_index]+'0';
		makeEquation(array,array_index+1,next_buf,buf_index+1,value+1,rest_mult,length);
	}

	if(value >= 2){
		char next_buf[12];
		for(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];
		next_buf[buf_index] = '*';
		makeEquation(array,array_index,next_buf,buf_index+1,value-1,rest_mult-1,length);
	}
}

void calc_mobile(int tmp_index,int buf_index,Info tmp_info[12],int info_index,stack<int> S){

	if(equation[num_stone][tmp_index][buf_index] == '\0'){

		Q.push(tmp_info[info_index-1].most_left_loc+tmp_info[info_index-1].most_right_loc);

		return;
	}

	int left,right;
	double left_weight,right_weight;
	char tmp_ch;
	stack<int> WORK,NEXT;

	while(!S.empty()){
		WORK.push(S.top());
		S.pop();
	}

	while(!WORK.empty()){
		NEXT.push(WORK.top());
		WORK.pop();
	}

	tmp_ch = equation[num_stone][tmp_index][buf_index];

	if(tmp_ch >= '0' && tmp_ch <= '5'){

		NEXT.push(tmp_ch-'0');

		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index,NEXT);

	}else{ //tmp_ch == '*'

		left = NEXT.top();
		NEXT.pop();
		right = NEXT.top();
		NEXT.pop();

		left_weight = tmp_info[left].weight;
		right_weight = tmp_info[right].weight;

		tmp_info[info_index].left = (right_weight)/(left_weight+right_weight);
		tmp_info[info_index].right = 1.0-tmp_info[info_index].left;
		tmp_info[info_index].type = Mobile;
		tmp_info[info_index].index_left = left;
		tmp_info[info_index].index_right = right;
		tmp_info[info_index].weight = left_weight+right_weight;
		tmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);
		tmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);

		NEXT.push(info_index);
		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);

		/*if(tmp_info[left].type == Stone && tmp_info[right].type == Stone)return;

		NEXT.pop();
		swap(left,right);

		swap(tmp_info[info_index].left,tmp_info[info_index].right);
		tmp_info[info_index].index_left = left;
		tmp_info[info_index].index_right = right;
		tmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);
		tmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);

		NEXT.push(info_index);
		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);*/
	}
}

void calc(int tmp_index){

	Info tmp_info[12];
	for(int i = 0; i < num_stone; i++)tmp_info[i] = info[i];
	for(int i = num_stone; i < 12; i++){
		tmp_info[i].index_left = -1;
		tmp_info[i].index_right = -1;
		tmp_info[i].most_left_loc = 0.0;
		tmp_info[i].most_right_loc = 0.0;
	}

	stack<int> S;

	calc_mobile(tmp_index,0,tmp_info,num_stone,S);
}

void func(){

	while(!Q.empty())Q.pop();

	scanf("%lf",&room_width);

	scanf("%d",&num_stone);

	for(int i = 0; i < num_stone; i++){
		scanf("%lf",&info[i].weight);
		info[i].type = Stone;
		info[i].index_left = -1;
		info[i].index_right = -1;
	}

	if(num_stone == 1){
		printf("0.00000000\n");
		return;
	}

	eq_index = eq_num[num_stone];

	for(int i = 0; i < eq_index; i++){
		calc(i);
	}

	while(!Q.empty()){

		if(Q.top() < room_width){
			printf("%.16lf\n",Q.top());
			return;
		}
		Q.pop();
	}

	printf("-1\n");
}

void recursive(int array[6],int array_index,int length){

	if(array_index == length){

		for(int i = 0; i < length; i++){
			table[length][table_index[length]][i] = array[i];
		}
		table_index[length]++;
		return;
	}

	bool check[length];
	for(int i = 0; i < length; i++)check[i] = false;

	for(int i = 0; i < array_index; i++){
		check[array[i]] = true;
	}

	for(int i = 0; i < length; i++){
		if(check[i] == false){
			int next_array[6];

			for(int k = 0; k < array_index; k++)next_array[k] = array[k];
			next_array[array_index] = i;
			recursive(next_array,array_index+1,length);
		}
	}
}

void makeTable(){

	for(int length = 2; length <= 6; length++){
		table_index[length] = 0;

		int first_array[6];

		recursive(first_array,0,length);
	}
}

int main(){

	makeTable();

	for(int length = 2; length <= 6; length++){
		char first_buf[12];
		eq_num[length] = 0;

		for(int i = 0; i < table_index[length]; i++){
			makeEquation(table[length][i],0,first_buf,0,0,length-1,length);
		}
	}

	int case_num;
	scanf("%d",&case_num);

	for(int i = 0; i < case_num; i++)func();

	return 0;
}
accuracy: 1
time_ms: 2520
memory_kb: 3824
final_rank: 19

code_snippet: #include <stdio.h>
#include <cmath>
#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;


enum Type{
	Stone,
	Mobile,
};

struct Info{
	Type type;
	int index_left,index_right;
	double weight,left,right,most_left_loc,most_right_loc;
};

Info info[12];
int num_stone,eq_index;
int table[7][720][6],table_index[7],eq_num[7];
double room_width;
char equation[7][100000][12];
priority_queue<double> Q;

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

void makeEquation(int array[6],int array_index,char buf[12],int buf_index,int value,int rest_mult,int length){

	if(rest_mult == 0){
		buf[buf_index] = '\0';
		strcpy(equation[length][eq_num[length]++],buf);
		return;
	}

	if(array_index < length){
		char next_buf[12];
		for(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];
		next_buf[buf_index] = array[array_index]+'0';
		makeEquation(array,array_index+1,next_buf,buf_index+1,value+1,rest_mult,length);
	}

	if(value >= 2){
		char next_buf[12];
		for(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];
		next_buf[buf_index] = '*';
		makeEquation(array,array_index,next_buf,buf_index+1,value-1,rest_mult-1,length);
	}
}

void calc_mobile(int tmp_index,int buf_index,Info tmp_info[12],int info_index,stack<int> S){

	if(equation[num_stone][tmp_index][buf_index] == '\0'){

		Q.push(tmp_info[info_index-1].most_left_loc+tmp_info[info_index-1].most_right_loc);

		return;
	}

	int left,right;
	double left_weight,right_weight;
	char tmp_ch;
	stack<int> WORK,NEXT;

	while(!S.empty()){
		WORK.push(S.top());
		S.pop();
	}

	while(!WORK.empty()){
		NEXT.push(WORK.top());
		WORK.pop();
	}

	tmp_ch = equation[num_stone][tmp_index][buf_index];

	if(tmp_ch >= '0' && tmp_ch <= '5'){

		NEXT.push(tmp_ch-'0');

		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index,NEXT);

	}else{ //tmp_ch == '*'

		left = NEXT.top();
		NEXT.pop();
		right = NEXT.top();
		NEXT.pop();

		left_weight = tmp_info[left].weight;
		right_weight = tmp_info[right].weight;

		tmp_info[info_index].left = (right_weight)/(left_weight+right_weight);
		tmp_info[info_index].right = 1.0-tmp_info[info_index].left;
		tmp_info[info_index].type = Mobile;
		tmp_info[info_index].index_left = left;
		tmp_info[info_index].index_right = right;
		tmp_info[info_index].weight = left_weight+right_weight;
		tmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);
		tmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);

		NEXT.push(info_index);
		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);

		if(tmp_info[left].type == Stone && tmp_info[right].type == Stone)return;

		NEXT.pop();
		swap(left,right);

		swap(tmp_info[info_index].left,tmp_info[info_index].right);
		tmp_info[info_index].index_left = left;
		tmp_info[info_index].index_right = right;
		tmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);
		tmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);

		NEXT.push(info_index);
		calc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);
	}
}

void calc(int tmp_index){

	Info tmp_info[12];
	for(int i = 0; i < num_stone; i++)tmp_info[i] = info[i];
	for(int i = num_stone; i < 12; i++){
		tmp_info[i].index_left = -1;
		tmp_info[i].index_right = -1;
		tmp_info[i].most_left_loc = 0.0;
		tmp_info[i].most_right_loc = 0.0;
	}

	stack<int> S;

	calc_mobile(tmp_index,0,tmp_info,num_stone,S);
}

void func(){

	while(!Q.empty())Q.pop();

	scanf("%lf",&room_width);

	scanf("%d",&num_stone);

	for(int i = 0; i < num_stone; i++){
		scanf("%lf",&info[i].weight);
		info[i].type = Stone;
		info[i].index_left = -1;
		info[i].index_right = -1;
	}

	if(num_stone == 1){
		printf("0.00000000\n");
		return;
	}

	eq_index = eq_num[num_stone];

	for(int i = 0; i < eq_index; i++){
		calc(i);
	}

	while(!Q.empty()){

		if(Q.top() < room_width){
			printf("%.16lf\n",Q.top());
			return;
		}
		Q.pop();
	}

	printf("-1\n");
}

void recursive(int array[6],int array_index,int length){

	if(array_index == length){

		for(int i = 0; i < length; i++){
			table[length][table_index[length]][i] = array[i];
		}
		table_index[length]++;
		return;
	}

	bool check[length];
	for(int i = 0; i < length; i++)check[i] = false;

	for(int i = 0; i < array_index; i++){
		check[array[i]] = true;
	}

	for(int i = 0; i < length; i++){
		if(check[i] == false){
			int next_array[6];

			for(int k = 0; k < array_index; k++)next_array[k] = array[k];
			next_array[array_index] = i;
			recursive(next_array,array_index+1,length);
		}
	}
}

void makeTable(){

	for(int length = 2; length <= 6; length++){
		table_index[length] = 0;

		int first_array[6];

		recursive(first_array,0,length);
	}
}

int main(){

	makeTable();

	for(int length = 2; length <= 6; length++){
		char first_buf[12];
		eq_num[length] = 0;

		for(int i = 0; i < table_index[length]; i++){
			makeEquation(table[length][i],0,first_buf,0,0,length-1,length);
		}
	}

	int case_num;
	scanf("%d",&case_num);

	for(int i = 0; i < case_num; i++)func();

	return 0;
}
accuracy: 1
time_ms: 6450
memory_kb: 7548
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define INF 10000
#define N 10
typedef pair<double,double> P;
typedef vector<P> V;
int n;
double w[N];

int getW(int bit){
  int res = 0;
  for(int i=0;i<n;i++) if(bit>>i&1) res += w[i];
  return res;
}

P Merge(P a,P b){
  double l = min(a.first,b.first);
  double r = max(a.second,b.second);
  return P(l,r);
}

V dfs(double x,int bit){

  if(__builtin_popcount(bit)==1) return V(1,P(x,x));
  if(__builtin_popcount(bit)==0) return V(1,P(-INF,INF));
  
  vector<P> res;
  for(int i=0;i<(1<<n);i++){
    if( (i | bit) != bit ) continue;
    int lbit = i;
    int rbit = bit ^ i;
    double lw = getW(lbit);
    double rw = getW(rbit);
    if(lw == 0 || rw == 0) continue;
    double lx = x - rw/(lw+rw);
    double rx = x + lw/(lw+rw);
    V L = dfs(lx,lbit);
    V R = dfs(rx,rbit);

    for(P l:L)
      for(P r:R) res.push_back(Merge(l,r));
  }
  
  return res;
}


signed main(){
  int q;
  cin>>q;
  while(q--){
    double len;
    cin>>len;
    cin>>n;
    for(int i=0;i<n;i++) cin>>w[i];

    V v = dfs(0,(1<<n)-1);
    double ans = -1;
    for(P p:v)
      if(p.second-p.first<len)ans = max(ans,p.second - p.first);
    
    if(ans<0) printf("-1\n");
    else printf("%.10Lf\n",ans);
  }
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 5068
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
const double eps = 1e-8;

struct state{
  double left,right,weight;
};

double r;
int n;
int w[6];

state merge(state a,state b){
  double sum=a.weight+b.weight;
  double aleft=a.left - b.weight/sum;
  double bleft=b.left + a.weight/sum;
  double left=min(aleft,bleft);
  double aright=a.right - b.weight/sum;
  double bright=b.right + a.weight/sum;
  double right=max(aright,bright);
  return (state){left,right,sum};
}

double calc(vector<state> u){
  vector<state> tmp=u;
  
  int t[6]={0,1,2,3,4,5};
  int a[6];
  double res=-1;
  
  do{
    u=tmp;
    
    for(int i=0;i<n-1;i++)a[i]=t[i];
    
    for(int i=0;i<n-1;i++){
      int c=0;
      for(int j=i+1;j<n-1;j++){
        if(a[i]<a[j])c++;
      }
      a[i]=c;
    }

    for(int i=0;i<n-1;i++){
      int id=a[i];
      u[id]=merge(u[id],u[id+1]);
      u.erase( u.begin() + id + 1);
    }
    double value=u[0].right-u[0].left;
    if( value < r + eps )      res=max(res,value);
    
  }while( next_permutation(t,t+n-1) );
  
  return res;
}

double solve(){
  int t[6]={0,1,2,3,4,5};
  double res=-1;
  
  do{
    vector< state > u(n);
    
    for(int i=0;i<n;i++){
      u[i]=(state){ 0.0, 0.0, (double)w[ t[i] ]};
    }


    res=max(res,calc(u));
    
  }while( next_permutation(t,t+n) );

  if(res < r + eps )return res;
  else return -1;
}

int main(){
  int Tc;
  cin>>Tc;
  while(Tc--){
    cin>>r;
    cin>>n;
    for(int i=0;i<n;i++)cin>>w[i];
    double ans=solve();
    if(ans<0){
      cout<<"-1"<<endl;
    }else{
      printf("%.10f\n",ans);
    }
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3228
final_rank: 11

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
#define repeat(i,n) repeat_from(i,0,n)
using namespace std;
struct mobile_t {
    int w; // weight
    double l; // left
    double r; // right
};
const double eps = 0.0001;
double evaluate(vector<int> const & p) {
    stack<mobile_t> stk;
    for (int w : p) {
        if (w == 0) {
            if (stk.size() < 2) return -1;
            mobile_t x = stk.top(); stk.pop();
            mobile_t y = stk.top(); stk.pop();
            mobile_t z;
            z.w = x.w + y.w;
            double dx = y.w /(double) (x.w + y.w);
            double dy = x.w /(double) (x.w + y.w);
            z.l = max(x.l + dx, y.l - dy);
            z.r = max(x.r - dx, y.r + dy);
            stk.push(z);
        } else {
            stk.push((mobile_t){ w, 0.0, 0.0 });
        }
    }
    if (stk.size() != 1) return -1;
    return stk.top().l + stk.top().r;
}
int main() {
    int datasets; cin >> datasets;
    repeat (dataset, datasets) {
        double r; cin >> r;
        int s; cin >> s;
        vector<int> w(s); repeat (i,s) cin >> w[i];
        double result = -1;
        vector<int> p;
        repeat (i,s)   p.push_back(w[i]);
        repeat (i,s-1) p.push_back(0);
        sort(p.begin(), p.end());
        do {
            double it = evaluate(p);
            if (r + eps < it) continue;
            result = max(result, it);
        } while (next_permutation(p.begin(), p.end()));
        if (result == -1) {
            printf("-1\n");
        } else {
            printf("%.16lf\n", result);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 1870
memory_kb: 1268
final_rank: 6

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
#define repeat(i,n) repeat_from(i,0,n)
using namespace std;
struct mobile_t {
    int w; // weight
    double l; // left
    double r; // right
};
const double eps = 0.0001;
double evaluate(vector<int> const & p, double r) {
    stack<mobile_t> stk;
    for (int w : p) {
        if (w == 0) {
            if (stk.size() < 2) return -1;
            mobile_t x = stk.top(); stk.pop();
            mobile_t y = stk.top(); stk.pop();
            mobile_t z;
            z.w = x.w + y.w;
            double dx = y.w /(double) (x.w + y.w);
            double dy = x.w /(double) (x.w + y.w);
            z.l = max(x.l + dx, y.l - dy);
            z.r = max(x.r - dx, y.r + dy);
            if (r + eps < z.r + z.l) return -1;
            stk.push(z);
        } else {
            stk.push((mobile_t){ w, 0.0, 0.0 });
        }
    }
    if (stk.size() != 1) return -1;
    return stk.top().l + stk.top().r;
}
int main() {
    int datasets; cin >> datasets;
    repeat (dataset, datasets) {
        double r; cin >> r;
        int s; cin >> s;
        vector<int> w(s); repeat (i,s) cin >> w[i];
        double result = -1;
        vector<int> p;
        repeat (i,s)   p.push_back(w[i]);
        repeat (i,s-1) p.push_back(0);
        sort(p.begin(), p.end());
        do {
            result = max(result, evaluate(p, r));
        } while (next_permutation(p.begin(), p.end()));
        if (result == -1) {
            printf("-1\n");
        } else {
            printf("%.16lf\n", result);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 1880
memory_kb: 1268
final_rank: 7

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#include <cassert>
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
#define repeat(i,n) repeat_from(i,0,n)
#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= (m); --(i))
#define repeat_reverse(i,n) repeat_from_reverse(i,0,n)
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
using namespace std;
struct mobile_t {
    int w; // weight
    double l; // left
    double r; // right
};
const double eps = 0.0001;
double evaluate(vector<int> const & p, double r) {
    stack<mobile_t> stk;
    for (int w : p) {
        if (w == 0) {
            if (stk.size() < 2) return -1;
            mobile_t x = stk.top(); stk.pop();
            mobile_t y = stk.top(); stk.pop();
            mobile_t z;
            z.w = x.w + y.w;
            double dx = y.w /(double) (x.w + y.w);
            double dy = x.w /(double) (x.w + y.w);
            z.l = max(x.l + dx, y.l - dy);
            z.r = max(x.r - dx, y.r + dy);
            if (r + eps < z.r + z.l) return -1;
            stk.push(z);
        } else {
            stk.push((mobile_t){ w, 0.0, 0.0 });
        }
    }
    if (stk.size() != 1) return -1;
    return stk.top().l + stk.top().r;
}
int main() {
    int datasets; cin >> datasets;
    repeat (dataset, datasets) {
        double r; cin >> r;
        int s; cin >> s;
        vector<int> w(s); repeat (i,s) cin >> w[i];
        double result = -1;
        vector<int> p;
        repeat (i,s)   p.push_back(w[i]);
        repeat (i,s-1) p.push_back(0);
        sort(p.begin(), p.end());
        do {
            result = max(result, evaluate(p, r));
        } while (next_permutation(p.begin(), p.end()));
        if (result == -1) {
            cout << -1 << endl;
        } else {
            printf("%.16lf\n", result);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 1900
memory_kb: 1308
final_rank: 8

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

using namespace std;

#define whole(xs) (xs).begin(),(xs).end()

namespace {
    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }

    typedef long double real;

    real EPS = 1e-9;

    real R;
    int S;
    vector<int> W;
    void input() {
        cin >> R >> S;
        W.clear(); W.resize(S);
        for (int i = 0; i < S; i++) {
            cin >> W[i];
        }
    }

    real solve();

    vector<int> pat;
    void init() {
        real ans = -1;
        pat.clear();
        for (int k = 0; k < S - 1; k++) pat.push_back(0);
        for (int i = 0; i < S; i++) pat.push_back(W[i]);
        ans = max(ans, solve());
        if (ans < 0) {
            cout << -1 << endl;
        } else {
            printf("%.18Lf\n", ans);
        }
    }

    struct State {
        int w;
        real left, right;
        State(int w, real left, real right) : w(w), left(left), right(right) {}
    };

    real parse() {
        vector<State> stack;
        real ans = 0;
        for (int i = 0; i < pat.size(); i++) {
            if (pat[i] == 0) { // 結合
                if (stack.size() < 2) return -1.0;
                assert(stack.size() >= 2);
                State a = stack.back(); stack.pop_back();
                State b = stack.back(); stack.pop_back();
                int w = a.w + b.w;
                real L = 1.0 / w * b.w, R = 1.0 / w * a.w;
                real nleft = max(L + a.left, b.left - R);
                real nright = max(R + b.right, a.right - L);
                stack.push_back(State(a.w + b.w, nleft, nright));
                ans = max(ans, nleft + nright);
            } else {
                assert(pat[i] >= 1); // 石
                stack.push_back(State(pat[i], 0, 0));
            }
        }
        assert(stack.size() == 1);
        return ans;
    }

    real solve() {
        real ans = -1.0;
        sort(whole(pat));
        do {
            real c = parse();
            //printf("c: %.18Lf  ", c); cout << pat << endl;
            if (c > R + EPS) continue;
            ans = max(c, ans);
        } while (next_permutation(whole(pat)));
        return ans;
    }
}

int main() {
    int T; cin >> T;
    for (int i = 0; i < T; i++) {
        input(); init();
    }
    return 0;
}
accuracy: 1
time_ms: 740
memory_kb: 1316
final_rank: 4

code_snippet: #include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
void prB(int S){
  for(int i=0;i<6;i++){
    int x=(S>>i);
    cout<<(x&1)<<" ";
  }
  cout<<endl;
}
double width;
int n;
int t[6];
 
int mem[(1<<7)];
int calc(int S){
  if(mem[S]!=-1)return mem[S];
  int x=S&-S;
  return mem[S]=calc(S-x)+calc(x);
}
 
vector<double> dp[(1<<7)];
vector<double> dp2[(1<<7)];
bool vd[(1<<7)];
void rec(int S){
  if(vd[S])return;
  vd[S]=true;
  int sub=S;
  do{
    if(sub!=S&&sub!=0){
      int lS=sub,rS=S-sub;
      rec(lS);rec(rS);
      double lsum=calc(lS),rsum=calc(rS);
     
      double ld=rsum/(lsum+rsum);
      double rd=lsum/(lsum+rsum);
      for(int i=0;i<(int)dp[lS].size();i++){
    for(int j=0;j<(int)dp[rS].size();j++){
      double nl=min(dp[lS][i]-ld,dp[rS][j]+rd);
      double nr=max(dp2[lS][i]-ld,dp2[rS][j]+rd);
      dp[S].push_back(nl);
      dp2[S].push_back(nr);
    }
      }
    }
    sub=(sub-1)&S;
  }while(sub!=S);
}
 
void solve(){
  for(int i=0;i<n;i++){
    vd[(1<<i)]=true;
    dp[(1<<i)].push_back(0);
    dp2[(1<<i)].push_back(0);
  }
  int U=(1<<n)-1;
  rec(U);
  double ans=-1.0;
  for(int i=0;i<(int)dp[U].size();i++){
    double d=dp2[U][i]-dp[U][i];
    if( d < width + 0.00001  )ans=max(ans,d);
  }
  if(ans==-1.0)cout<<"-1"<<endl;
  else printf("%.10f\n",ans);
}
 
void init(){
  for(int i=0;i<(1<<7);i++){
    vd[i]=0;
    mem[i]=-1;
    dp[i].clear();
    dp2[i].clear();
  }
  mem[0]=0;
  for(int i=0;i<n;i++)mem[(1<<i)]=t[i];
}
 
int main(){
  int Tc;
  cin>>Tc;
  for(int tc=1;tc<=Tc;tc++){
    cin>>width>>n;
    for(int i=0;i<n;i++)cin>>t[i];
    init();
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1896
final_rank: 3

code_snippet: #include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <tuple>
#include <vector>
using namespace std;

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }

struct node_t {
	shared_ptr<node_t> left_child, right_child;

	inline bool is_leaf() const {
		return left_child == nullptr;
	}

	node_t(const shared_ptr<node_t> &l = nullptr, const shared_ptr<node_t> &r = nullptr):
		left_child(l), right_child(r) {}
};

constexpr int MAX_S = 6;

typedef array<vector<shared_ptr<node_t>>, MAX_S + 1> T_array;
typedef tuple<int, double, double> result;
enum { WEIGHT, LEFT, RIGHT };

T_array init_trees() {
	T_array trees;

	trees[1].emplace_back(new node_t());

	for(int s = 2; s <= 6; ++s) {
		for(int l = 1; l < s; ++l) {
			const int r = s - l;

			for(const auto &p1 : trees[l]) {
				for(const auto &p2 : trees[r]) {
					trees[s].emplace_back(new node_t(p1, p2));
				}
			}
		}
	}

	return trees;
}

result dfs(const shared_ptr<node_t> &node, vector<int>::const_iterator &it) {
	if(node->is_leaf()) {
		const result res(*it, 0.0, 0.0);
		++it;
		return res;
	}

	const auto l_result = dfs(node->left_child, it);
	const auto r_result = dfs(node->right_child, it);

	const int weight = get<WEIGHT>(l_result) + get<WEIGHT>(r_result);

	const double left_pos = -static_cast<double>(get<WEIGHT>(r_result)) / weight;
	const double right_pos = static_cast<double>(get<WEIGHT>(l_result)) / weight;

	const double left = min(get<LEFT>(l_result) + left_pos, get<LEFT>(r_result) + right_pos);
	const double right = max(get<RIGHT>(l_result) + left_pos, get<RIGHT>(r_result) + right_pos);

	return result(weight, left, right);
}

void solve(const T_array &trees) {
	double r;
	int s;
	cin >> r >> s;

	vector<int> w(s);
	for(auto &e : w) cin >> e;

	sort(w.begin(), w.end());

	double ans = -1;
	do {
		for(const auto &root : trees[s]) {
			auto it = w.cbegin();
			const auto tmp = dfs(root, it);
			const double width = get<RIGHT>(tmp) - get<LEFT>(tmp);
			if(width < r) chmax(ans, width);
		}

	} while(next_permutation(w.begin(), w.end()));


	if(ans < 0) {
		cout << "-1" << endl;
	}
	else {
		cout << ans << endl;
	}
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(10);

	const auto trees = init_trees();

	int T;
	cin >> T;

	while(T--) solve(trees);

	return EXIT_SUCCESS;
}
accuracy: 1
time_ms: 50
memory_kb: 1312
final_rank: 1

code_snippet: #include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <tuple>
#include <vector>
using namespace std;

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }

struct node_t {
	shared_ptr<node_t> left_child, right_child;

	inline bool is_leaf() const {
		return left_child == nullptr;
	}

	node_t():left_child(nullptr), right_child(nullptr) {}
};

constexpr int MAX_S = 6;

typedef array<vector<shared_ptr<node_t>>, MAX_S + 1> T_array;
typedef tuple<int, double, double> result;
enum { WEIGHT, LEFT, RIGHT };

T_array init_trees() {
	T_array trees;

	trees[1].emplace_back(shared_ptr<node_t>(new node_t));

	for(int s = 2; s <= 6; ++s) {
		for(int l = 1; l < s; ++l) {
			const int r = s - l;

			for(const auto &p1 : trees[l]) {
				for(const auto &p2 : trees[r]) {
					shared_ptr<node_t> p(new node_t);
					p->left_child = p1;
					p->right_child = p2;
					trees[s].emplace_back(p);
				}
			}
		}
	}

	return trees;
}

result dfs(const shared_ptr<node_t> &node, vector<int>::const_iterator &it) {
	if(node->is_leaf()) {
		const result res(*it, 0.0, 0.0);
		++it;
		return res;
	}

	const auto l_result = dfs(node->left_child, it);
	const auto r_result = dfs(node->right_child, it);

	const int weight = get<WEIGHT>(l_result) + get<WEIGHT>(r_result);

	const double left_pos = -static_cast<double>(get<WEIGHT>(r_result)) / weight;
	const double right_pos = static_cast<double>(get<WEIGHT>(l_result)) / weight;

	const double left = min(get<LEFT>(l_result) + left_pos, get<LEFT>(r_result) + right_pos);
	const double right = max(get<RIGHT>(l_result) + left_pos, get<RIGHT>(r_result) + right_pos);

	return result(weight, left, right);
}

void solve(const T_array &trees) {
	double r;
	int s;
	cin >> r >> s;

	vector<int> w(s);
	for(auto &e : w) cin >> e;

	sort(w.begin(), w.end());

	double ans = -1;
	do {
		for(const auto &root : trees[s]) {
			auto it = w.cbegin();
			const auto tmp = dfs(root, it);
			const double width = get<RIGHT>(tmp) - get<LEFT>(tmp);
			if(width < r) chmax(ans, width);
		}

	} while(next_permutation(w.begin(), w.end()));


	if(ans < 0) {
		cout << "-1" << endl;
	}
	else {
		cout << ans << endl;
	}
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(10);

	const auto trees = init_trees();

	int T;
	cin >> T;

	while(T--) solve(trees);

	return EXIT_SUCCESS;
}
accuracy: 1
time_ms: 50
memory_kb: 1320
final_rank: 2