#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 998244354
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

int n;
ll dp[100010][5];
vll a(100010);
string s;
string t="xhard";

void solve()
{
  for(int i=1;i<n;i++) for(int j=1;j<5;j++)
  {
	  if(s[i]==t[j])
	  {
		  if(dp[i][j]!=inf) dp[i][j]+=a[i];
		  else dp[i][j]=min(dp[i-1][j-1],a[i]);

		  dp[i+1][j]=dp[i][j];
	  }
	  else
	  	dp[i][j]=min(dp[i-1][j],dp[i-1][j-1]);

  }
  cout <<(dp[n-1][4]==inf?0:dp[n-1][4])<<endl;
}
int main()
{
	for(int i=0;i<=int(1e5)+5;i++) for(int j=0;j<5;j++) dp[i][j]=inf;
  cin >> n;n++;
  cin >> s;
  s.insert(s.begin(),'x');
  for(int i=1;i<n;i++) cin >> a[i];

  solve();

}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 998244354
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

int n;
ll dp[100005][5];
vll a(10005);
string s;
string t="xhard";

void solve()
{
  for(int i=1;i<n;i++) for(int j=1;j<5;j++)
  {
    if(s[i]==t[j])
    {
      if(dp[i-1][j]!=inf)
        dp[i][j]=dp[i-1][j]+a[i];
      else
        dp[i][j]=min(dp[i-1][j-1],a[i]);
    }
    else
      dp[i][j]=(dp[i-1][j]);
  }
  cout << (dp[n-1][4]==inf?0:dp[n-1][4])<<endl;
}
int main()
{
	for(int i=0;i<=int(1e5)+5;i++) for(int j=0;j<5;j++) dp[i][j]=inf;
  cin >> n;n++;
  cin >> s;
  s.insert(s.begin(),'x');
  for(int i=1;i<n;i++) cin >> a[i];

  solve();

}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 998244354
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

int n;
ll dp[100005][5];
vll a(10005);
string s;
string t="xhard";

void solve()
{
  for(int i=1;i<n;i++) for(int j=1;j<5;j++)
  {
    if(s[i]==t[j])
    {
      if(dp[i-1][j]!=inf)
        dp[i][j]=dp[i-1][j]+a[i];
      else
        dp[i][j]=min(dp[i-1][j-1],a[i]);
    }
    else
      dp[i][j]=(dp[i-1][j]);
  }
  cout << (dp[n-1][4]==inf?0:dp[n-1][4])<<endl;
}
int main()
{
	for(int i=0;i<int(1e5);i++) for(int j=0;j<5;j++) dp[i][j]=inf;
  cin >> n;n++;
  cin >> s;
  s.insert(s.begin(),'x');
  for(int i=1;i<n;i++) cin >> a[i];

  solve();

}


#include <bits/stdc++.h>
#define vi vector<int>
#define ll long long
#define inf 1e18

using namespace std;

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

  string s;
  cin >> s;

  vi a(n);
  ll dp[n+1][5];
  for(int i=0;i<n+1;i++) for(int j=0;j<5;j++) dp[i][j]=inf;

  for(int i=0;i<n;i++) cin >> a[i];
  string hard="hard";
  dp[0][0]=0LL;
  for(int i=0;i<n;i++) for(int j=0;j<4;j++)
  {
    dp[i + 1][j + (s[i] == hard[j])] = min(dp[i + 1][j + (s[i] == hard[j])], dp[i][j]);
		dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
  }
  cout << *min_element(dp[n],dp[n]+4)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim=1e6;
int inf=1e9+7;
ll solve(vector<vector<ll> > &dp,int i,int j,ll* pref,ll* a,int n,string &s,string t){
	if(dp[i][j]!=-1){
		return dp[i][j];
	}else{
		if(j==0){
			return pref[i];
		}else if(i==0){
			return 0;
		}else{
			if(s[i]==t[j]){
				return min(solve(dp,i-1,j,pref,a,n,s,t)+a[i],solve(dp,i-1,j-1,pref,a,n,s,t));
			}else{
				return solve(dp,i-1,j,pref,a,n,s,t);
			}

		}
	}
}
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	string s;cin>>s;
	ll a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	vector<vector<ll> > dp(n);
	for(int i=0;i<n;i++){
		vector<ll> tmp(4,-1);
		dp[i]=tmp;
	}
	ll pref[n];
	if(s[0]=='h'){
		pref[0]=a[0];
	}else{
		pref[0]=0;
	}
	for(int i=1;i<n;i++){
		if(s[i]=='h'){
			pref[i]=pref[i-1]+a[i];
		}else{
			pref[i]=pref[i-1];
		}
	}
	string t="hard";
	cout<<solve(dp,n-1,3,pref,a,n,s,t)<<ln;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim=1e6;
int inf=1e9+7;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	string s;cin>>s;
	ll a1[n];
	for(int i=0;i<n;i++){
		cin>>a1[i];
	}
	ll ans=0;
	bool h=0,a=0,r=0;
	ll hpt=0,apt=0,rpt=0;
	for(int i=0;i<n;i++){
		if(s[i]=='h'){
			h=true;
			hpt+=a1[i];
		}else if(s[i]=='a'){
			if(h){
				a=true;
				apt+=a1[i];
			}
		}else if(s[i]=='r'){
			if(h and a){
				r=true;
				rpt+=a1[i];
			}
		}else if(s[i]=='d'){
			if(h and r and a){
				ll ans1=min(min(min(ans+a1[i],ans+hpt),ans+apt),ans+rpt);
				if(ans1==ans+hpt){
					hpt=0;
					apt=0;rpt=0;
				}else if(ans1==ans+apt){
					apt=0;rpt=0;
				}else if(ans1==ans+rpt){
					rpt=0;
				}
				ans=ans1;
			}
		}
	}
	cout<<ans<<ln;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim=1e6;
int inf=1e9+7;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	string s;cin>>s;
	ll a1[n];
	for(int i=0;i<n;i++){
		cin>>a1[i];
	}
	ll ans=0;
	bool h=0,a=0,r=0;
	ll hpt=0,apt=0,rpt=0;
	for(int i=0;i<n;i++){
		if(s[i]=='h'){
			h=true;
			hpt+=a1[i];
		}else if(s[i]=='a'){
			if(h){
				a=true;
				apt+=a1[i];
			}
		}else if(s[i]=='r'){
			if(h and a){
				r=true;
				rpt+=a1[i];
			}
		}else if(s[i]=='d'){
			if(h and r and a){
				ll ans1=min(min(min(ans+a1[i],ans+hpt),ans+apt),ans+rpt);
				if(ans1==ans+hpt){
					hpt=0;
				}else if(ans1==ans+apt){
					apt=0;
				}else if(ans1==ans+rpt){
					rpt=0;
				}
				ans=ans1;
			}
		}
	}
	cout<<ans<<ln;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim=1e6;
int inf=1e9+7;
ll solve(vector<vector<ll> > &dp,int i,int j,ll* pref,ll* a,int n,string &s,string &t){
	if(dp[i][j]!=-1){
		return dp[i][j];
	}else{
		if(j==0){
			return pref[i];
		}else if(i==0){
			return 0;
		}else{
			if(s[i]==t[j]){
				ll ans= min(solve(dp,i-1,j,pref,a,n,s,t)+a[i],solve(dp,i-1,j-1,pref,a,n,s,t));
				dp[i][j]=ans;
				return ans;
			}else{
				ll ans = solve(dp,i-1,j,pref,a,n,s,t);
				dp[i][j]=ans;
				return ans;
			}

		}
	}
}
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	string s;cin>>s;
	ll a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	vector<vector<ll> > dp(n);
	for(int i=0;i<n;i++){
		vector<ll> tmp(4,-1);
		dp[i]=tmp;
	}
	ll pref[n];
	if(s[0]=='h'){
		pref[0]=a[0];
	}else{
		pref[0]=0;
	}
	for(int i=1;i<n;i++){
		if(s[i]=='h'){
			pref[i]=pref[i-1]+a[i];
		}else{
			pref[i]=pref[i-1];
		}
	}
	string t="hard";
	cout<<solve(dp,n-1,3,pref,a,n,s,t)<<ln;
}


#include <iostream>
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e6 + 100;

int dp[N][10];

signed main(void){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 10; ++j){
            dp[i][j] = 1e18;
        }
    }
    string s;
    const string t = "hard";
    cin >> s;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= 3; ++j){
            dp[i + 1][j + (s[i] == t[j])] = min(dp[i + 1][j + (s[i] == t[j])], dp[i][j]);
		    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        }
    }
    int ans = 1e18;
    for(int j = 0; j <= 3; ++j){
        ans = min(ans, dp[n][j]);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e6 + 100;

int dp[N][10];

signed main(void){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 10; ++j){
            dp[i][j] = 1e18;
        }
    }
    string s;
    const string t = "hard";
    cin >> s;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= 3; ++j){
            dp[i + 1][j + (s[i] == t[j])] = min(dp[i + 1][j + (s[i] == t[j])], dp[i][j]);
		    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        }
    }
    int ans = 1e18;
    for(int j = 0; j <= 3; ++j){
        ans = min(ans, dp[n - 1][j]);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e5 + 100;

int dp[N][10];

signed main(void){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 10; ++j){
            dp[i][j] = 1e15;
        }
    }
    string s, t = "hard";
    cin >> s;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= 3; ++j){
            dp[i + 1][j + (s[i] == t[j])] = min(dp[i + 1][j + (s[i] == t[j])], dp[i][j]);
		    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        }
    }
    int ans = 1e18;
    for(int j = 0; j <= 4; ++j){
        ans = min(ans, dp[n - 1][j]);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e5 + 100;

int dp[N][10];

signed main(void){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 10; ++j){
            dp[i][j] = 1e15;
        }
    }
    string s, t = "hard";
    cin >> s;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= 3; ++j){
            dp[i + 1][j + (s[i] == t[j])] = min(dp[i + 1][j + (s[i] == t[j])], dp[i][j]);
		    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        }
    }
    int ans = 1e15;
    for(int j = 0; j < 4; ++j){
        ans = min(ans, dp[n - 1][j]);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e5 + 100;

int dp[N][10];

signed main(void){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 10; ++j){
            dp[i][j] = 1e18;
        }
    }
    string s, t = "hard";
    cin >> s;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= 3; ++j){
            dp[i + 1][j + (s[i] == t[j])] = min(dp[i + 1][j + (s[i] == t[j])], dp[i][j]);
		    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        }
    }
    int ans = 1e18;
    for(int j = 0; j < 4; ++j){
        ans = min(ans, dp[n - 1][j]);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e5 + 100;

int dp[N][10];

signed main(void){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 10; ++j){
            dp[i][j] = 1e18;
        }
    }
    string s;
    const string t = "hard";
    cin >> s;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= 3; ++j){
            dp[i + 1][j + (s[i] == t[j])] = min(dp[i + 1][j + (s[i] == t[j])], dp[i][j]);
		    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        }
    }
    int ans = 1e18;
    for(int j = 0; j <= 3; ++j){
        ans = min(ans, dp[n - 1][j]);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e6 + 100;

int dp[N][10];

signed main(void){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < 10; ++j){
            dp[i][j] = 1e18;
        }
    }
    string s;
    const string t = "hard";
    cin >> s;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= 3; ++j){
            dp[i + 1][j + (s[i] == t[j])] = min(dp[i + 1][j + (s[i] == t[j])], dp[i][j]);
		    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        }
    }
    int ans = 1e18;
    for(int j = 0; j <= 3; ++j){
        ans = min(ans, dp[n][j]);
    }
    cout << ans << endl;
    return 0;
}


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

using ll = unsigned long long;
using vi = vector<int>;

const int MAX= 1e5+10;
int a[MAX];
char S[MAX];

char w[5] = "hard";

ll p[4][MAX];

int main(){
	int n;
	scanf("%d",&n);
	scanf("%s",S);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( S[i-1] == w[l-1]){
				if( l > 1) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1] + a[i];
			}
			else p[l][i] = p[l][i-1];
		}
	}
	cout << p[4][n] << endl;
}


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

using ll = unsigned long long;
using vi = vector<int>;

const int MAX= 1e5+10;
ll a[MAX];
char S[MAX];

char w[5] = "hard";

ll p[4][MAX];

int main(){
	int n;
	scanf("%d",&n);
	scanf("%s",S);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( S[i-1] == w[l-1]){
				if( l > 1) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1] + a[i];
			}
			else p[l][i] = p[l][i-1];
		}
	}
	cout << p[4][n] << endl;
}


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

using ll = long long;
using vi = vector<int>;

const int MAX= 1e5+10;
int a[MAX];
string S;

char w[6] = "hard";

ll p[5][MAX];

int main(){
	int n;
	cin >> n;
	cin >> S;
	for(int i=1;i<=n;i++){
		cin >> a[i];
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( S[i-1] == w[l-1]){
				if( l > 1) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1] + a[i];
			}
			else p[l][i] = p[l][i-1];
		}
	}
	cout << p[4][n] << endl;
}


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

using ll = long long;
using vi = vector<int>;

const int MAX= 1e5+10;
int a[MAX];
string S;

char w[6] = "hard";

ll p[4][MAX];

int main(){
	int n;
	cin >> n;
	cin >> S;
	for(int i=1;i<=n;i++){
		cin >> a[i];
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( S[i-1] == w[l-1]){
				if( l > 1) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1] + a[i];
			}
			else p[l][i] = p[l][i-1];
		}
	}
	cout << p[4][n] << endl;
}


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

using ll = unsigned long long;
using vi = vector<int>;

const int MAX= 1e5+10;
int a[MAX];
char S[MAX];

char w[5] = "hard";

ll p[4][MAX];

int main(){
	int n;
	scanf("%d",&n);
	scanf("%s",S);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( S[i-1] == w[l-1]){
				if( l > 1) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1] + a[i];
			}
			else p[l][i] = p[l][i-1];
		}
	}
	cout << p[4][n] << endl;
}


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

using ll = long long;
using vi = vector<int>;

const int MAX= 1e5+10;
int a[MAX];
char S[MAX];

char w[5] = "hard";

ll p[4][MAX];

int main(){
	int n;
	scanf("%d",&n);
	scanf("%s",S);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( l > 1){
				if( S[i-1] == w[l-1]) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1];
			}
			if( l == 1){
				if( S[i-1] == w[0] ) p[l][i] = p[l][i-1] + a[i];
				else p[l][i] = p[l][i-1];
			}
		}
	}

	printf("%lld\n",p[4][n]);
}


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

using ll = long long;
using vi = vector<int>;

const int MAX= 1e5+10;
int a[MAX];
char S[MAX];

char w[5] = "hard";

int p[4][MAX];

int main(){
	int n;
	scanf("%d",&n);
	scanf("%s",S);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( l > 1){
				if( S[i-1] == w[l-1]) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1];
			}
			if( l == 1){
				if( S[i-1] == w[0] ) p[l][i] = p[l][i-1] + a[i];
				else p[l][i] = p[l][i-1];
			}
		}
	}

	printf("%lld\n",p[4][n]);
}


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

using ll = unsigned long long;
using vi = vector<int>;

const int MAX= 1e5+10;
int a[MAX];
string S;

char w[5] = "hard";

ll p[4][MAX];

int main(){
	int n;
	cin >> n;
	cin >> S;
	for(int i=1;i<=n;i++){
		cin >> a[i];
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( S[i-1] == w[l-1]){
				if( l > 1) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1] + a[i];
			}
			else p[l][i] = p[l][i-1];
		}
	}
	cout << p[4][n] << endl;
}


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

using ll = long long;
using vi = vector<int>;

const int MAX= 1e5+10;
ll a[MAX];
char S[MAX];

char w[5] = "hard";

ll p[4][MAX];

int main(){
	int n;
	scanf("%d",&n);
	scanf("%s",S);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	for(int l = 1;l<=4;l++){
		for(int i=1;i<=n;i++){
			if( S[i-1] == w[l-1]){
				if( l > 1) p[l][i] = min(p[l-1][i-1],p[l][i-1] + a[i]);
				else p[l][i] = p[l][i-1] + a[i];
			}
			else p[l][i] = p[l][i-1];
		}
	}

	printf("%lld\n",p[4][n]);
}


  #include <iostream>
#include <list>
#include <algorithm>
#include <map>
#include <math.h>

#include <set>
#include <string>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#include <vector>
#include <cstdlib>
#include <stack>
#include <ctime>
#define lld long long int
#define rep(i,start,end) for(lld i = start;i<end;i++)
#define newline cout << endl
#define fastio ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define in insert
#define mp make_pair
#define se second
#define fi first
#define starttime chrono::time_point<chrono::system_clock> start, end; start = chrono::system_clock::now()
#define showtime end = chrono::system_clock::now();  chrono::duration<double> elapsed_seconds = end - start; time_t end_time = chrono::system_clock::to_time_t(end); cout << "elapsed time: " << elapsed_seconds.count() << "s\n"
#define INF 1e16+1
#define MOD (lld)(1e9+7)
#define mh make heap
#define ph push_heap
#define poph pop_heap
#define openf   freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
using namespace std;
lld add(long long a, long long b){return ((a%MOD)+(b%MOD))%MOD;}
lld mul(long long a, long long b){return ((a%MOD)*(b%MOD))%MOD;}
lld modInverse(lld a){   lld m = MOD,m0 = m, y = 0, x = 1;  if (m == 1) return 0;   while (a > 1) {  lld q = a / m, t = m;  m = a % m, a = t, t = y;          y = x - q * y, x = t;   } if (x < 0)  x += m0; return x; }
lld gcd(lld a, lld b){if (b == 0) return a; return gcd(b, a % b);}
void MAIN();
int get(){lld t;cin>>t; return t;}
int log(string name){cout<<name<<endl; return 0;}
void test(int t) { while(t--){MAIN();} }
#define all(v) (v).begin(),(v).end()
string s;
lld dp[100010][10];
vector<lld> cost;
lld n;
int solve(int i,int flag,int pos1,int pos2, int pos3,int pos4)
{
    if(dp[i][flag]!=-1)
        return dp[i][flag];
    if(i==n)
        return 0;
    else
    {
        if(s[i]=='h'&&flag==0)
        {
            flag = 1;
            pos1 = i;
        }
        else if(s[i]=='a'&&flag==1)
        {
            flag = 2;
            pos2 = i;
        }
        else if(s[i]=='r'&&flag==2)
        {
            flag = 3;
            pos3 = i;
        }
        else if(s[i]=='d'&&flag==3)
        {
            flag = 4;
            pos4 = i;
        }
    }
    if(flag!=4)
    return dp[i][flag] = solve(i+1,flag,pos1,pos2,pos3,pos4);
    else
    {
        lld a = solve(pos1+1,0,pos1,pos2,pos3,pos4)+cost[pos1];
        lld b = solve(pos2+1,1,pos2,pos1,pos3,pos4)+cost[pos2];
        lld c = solve(pos3+1,2,pos1,pos2,pos3,pos4)+cost[pos3];
        lld d = solve(pos4+1,3,pos1,pos2,pos3,pos4)+cost[pos4];
        return dp[i][flag] = min(a,min(min(b,c),d));
    }
}
void MAIN(){
    cin >> n >> s;
    cost.resize(n);
    for(lld i = 0;i<n;i++)
        cin >> cost[i];
    for(lld i = 0;i<100000;i++)
            for(lld j = 0;j<5;j++)
                dp[i][j] = -1;
    cout << solve(0,0,0,0,0,0);
}

int main(){
    fastio;
    test(1);
    return 0;
}


  #include <iostream>
#include <list>
#include <algorithm>
#include <map>
#include <math.h>

#include <set>
#include <string>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#include <vector>
#include <cstdlib>
#include <stack>
#include <ctime>
#define lld long long int
#define rep(i,start,end) for(lld i = start;i<end;i++)
#define newline cout << endl
#define fastio ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define in insert
#define mp make_pair
#define se second
#define fi first
#define starttime chrono::time_point<chrono::system_clock> start, end; start = chrono::system_clock::now()
#define showtime end = chrono::system_clock::now();  chrono::duration<double> elapsed_seconds = end - start; time_t end_time = chrono::system_clock::to_time_t(end); cout << "elapsed time: " << elapsed_seconds.count() << "s\n"
#define INF 1e16+1
#define MOD (lld)(1e9+7)
#define mh make heap
#define ph push_heap
#define poph pop_heap
#define openf   freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
using namespace std;
lld add(long long a, long long b){return ((a%MOD)+(b%MOD))%MOD;}
lld mul(long long a, long long b){return ((a%MOD)*(b%MOD))%MOD;}
lld modInverse(lld a){   lld m = MOD,m0 = m, y = 0, x = 1;  if (m == 1) return 0;   while (a > 1) {  lld q = a / m, t = m;  m = a % m, a = t, t = y;          y = x - q * y, x = t;   } if (x < 0)  x += m0; return x; }
lld gcd(lld a, lld b){if (b == 0) return a; return gcd(b, a % b);}
void MAIN();
int get(){lld t;cin>>t; return t;}
int log(string name){cout<<name<<endl; return 0;}
void test(int t) { while(t--){MAIN();} }
#define all(v) (v).begin(),(v).end()
string s;
lld dp[100010][7];
vector<lld> cost;
lld n;
int solve(int i,int flag,int pos1,int pos2, int pos3,int pos4)
{
    if(i==n)
        return 0;
    if(dp[i][flag]!=-1)
        return dp[i][flag];
    else
    {
        if(s[i]=='h'&&flag==0)
        {
            flag = 1;
            pos1 = i;
        }
        else if(s[i]=='a'&&flag==1)
        {
            flag = 2;
            pos2 = i;
        }
        else if(s[i]=='r'&&flag==2)
        {
            flag = 3;
            pos3 = i;
        }
        else if(s[i]=='d'&&flag==3)
        {
            flag = 4;
            pos4 = i;
        }
    }
    if(flag!=4)
    return dp[i][flag] = solve(i+1,flag,pos1,pos2,pos3,pos4);
    else
    {

        if(pos1<pos2<pos3<pos3)
    {        lld a = solve(pos1+1,0,pos1,pos2,pos3,pos4)+cost[pos1];
            lld b = solve(pos2+1,1,pos2,pos1,pos3,pos4)+cost[pos2];
            lld c = solve(pos3+1,2,pos1,pos2,pos3,pos4)+cost[pos3];
            lld d = solve(pos4+1,3,pos1,pos2,pos3,pos4)+cost[pos4];
            return dp[i][flag] = min(a,min(min(b,c),d));}
    }
}
void MAIN(){
    cin >> n >> s;
    cost.resize(n);
    for(lld i = 0;i<n;i++)
        cin >> cost[i];
    for(lld i = 0;i<100000;i++)
            for(lld j = 0;j<6;j++)
                dp[i][j] = -1;
    cout << solve(0,0,0,0,0,0);
}

int main(){
    fastio;
    test(1);
    return 0;
}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0;
    unsigned int min = -1,remainder=0;

    scanf("%d",&n);

    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);

        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }

    }

    if(min > n){
        printf("-1");
    }else {

        liters = n;
        remainder = liters % min;
        if( remainder == 0 )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {

            liters = n;
            int i=8;
            while(i >= 0  && remainder > 0  && liters > min) {
                if( (v[i] >=  min) && (v[i] - min) <= remainder) {

                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                    printf("%u",i+1);

                }else {
                    i--;
                }
            }
            while(liters >= min ){

                liters = liters - min;

                printf("%u",min_num);
            }

        }
    }

}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(System.in);
		int lit = Integer.parseInt(scanner.nextLine());
		String s = scanner.nextLine();
		String snum[] = s.split(" ");
		ArrayList<Integer> num = new ArrayList<Integer>();
		for(int i=0;i<snum.length;i++){
			int d = Integer.parseInt(snum[i]);
			num.add(d);
		}

		Integer min_lit = num.get(0);
		int digit = -1;
		for(int i=0;i<num.size();i++){
			int lit_to_write_digit = num.get(i);
			if(min_lit > lit_to_write_digit){
				min_lit = lit_to_write_digit;
				digit = i+1;
			}else if(min_lit == lit_to_write_digit){
				if(digit < i+1){
					min_lit = lit_to_write_digit;
					digit = i+1;
				}
			}
		}

		int itr = lit/min_lit;
		int rem = lit%min_lit;
		StringBuilder sb = new StringBuilder();
		if(itr == 0 && rem < min_lit){
			sb.append(-1);
		}else if(itr > 0 && rem ==0){
			for(int i=itr;i>0;i--){
				sb.append(digit);
			}
		}else if(rem > 0){
			ArrayList<Integer> t = (ArrayList<Integer>) num.clone();
			Collections.sort(t);
			Integer match = null;
			Integer matchPos = null;
			for(int i=t.size()-1;i>0;i--){
				if(lit > t.get(i)){
				  match = t.get(i);
				  matchPos = i;
				  break;
				}
			}
			while(lit > 0){
				match = t.get(matchPos);
				int val = num.indexOf(match);
				sb.append(val+1);
				lit = lit - match;
				matchPos--;
			}

		}

		System.out.println(sb.toString());

	}
}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
    }

    if(min > n){
        printf("%d",-1);
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }else {
            for(int j=8; j >= 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (n - v[last_digit]);
            n = liters;
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }
    }

}


main()
{
    int v=0,liters=0, n[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&v);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j;
        }
    }

    if(min > v){
        printf("-1");
    }else {
        liters = v;
        if( liters % min == 0)
        {
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }else {
            for(j=9; j > 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = liters - v[last_digit];
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
            flag = 1;
        }else {
        int max_nums=0,maxnum_index=0;
        for(int i=0; i <= 8; i--)
        {

            if( liters % v[i] == 0 && max_nums <= liters / v[i])
            {
                max_nums = liters / v[i];
                maxnum_index = i;
                flag=1;
            }
        }
             while(liters >=  v[maxnum_index] && flag)
            {
                    printf("%d",maxnum_index+1);
                    liters = (liters - v[maxnum_index]);
            }
        }

        }
        if(flag == 0)
        {
            digit_index = 8;
         do {
            for(int j=digit_index; j >= 0 ; j--)
            {
                if( (min + diff)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
            if(digit_index > 0)
            digit_index--;
         }while(liters >= min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=0;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    diff = max - min;

    if(min > n){
        printf("%d",-1);
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
            flag = 1;
        }else {
        int max_nums=0,maxnum_index=0;
        for(int i=0; i <= 8; i--)
        {

            if( liters % v[i] == 0 && max_nums <= liters / v[i])
            {
                max_nums = liters / v[i];
                maxnum_index = i;
                flag=1;
            }
        }
             while(liters >=  v[maxnum_index] && flag)
            {
                    printf("%d",maxnum_index+1);
                    liters = (liters - v[maxnum_index]);
            }
        }

        }
        if(flag == 0)
        {
            digit_index = 8;
         do {
            for(int j=digit_index; j >= 0 ; j--)
            {
                if( (min + diff)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
            if(digit_index > 0)
            digit_index--;
         }while(liters >= min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    diff = max - min;

    if(min > n){
        printf("%d",-1);
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }else {
            digit_index = 8;
         do {
            for(int j=digit_index; j >= 0 ; j--)
            {
                if( (min + diff)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
            if(digit_index > 0)
            digit_index--;
         }while(liters >= min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2,remainder=0,sorted_array[9] = {'\0'};
    unsigned num_digits[100000],max_digits=0;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        sorted_array[j] = v[j];
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    int second_min =0;
    for(int i=0; i < 9; i++)
    {

        for(int j=0; j < 9; j++)
        {
            if(sorted_array[i] < sorted_array[j]) {
                int tmp = sorted_array[j];
                sorted_array[j] = sorted_array[i];
                sorted_array[i] = tmp;
            }
        }
        if(sorted_array[i] > second_min)
        {
                second_min = sorted_array[i];
        }
    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        remainder = liters % min;
        if( remainder == 0 || (sorted_array[1] - second_min) > remainder )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {
            liters = n;
            int i=0;
            while(i <= 8 && remainder > 0  && liters > 0) {
                if((v[i] - min) <= remainder) {
                    num_digits[max_digits++] = i+1;
                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                }else {
                    i++;
                }
            }
            while(liters > min ){
                  num_digits[max_digits++] = min_num;
                liters = liters - min;
            }

    for(int i=0; i < max_digits; i++)
    {

        for(int j=0; j < max_digits; j++)
        {
            if(num_digits[i] > num_digits[j]) {
                int tmp = num_digits[j];
                num_digits[j] = num_digits[i];
                num_digits[i] = tmp;
            }
        }
        printf("%u",num_digits[i]);
    }

        }
    }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
    }

    if(min > n){
        printf("%d",-1);
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }else {
         do {
            for(int j=8; j >= 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] && liters > v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
         }while(liters > min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }
    }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j;
        }
    }

    if(min > v){
        printf("-1");
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }else {
            for(int j=9; j > 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = liters - v[last_digit];
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
            flag = 1;
        }else {
        int max_nums=0,maxnum_index=0;
        for(int i=0; i <= 8; i++)
        {

            if( liters % v[i] == 0 && max_nums <= liters / v[i])
            {
                max_nums = liters / v[i];
                maxnum_index = i;
                flag=1;
            }
        }
             while(liters >=  v[maxnum_index] && flag == 1)
            {
                    printf("%d",maxnum_index+1);
                    liters = (liters - v[maxnum_index]);
            }
        }

        }
        if(flag == 0)
        {
            digit_index = 8;
         do {
            for(int j=digit_index; j >= 0 ; j--)
            {
                if( (min + diff)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
            if(digit_index > 0)
            digit_index--;
         }while(liters >= min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
    }

    if(min > v){
        printf("-1");
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }else {
            for(int j=8; j >= 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (n - v[last_digit]);
            n = liters;
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }
    }

}
main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
    }

    if(min > v){
        printf("%d",-1);
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }else {
            for(int j=8; j >= 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (n - v[last_digit]);
            n = liters;
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }
    }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
    }

    if(min > v){
        printf("-1");
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }else {
            for(int j=8; j >= 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (n - v[last_digit]);
            n = liters;
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }
    }

}


main()
{
    int v=0,liters=0 n[9] = {'\0'},r[9] = {''\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&v);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j;
        }
    }

    if(min > v){
        printf("-1");
    }else {
        liters = v;
        if( liters % min == 0)
        {
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }else {
            for(j=9; j > 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = liters - v[last_digit];
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2,remainder=0,sorted_array[9] = {'\0'};
    unsigned num_digits[100000],max_digits=0;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        sorted_array[j] = v[j];
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    int second_min =0;
    for(int i=0; i < 9; i++)
    {

        for(int j=0; j < 9; j++)
        {
            if(sorted_array[i] < sorted_array[j]) {
                int tmp = sorted_array[j];
                sorted_array[j] = sorted_array[i];
                sorted_array[i] = tmp;
            }
        }
        if(sorted_array[i] > second_min)
        {
                second_min = sorted_array[i];
        }
    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        remainder = liters % min;
        if( remainder == 0 || (sorted_array[1] - second_min) > remainder )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {
            liters = n;
            while(i <= 8 && remainder > 0  && liters > 0) {
                if((v[i] - min) <= remainder) {
                    num_digits[max_digits++] = i+1;
                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                }else {
                    i++;
                }
            }
            while(liters > min ){
                  num_digits[max_digits++] = min_num;
                liters = liters - min;
            }

    for(int i=0; i < max_digits; i++)
    {

        for(int j=0; j < max_digits; j++)
        {
            if(num_digits[i] > num_digits[j]) {
                int tmp = num_digits[j];
                num_digits[j] = num_digits[i];
                num_digits[i] = tmp;
            }
        }
        printf("%u",num_digits[i]);
    }

        }

}


main()
{
    int v=0,liters=0 n[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&v);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j;
        }
    }

    if(min > v){
        printf("-1");
    }else {
        liters = v;
        if( liters % min == 0)
        {
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }else {
            for(j=9; j > 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = liters - v[last_digit];
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = liters - min;
            }
        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2,remainder=0,sorted_array[9] = {'\0'};
    unsigned num_digits[100000],max_digits=0;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        sorted_array[j] = v[j];
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    int second_min =0;
    for(int i=0; i < 9; i++)
    {

        for(int j=0; j < 9; j++)
        {
            if(sorted_array[i] > sorted_array[j]) {
                int tmp = sorted_array[j];
                sorted_array[j] = sorted_array[i];
                sorted_array[i] = tmp;
            }
        }
        if(sorted_array[i] > second_min)
        {
                second_min = sorted_array[i];
        }
    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        remainder = liters % min;
        if( remainder == 0 )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {
            liters = n;
            int i=8;
            while(i >= 0  && remainder > 0  && liters > 0) {
                if((v[i] - min) <= remainder) {
                    num_digits[max_digits++] = i+1;
                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                }else {
                    i--;
                }
            }
            while(liters > min ){
                  num_digits[max_digits++] = min_num;
                liters = liters - min;
            }

    for(int i=0; i < max_digits; i++)
    {

        for(int j=0; j < max_digits; j++)
        {
            if(num_digits[i] < num_digits[j]) {
                int tmp = num_digits[j];
                num_digits[j] = num_digits[i];
                num_digits[i] = tmp;
            }
        }
        printf("%u",num_digits[i]);
    }

        }
    }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    diff = max - min;

    if(min > n){
        printf("%d",-1);
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
        }else {
            digit_index = 8;
         do {
            for(int j=digit_index; j >= 0 ; j--)
            {
                if( (min + diff)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
            if(digit_index > 0)
            digit_index--;
         }while(liters > min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2,remainder=0,sorted_array[9] = {'\0'};
    unsigned num_digits[100000],max_digits=0;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        sorted_array[j] = v[j];
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    int second_min =0;
    for(int i=0; i < 9; i++)
    {

        for(int j=0; j < 9; j++)
        {
            if(sorted_array[i] > sorted_array[j]) {
                int tmp = sorted_array[j];
                sorted_array[j] = sorted_array[i];
                sorted_array[i] = tmp;
            }
        }
        if(sorted_array[i] > second_min)
        {
                second_min = sorted_array[i];
        }
    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        remainder = liters % min;
        if( remainder == 0 )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {
            liters = n;
            int i=8;
            while(i >= 0  && remainder > 0  && liters > 0) {
                if( (v[i] >  min) && (v[i] - min) <= remainder) {
                    num_digits[max_digits++] = i+1;
                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                }else {
                    i--;
                }
            }
            while(liters > min ){
                  num_digits[max_digits++] = min_num;
                liters = liters - min;
            }

    for(int i=0; i < max_digits; i++)
    {

        for(int j=0; j < max_digits; j++)
        {
            if(num_digits[i] < num_digits[j]) {
                int tmp = num_digits[j];
                num_digits[j] = num_digits[i];
                num_digits[i] = tmp;
            }
        }
        printf("%u",num_digits[i]);
    }

        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
            flag = 1;
        }else {
        int max_nums=0,maxnum_index=0;
        for(int i=0; i <= 8; i++)
        {

            if( liters % v[i] == 0 && max_nums <= liters / v[i])
            {
                max_nums = liters / v[i];
                maxnum_index = i;
                flag=1;
            }
        }
             while(liters >=  v[maxnum_index] && flag == 1)
            {
                    printf("%d",maxnum_index+1);
                    liters = (liters - v[maxnum_index]);
            }
            if(flag == 0 && diff >= min-1) {
                 while(liters >=  min)
                {
                    printf("%d",min_num);
                    liters = (n - min);
                    n = liters;
                }
               flag = 1;

            }
        }

        }
        if(flag == 0)
        {
            digit_index = 8;
         do {
            for(int j=digit_index; j >= 0 ; j--)
            {
                if( (min + diff)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
            if(digit_index > 0)
            digit_index--;
         }while(liters >= min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        if( liters % min == 0)
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }
            flag = 1;
        }else {
        int max_nums=0,maxnum_index=0;
        for(int i=0; i <= 8; i++)
        {

            if( liters % v[i] == 0 && max_nums <= liters / v[i])
            {
                max_nums = liters / v[i];
                maxnum_index = i;
                flag=1;
            }
        }
             while(liters >=  v[maxnum_index] && flag == 1)
            {
                    printf("%d",maxnum_index+1);
                    liters = (liters - v[maxnum_index]);
            }
            if(flag == 0 && diff > min-1) {
                 while(liters >=  min)
                {
                    printf("%d",min_num);
                    liters = (n - min);
                    n = liters;
                }
               flag = 1;

            }
        }

        }
        if(flag == 0)
        {
            digit_index = 8;
         do {
            for(int j=digit_index; j >= 0 ; j--)
            {
                if( (min + diff)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit-1]);
            if(digit_index > 0)
            digit_index--;
         }while(liters >= min);

            /*
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (n - min);
                n = liters;
            }  */
        }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2,remainder=0,sorted_array[9] = {'\0'};
    unsigned num_digits[100000],max_digits=0;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        sorted_array[j] = v[j];
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    int second_min =0,flag=0;
    for(int i=0; i < 9; i++)
    {

        for(int j=0; j < 9; j++)
        {
            if(sorted_array[i] > sorted_array[j]) {
                int tmp = sorted_array[j];
                sorted_array[j] = sorted_array[i];
                sorted_array[i] = tmp;
            }
        }
        if(sorted_array[i] > second_min)
        {
                second_min = sorted_array[i];
        }
    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        remainder = liters % min;
        if( remainder == 0 )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {

            liters = n;
            int i=8;
            while(i >= 0  && remainder > 0  && liters > min) {
                if( (v[i] >=  min) && (v[i] - min) <= remainder) {
                    num_digits[max_digits] = i+1;
                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                    max_digits++;
                    flag = 1;
                }else {
                    i--;
                }
            }
            while(liters >= min ){
                num_digits[max_digits] = min_num;
                liters = liters - min;
                max_digits++;
            }

    for(int i=0; i < max_digits; i++)
    {
       /*
        for(int j=i; j < max_digits; j++)
        {
            if(num_digits[i] < num_digits[j]) {
                int tmp = num_digits[j];
                num_digits[j] = num_digits[i];
                num_digits[i] = tmp;
            }
        }  */
        printf("%u",num_digits[i]);
    }

        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2,remainder=0,sorted_array[9] = {'\0'};
    unsigned num_digits[100000],max_digits=0;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        sorted_array[j] = v[j];
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    int second_min =0;
    for(int i=0; i < 9; i++)
    {

        for(int j=0; j < 9; j++)
        {
            if(sorted_array[i] > sorted_array[j]) {
                int tmp = sorted_array[j];
                sorted_array[j] = sorted_array[i];
                sorted_array[i] = tmp;
            }
        }
        if(sorted_array[i] > second_min)
        {
                second_min = sorted_array[i];
        }
    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        remainder = liters % min;
        if( remainder == 0 )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {
            liters = n;
            int i=8;
            while(i >= 0  && remainder > 0  && liters > 0) {
                if( (v[i] >  min) && (v[i] - min) <= remainder) {
                    num_digits[max_digits] = i+1;
                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                    printf("num digits is %u\n",num_digits[max_digits]);
                    max_digits++;

                }else {
                    i--;
                }
            }
            while(liters > min ){
                  num_digits[max_digits++] = min_num;
                liters = liters - min;
            }

    for(int i=0; i < max_digits; i++)
    {

        for(int j=0; j < max_digits; j++)
        {
            if(num_digits[i] < num_digits[j]) {
                int tmp = num_digits[j];
                num_digits[j] = num_digits[i];
                num_digits[i] = tmp;
            }
        }
        printf("%u",num_digits[i]);
    }

        }
    }

}


main()
{
    int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0;
    unsigned int min = -1;

    scanf("%d",&n);
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
    }

    if(min > v){
        printf("-1");
    }else {
        liters = n;
        if( liters % min == 0)
        {
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = (liters - min);
            }
        }else {
            for(int j=8; j >= 0 ; j--)
            {
                if( (2 * min - 1)  >= v[j] ) {
                    last_digit = j+1;
                    break;
                }
            }
            printf("%d",last_digit);
            liters = (liters - v[last_digit]);
            while(liters-min >= 0)
            {
                printf("%d",min_num);
                liters = (liters - min);
            }
        }
    }

}


main()
{
    unsigned int n=0,liters=0, v[9] = {'\0'},min_num=0,last_digit=0,max=0,max_num=0,diff=0;
    unsigned int min = -1,digit_index=0,flag=2,remainder=0,sorted_array[9] = {'\0'};
    unsigned num_digits[100000],max_digits=0;

    scanf("%d",&n);
    max = v[0];
    for(int j=0; j < 9; j++)
    {
        scanf("%d", &v[j]);
        sorted_array[j] = v[j];
        if(v[j] <= min)
        {
            min = v[j];
            min_num = j+1;
        }
        if(v[j] >= max)
        {
            max = v[j];
            max_num = j+1;
        }

    }
    int second_min =0;
    for(int i=0; i < 9; i++)
    {

        for(int j=0; j < 9; j++)
        {
            if(sorted_array[i] > sorted_array[j]) {
                int tmp = sorted_array[j];
                sorted_array[j] = sorted_array[i];
                sorted_array[i] = tmp;
            }
        }
        if(sorted_array[i] > second_min)
        {
                second_min = sorted_array[i];
        }
    }

    diff = max - min;

    if(min > n){
        printf("-1");
    }else {
        flag=0;
        liters = n;
        remainder = liters % min;
        if( remainder == 0 )
        {
            while(liters >=  min)
            {
                printf("%d",min_num);
                liters = (liters - min);

            }

        }else {

            liters = n;
            int i=8;
            while(i >= 0  && remainder > 0  && liters > min) {
                if( (v[i] >=  min) && (v[i] - min) <= remainder) {
                 //   num_digits[max_digits] = i+1;
                    remainder = remainder - (v[i] - min);
                    liters = liters - v[i];
                    printf("%u",i+1);
           //         max_digits++;
                    flag = 1;
                }else {
                    i--;
                }
            }
            while(liters >= min ){
          //      num_digits[max_digits] = min_num;
                liters = liters - min;
          //      max_digits++;
                printf("%u",min_num);
            }

 //   for(int i=0; i < max_digits; i++)

       /*
        for(int j=i; j < max_digits; j++)
        {
            if(num_digits[i] < num_digits[j]) {
                int tmp = num_digits[j];
                num_digits[j] = num_digits[i];
                num_digits[i] = tmp;
            }
        }  */
   //     printf("%u",num_digits[i]);

        }
    }

}


#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
int t,x;
//scanf("%d",&t);
//for(x=0;x<t;x++){
int In,A[10],i,min=1000000000,j;
scanf("%d",&In);
for(i=1;i<=9;i++){
   scanf("%d",&A[i]);
   if(i==1)min=i;
if(A[i]<=A[min])min=i;
}
if(A[min]>In){
 printf("-1");
 //break;
return 0;
    }

int len=i,Pleft=In-A[min]*len,minTemp=min;
    for(i=0;i<In/A[min];i++){
    for(j=9;j>minTemp;j--)
if(A+A[minTemp]-A[j]>=0)
    Pleft=Pleft+A[minTemp]-A[j];
    minTemp = j;
    printf("%d",minTemp);
			}

			printf("\n");

//}
return 0;
}


#include<stdio.h>

int main(){
/*int t,x;
scanf("%d",&t);
for(x=0;x<t;x++){*/
int In,i,A[10]={0},len,min=1000000,minTemp;
scanf("%d",&In);

    for(i=1;i<=9;i++){
        scanf("%d",&A[i]);
        if(A[i]<=min)
            min=A[i];
    }

    len=In/min;
    if(In<min)
        printf("-1");

else{
    while(len--){
        for(i=9;i>0;i--){
            if(((In-A[i])/min)==len && In>=A[i]){
                printf("%d",i);
                In=In-A[i];
                break;
            }
        //}

    }}
}

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
int t,x;
//scanf("%d",&t);
//for(x=0;x<t;x++){
int In,A[10],i,min=1000000,j;
scanf("%d",&In);
for(i=1;i<=9;i++){
   scanf("%d",&A[i]);
   if(i==1)min=i;
if(A[i]<=A[min])min=i;
}
if(min>In){
 printf("-1");
 //break;
return 0;
    }

int len=i,Pleft=In-A[min]*len,minTemp=min;
    for(i=0;i<In/A[min];i++){
    for(j=9;j>minTemp;j--)
if(A+A[minTemp]-A[j]>=0)
    Pleft=Pleft+A[minTemp]-A[j];
    minTemp = j;
    printf("%d",minTemp);
			}

			printf("\n");

//}
return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
int t,x;
//scanf("%d",&t);
//for(x=0;x<t;x++){
int In,A[10],i,min=1000000,j;
scanf("%d",&In);
for(i=1;i<=9;i++){
   scanf("%d",&A[i]);
   if(i==1)min=i;
if(A[i]<=A[min])min=i;
}
if(A[min]>In){
 printf("-1");
 //break;
return 0;
    }

int len=i,Pleft=In-A[min]*len,minTemp=min;
    for(i=0;i<In/A[min];i++){
    for(j=9;j>minTemp;j--)
if(A+A[minTemp]-A[j]>=0)
    Pleft=Pleft+A[minTemp]-A[j];
    minTemp = j;
    printf("%d",minTemp);
			}

			printf("\n");

//}
return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int litros,i,menor=0,posmenor=0;
    int  v[9];

    scanf("%d",&litros);
    menor=litros;
    for(i=0; i<9; i++){
            scanf("%d",&v[i]);
            if(v[i]<=menor && v[i]!=0){
                    menor=v[i];
                    posmenor=i;
                }

    }
    if(v[posmenor]<litros){

        while(litros>=v[posmenor]){
            printf("%d",posmenor+1);
            litros=litros-v[posmenor];
        }

    }else{
        printf("%d",-1);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int litros,i,menor=0,posmenor=0,sobrante,k,longitud;
    int  v[9];

    scanf("%d",&litros);
    menor=litros;
    for(i=0; i<9; i++){
            scanf("%d",&v[i]);
            if(v[i]<=menor && v[i]!=0){
                    menor=v[i];
                    posmenor=i;
                }

    }
    if(v[posmenor]<litros){
        longitud=litros/v[posmenor];
        sobrante=litros%v[posmenor];

        for(i=0; i<longitud; i++){
                for(k=9; k>posmenor; k--){
                    if(v[k]-v[posmenor]<=sobrante){
                            sobrante=sobrante-(v[k]-v[posmenor]);
                            break;

                    }
                }
                printf("%d",k+1);

        }

    }else{
        printf("%d",-1);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int litros,i,menor=0,posmenor=0;
    int v[9];

    scanf("%d",&litros);
    menor=litros;
    for(i=0; i<9; i++){
            scanf("%d",&v[i]);
            if(v[i]<=menor && v[i]!=0){
                    menor=v[i];
                    posmenor=i;
                }

    }
    if(v[posmenor]<litros){
        while(litros>=v[posmenor]){
                fflush(stdout);
            printf("%d",posmenor+1);
            litros=litros-v[posmenor];
        }
    }else{
        printf("%d",-1);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int litros,i,menor=0,posmenor=0,sobrante,k,longitud;
    int  v[9];

    scanf("%d",&litros);
    menor=litros;
    for(i=0; i<9; i++){
            scanf("%d",&v[i]);
            if(v[i]<=menor && v[i]!=0){
                    menor=v[i];
                    posmenor=i;
                }

    }
    if(v[posmenor]<litros){
        longitud=litros/v[posmenor];
        sobrante=litros%v[posmenor];
        for(i=0; i<longitud; i++){
                for(k=9; k>posmenor; k--){
                    if(v[k]-v[posmenor]<=sobrante){
                            sobrante=sobrante-(v[k]-v[posmenor]);
                            break;

                    }
                }
                printf("%d",k+1);

        }

    }else{
        printf("%d",-1);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int litros,i,menor=0,posmenor=0;
    int v[9];

    scanf("%d",&litros);
    menor=litros;
    for(i=0; i<9; i++){
            scanf("%d",&v[i]);
            if(v[i]<=menor && v[i]!=0){
                    menor=v[i];
                    posmenor=i;
                }

    }
    if(v[posmenor]<litros){
        while(litros>=v[posmenor]){
                fflush(stdin);
            printf("%d",posmenor+1);
            litros=litros-v[posmenor];
        }
    }else{
        printf("%d",-1);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int litros,i,menor=0,posmenor=0;
    int v[9];

    scanf("%d",&litros);
    menor=litros;
    for(i=0; i<9; i++){
            scanf("%d",&v[i]);
            if(v[i]<menor && v[i]!=0){
                    menor=v[i];
                    posmenor=i;
                }

    }
    if(v[posmenor]<litros){
        while(litros>=v[posmenor]){
            printf("%d",posmenor+1);
            litros=litros-v[posmenor];
        }
    }else{
        printf("%d",-1);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int litros,i,menor=0,posmenor=0;
    int v[9];

    scanf("%d",&litros);
    menor=litros;
    for(i=0; i<9; i++){
            scanf("%d",&v[i]);
            if(v[i]<=menor && v[i]!=0){
                    menor=v[i];
                    posmenor=i;
                }

    }
    if(v[posmenor]<litros){
        while(litros>=v[posmenor]){
            printf("%d",posmenor+1);
            litros=litros-v[posmenor];
        }
    }else{
        printf("%d",-1);
    }

    return 0;
}


#include<stdio.h>
int main(void)
{
    int n,i,min,x,nn,a[10];
    while(scanf("%d",&n)!=EOF){

        for(i=0,min=0;i<9;i++){
            scanf("%d",&a[i]);
            if(a[i]<=a[min]) min=i;
        }
        if(n<a[min]) printf("-1");

        while(n>=a[min]){
            if(n%a[min]==0){
                x=n/a[min];
                for(i=0;i<x;i++) printf("%d",min+1);
                break;
            }
            else{
                nn=n%a[min]+a[min];
                for(i=8;i>=0;i--){
                    if(nn>=a[i]) {printf("%d",i+1);n-=a[i];break;}
                }

            }
        }
        printf("\n");

    }
}


#include<stdio.h>
int main(void)
{
   int v,a[10],min=999999,h,i,m;
   while(scanf("%d",&v)!=EOF)
   {
    for(i=1;i<10;i++)
    {
        scanf("%d",&a[i]);
        min=min<a[i]?min:a[i];
    }
    if(v==0) printf("-1\n");
    else{
        for(i=1;i<10;i++)
           if(a[i]==min) h=i;
        m=v/min;
        if(m==0) printf("-1\n");
        else while(m--){
           m!=0?printf("%d",h):printf("%d\n",h);
    }}}
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int a[10],nm[1000005];
int rmax(int x)
{
    int t=0,i;
    for(i=1;i<10;i++)
    {
        if(a[i]<=x)t=i;
    }
    return t;
}
int main()
{
    int v,min,t,k,i;
    while(scanf("%d",&v)!=EOF)
    {
        for(i=1;i<10;i++)
        {
            scanf("%d",&a[i]);
            if(i==1)min=a[i];
            else
            {
                if(min>a[i])min=a[i];
            }
        }
        t=rmax(min);
        for(i=0;i<v/min;)
        {
            ++i;
            nm[i]=t;
        }
        k=v/min;v=v%min;
        for(i=1;i<=k;i++)
        {
            nm[i]=rmax(v+min);
            if(a[i]==t)break;
            else
            {
                v-=a[nm[i]]-min;
            }
        }
        if(k==0)printf("-1");
        else
        for(i=1;i<=k;i++)
        {
            printf("%d",nm[i]);
        }
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
int main(void)
{
    int n,i,min,x,nn,a[10];
    while(scanf("%d",&n)!=EOF){

        for(i=0,min=0;i<9;i++){
            scanf("%d",&a[i]);
            if(a[i]<=a[min]) min=i;
        }
        if(n<a[min]) printf("-1");

        while(n>=a[min]){
            if(n%a[min]==0){
                x=n/a[min];
                for(i=0;i<x;i++) printf("%d",min+1);
                break;
            }
            else{
                nn=n%a[min]+a[min];
                for(i=8;i>=0;i--){
                    if(nn>=a[i]) {printf("%d",i+1);n-=a[i];break;}
                }

            }
        }
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000,flag1=0;
	    scanf("%d",&b);
	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	  if(a[j]<b)
	      flag1=1;
	    }
	    b=b/min;
	   if(flag1==1)
	    { for(j=0;j<b;j++)
		printf("%d",count);}
	   else
	   printf("-1");

	return 0;
  }


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000,flag1=0;
	    scanf("%d",&b);
if(b==0)
printf("-1");
	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	  if(a[j]<b)
	      flag1=1;
	    }
	    b=b/min;
	   if(flag1==1)
	    { for(j=0;j<b;j++)
		printf("%d",count);}
	   else
	   printf("-1");
	       printf("\n");

	return 0;
  }


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000,flag=0;
	    scanf("%d",&b);

	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	    if(a[i]<b)
		flag=1;
	    }
	    b=b/min;
	    if(flag==1)
	    { for(j=0;j<b;j++)
		printf("%d",count);}
	    else
		printf("-1");
	    printf("\n");

	return 0;
  }


#include<stdio.h>
int main()
{
    int i,j,n,a[1000],t;
    scanf("%d",&t);
    while(t--)
    {
	scanf("%d%d",&n,&a[1]);
    int min=a[1],idx=1;
    for(i=2;i<=9;i++)
    {
	scanf("%d",&a[i]);
	if(a[i]<=min)
	{min=a[i];
	    idx=i;}
    }
    int p=n/min,q=n%min;
    if(n<min)
	printf("-1");
    else
    {
	int arr[p+3],l=n-min*p;;
	for(i=0;i<p;i++)
	    arr[i]=idx;
	for(j=p-1;j>=0;j--)
	{
	    for(i=9;i>=idx;i--)
	    {
		if((a[i]-min)<=l)
		{
		    arr[j]=i;
		    l=l-a[i]+min;
		    break;
		}
	    }

	    printf("%d",arr[j]);
	}
   		printf("\n");
    }
}
    return 0;
}


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count;
	    scanf("%d",&b);
	    if(b<=0)
		printf("-1\n");
	    else
	    {
	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]==1)
		    count=j;
	    }
	    for(j=0;j<b;j++)
		printf("%d",count);
	    printf("\n");
	    }
	return 0;
  }


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000;
	    scanf("%d",&b);
	    if(b<=0)
		printf("-1\n");
	    else
	    {
	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<min)
		{ min=a[j];
		    count=j;}
	    }
	    j=j/min;
	    for(j=0;j<b;j++)
		printf("%d",count);
	    printf("\n");
	    }
	return 0;
  }


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000;
	    scanf("%d",&b);
	    if(b<=0)
		printf("-1\n");
	    else
	    {
	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	    }
	    b=b/min;
	    for(j=0;j<b;j++)
		printf("%d",count);
	    printf("\n");
	    }
	return 0;
  }


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000,flag1=0;
	    scanf("%d",&b);
	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	  if(a[j]<b)
	      flag1=1;
	    }
	    b=b/min;
	   if(flag1==1)
	    { for(j=0;j<b;j++)
		printf("%d",count);}
	   else
	   printf("-1");
	       printf("\n");

	return 0;
  }


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000,flag=0;
	    scanf("%d",&b);

	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	    if(a[i]<b)
		flag=1;
	    }
	    b=b/min;
	    if(flag==1)
	    for(j=0;j<b;j++)
		printf("%d",count);
	    else
		printf("-1");
	    printf("\n");

	return 0;
  }


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000,flag1=0;
	    scanf("%d",&b);

	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	  if(a[j]<b)
	      flag1=1;
	    }
	    b=b/min;
	   if(flag1==1)
	    { for(j=0;j<b;j++)
		printf("%d",count);}
	   else
	    printf("\n");

	return 0;
  }


#include<stdio.h>
int main()
{
    int i,j,n,a[1000],t;
  //  scanf("%d",&t);
//    while(t--)
   // {
	scanf("%d%d",&n,&a[1]);
    int min=a[1],idx=1;
    for(i=2;i<=9;i++)
    {
	scanf("%d",&a[i]);
	if(a[i]<=min)
	{min=a[i];
	    idx=i;}
    }
    int p=n/min,q=n%min;
    if(n<min)
	printf("-1");
    else
    {
	int arr[p+3],l=n-min*p;;
	for(i=0;i<p;i++)
	    arr[i]=idx;
	for(j=p-1;j>=0;j--)
	{
	    for(i=9;i>=idx;i--)
	    {
		if((a[i]-min)<=l)
		{
		    arr[j]=i;
		    l=l-a[i]+min;
		    break;
		}
	    }

	    printf("%d",arr[j]);
	}
   		printf("\n");
    }
//}
    return 0;
}


#include<stdio.h>
int main()
  {int i,j,n,a[10],b,count,min=100000,flag=0;
	    scanf("%d",&b);

	    for(j=1;j<=9;j++)
	    {	scanf("%d",&a[j]);
		if(a[j]<=min)
		{ min=a[j];
		    count=j;}
	    if(a[i]<b)
		flag=1;
	    }
	    b=b/min;
	    if(flag==1)
	    { for(j=0;j<b;j++)
		printf("%d",count);}

	    printf("\n");

	return 0;
  }


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, v, menor, imenor, pint, mayor, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	for(i=2; i<10; i++)
	{
		if(menor>=a[i])
		{
			menor = a[i];
			imenor = i;
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	for(i=1; i<10; i++)
	{
		if(a[i]>=max && a[i]<=mayor)
		{
			max = a[i];
			imax = i;
		}
	}

	if(pint>0)
	{
		printf("%d ", imax);
		for(i=0; i<pint-1; i++)
		{
			printf("%d ", imenor);
		}
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, v, menor, imenor, pint, mayor, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	for(i=2; i<10; i++)
	{
		if(menor>=a[i])
		{
			menor = a[i];
			imenor = i;
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	for(i=1; i<10; i++)
	{
		if(a[i]>=max && a[i]<=mayor)
		{
			max = a[i];
			imax = i;
		}
	}

	if(pint>0)
	{
		printf("%d", imax);
		for(i=0; i<pint-1; i++)
		{
			printf("%d", imenor);
		}
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, j, v, menor, imenor, divmenor, pint, sobra, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	divmenor = v/menor;

	for(i=2; i<10; i++)
	{
		if(divmenor <= (v/a[i]))
		{
			menor = a[i];
			imenor = i;
			divmenor = v/a[i];
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
/*	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	int numero [pint];

	for(i=0; i<pint; i++)
		numero[i] = imenor;

/*		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
*/
	sobra= v%menor;
/*	printf("sobra %d\n", sobra);
*/	if(sobra>0)
	{
		for(i=0; i<pint; i++)
		{
			j=9;
			while(menor+sobra < a[j] && j>numero[i]) j--;

			numero[i] = j;
			sobra= sobra - a[j];
		}
	}
	if(pint>0)
	{
		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, v, menor, imenor, divmenor, pint, mayor, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	divmenor = v/menor;

	for(i=2; i<10; i++)
	{
		if(divmenor <= (v/a[i]))
		{
			menor = a[i];
			imenor = i;
			divmenor = v/a[i];
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	for(i=1; i<10; i++)
	{
		if(a[i]>=max && a[i]<=mayor)
		{
			max = a[i];
			imax = i;
		}
	}

	if(pint>0)
	{
		printf("%d", imax);
		for(i=0; i<pint-1; i++)
		{
			printf("%d", imenor);
		}
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, j, v, menor, imenor, divmenor, pint, sobra, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	divmenor = v/menor;

	for(i=2; i<10; i++)
	{
		if(divmenor <= (v/a[i]))
		{
			menor = a[i];
			imenor = i;
			divmenor = v/a[i];
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
/*	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	int numero [pint];

	for(i=0; i<pint; i++)
		numero[i] = imenor;

/*		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
*/
	sobra= v%menor;
/*	printf("sobra %d\n", sobra);
*/	if(sobra>0)
	{
		for(i=0; i<pint; i++)
		{
			j=9;
			while(menor+sobra < a[j] && j>0) j--;

			numero[i] = j;
			sobra-= (a[j]-menor);
		}
	}
	if(pint>0)
	{
		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, v, menor, imenor, divmenor, pint, mayor, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	divmenor = v/menor;

	for(i=2; i<10; i++)
	{
		if(divmenor >= (v/a[i]))
		{
			menor = a[i];
			imenor = i;
			divmenor = (v/a[i]);
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	for(i=1; i<10; i++)
	{
		if(a[i]>=max && a[i]<=mayor)
		{
			max = a[i];
			imax = i;
		}
	}

	if(pint>0)
	{
		printf("%d", imax);
		for(i=0; i<pint-1; i++)
		{
			printf("%d", imenor);
		}
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, j, v, menor, imenor, divmenor, pint, sobra, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	divmenor = v/menor;

	for(i=2; i<10; i++)
	{
		if(divmenor <= (v/a[i]))
		{
			menor = a[i];
			imenor = i;
			divmenor = v/a[i];
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
/*	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	int numero [pint];

	for(i=0; i<pint; i++)
		numero[i] = imenor;

/*		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
*/
	sobra= v%menor;
/*	printf("sobra %d\n", sobra);
*/	if(sobra>0)
	{
		for(i=0; i<pint; i++)
		{
			j=9;
			while(menor+sobra < a[j] && j>0) j--;

			numero[i] = j;
			sobra-= a[j]-menor;
		}
	}
	if(pint>0)
	{
		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

main()
{
	int i, j, v, menor, imenor, divmenor, pint, sobra, max=0, imax;
	scanf("%d", &v);
	int a[10];

	for(i=1; i<10; i++)
	{
		scanf("%d", &a[i]);
	}

	menor = a[1];
	imenor = 1;
	divmenor = v/menor;

	for(i=2; i<10; i++)
	{
		if(divmenor <= (v/a[i]))
		{
			menor = a[i];
			imenor = i;
			divmenor = v/a[i];
		}
	}

	pint = v/menor;		/*cantidad de digitos que puede hacer con el numero mas peque�o*/
/*	mayor = menor+(v%menor);	/*digito mas grande que puede escribir*/

	int numero [pint];

	for(i=0; i<pint; i++)
		numero[i] = imenor;

/*		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
*/
	sobra= v%menor;
/*	printf("sobra %d\n", sobra);
*/
	i=0;
	while(sobra>0 && i<pint)
	{
		j=9;
		while(menor+sobra < a[j] && j>numero[i]) j--;

		numero[i] = j;
		sobra= sobra - a[j];

	}
	if(pint>0)
	{
		for(i=0; i<pint; i++)
			printf("%d", numero[i]);
	}
	else
		printf("-1");

	return 0;
}


#include<stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	int a[9];
	int i;
	int j,k;
	for(i=0;i<9;i++)
		scanf("%d",&a[i]);
	int pos=0;
	int min=a[0];
	for(i=0;i<8;i++)
	{
		if(a[i+1]<min)
		{
			pos=i+1;
			min=a[i+1];
		}
	}
	pos=pos+1;

	int t=(n/(a[pos-1]));
	if(min>n)
		printf("-1\n");
	else
	{
		for(i=0;i<t;i++)
			printf("%d",pos);
		printf("\n");
	}

	return 0;

}


#include<stdio.h>
int main()
{

	long long int j,max,no;

	long long int n,a[9],i;
	long long int b[9];
	long long int rem;
 	long long int k;

	scanf("%lld",&n);
	for(j=0;j<9;j++)
		scanf("%lld",&a[j]);

	int 	min=a[0];
	for(j=0;j<8;j++)
	{
		if(a[j+1]<=min)
		{
			min=a[j+1];
			no=j+2;
		}
	}
	long long int digit=n/min;
	long long int c[digit];
	for(j=0;j<digit;j++)
		c[j]=no;

	for(j=0;j<digit;j++)

		rem=n-digit*min;
	for(j=0;j<digit;j++)
	{

		for(k=8;k>=no;k--)
		{
			if(a[k]<=rem+min)
			{
				c[j]=k+1;
				rem=rem+min-a[k];
				break;
			}
		}
	}
	for(j=0;j<digit;j++)
		printf("%lld",c[j]);

if(min>n)
printf("-1");

return 0;
}


#include<stdio.h>
int main()
{

int z;

	int n;
	scanf("%d",&n);
	int a[9];
	int i;
	int j,k;
	for(i=0;i<9;i++)
		scanf("%d",&a[i]);

int 	min=a[0];
	for(i=0;i<8;i++)
	{
		if(a[i+1]<min)
			min=a[i+1];
	}

	if(min>n)
		printf("-1\n");
	else
	{

	int b[9];
	for(i=0;i<9;i++)
		b[i]=n/a[i];
int max=b[0];
 int	pos=0;
	for(i=0;i<8;i++)
	{
		if(b[i+1]>=max)
		{
                   max=b[i+1];
		   pos=i+1;
		}
	}

		for(i=0;i<max;i++)
		printf("%d",pos+1);

printf("\n");
	}

return 0;
}


#include<stdio.h>
int main()
{

	long long int j,max,no;

	long long int n,a[9],i;
	long long int b[9];
	long long int rem;
 	long long int k;

	scanf("%lld",&n);
	for(j=0;j<9;j++)
		scanf("%lld",&a[j]);

	int 	min=a[0];
	for(j=0;j<8;j++)
	{
		if(a[j+1]<=min)
		{
			min=a[j+1];
			no=j+2;
		}
	}
	long long int digit=n/min;
	long long int c[digit];
	for(j=0;j<digit;j++)
		c[j]=no;

	for(j=0;j<digit;j++)

		rem=n-digit*min;
	for(j=0;j<digit;j++)
	{

		for(k=8;k>=no;k--)
		{
			if(a[k]<=rem+min)
			{
				c[j]=k+1;
				rem=rem+min-a[k];
				break;
			}
		}
	}
	for(j=0;j<digit;j++)
		printf("%lld",c[j]);
	return 0;
}


#include<stdio.h>
int main()
{

	long long int j,max,no;

	long long int n,a[9],i;
	long long int b[9];
	long long int rem;
 	long long int k;

	scanf("%lld",&n);
	for(j=0;j<9;j++)
		scanf("%lld",&a[j]);

	int 	min=a[0];
	for(j=0;j<8;j++)
	{
		if(a[j+1]<=min)
		{
			min=a[j+1];
			no=j+2;
		}
	}
	long long int digit=n/min;
	long long int c[digit];
	for(j=0;j<digit;j++)
		c[j]=no;

	for(j=0;j<digit;j++)

		rem=n-digit*min;
	for(j=0;j<digit;j++)
	{

		for(k=8;k>=no;k--)
		{
			if(a[k]<=rem+min)
			{
				c[j]=k+1;
				rem=rem+min-a[k];
				break;
			}
		}
	}
	for(j=0;j<digit;j++)
		printf("%lld ",c[j]);
	return 0;
}


#include<stdio.h>
int b[1000000];
int main()
{
	int t,n,a[9],i,j,min=9000000,idx,k,l,flag=1,d;
//	scanf("%d",&t);
//	while(t--)
//	{
		scanf("%d",&n);
		d=n;
		for(j=0;j<9;j++)
		{
			scanf("%d",&a[j]);
			if(min>=a[j])
			{
				min=a[j];
				idx=j+1;
			}
		}
		for(j=0;j<n/min;j++)
		{
			b[j]=idx ;
		}
		if(min>n)
		{
			printf("-1\n");
//			continue ;
		}
		if(n%min==0)
		{
			for(j=0;j<n/min;j++)
				printf("%d",b[j]);
	 		printf("\n");
//			continue;
		}
		else
		{
			n=((n-(n/min)*min+min));
			for(k=0;k<d/min;k++)
			{
				for(j=8;j>=0;j--)
				{
					if(n>=a[j])
					{
						flag=0;
						n = n-a[j]+min;
						b[k]=j+1;
						break;
					}
					else
						flag=1;
				}
				if(flag)
					break;
				else
					continue;
			}
			j=0;
			while(b[j]!=0)
			{
				printf("%d",b[j]);
					j++;
			}
			printf("\n");
		}
//	}
	return 0;
}


#include<stdio.h>
int b[1000000];
int main()
{
	int t,n,a[9],i,j,min=9000000,idx,k,l,flag=1,d;
//	scanf("%d",&t);
//	for(i=0;i<t;i++)
//	{
		scanf("%d",&n);
		d=n;
		for(j=0;j<9;j++)
		{
			scanf("%d",&a[j]);
			if(min>=a[j])
			{
				min=a[j];
				idx=j+1;
			}
		}
	//	printf("the value of n is %d\n",n);
		for(j=0;j<n/min;j++)
		{
			b[j]=idx ;
		}
		if(min>n)
		{
			printf("-1\n");
			return 0;
		}
		if(n%min==0)
		{
			for(j=0;j<n/min;j++)
				printf("%d",b[j]);
			printf("\n");
			return 0;
		}
		else
		{
			n=((n-(n/min)*min+min));
		//	printf("the new value of n is %d\n",n);
			for(k=0;k<d/min;k++)
			{
				for(j=8;j>=0;j--)
				{
					if(n>=a[j])
					{
//						printf("the value of a[%d] is %d\n",j,a[j]);
						flag=0;
						n = n-a[j]+min;
//						printf("the value of n after the swap is %d\n",n);
						b[k]=j+1;
						break;
					}
					else
						flag=1;
				}
//				printf("the value of flag is %d\n",flag);
				if(flag)
					break;
				else
					continue;
			}
			j=0;
			while(b[j]!=0)
			{
				printf("%d",b[j]);
					j++;
			}
			printf("\n");
		}
//	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	long long int n,x,a[9],i,j,k,count=0,count2=0,b,b2,nod,rem,flag=0,min;
	scanf("%lld",&n);
	for(i=1;i<=9;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(i=1;i<=9;i++)
	{
		if(a[i]<=n) flag=1;
	}
	if(flag==0) printf("-1\n");
	else if(flag==1)
	{
	min=a[1];
	for(i=1;i<=9;i++)
		{
			if(a[i]<=min)
			{min=a[i]; count=i;}
		}
	nod=(n/min)+1;
	rem=n%min;
	for(i=1;i<=count;i++)
	{
		if(a[i]<=rem)
		{
			rem=a[i];
			count2=i;
		}
	}
	if(count2!=0)
	{
		for(i=1;i<nod;i++)
		{
			printf("%d",count);
		}
		printf("%d",count2);
		printf("\n");
	}
	else if(count2==0)
	{
		for(i=1;i<nod;i++)
		{
			printf("%d",count);
		}
		printf("\n");
	}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	long long int n,x,a[10],t,i,j,k,count=0,count2=0,b,b2,nod,rem,flag=0,min;
	//scanf("%lld",&t);
	//for(j=0;j<t;j++)
	{
		count=0;
		count2=0;
		flag=0;
		min=0;
		scanf("%lld",&n);
		for(i=1;i<=9;i++)
		{
			scanf("%lld",&a[i]);
		}
		for(i=1;i<=9;i++)
		{
			if(a[i]<=n) flag=1;
		}
		if(flag==0) printf("-1\n");
		else if(flag==1)
		{
			min=a[1];
			for(i=1;i<=9;i++)
			{
				if(a[i]<=min)
				{min=a[i]; count=i;}
			}
			nod=(n/min)+1;
			rem=n%min;
			for(i=1;i<=count;i++)
			{
				if(a[i]<=rem)
				{
					rem=a[i];
					count2=i;
				}
			}
			if(count2!=0)
			{
				for(i=1;i<nod;i++)
				{
					printf("%d",count);
				}
				printf("%d",count2);
				printf("\n");
			}
			else if(count2==0)
			{
				for(i=1;i<nod;i++)
				{
					printf("%d",count);
				}
				printf("\n");
			}
		}
	}
	return 0;
}


#include<stdio.h>
long output[1000001];
int main()
{
	int i=0,j=0,k;
	long digit[9][2],v,temp,digit1[9];
	scanf("%ld",&v);
	while(i<9)
	{
		scanf("%ld",&digit[i][0]);
		digit[i][1]=i;
		digit1[i]=digit[i][0];
		i++;
	}
	for(i=0;i<9;i++)
	{
		for(j=i;j<9;j++)
		{
			if(digit[i][0]>=digit[j][0])
			{
				temp=digit[i][0];
				digit[i][0]=digit[j][0];
				digit[j][0]=temp;

				k=digit[i][1];
				digit[i][1]=digit[j][1];
				digit[j][1]=k;
			}
		}
	}
	if(v<digit[0][0])
	{
		printf("-1");
		return 0;
	}
	i=0;
	while(v>=digit[0][0])
	{
		output[i]=digit[0][1]+1;
		v=v-digit[0][0];
		i++;
	}
	j=0;
	while(v >= (digit[1][0]-digit[0][0]) && v!=0)
	{

		i=8;
		while(i>0)
		{
			if(v >= (digit1[i]-digit[0][0]))
			{
				if(digit1[i]==digit[0][0])
				{
					i=0;
					while(output[i]!=0)
					{
						printf("%ld",output[i]);
						i++;
					}
					return 0;
				}
				if(output[j]<i+1)
				{
					output[j]=i+1;

					v=v-(digit1[i]-digit[0][0]);
					j++;
					break;
				}
			}
			i--;
		}

	}
	i=0;
	while(output[i]!=0)
	{
		printf("%ld",output[i]);
		i++;
	}
	printf("\n");
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	long long int i=0,j=0,k;
	long long int digit[9][2],v,temp,digit1[9],output[1000001];
	scanf("%lld",&v);
	while(i<9)
	{
		scanf("%lld",&digit[i][0]);
		digit[i][1]=i;
		digit1[i]=digit[i][0];
		i++;
	}
	for(i=0;i<9;i++)
	{
		for(j=i;j<9;j++)
		{
			if(digit[i][0]>=digit[j][0])
			{
				temp=digit[i][0];
				digit[i][0]=digit[j][0];
				digit[j][0]=temp;

				k=digit[i][1];
				digit[i][1]=digit[j][1];
				digit[j][1]=k;
			}
		}
	}
	if(v<digit[0][0])
	{
		printf("-1");
		return 0;
	}
	i=0;
	while(v>=digit[0][0])
	{
		output[i]=digit[0][1]+1;
		v=v-digit[0][0];
		i++;
	}
	j=0;
	while(v >= (digit[1][0]-digit[0][0]) && v!=0)
	{

		i=8;
		while(i>0)
		{
			if(v >= (digit1[i]-digit[0][0]))
			{
				if(digit1[i]==digit[0][0])
				{
					i=0;
					while(output[i]!=0)
					{
						printf("%ld",output[i]);
						i++;
					}
					return 0;
				}
				if(output[j]<i+1)
				{
					output[j]=i+1;

					v=v-(digit1[i]-digit[0][0]);
					j++;
					break;
				}
			}
			i--;
		}

	}
	i=0;
	while(output[i]!=0)
	{
		printf("%lld",output[i]);
		i++;
	}
	printf("\n");
	return 0;
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B349 solver = new B349();
        solver.solve(1, in, out);
        out.close();
    }

    static class B349 {
        Number[][] memo;
        int V;
        int[] a = new int[10];

        Number dp(int cash, int on) {
            if (memo[cash][on] != null) return memo[cash][on];
            if (on == 0 || cash == 0) return new Number();
            Number best = new Number();
            if (cash >= a[on])
                best = best.add(on).add(dp(cash - a[on], on));
            Number drop = dp(cash, on - 1);
            if (drop.compareTo(best) > 0)
                best = drop.copy();
            return memo[cash][on] = best;
        }

        public void solve(int testNumber, FastScanner s, PrintWriter out) {
            V = s.nextInt();
            for (int i = 1; i <= 9; i++)
                a[i] = s.nextInt();
            memo = new Number[V + 1][10];
            Number ans = dp(V, 9);
            if (ans.len == 0)
                out.println(-1);
            else {
                out.println(ans);
            }
        }

        class Number implements Comparable<Number> {
            int[] counts;
            int len = 0;

            public Number() {
                counts = new int[10];
            }

            public Number(int[] nums, int len) {
                counts = new int[10];
                for (int i = 1; i < nums.length; i++)
                    counts[i] = nums[i];
                this.len = len;
            }

            Number add(int dig) {
                counts[dig]++;
                Number ret = new Number(counts, len + 1);
                counts[dig]--;
                return ret;
            }

            Number add(Number NN) {
                int[] ncount = new int[10];
                for (int i = 1; i <= 9; i++) {
                    ncount[i] = counts[i] + NN.counts[i];
                }
                return new Number(ncount, len + NN.len);
            }

            Number copy() {
                return new Number(counts, len);
            }

            public int compareTo(Number NN) {
                if (len != NN.len) return len - NN.len;
                for (int i = 9; i >= 1; i--) {
                    if (counts[i] != NN.counts[i]) return counts[i] - NN.counts[i];
                }
                return 0;
            }

            public String toString() {
                String ret = "";
                for (int i = 9; i >= 1; i--)
                    for (int j = 0; j < counts[i]; j++)
                        ret += i;
                return ret;
            }

        }

    }

    static class FastScanner {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public FastScanner(InputStream stream) {
            this.stream = stream;
        }

        int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

    }
}