Problem:
Problem G. Additions
You are given an integer $N$ and a string consisting of '
+
' and digits. You are asked to transform the string into a valid formula whose calculation result is smaller than or equal to $N$ by modifying some characters. Here, you replace one character with another character any number of times, and the converted string should still consist of '
+
' and digits. Note that leading zeros and unary positive are prohibited.
For instance, '
0123+456
' is assumed as invalid because leading zero is prohibited. Similarly, '
+1+2
' and '
2++3
' are also invalid as they each contain a unary expression. On the other hand, '
12345
', '
0+1+2
' and '
1234+0+0
' are all valid.
Your task is to find the minimum number of the replaced characters. If there is no way to make a valid formula smaller than or equal to $N$, output $-1$ instead of the number of the replaced characters.
Input
The input consists of a single test case in the following format.
$N$
$S$
The first line contains an integer $N$, which is the upper limit of the formula ($1 \leq N \leq 10^9$). The second line contains a string $S$, which consists of '
+
' and digits and whose length is between $1$ and $1,000$, inclusive. Note that it is
not
guaranteed that initially $S$ is a valid formula.
Output
Output the minimized number of the replaced characters. If there is no way to replace, output $-1$ instead.
Examples
Sample Input 1
100
+123
Output for Sample Input 1
2
Sample Input 2
10
+123
Output for Sample Input 2
4
Sample Input 3
1
+123
Output for Sample Input 3
-1
Sample Input 4
10
++1+
Output for Sample Input 4
2
Sample Input 5
2000
1234++7890
Output for Sample Input 5
2
In the first example, you can modify the first two characters and make a formula '
1+23
', for instance. In the second example, you should make '
0+10
' or '
10+0
' by replacing all the characters. In the third example, you cannot make any valid formula less than or equal to $1$.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define loop(i ,m, n) for (ll i = m; i <= n; ++i)
#define pll pair<ll,ll>
#define vl vector<ll>
#define vvl vector<vl>
#define inf 100000000000000000LL

int main() {
	ll n;
	string s;
	cin>>n>>s;
	s.push_back('+');

	//dp[i][j]=i文字目まで見ていて、そのi文字目が＋となっている時。
	//j文字変更している時の合計の最小値
	vvl dp(s.size()+1,vl(s.size(),inf));
	dp[0][0]=0;
	

	rep(i,s.size()){
		rep(j,s.size()){
			if(dp[i][j]==inf)continue;

			//何文字先に遷移するか、+を置く所までで最大11文字以上一気には置けない。
			loop(k,2,11){
				if(i+k>s.size())break;

				//何文字変更するか
				loop(l,0,k){
					//残り変更数,整数部分
					ll cnt=l;
					string tmp=s.substr(i,k-1);

					//最初と最後は特殊なので前処理
					if(s[i+k-1]!='+')cnt--;
					if(tmp[0]=='+'){
						cnt--;
						if(k==2)tmp[0]='0';
						else tmp[0]='1';
					}
					if(tmp[0]=='0'&&k!=2){
						cnt--;
						tmp[0]='1';
					}

					//2文字目以降の+の残りを処理
					loop(m,1,k-2){
						if(tmp[m]=='+'){
							tmp[m]='0';
							cnt--;
						}
					}
					
					//変更文字数が足りない場合,continue
					if(cnt<0)continue;

					//変更がまだ可能な場合、1文字目だけは特殊処理
					if(cnt!=0){
						if(k==2){
							if(tmp[0]!='0'){
								cnt--;
								tmp[0]='0';
							}
						}else{
							if(tmp[0]!='1'){
								cnt--;
								tmp[0]='1';
							}
						}
					}

					//2文字目以降を貪欲に0に変更
					loop(m,1,k-2){
						if(cnt==0)break;
						if(tmp[m]!='0'){
							tmp[m]='0';
							cnt--;
						}
					}
					
					//変更文字数が余ってる場合不適
					if(cnt!=0)break;

					dp[i+k][j+l]=min(dp[i+k][j+l],dp[i][j]+stoll(tmp));
					
				}
			}
		}
	}

	//出力
	rep(j,s.size()){
		if(dp[s.size()][j]<=n){
			cout<<j<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
    return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 10948
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define loop(i ,m, n) for (ll i = m; i <= n; ++i)
#define pll pair<ll,ll>
#define vl vector<ll>
#define vvl vector<vl>
#define inf 100000000000000000LL

int main() {
	ll n;
	string s;
	cin>>n>>s;
	s.push_back('+');

	//dp[i][j]=i文字目まで見ていて、そのi文字目が＋となっている時。
	//j文字変更している時の合計の最小値
	vvl dp(s.size()+1,vl(s.size(),inf));
	dp[0][0]=0;
	

	rep(i,s.size()){
		rep(j,s.size()){
			if(dp[i][j]==inf)continue;

			//何文字先に遷移するか、+を置く所までで最大11文字以上一気には置けない。
			loop(k,2,11){
				if(i+k>s.size())break;

				//何文字変更するか
				loop(l,0,k){
					//残り変更数,整数部分
					ll cnt=l;
					string tmp=s.substr(i,k-1);

					//最初と最後は特殊なので前処理
					if(s[i+k-1]!='+')cnt--;
					if(tmp[0]=='+'){
						cnt--;
						if(k==2)tmp[0]='0';
						else tmp[0]='1';
					}
					if(tmp[0]=='0'&&k!=2){
						cnt--;
						tmp[0]='1';
					}

					//2文字目以降の+の残りを処理
					loop(m,1,k-2){
						if(tmp[m]=='+'){
							tmp[m]='0';
							cnt--;
						}
					}
					
					//変更文字数が足りない場合,continue
					if(cnt<0)continue;
					
					//変更がまだ可能な場合、1文字目だけは特殊処理
					if(cnt!=0){
						if(k==2){
							if(tmp[0]=='0')continue;
							cnt--;
							tmp[0]='0';
						}else{
							if(tmp[0]=='1')continue;
							cnt--;
							tmp[0]='1';
						}
					}

					//2文字目以降を貪欲に0に変更
					loop(m,1,k-2){
						if(cnt==0)break;
						if(tmp[m]!='0'){
							tmp[m]='0';
							cnt--;
						}
					}
					
					//変更文字数が余ってる場合不適
					if(cnt!=0)break;

					dp[i+k][j+l]=min(dp[i+k][j+l],dp[i][j]+stoll(tmp));
					
				}
			}
		}
	}

	
	rep(j,s.size()){
		if(dp[s.size()][j]<=n){
			cout<<j<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
    return 0;
}
accuracy: 0.9487179487179487
time_ms: 820
memory_kb: 11032
final_rank: 13

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

void solve(){
    ll n; cin >> n;
    string s; cin >> s;
    s = "+" + s;
    int m = ssize(s);
    vector dp(m+1, vector<ll>(m+1, n+100));
    dp[0][0] = 0;
    auto chmin = [&](ll& a, ll b) {
        if(a > b) a = b;
    };
    for(int i = 2; i <= m; i++) {
        for(int l = 1; l <= 10; l++) {
            int k = i-l;
            if(k <= 0) continue;
            string t = s.substr(k, l);
            int must_modify = s[k-1] != '+';
            k--;
            for(int d = 0; d < l; d++) {
                if(t[d] == '+') {
                    must_modify++;
                    if(l >= 2 && d == 0) t[d] = '1';
                    else t[d] = '0';
                } else if(l >= 2 && d == 0 && t[d] == '0') {
                    must_modify++;
                    t[d] = '1';
                }
            }
            for(int j = 0; j <= m; j++) {
                int modify = must_modify;
                string cur = t;
                if(dp[k][j] == n+100) continue;
                if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(cur));
                for(int d = 0; d < l; d++) {
                    char ideal = (l >= 2 && d == 0) ? '1' : '0';
                    if(cur[d] != ideal) {
                        cur[d] = ideal;
                        modify++;
                        if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(cur));
                    }
                }
            }
        }
    }
    /* for(int i = 0; i <= m; i++) { */
    /*     for(int j = 0; j <= m; j++) { */
    /*         cerr << ' ' << dp[i][j]; */
    /*     } */
    /*     cerr << endl; */
    /* } */
    for(int j = 0; j <= m; j++) {
        if(dp[m][j] <= n) {
            cout << j << '\n';
            return;
        }
    }
    cout << "-1\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
accuracy: 1
time_ms: 440
memory_kb: 11264
final_rank: 9

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

void solve(){
    ll n; cin >> n;
    string s; cin >> s;
    s = "+" + s;
    int m = ssize(s);
    vector dp(m+1, vector<ll>(m+1, n+100));
    dp[0][0] = 0;
    auto chmin = [&](ll& a, ll b) {
        if(a > b) a = b;
    };
    for(int i = 2; i <= m; i++) {
        for(int l = 1; l <= 13; l++) {
            int k = i-l;
            if(k <= 0) continue;
            string t = s.substr(k, l);
            int must_modify = s[k-1] != '+';
            k--;
            for(int d = 0; d < l; d++) {
                if(t[d] == '+') {
                    must_modify++;
                    if(l >= 2 && d == 0) t[d] = '1';
                    else t[d] = '0';
                } else if(l >= 2 && d == 0 && t[d] == '0') {
                    must_modify++;
                    t[d] = '1';
                }
            }
            int modify = must_modify;
            for(int j = 0; j <= m; j++) {
                if(dp[k][j] == n+100) continue;
                if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                for(int d = 0; d < l; d++) {
                    char ideal = (l >= 2 && d == 0) ? '1' : '0';
                    if(t[d] != ideal) {
                        t[d] = ideal;
                        modify++;
                        if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                    }
                }
            }
        }
    }
    for(int j = 0; j <= m; j++) {
        if(dp[m][j] <= n) {
            cout << j << '\n';
            return;
        }
    }
    cout << "-1\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
accuracy: 0.2564102564102564
time_ms: 50
memory_kb: 7808
final_rank: 19

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

void solve(){
    ll n; cin >> n;
    string s; cin >> s;
    s = "+" + s;
    int m = ssize(s);
    vector dp(m+1, vector<ll>(m+1, n+100));
    dp[0][0] = 0;
    auto chmin = [&](ll& a, ll b) {
        if(a > b) a = b;
    };
    for(int i = 2; i <= m; i++) {
        for(int l = 1; l <= 10; l++) {
            int k = i-l;
            if(k <= 0) continue;
            string t = s.substr(k, l);
            int must_modify = s[k-1] != '+';
            k--;
            for(int d = 0; d < l; d++) {
                if(t[d] == '+') {
                    must_modify++;
                    if(l >= 2 && d == 0) t[d] = '1';
                    else t[d] = '0';
                } else if(l >= 2 && d == 0 && t[d] == '0') {
                    must_modify++;
                    t[d] = '1';
                }
            }
            int modify = must_modify;
            for(int j = 0; j <= m; j++) {
                if(dp[k][j] == n+100) continue;
                if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                for(int d = 0; d < l; d++) {
                    char ideal = (l >= 2 && d == 0) ? '1' : '0';
                    if(t[d] != ideal) {
                        t[d] = ideal;
                        modify++;
                        if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                    }
                }
            }
        }
    }
    for(int j = 0; j <= m; j++) {
        if(dp[m][j] <= n) {
            cout << j << '\n';
            return;
        }
    }
    cout << "-1\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
accuracy: 0.2564102564102564
time_ms: 40
memory_kb: 7808
final_rank: 17

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

void solve(){
    ll n; cin >> n;
    string s; cin >> s;
    s = "+" + s;
    int m = ssize(s);
    vector dp(m+1, vector<ll>(m+1, n+100));
    dp[0][0] = 0;
    auto chmin = [&](ll& a, ll b) {
        if(a > b) a = b;
    };
    for(int i = 2; i <= m; i++) {
        for(int l = 1; l <= 9; l++) {
            int k = i-l;
            if(k <= 0) continue;
            string t = s.substr(k, l);
            int must_modify = s[k-1] != '+';
            k--;
            for(int d = 0; d < l; d++) {
                if(t[d] == '+') {
                    must_modify++;
                    if(l >= 2 && d == 0) t[d] = '1';
                    else t[d] = '0';
                } else if(l >= 2 && d == 0 && t[d] == '0') {
                    must_modify++;
                    t[d] = '1';
                }
            }
            int modify = must_modify;
            for(int j = 0; j <= m; j++) {
                if(dp[k][j] == n+100) continue;
                if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                for(int d = 0; d < l; d++) {
                    char ideal = (l >= 2 && d == 0) ? '1' : '0';
                    if(t[d] != ideal) {
                        t[d] = ideal;
                        modify++;
                        if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                    }
                }
            }
        }
    }
    for(int j = 0; j <= m; j++) {
        if(dp[m][j] <= n) {
            cout << j << '\n';
            return;
        }
    }
    cout << "-1\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
accuracy: 0.2564102564102564
time_ms: 30
memory_kb: 7808
final_rank: 15

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

void solve(){
    ll n; cin >> n;
    string s; cin >> s;
    s = "+" + s;
    int m = ssize(s);
    vector dp(m+1, vector<ll>(m+1, n+100));
    dp[0][0] = 0;
    auto chmin = [&](ll& a, ll b) {
        if(a > b) a = b;
    };
    for(int i = 2; i <= m; i++) {
        for(int l = 1; l <= 9; l++) {
            int k = i-l;
            if(k <= 0) continue;
            string t = s.substr(k, l);
            int must_modify = s[k-1] != '+';
            k--;
            for(int d = 0; d < l; d++) {
                if(t[d] == '+') {
                    must_modify++;
                    if(l >= 2 && d == 0) t[d] = '1';
                    else t[d] = '0';
                } else if(l >= 2 && d == 0 && t[d] == '0') {
                    must_modify++;
                    t[d] = '0';
                }
            }
            int modify = must_modify;
            for(int j = 0; j <= m; j++) {
                if(dp[k][j] == n+100) continue;
                if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                for(int d = 0; d < l; d++) {
                    char ideal = (l >= 2 && d == 0) ? '1' : '0';
                    if(t[d] != ideal) {
                        t[d] = ideal;
                        modify++;
                        if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                    }
                }
            }
        }
    }
    for(int j = 0; j <= m; j++) {
        if(dp[m][j] <= n) {
            cout << j << '\n';
            return;
        }
    }
    cout << "-1\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
accuracy: 0.2564102564102564
time_ms: 30
memory_kb: 7808
final_rank: 15

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

void solve(){
    ll n; cin >> n;
    string s; cin >> s;
    int m = ssize(s);
    vector dp(m+1, vector<ll>(m+1, n+100));
    dp[0][0] = 0;
    auto chmin = [&](ll& a, ll b) {
        if(a > b) a = b;
    };
    for(int i = 1; i <= m; i++) {
        for(int l = 1; l <= 9; l++) {
            if(i-l < 0) continue;
            if(i-l == 1) continue;
            int k = i-l;
            if(k < 0) continue;
            if(k == 1) continue;
            int must_modify = 0;
            string t = s.substr(k, l);
            if(k > 0) {
                must_modify += s[k-1] != '+';
                k--;
            }
            for(int d = 0; d < l; d++) {
                if(t[d] == '+') {
                    must_modify++;
                    if(l >= 2 && d == 0) t[d] = '1';
                    else t[d] = '0';
                } else if(l >= 2 && d == 0 && t[d] == '0') {
                    must_modify++;
                    t[d] = '0';
                }
            }
            int modify = must_modify;
            for(int j = 0; j <= m; j++) {
                if(dp[k][j] == n+100) continue;
                if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                for(int d = 0; d < l; d++) {
                    char ideal = (l >= 2 && d == 0) ? '1' : '0';
                    if(t[d] != ideal) {
                        t[d] = ideal;
                        modify++;
                        if(j+modify <= m) chmin(dp[i][j+modify], dp[k][j] + stoll(t));
                    }
                }
            }
        }
    }
    for(int j = 0; j <= m; j++) {
        if(dp[m][j] <= n) {
            cout << j << '\n';
            return;
        }
    }
    cout << "-1\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
accuracy: 0.2564102564102564
time_ms: 40
memory_kb: 7808
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 50);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

void chmin(ll &a, ll b) {a = min(a, b);}

vector<ll> solve(string s) {
    // cout << s << endl;
    int n = s.size();
    int ans = (s[0] != '+');
    vector<ll> dp(n + 1, INF);
    if(n == 2) {
        if(s[1] == '0') {
            dp[ans] = 0;
        } else if(s[1] == '+') {
            dp[ans + 1] = 0;
        } else {
            dp[ans] = s[1] - '0';
            dp[ans + 1] = 0;
        }
        return dp;
    }
    vector<ll> v;
    ll d = 1;
    ll sum = 0;
    for(ll i = n - 1; i >= 2; i --) {
        if(s[i] == '+') {
            ans ++;
            d *= 10;
            continue;
        }
        ll num = s[i] - '0';
        v.pb(d * num);
        sum += d * num;
        d *= 10;
    }
    if(s[1] == '0') {
        ans ++;
        sum += d;
    } else if(s[1] == '+') {
        ans ++;
        sum += d;
    } else if(s[1] == '1') {
        sum += d;
    } else {
        ll num = s[1] - '1';
        sum += d * (num + 1);
        v.pb(d * num);
    }
    sort(all(v));
    reverse(all(v));
    int sz = v.size();
    dp[ans] = sum;
    rep(i, sz) {
        dp[ans + i + 1] = dp[ans + i] - v[i];
    }
    return dp;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll th; cin >> th;
    string s; cin >> s;
    s = "+" + s + "+";
    int n = s.size();
    vector<vector<ll>> dp(n, vector(n + 1, INF));
    dp[n - 1][0] = 0;
    for(int i = n - 1; i >= 0; i --) {
        for(int j = i - 2; j >= i - 10; j --) {
            if(j < 0) break;
            auto v = solve(s.substr(j, i - j));
            int sz = v.size();
            for(int k = 0; k <= n - i; k ++) {
                rep(x, sz) {
                    chmin(dp[j][k + x], dp[i][k] + v[x]);
                }
            }
        }
        // for(int j = 0; j <= n; j ++) {
        //     cout << dp[i][j] << " ";
        // }
        // cout << endl;
        // cout << endl;
    }
    for(int i = 0; i <= n; i ++) {
        if(dp[0][i] <= th) {
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 10900
final_rank: 3

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll m = s.length();
    vector<vector<ll>> dp(m, vector<ll>(m + 1, inf));
    auto ar = make_vec<ll>({m, 11, 11}, inf);
    rep(i, 0, m)
    {
        rep(j, 1, 11)
        {
            if (i + j > m)
            {
                continue;
            }
            string t = s.substr(i, j);
            ll cnt = 0;
            rep(k, 0, t.length())
            {
                if (t[k] == '+')
                {
                    cnt++;
                    if (j == 1 || k != 0)
                    {
                        t[k] = '0';
                    }
                    else
                    {
                        t[k] = '1';
                    }
                }
                else if (t[k] == '0')
                {
                    if (j != 1 && k == 0)
                    {
                        t[k] = '1';
                        cnt++;
                    }
                }
            }
            queue<ll> que;
            rep(k, 0, t.length())
            {
                if (k == 0)
                {
                    if (j == 1)
                    {
                        if (t[k] != '0')
                        {
                            que.push(k);
                        }
                    }
                    else
                    {
                        if (t[k] != '1')
                        {
                            que.push(k);
                        }
                    }
                }
                else
                {
                    if (t[k] != '0')
                    {
                        que.push(k);
                    }
                }
            }
            rep(k, cnt, t.length() + 1)
            {
                ar[i][j][k] = stoll(t);
                if (!que.empty())
                {
                    ll x = que.front();
                    que.pop();
                    if (j != 1 && x == 0)
                    {
                        t[x] = '1';
                    }
                    else
                    {
                        t[x] = '0';
                    }
                }
            }
        }
    }
    rep(i, 0, m)
    {
        rep(j, 0, m + 1)
        {
            rep(k, 1, 11)
            {
                ll f = 1;
                if (i - k < -1 || i - k == 0)
                {
                    continue;
                }
                if (i - k == -1)
                {
                    f = 0;
                }
                else
                {
                    if (s[i - k] == '+')
                    {
                        f = 0;
                    }
                }
                string t = s.substr(i - k + 1, k);
                rep(x, 0, t.length() + 1)
                {
                    if (j - x - f < 0)
                    {
                        continue;
                    }
                    if (ar[i - k + 1][k][x] > n)
                    {
                        continue;
                    }
                    ll sub = ar[i - k + 1][k][x];
                    if (i - k - 1 >= 0)
                    {
                        sub += dp[i - k - 1][j - x - f];
                    }
                    chmin(dp[i][j], sub);
                }
            }
        }
    }
    ll ans = inf;
    rep(i, 0, m + 1)
    {
        if (dp[m - 1][i] <= n)
        {
            chmin(ans, i);
        }
    }
    if (ans == inf)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << ans << Endl;
    }
}
accuracy: 1
time_ms: 100
memory_kb: 12800
final_rank: 7

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll m = s.length();
    vector<vector<ll>> dp(m, vector<ll>(m + 1, inf));
    // dp[i][j]:i番目まで見てj個変更したときの値の最小値　ただしi番目までがvalidである
    auto ar = make_vec<ll>({m, 11, 11}, inf);
    // ar[i][j][k]:s[i:i+j-1]からk個変更したときの値の最小値　ただし全部数字にする　できないならinf
    rep(i, 0, m)
    {
        rep(j, 1, 11)
        {
            if (i + j > m)
            {
                continue;
            }
            string t = s.substr(i, j);
            ll cnt = 0;
            rep(k, 0, t.length())
            {
                if (t[k] == '+')
                {
                    cnt++;
                    if (j == 1 || k != 0)
                    {
                        t[k] = '0';
                    }
                    else
                    {
                        t[k] = '1';
                    }
                }
                else if (t[k] == '0')
                {
                    if (j != 1 && k == 0)
                    {
                        t[k] = '1';
                        cnt++;
                    }
                }
            }
            queue<ll> que;
            rep(k, 0, t.length())
            {
                if (k == 0)
                {
                    if (j == 1)
                    {
                        if (t[k] != '0')
                        {
                            que.push(k);
                        }
                    }
                    else
                    {
                        if (t[k] != '1')
                        {
                            que.push(k);
                        }
                    }
                }
                else
                {
                    if (t[k] != '0')
                    {
                        que.push(k);
                    }
                }
            }
            rep(k, cnt, t.length() + 1)
            {
                ar[i][j][k] = stoll(t);
                if (!que.empty())
                {
                    ll x = que.front();
                    que.pop();
                    if (x == 0)
                    {
                        t[x] = '1';
                    }
                    else
                    {
                        t[x] = '0';
                    }
                }
            }
        }
    }
    rep(i, 0, m)
    {
        rep(j, 0, m + 1)
        {
            rep(k, 1, 11)
            {
                ll f = 1;
                if (i - k < -1 || i - k == 0)
                {
                    continue;
                }
                if (i - k == -1)
                {
                    f = 0;
                }
                else
                {
                    if (s[i - k] == '+')
                    {
                        f = 0;
                    }
                }
                string t = s.substr(i - k + 1, k);
                // cout << t << endl;
                //  知りたいこと：tをx個変化させた時の最小値(ただし、全部数字にする)
                //  これをx=0~t.length()まで求める
                //  前計算しておくべきだな多分
                rep(x, 0, t.length() + 1)
                {
                    if (j - x - f < 0)
                    {
                        continue;
                    }
                    if (ar[i - k + 1][k][x] > n)
                    {
                        continue;
                    }
                    ll sub = ar[i - k + 1][k][x];
                    if (i - k - 1 >= 0)
                    {
                        sub += dp[i - k - 1][j - x - f];
                    }
                    chmin(dp[i][j], sub);
                }
            }
        }
    }
    ll ans = inf;
    rep(i, 0, m + 1)
    {
        if (dp[m - 1][i] <= n)
        {
            chmin(ans, i);
        }
    }
    if (ans == inf)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << ans << Endl;
    }
}
accuracy: 0.38461538461538464
time_ms: 60
memory_kb: 10240
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<ll(n); i++)
#define all(v) v.begin(), v.end()
template<class T> void chmin(T& l, const T& r){ if(r < l) l = r; }
template<class T> void chmax(T& l, const T& r){ if(l < r) l = r; }
const ll INF = 1ll << 60;
ll dp[1010][2000] = {};

void testcase(){
    ll N; cin >> N;
    string S; cin >> S; S.push_back('+');
    ll M = S.size();
    rep(i,M+1) rep(j,M+10) dp[i][j] = INF;
    dp[0][0] = 0;
    rep(s,M){
        if(s+2 <= M){
            ll d0 = 0;
            if(S[s+0] != '0') d0++;
            if(S[s+1] != '+') d0++;
            rep(i,M) chmin(dp[s+2][i+d0], dp[s][i]);
        }
        if(s+3 <= M){
            ll d0 = 0;
            if(S[s+0] != '1') d0++;
            if(S[s+1] != '0') d0++;
            if(S[s+2] != '+') d0++;
            rep(i,M) chmin(dp[s+3][i+d0], dp[s][i] + 10);
        }
        for(ll t=1; t<=10; t++) if(s+t+1 <= M){
            ll d = 0;
            ll v = 0;
            rep(i,t){
                v *= 10;
                if(i == 0){
                    if(S[s+i] == '+' || S[s+i] == '0'){
                        v += 1; d++;
                    } else{ v += S[s+i] - '0'; }
                } else {
                    if(S[s+i] == '+'){
                        v += 0; d++;
                    } else{ v += S[s+i] - '0'; }
                }
            }
            if(S[s+t] != '+') d++;
            rep(i,M) chmin(dp[s+t+1][i+d], dp[s][i] + v);
        }
    }
    ll ans = M+100;
    rep(i,M+1) if(dp[M][i] <= N){ ans = i; break; }
    if(ans > M) ans = -1;
    cout << ans << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    testcase();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 18216
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    ll X;
    string S;
    cin >> X >> S;
    int N = S.size();
    auto Calc = [&](int L, int R) -> vector<pair<int,ll>> {
        vector<pair<int,ll>> Ret;
        if (R - L == 1) {
            if (S[L] == '0') {
                Ret.push_back({0,0});
                return Ret;
            }
            else if (isdigit(S[L])) {
                Ret.push_back({0,S[L]-'0'});
                Ret.push_back({1,0});
                return Ret;
            }
            else {
                Ret.push_back({1,0});
                return Ret;
            }
        }
        string T = S.substr(L,R-L);
        int Count = 0;
        rep(i,0,T.size()) {
            if (!isdigit(T[i])) {
                Count++;
                if (i == 0) T[i] = '1';
                else T[i] = '0';
            }
        }
        if (T[0] == '0') {
            Count++;
            T[0] = '1';
        }
        vector<char> C(R-L,'0');
        C[0] = '1';
        Ret.push_back({Count,stoll(T)});
        rep(i,0,T.size()) {
            if (T[i] != C[i]) {
                Count++;
                T[i] = C[i];
                Ret.push_back({Count,stoll(T)});
            }
        }
        return Ret;
    };
    vector DP(N+1,vector<ll>(N+1,INF));
    DP[0][0] = 0;
    rep(i,0,N+1) {
        rep(j,1,11) { //i -> i+j+1
            if (i+j+1 > N) break;
            auto V = Calc(i,i+j);
            if (S[i+j] != '+') {
                for (auto& P : V) P.first++;
            }
            rep(k,0,i+1) {
                if (DP[i][k] > X) continue;
                for (auto P : V) {
                    chmin(DP[i+j+1][k+P.first], DP[i][k] + P.second);
                }
            }
        }
    }
    vector<ll> ANS(N+1,INF);
    rep(i,0,N+1) {
        rep(j,1,11) { //N-j -> N
            if (N-j < 0) break;
            auto V = Calc(N-j,N);
            if (DP[N-j][i] > X) continue;
            for (auto P : V) {
                chmin(ANS[i+P.first], DP[N-j][i] + P.second);
            }
        }
    }
    rep(i,0,N+1) {
        if (ANS[i] <= X) {
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 10948
final_rank: 4

code_snippet: #line 1 "library/Template/template.hpp"
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}
#line 2 "library/Utility/fastio.hpp"
#include <unistd.h>
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf

uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio

using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */
#line 3 "sol.cpp"

int main() {
    int X;
    string s;
    read(X, s);
    int n = SZ(s);

    vector val(n, vector(11, vector<ll>(12, -1))); // left,length,change
    rep(i, 0, n) {
        // len=1
        if (s[i] == '0') {
            rep(ch, 0, 12) val[i][1][ch] = 0;
        } else if (s[i] == '+') {
            rep(ch, 1, 12) val[i][1][ch] = 0;
        } else {
            val[i][1][0] = (s[i] - '0');
            rep(ch, 1, 12) val[i][1][ch] = 0;
        }
        rep(len, 2, 11) {
            if (i + len > n)
                break;
            string t = s.substr(i, len);
            int plus = 0;
            if (t[0] == '0') {
                t[0] = '1';
                plus++;
            }
            for (auto &c : t)
                if (c == '+') {
                    plus++;
                    c = '0';
                }
            if (t[0] == '0') {
                t[0] = '1';
            }
            rep(ch, plus, 12) {
                show(i, len, ch, stoll(t));
                val[i][len][ch] = stoll(t);
                rep(i, 0, SZ(t)) {
                    if (i == 0 and t[i] != '1') {
                        t[i] = '1';
                        break;
                    }
                    if (i != 0 and t[i] != '0') {
                        t[i] = '0';
                        break;
                    }
                }
            }
        }
    }

    vector dp(n + 1, vector<ll>(n + 5, INF));
    dp[0][0] = 0;
    rep(i, 0, n) {
        rep(j, 0, n + 1) if (dp[i][j] != INF) {
            rep(to, 1, 11) rep(ch, 0, 12) if (i + to <= n and j + ch <= n and
                                              val[i][to][ch] != -1) {
                if (i + to + 1 < n) {
                    chmin(dp[i + to + 1][j + ch + (s[i + to] != '+')],
                          dp[i][j] + val[i][to][ch]);
                }
                if (i + to == n) {
                    chmin(dp[i + to][j + ch], dp[i][j] + val[i][to][ch]);
                }
            }
        }
    }
    show(dp);
    int ret = inf;
    rep(ch, 0, n + 1) if (dp[n][ch] <= X) {
        ret = ch;
        break;
    }
    if (ret == inf)
        print(-1);
    else
        print(ret);
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 12604
final_rank: 8

code_snippet: #line 1 "a.cpp"
/*	author:  Kite_kuma
	created: 2022.07.07 18:00:33 */

#line 2 "SPJ-Library/template/kuma.hpp"

#line 2 "SPJ-Library/template/basic_func.hpp"

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool flag = true) { std::cout << (flag ? "Yes" : "No") << '\n'; }
void YES(bool flag = true) { std::cout << (flag ? "YES" : "NO") << '\n'; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(const T &x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(const Head &H, const Tail &... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T value) {
	for(auto &a : v) a += value;
	return;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

// ceil(a / b);
template <class T>
T dup(T a, T b) {
	assert(b);
	if(b < 0) {
		a *= -1;
		b *= -1;
	}
	return least_upper_multiple(a, b) / b;
}

long long pow_ll(long long a, long long n) {
	assert(n >= 0LL);
	if(n == 0) return 1LL;
	if(a == 0) return 0LL;
	if(a == 1) return 1LL;
	if(a == -1) return (n & 1LL) ? -1LL : 1LL;
	long long res = 1;
	while(n > 1LL) {
		if(n & 1LL) res *= a;
		a *= a;
		n >>= 1;
	}
	return res * a;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, const long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return (int)std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return (int)std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(const std::vector<T> &a) {
	std::vector<T> vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(const auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}
#line 1 "SPJ-Library/template/header.hpp"
#include <bits/stdc++.h>
#line 2 "SPJ-Library/template/io.hpp"

#line 4 "SPJ-Library/template/io.hpp"

#line 8 "SPJ-Library/template/debug.hpp"

#line 3 "SPJ-Library/template/constants.hpp"

constexpr int inf = 1000'000'000;
constexpr long long INF = 1'000'000'000'000'000'000LL;
constexpr int mod_1000000007 = 1000000007;
constexpr int mod_998244353 = 998244353;
const long double pi = acosl(-1.);
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#line 10 "SPJ-Library/template/debug.hpp"

namespace viewer {

template <typename T, typename U>
void view(const std::pair<T, U> &p);

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p);

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p);

void view(const long long &e);

void view(const int &e);

template <typename T>
void view(const T &e);

template <typename T>
void view(const std::set<T> &s);

template <typename T>
void view(const std::unordered_set<T> &s);

template <typename T>
void view(const std::multiset<T> &s);

template <typename T>
void view(const std::unordered_multiset<T> &s);

template <typename T>
void view(const std::vector<T> &v);

template <typename T, std::size_t ary_size>
void view(const std::array<T, ary_size> &v);

template <typename T>
void view(const std::vector<std::vector<T>> &vv);

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v);

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v);

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v);

template <typename map_type>
void view_map_container(const map_type &m);

template <typename T, typename U>
void view(const std::map<T, U> &m);

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m);

template <typename container_type>
void view_container(const container_type &c, bool vertically = false) {
	typename container_type::const_iterator begin = c.begin();
	const typename container_type::const_iterator end = c.end();
	if(vertically) {
		std::cerr << "{\n";
		while(begin != end) {
			std::cerr << '\t';
			view(*(begin++));
			if(begin != end) std::cerr << ',';
			std::cerr << '\n';
		}
		std::cerr << '}';
		return;
	}
	std::cerr << "{ ";
	while(begin != end) {
		view(*(begin++));
		if(begin != end) std::cerr << ',';
		std::cerr << ' ';
	}
	std::cerr << '}';
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << '(';
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ')';
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << '(';
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ')';
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << '(';
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ')';
}

void view(const long long &e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int &e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T &e) {
	std::cerr << e;
}

template <typename T>
void view(const std::set<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::multiset<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::unordered_multiset<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::vector<T> &v) {
	view_container(v);
}

template <typename T, std::size_t ary_size>
void view(const std::array<T, ary_size> &v) {
	view_container(v);
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	view_container(vv, true);
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	view_container(v, true);
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	view_container(v, true);
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	view_container(v, true);
}

template <typename map_type>
void view_map_container(const map_type &m) {
	std::cerr << "{\n";
	for(typename map_type::const_iterator it = m.begin(); it != m.end(); it++) {
		std::cerr << "\t[";
		view(it->first);
		std::cerr << "] : ";
		view(it->second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	view_map_container(m);
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	view_map_container(m);
}

}  // namespace viewer

// when compiling : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename T>
void debug_out(const T &x) {
	viewer::view(x);
}
template <typename Head, typename... Tail>
void debug_out(const Head &H, const Tail &... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "]\n";                                       \
	} while(0)
#else
#define debug(...) (void(0))
#endif
#line 2 "SPJ-Library/template/scanner.hpp"

#line 6 "SPJ-Library/template/scanner.hpp"

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#line 7 "SPJ-Library/template/io.hpp"

#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

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

template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << ' ' << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++) {
		if(it != v.begin()) std::cerr << ' ';
		os << *it;
	}
	return os;
}

struct fast_io {
	fast_io() {
		std::ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		std::cout << std::fixed << std::setprecision(15);
		srand((unsigned)time(NULL));
	}
} fast_io_;
#line 2 "SPJ-Library/template/macros.hpp"

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;
#line 7 "SPJ-Library/template/kuma.hpp"

using namespace std;
#line 5 "a.cpp"
int solve(ll n) {
	string s;
	cin >> s;
	map<char, int> ids;
	rep(i, 10) ids[char('0' + i)] = i;
	ids['+'] = (int)ids.size();

	int csize = int(ids.size());
	s.insert(s.begin(), '+');
	int ss = int(s.size());

	vvll minvalue(ss + 1, vll(ss + ss + 1, n + 1));
	minvalue[0][0] = 0LL;

	V<string> smallest_terms(11);
	rep(termlen, 2, 11) {
		if(termlen == 2) {
			smallest_terms[2] = "+0";
		} else {
			smallest_terms[termlen] = "+1";
			rep(termlen - 2) smallest_terms[termlen] += '0';
		}
	}

	rep(leftside, ss) rep(c, ss + 1) {
		rep(termlen, 2, 11) {
			const int rightside = leftside + termlen;
			if(rightside > ss) continue;
			vector<int> changed_place(termlen);
			string cut = s.substr(leftside, termlen);
			rep(i, termlen) {
				if(i == 0 && cut[i] != '+') {
					changed_place[i] = 1;
					cut[i] = '+';
				}
				if(i && cut[i] == '+') {
					changed_place[i] = 1;
					cut[i] = '0';
				}
				if(i == 1 && cut[i] == '0' && termlen > 2) {
					changed_place[i] = 1;
					cut[i] = '1';
				}
			}
			int changed = count(all(changed_place), 1);
			auto update = [&]() {
				chmin(minvalue[rightside][c + changed], minvalue[leftside][c] + stoll(cut.substr(1)));
				return;
			};
			update();
			auto minnvalue_update = [&](int place) -> int {
				if(place != 1) return 0;
				if(termlen == 2) return 0;
				return 1;
			};
			rep(i, 1, termlen) {
				if(changed_place[i] == 0 && minnvalue_update(i) != cut[i]) {
					cut[i] = minnvalue_update(i) + '0';
					changed++;
					changed_place[i] = 1;
					update();
				}
			}
		}
	}

	int ans = inf;
	rep(i, ss + 1) if(minvalue[ss][i] <= n) chmin(ans, i);
	return ans == inf ? -1 : ans;
}

int main() {
	ll n;
	while(cin >> n && n) {
		cout << solve(n) << '\n';
	}
}
accuracy: 1
time_ms: 1500
memory_kb: 18920
final_rank: 12

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <complex>
#include <bitset>
#include <iomanip>
#include <memory>
#include <functional>

#define rep(i, n, s) for (int i = (s); i < int(n); i++)
#define per(i, n, s) for (int i = (n) - 1; i >= int(s); i--)
#define MM << " " <<
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using MaxHeap = std::priority_queue<T>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
using Pdd = std::pair<double, double>;

template <class T>
bool chmin(T &a, const T b) {
    if (b < a) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
bool chmax(T &a, const T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void vdeb(const std::vector<T> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        if (i == n - 1)
            std::cout << da[i];
        else
            std::cout << da[i] << " ";
    }
    std::cout << std::endl;
}
template<class T>
void vdeb(const std::vector<std::vector<T>> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        // std::cout << i << " : ";
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

using namespace std;

int main() {
    ll n; cin >> n;
    string s; cin >> s;
    int m = s.size();
    // len, modified
    vector<vector<ll>> dp(m, vector<ll>(m+1, n+2));
    dp[0][s[0]!='0'?1:0] = 0;
    int k = to_string(n).size();
    vector<ll> pow10(k+1,1);
    rep(i,k,0) pow10[i+1] = pow10[i] * 10;
    rep(i,k+1,1) {
        if(m < i) break;
        int c = 0;
        ll now = 0;
        string t = s.substr(0, i);
        rep(j,i,0) {
            if(t[j] == '+') {
                c++;
                t[j] = j==0 ? '1' : '0';
            }
            if(j == 0 && t[j] == '0') {
                c++;
                t[j] = '1';
            }
            now = now * 10 + t[j] - '0';
        }
        chmin(dp[i-1][c], now);
        rep(j,i,0) {
            if(j == 0 && t[j] != '1') {
                c++;
                now -= pow10[i-j-1] * (t[j] - '1');
            } else if(j != 0 && t[j] != '0') {
                c++;
                now -= pow10[i-j-1] * (t[j] - '0');
            }
            chmin(dp[i-1][c], now);
        }
        // assert(now);
    }
    // vdeb(dp);
    rep(i,m,0) {
        rep(j,k+2,2) {
            if(i+j >= m) break;
            int c = 0;
            ll now = 0;
            string t = s.substr(i+1, j);
            if(t[0] != '+') c++;
            rep(ii,j,1) {
                if(t[ii] == '+') {
                    c++;
                    t[ii] = j!=2&&ii==1 ? '1' : '0';
                }
                if(j != 2 && ii == 1 && t[ii] == '0') {
                    c++;
                    t[ii] = '1';
                }
                now = now * 10 + t[ii] - '0';
            }
            vector<ll> mod(j+1, n+2);
            mod[c] = now;
            rep(ii,j,1) {
                if(j != 2 && ii == 1 && t[ii] != '1') {
                    c++;
                    now -= pow10[j-ii-1] * (t[ii] - '1');
                } else if((j == 2 || ii != 1) && t[ii] != '0') {
                    c++;
                    now -= pow10[j-ii-1] * (t[ii] - '0');
                }
                mod[c] = now;
            }
            // assert(now);
            rep(ii,m+1,0) {
                rep(jj,j+1,0) {
                    if(m < ii+jj) break;
                    chmin(dp[i+j][ii+jj], dp[i][ii] + mod[jj]);
                }
            }
        }
    }
    // vdeb(dp);
    int ans = m+1;
    per(i,m+1,0) {
        if(dp[m-1][i] <= n) ans = i;
    }
    cout << (ans == m+1 ? -1 : ans) << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 11008
final_rank: 5

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-7;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> K;
	string s;
	cin >> s;
	s.push_back('+');
	N = s.size();
	vector<vector<int>>dp(N + 1, vector<int>(N + 1, 1 << 30));
	dp[0][0] = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (dp[i][j] == 1 << 30)continue;
			for (int k = 1; k < 10; k++) {
				if (i + k + 1 > N)break;
				for (int l = 0; l <= k; l++) {
					int add = s[i + k] != '+';
					int num = 0;
					int amari = l;
					string t;
					for (int m = i; m < i + k; m++) {
						t.push_back(s[m]);
						if (t.back() == '+') {
							amari--;
							if (m != i || k == 1)t.back() = '0';
							else t.back() = '1';
						}
						else if (t.back() == '0') {
							if (m == i && k > 1) {
								t.back() = '1';
								amari--;
							}
						}
					}
					if (amari >= 0) {
						for (int m = 0; m < k; m++) {
							num *= 10;
							if (amari) {
								if (m != 0 || k == 1) {
									if (t[m] != '0')amari--;
								}
								else {
									num += 1;
									if (t[m] != '1')amari--;
								}
							}
							else {
								num += t[m] - '0';
							}
						}
						dp[i + k + 1][j + add+l] = min(dp[i + k + 1][j + add+l], dp[i][j] + num);
					}
				}
			}
		}
	}
	int ans = N;
	//for (int i = 0; i <= N; i++) {
	//	for (int j = 0; j <= N; j++) {
	//		if (dp[i][j] == 1 << 30)continue;
	//		cout << i << " " << j << " " << dp[i][j] << endl;
	//	}
	//}
	for (int i = 0; i <= N; i++) {
		if (dp[N][i] <= K)ans = min(ans, i);
	}
	if (ans == N)ans = -1;
	cout << ans << endl;
}
accuracy: 1
time_ms: 430
memory_kb: 7048
final_rank: 2

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    string s; cin >> s;
    int m = s.size();
    if(m%2 == 0){
        if(n < 10){
            cout << -1 << endl;
            return 0;
        }
    }
    vector<ll> p10(10,1);
    for(int i=1;i<10;i++){
        p10[i] = p10[i-1] * 10LL;
    }
    // dp[i][j]:i番目まで見てj個修正した時の最小値
    // その時点で式が正しく合って欲しい
    // 大体数個おきに+がくるので遷移の計算量は小さくなりそう
    const int inf = 2e9;
    vector<vector<int>> dp(m+1,vector<int>(m+1,inf));
    dp[0][0] = 0;
    for(int i=0;i<m;i++){
        for(int j=0;j<=i;j++){
            if(dp[i][j] > n) continue;
            // s[i]~s[i+k] までで数字を作る
            { // 一桁
                if(s[i] != '+'){
                    if(i+2<m){
                        ll tmp = (s[i]-'0') + dp[i][j];
                        int nx = (s[i+1]!='+') + j;
                        if(tmp <= n){
                            dp[i+2][nx] = min(dp[i+2][nx], (int)tmp);
                        }
                        // 0に変換
                        dp[i+2][nx+1] = min(dp[i+2][nx+1], dp[i][j]);
                    }
                    else if(i+1 == m){
                        ll tmp = (s[i]-'0') + dp[i][j];
                        if(tmp <= n) dp[i+1][j] = min(dp[i+1][j], (int)tmp);
                        dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
                    }
                }
                else{
                    if(i+2<m){
                        int nx = (s[i+1]!='+') + j;
                        // 0に変換
                        dp[i+2][nx+1] = min(dp[i+2][nx+1], dp[i][j]);
                    }
                    else if(i+1 == m){
                        dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
                    }
                }
            }
            int plus = 0;
            vector<int> v;
            ll tmp = 0;
            if(s[i] == '+'){
                v.push_back(1);
                plus++;
            }
            else{
                if(s[i] == '0'){
                    v.push_back(1);
                    plus++;
                }
                else{
                    v.push_back(s[i]-'0');
                }
            }
            tmp = v[0];
            for(int k=1;k<=9;k++){
                if(i+k>=m) break;
                if(s[i+k] == '+'){
                    plus++;
                    v.push_back(0);
                }
                else{
                    v.push_back(s[i+k]-'0');
                }
                tmp = tmp * 10 + v.back();
                if(i+k+1 == m-1)continue;
                ll cp = tmp;
                int cnt = 0;
                { // 変更しない時の遷移
                    if(i+k == m-1){
                        if((ll)dp[i][j]+tmp <= n){
                            dp[m][j+plus] = min(dp[m][j+plus], dp[i][j]+(int)tmp);
                        }
                    }
                    else if(i+k+2<m){
                        if((ll)dp[i][j]+tmp <= n){
                            dp[i+k+2][j+plus+(s[i+k+1] != '+')] = min(dp[i+k+2][j+plus+(s[i+k+1] != '+')], dp[i][j]+(int)tmp);
                        }
                    }
                }
                for(int p=0;p<=k;p++){
                    int opt = (p==0?1:0);
                    if(v[p] != opt){
                        cnt++;
                        cp -= p10[k-p] * v[p];
                        cp += p10[k-p] * opt;
                    }
                    if(i+k == m-1){
                        if((ll)dp[i][j]+cp <= n){
                            dp[m][j+plus+cnt] = min(dp[m][j+plus+cnt], dp[i][j]+(int)cp);
                        }
                    }
                    else if(i+k+2<m){
                        if((ll)dp[i][j]+cp <= n){
                            dp[i+k+2][j+plus+(s[i+k+1] != '+')+cnt] = min(dp[i+k+2][j+plus+(s[i+k+1] != '+')+cnt], dp[i][j]+(int)cp);
                        }
                    }
                }
            }
        }
    }
    for(int i=0;i<=m;i++){
        if(dp[m][i] <= n){
            cout << i << endl;
            break;
        }
    }
}
accuracy: 1
time_ms: 130
memory_kb: 6960
final_rank: 1

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
const int mod = 998244353;

ll dp[1010][1010];

int main(){
    int x; cin >> x;
    string s; cin >> s;
    s.push_back('+');
    int n = s.size();
    rep(i,n+1) rep(j,n+1) dp[i][j] = inf;
    dp[0][0] = 0;
    vl p(12,1); rep(i,11) p[i+1] = p[i] * 10;
    rep(i,n) REP(k,1,min(11,i+1)){
        string t = s.substr(i-k,k);
        reverse(all(t));
        ll num = 0;
        int cost = 0;
        rep(l,k){
            if(k > 1 && l == k-1){
                if(t[l] == '0' || t[l] == '+'){
                    t[l] = '1';
                    cost++;
                }
            }else{
                if(t[l] == '+'){
                    t[l] = '0';
                    cost++;
                }
            }
            num += p[l] * (t[l] - '0');
        }
        if(s[i] != '+') cost++;
        rep(j,i+1){
            int c = cost;
            ll nu = num;
            if(j+c <= n) chmin(dp[i+1][j+c], dp[i-k][j] + nu);
            if(k == 1 && j+c+1 <= n) chmin(dp[i+1][j+c+1], dp[i-k][j]);
            for(int l=k-1; l>=0; l--){
                if(l==k-1) c += (t[l] != '1');
                else c += (t[l] != '0');
                nu -= p[l] * (t[l] - '0' - (l==k-1));
                if(j+c <= n) chmin(dp[i+1][j+c], dp[i-k][j] + nu);
            }
        }
    }
    rep(i,n+1){
        if(dp[n][i] <= x){
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 11324
final_rank: 6

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
const int mod = 998244353;

ll dp[1010][1010];

int main(){
    int x; cin >> x;
    string s; cin >> s;
    s.push_back('+');
    int n = s.size();
    rep(i,n+1) rep(j,n+1) dp[i][j] = inf;
    dp[0][0] = 0;
    vl p(12,1); rep(i,11) p[i+1] = p[i] * 10;
    rep(i,n) REP(k,1,min(11,i+1)){
        string t = s.substr(i-k,k);
        reverse(all(t));
        ll num = 0;
        int cost = 0;
        rep(l,k){
            if(k > 1 && l == k-1){
                if(t[l] == '0' || t[l] == '+'){
                    t[l] = '1';
                    cost++;
                }
            }else{
                if(t[l] == '+'){
                    t[l] = '0';
                    cost++;
                }
            }
            num += p[l] * (t[l] - '0');
        }
        if(s[i] != '+') cost++;
        rep(j,i+1){
            int c = cost;
            int nu = num;
            if(j+c <= n) chmin(dp[i+1][j+c], dp[i-k][j] + nu);
            if(k == 1 && j+c+1 <= n) chmin(dp[i+1][j+c+1], dp[i-k][j]);
            for(int l=k-1; l>=0; l--){
                if(l==k-1) c += (t[l] != '1');
                else c += (t[l] != '0');
                nu -= p[l] * (t[l] - '0' - (l==k-1));
                if(j+c <= n) chmin(dp[i+1][j+c], dp[i-k][j] + nu);
            }
        }
    }
    rep(i,n+1){
        if(dp[n][i] <= x){
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
accuracy: 0.2564102564102564
time_ms: 30
memory_kb: 9552
final_rank: 20