求调求调
查看原帖
求调求调
707424
monkeys_King楼主2024/10/22 00:04
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long c , t , a[10] , ans , len , dp[N][10];
string s;
long long qz(long long x)
{
	long long w = ans + x;
	while(x > 0)
	{
		int b = x % 10;
		x /= 10;
		w -= a[b];
	}
	return w;
}
int main()
{
	//freopen("test.in" , "r" , stdin);
	//freopen("test.out" , "w" , stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> c;
	cin >> t;
	while(t--)
	{
		for(int i = 0 ; i < N ; i++)
		{
			for(int j = 0 ; j <= 7 ; j++)
			{
				dp[i][j] = 0;
			}
		}	
		ans = 0;
		cin >> s;
		len = s.size();
		for(int i = 1 ; i <= 9 ; i++) cin >> a[i];
		for(int i = 0 ; i < len ; i++) ans += a[(s[i] - '0')];
		dp[0][1] = s[0] - '0';
		for(int i = 1 ; i < len ; i++)
		{
			for(int j = 0 ; j <= min(i + 1 , 6) ; j++)
			{
				if(j > 0 && i >= j)
				{
					if(qz(dp[i - 1][j]) <= qz(dp[i - 1][j - 1] * 10 + (s[i] - '0')) && qz(dp[i - 1][j]) <= qz(dp[i - 1][j] / 10 * 10 + (s[i] - '0'))) dp[i][j] = dp[i - 1][j];
					else if(qz(dp[i - 1][j] / 10 * 10 + (s[i] - '0')) <= qz(dp[i - 1][j]) && qz(dp[i - 1][j] / 10 * 10 + (s[i] - '0')) <= qz(dp[i - 1][j - 1] * 10 + s[i] - '0')) dp[i][j] = dp[i - 1][j] / 10 * 10 + (s[i] - '0');
					else dp[i][j] = dp[i - 1][j - 1] * 10 + (s[i] - '0');
				}
				else if(j > 0 && i + 1 == j)
				{
					dp[i][j] = dp[i - 1][j - 1] * 10 + (s[i] - '0');
				}
				//cout << i << " " << j << " " << dp[i][j] << endl;
			}
			//cout << endl;
		}
		long long res = 6666666666666666666;
	 	for(int i = 0 ; i < len ; i++)
	 	{
	 		for(int j = 0 ; j <= 6 ; j++)
	 		{
	 			res = min(res , qz(dp[i][j]));
			 }
		 }
		cout << res << "\n";
 	}
 	
	return 0;
}
2024/10/22 00:04
加载中...