Problem:
Problem B
お小遣いの最大化
鐘星くんの今月のお小遣いの金額は，次のようなゲームで決まることになった．
はじめに，
N
枚のカードが横一列に並んでいる．それぞれのカードには，1 から 9 までのいずれかの数字が 1 つ書かれている．また，整数
K
が指定される．ここで
N
は
K
の倍数である．
鐘星くんは，これら
N
枚のカードの中から，何枚かのカードを選んで破り捨てることができる．ここで，破り捨てるカードの枚数は，
K
の倍数でなければならない．なお，1 枚も破り捨てなくてもよいし，
N
枚全部破り捨ててもよい．
残ったカードの枚数は
K
の倍数である．これらのカードを，順番を変えることなく，左から順に
K
枚ずつのグループに分ける．
それぞれのグループのカードに書かれた数字を，
K
桁の十進数とみなす．これら
K
桁の数をすべて足し合わせた値が，鐘星くんの今月のお小遣いの金額となる．
たとえば，
N = 9
,
K = 3
で，カードに書かれた数字が左から順に 2, 9, 9, 7, 9, 2, 4, 5, 8 であったとする．これらのカードを 1 枚も破り捨てない場合，鐘星くんの今月のお小遣いは 299 + 792 + 458 = 1549 円である．一方，1 枚目，6 枚目，7 枚目の計 3 枚のカードを破り捨てる場合，残った 6 枚のカードに書かれた数字は左から順に 9, 9, 7, 9, 5, 8 となり，お小遣いは 997 + 958 = 1955 円となる．なお，すべてのカードを破り捨てた場合，お小遣いは 0 円である．
鐘星くんが破り捨てるカードを適切に選んだとき，今月のお小遣いは最大でいくらになるだろうか．
Input
入力は
50
個以下のデータセットからなる．各データセットは次の形式で表される．
K
s
1
s
2
...s
N
1 行目には整数
K
が与えられる．
K
は
2
以上
14
以下であることが保証される．
2 行目には文字列が与えられ，その長さが
N
である．
N
は
K
以上
200,000
以下の整数であり，かつ
K
の倍数であることが保証される．また，この文字列の
i
番目の文字
s
i
は，はじめに左から
i
番目のカードに書かれている数字であり，1 以上 9 以下であることが保証される．
入力の終わりは 1 つのゼロからなる行で表される．
Output
各データセットに対し，鐘星くんの今月のお小遣いの最大値を表す整数を，1 行に出力せよ．
なお，この問題の制約から，答えが
2
63
未満であることは証明できる．ただし，32 ビット整数型では表現できない場合があることに注意せよ．
Sample Input
3
299792458
14
37828944296678
2
9335413296572977435242595825577315838186532253894144498711392753253512
0
Output for the Sample Input
1955
37828944296678
2022

Submitted solutions:

code_snippet: #define _USE_MATH_DEFINES
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using H = pair<int, int>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define rng(i, s, n) for(int i = (s) ; i < (n) ; i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define siz(a) int((a).size())
#define crdcomp(b) sort(all((b))); (b).erase(unique(all((b))), (b).end())
#define getidx(b, i) (lower_bound(all((b)), (i))-(b).begin())
#define cdf(n) for(int __quetimes=(n); __quetimes>0; __quetimes--)
#define readv(a, n) (a).resize((n), 0); rep(i, (n)) (a)[i]=read()
#define found(a, x) (a.find(x)!=a.end())
#define slp(x, y) min((x), (y)), max((x), (y))
constexpr ll mod = (ll)1e9 + 7;
constexpr ll Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = (ll)3 * 1e18;
constexpr int Inf = 1e9 + 10;
constexpr int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T = ll>T read() { T u, k = scanf("%lld", &u); return u; }
template<typename T>bool ina(pair<T, T> t, T h, T w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
template<typename T>void fin(T x) { cout << x << endl; exit(0); }/*
#include<atcoder/all>
using namespace atcoder;//*/
//--------------------------------------------------------------
signed main() {
	int n, k;
	string s;
	while (cin >> k && k) {
		cin >> s;
		n = s.size();
		vec<vl>dp(n + 1, vl(k, -1));
		vl pw(k, 1);
		rng(i, 1, k) pw[i] = pw[i - 1] * 10;
		dp[0][0] = 0;
		rep(i, n)rep(j, k) if (~dp[i][j]) {
			chmax(dp[i + 1][j], dp[i][j]);
			chmax(dp[i + 1][(j + 1) % k], dp[i][j] + pw[k - 1 - j] * (s[i] - '0'));
		}
		cout << dp[n][0] << endl;
	}
}
accuracy: 1
time_ms: 380
memory_kb: 33040
final_rank: 10

code_snippet: #ifdef LOCAL
#define _GLIBCXX_DEBUG
#pragma GCC optimize("O0")
#else
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

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

// #include <atcoder/modint>
// using mint = atcoder::modint998244353;
// istream& operator>>(istream& in, mint& x) {
//     long long a;
//     in >> a;
//     x = a;
//     return in;
// }
// ostream& operator<<(ostream& out, const mint& x) {
//     return out << x.val();
// }

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
using vvi = vvc<int>;
using vvl = vvc<ll>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pqg = std::priority_queue<T, vector<T>, greater<T>>;
template <class T, class U>
using umap = unordered_map<T, U>;

// template <typename K>
// using tree = __gnu_pbds::tree<K, __gnu_pbds::null_type, std::less<>,
//                               __gnu_pbds::rb_tree_tag,
//                               __gnu_pbds::tree_order_statistics_node_update>;

#define vv(type, name, h, ...) \
    vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)     \
    vector<vector<vector<type>>> name( \
        h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)         \
    vector<vector<vector<vector<type>>>> name( \
        a, vector<vector<vector<type>>>(       \
               b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// FOR(a) :=  for (ll _ = 0; _ < (ll)a; ++_)
// FOR(i, a) := for (ll i = 0; i < (ll)a; ++i)
// FOR(i, a, b) := for (ll i = a; i < (ll)b; ++i)
// FOR(i, a, b, c) := for (ll i = a; i < (ll)b; i += (c))
// FOR_R(a) := for (ll i = (a) - 1; i >= 0; --i)
// FOR_R(i, a) := for (ll i = (a) - 1; i >= 0; --i)
// FOR_R(i, a, b) := for (ll i = (b) - 1; i >= (ll)a; --i)
#define FOR1(a) for (ll _ = 0; _ < (ll)a; ++_)
#define FOR2(i, a) for (ll i = 0; i < (ll)a; ++i)
#define FOR3(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < (ll)b; i += (c))
#define FOR1_R(a) for (ll i = (a) - 1; i >= 0; --i)
#define FOR2_R(i, a) for (ll i = (a) - 1; i >= 0; --i)
#define FOR3_R(i, a, b) for (ll i = (b) - 1; i >= (ll)a; --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) \
    for (int t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
    return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
    return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
    return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
    T q = floor(x, y);
    return {q, x - q * y};
}

template <typename T, typename U>
T POW(U x_, int n) {
    T x = x_;
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
    T sm = 0;
    for (auto &&a : A) sm += a;
    return sm;
}

#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
    sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <class T, class S>
inline bool chmax(T &a, const S &b) {
    return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
    return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
    vc<int> A(S.size());
    FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
    return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
    int N = A.size();
    vector<T> B(N + 1);
    FOR(i, N) { B[i + 1] = B[i] + A[i]; }
    if (off == 0) B.erase(B.begin());
    return B;
}

template <typename T>
vector<int> argsort(const vector<T> &A) {
    vector<int> ids(A.size());
    iota(all(ids), 0);
    sort(all(ids),
         [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
    return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
    vc<T> B(I.size());
    FOR(i, I.size()) B[i] = A[I[i]];
    return B;
}

template<class... T>
constexpr auto min(T... a){
    return min(initializer_list<common_type_t<T...>>{a...});
}
template<class... T>
constexpr auto max(T... a){
    return max(initializer_list<common_type_t<T...>>{a...});
}

void print(){
    cout << '\n';
}
template<class T>
void print(const T& a){
    cout << a << '\n';
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}
template<class T>
void print(vector<T> &a){
    for (int i = 0; i < (int)a.size(); ++i) {
        cout << a[i] << " \n"[i == (int)a.size() - 1];
    }
}
template<class T>
void print(vector<T> &&a){
    for (int i = 0; i < (int)a.size(); ++i) {
        cout << a[i] << " \n"[i == (int)a.size() - 1];
    }
}
template<class T>
void print(vector<vector<T>> &a){
    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < (int)a[i].size(); ++j) {
            cout << a[i][j] << " \n"[j == (int)a[i].size() - 1];
        }
    }
}
template<class T>
void print(vector<vector<T>> &&a){
    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < (int)a[i].size(); ++j) {
            cout << a[i][j] << " \n"[j == (int)a[i].size() - 1];
        }
    }
}
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

#ifdef LOCAL
// https://zenn.dev/sassan/articles/19db660e4da0a4
#include "/Library/cpp-dump/dump.hpp"
#define dump(...) cpp_dump(__VA_ARGS__)
// CPP_DUMP_DEFINE_EXPORT_OBJECT(mint, val());
#else
#define dump(...)
#define CPP_DUMP_SET_OPTION(...)
#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)
#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)
#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)
#endif


//----------------------------------------------------------------

bool solve() {
    ll K;
    cin >> K;
    if (K == 0) {
        return false;
    }
    string S;
    cin >> S;
    ll N = S.size();
    vl A(N);
    FOR(i, N) {
        A[i] = S[i] - '0';
    }
    vv(ll, dp, N + 1, K, -infty<ll>);
    vl tenpow(K);
    tenpow[0] = 1;
    FOR(i, 1, K) {
        tenpow[i] = tenpow[i - 1] * 10;
    }
    dp[0][K - 1] = 0;
    FOR(i, N) {
        FOR(j, K) {
            ll nj = (j == 0) ? K - 1 : j - 1;
            ll ni = i + 1;
            if (dp[i][j] < 0) continue;
            chmax(dp[ni][j], dp[i][j]);
            chmax(dp[ni][nj], dp[i][j] + A[i] * tenpow[j]);
        }
    }
    print(dp[N][K - 1]);

    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
    CPP_DUMP_SET_OPTION(max_line_width, 80);
    CPP_DUMP_SET_OPTION(log_label_func, cpp_dump::log_label::filename());
    CPP_DUMP_SET_OPTION(enable_asterisk, true);
    while (solve()) {
    }
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 35048
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; 0 <= i; --i)
template <typename T> bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void solve();
int K;
string S;
ll pow10[14];
signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);

  pow10[0]=1;
  rep(i,13)pow10[i+1]=pow10[i]*10;

  int TESTCASES = 1<<30;
  //cin >> TESTCASES;
  while (TESTCASES--) {
    cin>>K;
    if(K==0)break;
    cin>>S;
    solve();
  }
}
ll dp[2<<17][14];
void solve() {
  int n=S.size();
  constexpr ll inf=(ll)1<<60;
  rep(i,n+1)rep(j,K)dp[i][j]=-inf;
  dp[0][0]=0;
  rep(i,n)rep(j,K)if(dp[i][j]!=-inf){
    ll digit=S[i]-'0';
    chmax(dp[i+1][j],dp[i][j]+digit*pow10[(K-1-i%K+j)%K]);
    chmax(dp[i+1][(j+1)%K],dp[i][j]);
  }
  cout<<dp[n][0]<<'\n';
}
accuracy: 1
time_ms: 210
memory_kb: 26252
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; 0 <= i; --i)
template <typename T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
void solve();
int K;
string S;
signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int TESTCASES = 1 << 30;
    // cin >> TESTCASES;
    while (TESTCASES--) {
        cin >> K;
        if (K == 0) break;
        cin >> S;
        solve();
    }
}
// 17:20
void solve() {
    int N = (int)S.size();
    constexpr ll inf = (ll)1 << 60;
    vector<vector<ll>> dp(N + 1, vector<ll>(K, -inf));
    vector<ll> pow10(18, 1);
    rep(i, 17) pow10[i + 1] = pow10[i] * 10;
    dp[0][0] = 0;
    rep(i, N) {
        rep(j, K) {
            ll digit = S[i] - '0';
            if (dp[i][j] == -inf) continue;
            int kurai = (K - i % K + j + K - 1) % K;
            chmax(dp[i + 1][j], dp[i][j] + pow10[kurai] * digit);
            chmax(dp[i + 1][(j + 1) % K], dp[i][j]);
        }
    }
    cout << dp[N][0] << '\n';
}
accuracy: 1
time_ms: 340
memory_kb: 33336
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;
#define logy(bool)         \
  if (bool) {              \
    cout << "Yes" << endl; \
  } else {                 \
    cout << "No" << endl;  \
  }

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

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

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int k;
  while (1) {
    in(k);
    if (k == 0) break;
    string s;
    in(s);
    int n = s.size();

    vector<int> a(n);
    rep(i, n) {
      a[i] = s[i] - '0';
    }

    vector dp(n + 1, vector(k, 0ll));
    vector<lint> power(15);
    power[0] = 1;
    rep(i, 1, 15) {
      power[i] = power[i - 1] * 10;
    }
    power[k] = 1;
    rep(i, n) {
      rep(j, k) {
        // hirou
        if (i < j) continue;
        dp[i + 1][(j + 1) % k] = max(dp[i + 1][(j + 1) % k], dp[i][j] + power[k - j - 1] * a[i]);
        // suteru
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      }
    }

    out(dp[n][0]);
  }
}
accuracy: 1
time_ms: 250
memory_kb: 34248
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, vector<T>, greater<T>>;
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, l, r) for (ll i = (l); i < (r); i++)
#define rrep2(i, n) for (ll i = n; i-- > 0;)
#define rrep3(i, r, l) for (ll i = (r); i-- > (l);)
#define overload(a, b, c, d, ...) d
#define rep(...) overload(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rrep(...) overload(__VA_ARGS__, rrep3, rrep2)(__VA_ARGS__)
#define all(x) begin(x), end(x)
bool chmin(auto& lhs, auto rhs) {
    return lhs > rhs ? lhs = rhs, 1 : 0;
}
bool chmax(auto& lhs, auto rhs) {
    return lhs < rhs ? lhs = rhs, 1 : 0;
}
struct IOIO {
    IOIO() {
        std::cin.tie(0)->sync_with_stdio(0);
    }
} ioio;

void solve() {
    while (true) {
        int k;
        cin >> k;
        if (k == 0) break;
        string s;
        cin >> s;
        //  dp[i][j]: i枚目まで処理して、消した数がjであるときの最大値
        vector<ll> p10(k);
        p10[0] = 1;
        rep(i, k - 1) p10[i + 1] = p10[i] * 10;
        vector<ll> dp(k, -1);
        dp[0] = 0;
        for (auto c: s) {
            vector<ll> ndp(k, -1);
            int d = c - '0';
            rep(i, k) {
                if (dp[i] == -1) continue;
                //  remain
                chmax(ndp[(i - 1 + k) % k], dp[i] + (ll)d * p10[(i - 1 + k) % k]);

                //  erase
                chmax(ndp[i], dp[i]);
            }
            dp = std::move(ndp);
        }
        cout << dp[0] << '\n';
    }
}

int main() {
    int t = 1;
    //  cin >> t;
    while (t--) solve();
}
accuracy: 1
time_ms: 130
memory_kb: 4068
final_rank: 3

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

void solve(int k) {
    string s;
    cin >> s;
    int n = s.size();
    vector<int> a(n);
    for (int i=0; i<n; ++i) {
        a[i] = s[i]-'0';
    }

    int INFL = 4004004003094073385LL;
    vector<vector<int>> dp(n+1, vector<int>(k, -INFL));
    //dp[i][j] = i番目までみて既にj個消してる時の和の最大値

    dp[0][0] = 0;

    for (int i=0; i<n; ++i) {
        for (int j=0; j<k; ++j) {
            if (dp[i][j] == -INFL) continue;
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            dp[i+1][(j+1)%k] = max(dp[i+1][(j+1)%k], dp[i][j] + a[i]*(int)(powl(10, k-1-j)));
        }
    }
    cout << dp[n][0] << endl;
    
}

signed main() {
    while(true) {
        int k;
        cin >> k;
        if (k == 0) break;
        solve(k);
    }
}

/*
memo


*/
accuracy: 1
time_ms: 6960
memory_kb: 34988
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}
constexpr int INTINF = 1000001000;
constexpr int INTMAX = 2147483647;
constexpr ll LLMAX = 9223372036854775807;
constexpr ll LLINF = 4450000000011100000;

// a^n
ll pow(ll a, ll n) {
  ll base = a;
  ll ret = 1;
  while (n > 0) {
    if (n & 1) ret *= base;
    base *= base;
    n >>= 1;
  }
  return ret;
}

void solve(int K) {
  string S; cin >> S;
  int N = int(S.size());
  // dp[i][k] := いままで選んだ枚数modKがkのとき、金額の総和のmax
  vector<vector<ll>>dp(N+1, vector(K, -LLINF));

  dp[0][0] = 0;
  for (int i=0; i<N; i++) {
    ll a = S[i] - '0';
    for (int j=0; j<K; j++) {
      // 選ぶ場合
      chmax(dp[i+1][(j+1)%K], dp[i][j] + pow(10, K-j-1) * a);
      chmax(dp[i+1][j], dp[i][j]);
    }
    /*
    K==3の場合
    // from j==0
    {
      100 = 10**(k-1)
      = 10 ** (k-j-1)
      chmax(dp[i+1][(j+1)%K], dp[i][j] + 10**(k-j-1) * a);

      //chmax(dp[i+1][1], dp[i][0] + 100 * a);
    }
    // from j==1
    {
      chmax(dp[i+1][2], dp[i][1] + 10 * a);
    }
    // from j==2
    {
      chmax(dp[i+1][0], dp[i][2] + a);
    }
    */
  }
  cout << dp[N][0] << endl;
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  while (true) {
    int K; cin >> K;
    if (K == 0) break;
    else solve(K);
  }
  return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 33372
final_rank: 8

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

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<long long, long long>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
using vvc = vector<vector<char>>;
using vvvi = vector<vector<vector<int>>>;
using pii = pair<int, int>;
using vvb = vector<vector<bool>>;
using Graph = vector<vector<ll>>;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 998244353;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (m); i >= (int)(n); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

vi dx4 = {1, 0, -1, 0}, dy4 = {0, 1, 0, -1};
vi dx8 = {1, 1, 1, 0, 0, -1, -1, -1}, dy8 = {1, 0, -1, 1, -1, 1, 0, -1};

ll modpow(ll a, ll n, ll mod)
{
    ll res = 1;
    while (n > 0)
    {
        if (n & 1)
        {
            res = res * a % mod;
        }
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    while (1)
    {
        int k;
        cin >> k;
        if (k == 0)
        {
            break;
        }
        string s;
        cin >> s;
        int n = s.size();

        vvl dp(n, vl(k, -LINF));
        dp[0][0] = 0;
        dp[0][1] = (s[0] - '0') * modpow(10, k - 1, LINF);

        rep3(i, 1, n)
        {
            rep3(j, 1, k + 1)
            {
                chmax(dp[i][j % k], dp[i - 1][j - 1] + (s[i] - '0') * modpow(10, k - j, LINF));
                chmax(dp[i][j % k], dp[i - 1][j % k]);
            }
        }

        cout << dp[n - 1][0] << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 33432
final_rank: 16

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

long long mypow(long long b, long long e) {
    long long ret = 1;
    while(e) {
        if(e & 1) ret *= b;
        b *= b;
        e >>= 1;
    }
    return ret;
}

long long modpow(long long b, long long e, long long MOD=998244353) {
    long long ret = 1;
    while(e) {
        if(e & 1) ret = ret * b % MOD;
        b = b * b % MOD;
        e >>= 1;
    }
    return ret;
}

constexpr long long INF = 2e18;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    while(true) {
        int K;
        cin >> K;
        if(K == 0) {
            return 0;
        }

        string S;
        cin >> S;

        int N = S.size();
        reverse(S.begin(), S.end());

        vector dp(N + 1, vector<long long>(K, -INF));
        dp[0][0] = 0;

        for(int i = 0; i < N; i++) {
            for(int j = 0; j < K; j++) {
                dp[i + 1][(j + 1) % K] = max(dp[i + 1][(j + 1) % K], dp[i][j] + (S[i] - '0') * mypow(10, j));
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            }
        }


        cout << dp[N][0] << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 400
memory_kb: 33128
final_rank: 13

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

long long mypow(long long b, long long e) {
    long long ret = 1;
    while(e) {
        if(e & 1) ret *= b;
        b *= b;
        e >>= 1;
    }
    return ret;
}

long long modpow(long long b, long long e, long long MOD=998244353) {
    long long ret = 1;
    while(e) {
        if(e & 1) ret = ret * b % MOD;
        b = b * b % MOD;
        e >>= 1;
    }
    return ret;
}

constexpr long long INF = 2e18;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    while(true) {
        int K;
        cin >> K;
        if(K == 0) {
            return 0;
        }

        string S;
        cin >> S;

        int N = S.size();

        vector dp(N + 1, vector<long long>(K, -INF));
        dp[0][0] = 0;

        for(int i = 0; i < N; i++) {
            for(int j = 0; j < K; j++) {
                dp[i + 1][(j + 1) % K] = max(dp[i + 1][(j + 1) % K], dp[i][j] + (S[i] - '0') * mypow(10, K - j - 1));
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            }
        }


        cout << dp[N][0] << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 33140
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

vector<char> isprime;
void init() {
    isprime.assign(1000005, 1);
    isprime[0] = 0;
    isprime[1] = 0;
    for(int i = 2; i < 1000005; i++) {
        if(isprime[i]) {
            for(int j = i * 2; j < 1000005; j += i) {
                isprime[j] = 0;
            }
        }
    }
}

class UnionFind {
  private:
    int sz;
    vector<int> par, size_;

  public:
    UnionFind() {}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1) {
        iota(par.begin(), par.end(), 0);
    }
    int find(int x) {
        if(par[x] == x)
            return x;
        else
            return par[x] = find(par[x]);
    }
    void unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y)
            return;
        if(size_[x] < size_[y])
            swap(x, y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x) {
        x = find(x);
        return size_[x];
    }
    bool same(int x, int y) { return find(x) == find(y); }
};

ll n;
string s;
void input() { cin >> n >> s; }

void solve() {

    vector<vector<ll>> dp(s.size() + 1, vector<ll>(n, -1e18));
    vector<ll> ten(n);
    ten[0] = 1;
    for(int i = 0; i < n - 1; i++) {
        ten[i + 1] = ten[i] * 10;
    }
    dp[0][0] = 0;
    for(int i = 0; i < s.size(); i++) {
        for(int j = 0; j < n; j++) {
            dp[i + 1][j] = max(dp[i][j], dp[i][(j + n - 1) % n] +
                                             ten[(n - j) % n] * (s[i] - '0'));
        }
    }
    cout << dp.back()[0] << endl;
}

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

    // init();
    while(1) {
        input();
        if(n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 700
memory_kb: 33012
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

const int MOD = 998244353;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define mid (l+(r-l)/2)

using ll = long long;
using ld = long double;
using str = string;
using i128 = __int128;

using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pd = pair<ld,ld>;
#define f first
#define s second
#define cf cout<<flush

template <class T> using V = vector<T>;
using vi = V<int>;
using vvi = V<vi>;
using vl = V<ll>;
using vvl = V<vl>;
using vd = V<ld>;
using vpi = V<pii>;
using vpl = V<pll>;

#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define sorr(x) sort(all(x)),reverse(all(x))
#define uniq(x) sor(x), x.resize(unique(all(x))-x.begin())
#define pb push_back
#define ft front()
#define bk back()
#define chmi(x,y, ...) x = min(__VA_OPT__({) x,y __VA_OPT__(,)__VA_ARGS__ __VA_OPT__(}))
#define chma(x,y, ...) x = max(__VA_OPT__({) x,y __VA_OPT__(,)__VA_ARGS__ __VA_OPT__(}))
#define lwb(x,y) (int)(lower_bound(all(x),y)-x.begin())
#define upb(x,y) (int)(upper_bound(all(x),y)-x.begin())
#define compress(v) {auto _t=v;uniq(_t);for(auto &x:v)x=lwb(_t,x);}
#define in(x,y,n,m) (x>=0&&x<n&&y>=0&&y<m)
#define retu(args, ...) {ps(args __VA_OPT__(,)__VA_ARGS__);return ;}

#define For(i,a,b) for(int i = (a); i <= (b); i++)
#define F0r(i,a) for(int i = 0; i < (a); i++)
#define Rof(i,a,b) for(int i = (b); i >= (a); i--)
#define R0f(i,a) for(int i = (a)-1; i >= 0; i--)
#define rep(a) F0r(_,a)
#define each(a,x) for(auto &a:x)

const int dx[4]{1,0,-1,0},dy[4]{0,1,0,-1};
const int dx8[8]{1,1,0,-1,-1,-1,0,1},dy8[8]{0,1,1,1,0,-1,-1,-1};
template <class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

constexpr int pct(int x){return __builtin_popcount(x);}
constexpr int pctll(long long x){return __builtin_popcountll(x);}
constexpr int bits(int x){return 31-__builtin_clz(x);}

#define SFINAE(x, ...)                                                         \
    template <class, class = void> struct x : std::false_type {};              \
    template <class T> struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {}

SFINAE(DefaultI, decltype(std::cin >> std::declval<T &>()));
SFINAE(DefaultO, decltype(std::cout << std::declval<T &>()));
SFINAE(IsTuple, typename std::tuple_size<T>::type);
SFINAE(Iterable, decltype(std::begin(std::declval<T>())));

template <auto &is> struct Reader {
    template <class T> void Impl(T &t) {
        if constexpr (DefaultI<T>::value) is >> t;
        else if constexpr (Iterable<T>::value) {
            for (auto &x : t) Impl(x);
        } else if constexpr (IsTuple<T>::value) {
            std::apply([this](auto &...args) { (Impl(args), ...); }, t);
        } else static_assert(IsTuple<T>::value, "No matching type for read");
    }
    template <class... Ts> void read(Ts &...ts) { ((Impl(ts)), ...); }
};

template <class... Ts> void re(Ts &...ts) { Reader<cin>{}.read(ts...); }
#define def(t, args...)                                                        \
    t args;                                                                    \
    re(args);

template <auto &os, bool debug, bool print_nd> struct Writer {
    string comma() const { return debug ? "," : ""; }
    template <class T> constexpr char Space(const T &) const {
        return print_nd && (Iterable<T>::value or IsTuple<T>::value) ? '\n'
                                                                     : ' ';
    }
    template <class T> void Impl(T const &t) const {
        if constexpr (DefaultO<T>::value) os << t;
        else if constexpr (Iterable<T>::value) {
            if (debug) os << '{';
            int i = 0;
            for (auto &&x : t)
                ((i++) ? (os << comma() << Space(x), Impl(x)) : Impl(x));
            if (debug) os << '}';
        } else if constexpr (IsTuple<T>::value) {
            if (debug) os << '(';
            std::apply(
                [this](auto const &...args) {
                    int i = 0;
                    (((i++) ? (os << comma() << " ", Impl(args)) : Impl(args)),
                     ...);
                },
                t);
            if (debug) os << ')';
        } else static_assert(IsTuple<T>::value, "No matching type for print");
    }
    template <class T> void ImplWrapper(T const &t) const {
        if (debug) os << "\033[0;31m";
        Impl(t);
        if (debug) os << "\033[0m";
    }
    template <class... Ts> void print(Ts const &...ts) const {
        ((Impl(ts)), ...);
    }
    template <class F, class... Ts>
    void print_with_sep(const std::string &sep, F const &f,
                        Ts const &...ts) const {
        ImplWrapper(f), ((os << sep, ImplWrapper(ts)), ...), os << '\n';
    }
    void print_with_sep(const std::string &) const { os << '\n'; }
};

template <class... Ts> void pr(Ts const &...ts) {
    Writer<cout, false, true>{}.print(ts...);
}
template <class... Ts> void ps(Ts const &...ts) {
    Writer<cout, false, true>{}.print_with_sep(" ", ts...);
}

void setIn(str s) { freopen(s.c_str(), "r", stdin); }
void setOut(str s) { freopen(s.c_str(), "w", stdout); }
void setIO(str s = "") {
    cin.tie(0)->sync_with_stdio(0);
    cout << fixed << setprecision(12);
    if (sz(s)) setIn(s + ".in"), setOut(s + ".out");
}

//#include<atcoder/all>
//using namespace atcoder;
#define N 200005


void solve(){
    def(int,k);
    if(!k)exit(0);
    def(string,s);
    int n=sz(s);
    vl p(20);
    vvl dp(15,vl(n+1,-1e18));
    dp[0][0]=0;
    reverse(all(s));
    p[0]=1;
    F0r(i,15)p[i+1]=p[i]*10;
    F0r(i,n){
        F0r(j,k){
            chma(dp[j][i+1],dp[j][i]);
            chma(dp[(j+1)%k][i+1],dp[j][i]+p[j]*(s[i]-'0'));
            // ps(i,j,dp[i][j]);
        }
    }
    ps(dp[0][n]);
    
    return ;
}

int main(){
    setIO();
    int t=1;
    //re(t);
    while(true)solve();
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 29128
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const long long INF = 1e18;
ll myceil(ll a, ll b) {return (a+b-1)/b;}

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


int k;
string s;

vector<ll> pw(16);

void solve() {
    int n = s.length();
    vector<int> v(n);
    rep(i,n) {
        v[i] = (int)(s[i]-'0');
    }

    vector<vector<ll>> dp(n+1,vector<ll> (k,0));

    rep(i,n) {
        rep(j,min(k,i+1)) {
            dp[i+1][(j+1)%k] = max(dp[i][(j+1)%k],dp[i][j]+v[i]*pw[k-j-1]);
        }
    }

    // rep(i,n+1) {
    //     rep(j,k) {
    //         cout << dp[i][j] << ' ';
    //     }
    //     cout << endl;
    // }

    cout << dp[n][0] << endl;
    return;

}


int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    
    ll x = 1;

    rep(i,16) {
        pw[i] = x;
        x *= 10;
    }

    while (true) {
        cin >> k;
        if (k == 0) break;
        cin >> s;
        solve();
    }

	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 34116
final_rank: 12

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

void solve() {
    int K;
    cin >> K;
    if (K == 0) {
        exit(0);
    }
    auto pow10 = [&](int n) -> long long {
        long long e = 1;
        for (int i = 0; i < n; i++) {
            e *= 10;
        }
        return e;
    };
    string S;
    cin >> S;
    int N = S.size();
    vector<vector<long long>> dp(N + 10, vector<long long>(K + 10, -3e18));
    dp[0][0] = 0;
    for (int i = 0; i < N; i++) {
        long long num = S[i] - '0';
        for (int j = 0; j < K; j++) {
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            if (dp[i][j] != -3e18) {
                dp[i + 1][(j + 1) % K] = max(dp[i + 1][(j + 1) % K],
                                             dp[i][j] + num * pow10(K - 1 - j));
            }
        }
    }
    cout << dp[N][0] << endl;
}

int main() {
    while (1) {
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 540
memory_kb: 48672
final_rank: 19

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var) do { cout << #var << " :\n"; view(var); } while(0)
template<typename T>void view(const T& e) {cout << e;}
template<typename T1, typename T2>void view(const pair<T1, T2>& p) {cout << "{" << p.first << ", " << p.second << "}";}
template<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);cout << " ";} cout << endl;}
template<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cout << " ";} cout << endl;}

int K;

void solve(){
    string S; cin >> S;
    int N = len(S);
    vvl dp(2, vl(K, -INF));
    vl pw(1, 1); rep(i, K) pw.push_back(pw.back() * 10);
    int nw = 0, nxt = 1;
    dp[nw][K - 1] = 0;
    rep(i, N){
        dp[nxt] = dp[nw];
        rep(j, K){
            dp[nxt][(j - 1 + K) % K] = max(dp[nxt][(j - 1 + K) % K], dp[nw][j] + pw[j] * (S[i] - '0'));
        }
        swap(nw, nxt);
    }
    cout << dp[nw][K - 1] << endl;
}

int main(){
    while (true){
        cin >> K;
        if (K == 0) break;
        solve();
    }
}
accuracy: 1
time_ms: 110
memory_kb: 3792
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name
#define REP1(n) for(ll i=0;i<(n);i++)
#define REP2(i, n) for (ll i=0;i<(n);i++)
#define REP3(i, a, n) for (ll i=a;i<(n);i++)
#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name
#define RREP1(n) for(ll i=(n)-1;i>=0;i--)
#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)
#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)
#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))
#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define len(n) (long long)(n).size()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvvc = vector<vvc>;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

ll intpow(ll a,ll b){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
        }
        a *= a;
        b /= 2;
    }
    return ans;
}
ll modpow(ll a,ll b,ll c){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
            ans %= c;
        }
        a *= a;
        a %= c;
        b /= 2;
    }
    return ans;
}

template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}

#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)
#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}
#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}
#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}
#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}
#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}
#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define PLL(name) pll name;cin>>name.first>>name.second;
#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}

void print(){cout << "\n";}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
    os << "[";
    for (size_t i = 0; i < vec.size(); ++i) {
        os << vec[i];
        if (i + 1 < vec.size()) os << ", ";
    }
    os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {
    os << "[";
    for (size_t j = 0; j < a.size(); ++j) {
        os << "(" << a[j].first << ", " << a[j].second << ")";
		if (j + 1 < a.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {
	os << "[";
    for (size_t i = 0; i < mat.size(); ++i) {
		os << "[";
        for (size_t j = 0; j < mat[i].size(); ++j) {
            os << "(" << mat[i][j].first << ", " << mat[i][j].second << ")";
			if (j + 1 < mat[i].size()) os << ", ";
        }
		os << "]";
		if (i + 1 < mat.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
    os << "{";
    bool first = true;
    for (const auto& x : s) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "}";
    return os;
}

template <typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {
    os << "{";
    bool first = true;
    for (const auto& [key, val] : m) {
        if (!first) os << ", ";
        os << key << ": " << val;
        first = false;
    }
    os << "}";
    return os;
}

template<class T, class... Ts>
void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}

void write(){cout << "\n";}
template<class T, class... Ts>
void write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
void write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(pll x){cout << x.first << ' ' << x.second << '\n';}
void write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\n';}}
void write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}

template <typename T>
T sum(const std::vector<T>& v) {
    return std::accumulate(v.begin(), v.end(), T(0));
}

template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }

long long mod_mul(long long a, long long b, long long mod) {
    return (__int128)a * b % mod;
}

long long mod_pow(long long a, long long b, long long mod) {
    long long result = 1;
    a %= mod;
    while (b > 0) {
        if (b & 1) result = mod_mul(result, a, mod);
        a = mod_mul(a, a, mod);
        b >>= 1;
    }
    return result;
}

bool MillerRabin(ll x){
    
    if(x <= 1){return false;}
    if(x == 2){return true;}
    if((x & 1) == 0){return false;}
    vll test;
    if(x < (1 << 30)){
        test = {2, 7, 61};
    } 
    else{
        test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    }
    ll s = 0;
    ll d = x - 1;
    while((d & 1) == 0){
        s++;
        d >>= 1;
    }
    for(ll i:test){
        if(x <= i){return true;}
        ll y = mod_pow(i,d,x);
        if(y == 1 || y == x - 1){continue;}
        ll c = 0;
        for(int j=0;j<s-1;j++){
            y = mod_mul(y,y,x);
            if(y == x - 1){
                c = 1;
                break;
            }
        }
        if(c == 0){
            return false;
        }
    }
    return true;
}

#include<atcoder/dsu>
vpll rle(vll seq){
    vpll res;
    for(auto x:seq){
        if(res.empty() || res.back().first != x){
            res.emplace_back(x,1);
        }
        else{
            res.back().second++;
        }
    }
    return res;
}
int main(){
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
    
    while(1){
        LL(k);
        if(k == 0){break;}
        STR(s);
        ll n = len(s);
        vvll dp(n+1,vll(k,-1LL<<60));
        dp[0][k-1] = 0;
        vll t(k);
        t[0] = 1;
        rep(i,1,k){
            t[i] = t[i-1] * 10LL;
        }
        rep(i,n){
            rep(j,k){
                chmax(dp[i+1][j],dp[i][j]);
                chmax(dp[i+1][(j-1+k)%k],dp[i][j] + (s[i] - '0') * t[j]);
            }
        }
        print(dp[n][k-1]);
    }
    
}
accuracy: 1
time_ms: 280
memory_kb: 33424
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
using ll = long long;

int main() {
    while (true) {
        int K; cin >> K;
        if (K == 0) return 0;
        string S; cin >> S;
        int N = S.size();
        vector dp(N+10, vector<ll>(K, -1LL<<60));
        dp[0][0] = 0;
        vector<ll> a(K, 1);
        for (int i = 1; i < K; i++) a[i] = a[i-1] * 10;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < K; j++) {
                ll nxt = dp[i][j] + (S[i] - '0') * a[K-1-j];
                if (dp[i][j] >= 0) dp[i+1][(j+1) % K] = max(dp[i+1][(j+1) % K], nxt);
                if (dp[i][j] >= 0) dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            }
        }
        cout << dp[N][0] << "\n";
    }
}
accuracy: 1
time_ms: 270
memory_kb: 33300
final_rank: 6

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

bool solve(){
    int K; cin >> K;
    if(K == 0) return false;
    string s; cin >> s;
    int N = s.size();
    vector<ll> p(K, 1);
    for(int i = 1; i < K; ++i) p[i] = p[i - 1] * 10;
    vector dp(N + 1, vector(K, -1LL));
    dp[0][K - 1] = 0;
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < K; ++j){
            if(dp[i][j] == -1) continue;
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            int k = (j - 1 + K) % K;
            dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + (s[i] - '0') * p[j]);
        }
    }
    cout << dp[N][K - 1] << endl;
    return true;
}

int main(){
    while(1){
        // cerr << "# TESTCASE" << endl;
        if(!solve()) break;
    }
}
accuracy: 1
time_ms: 360
memory_kb: 33104
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int K;
    cin >> K;
    if (K == 0) break;
    vector<long long> pw(K + 1);
    pw[0] = 1;
    for (int i = 0; i < K; i++) {
      pw[i + 1] = pw[i] * 10LL;
    }
    string S;
    cin >> S;
    int N = S.size();
    vector<long long> dp(K, -1);
    dp[0] = 0;
    for (int i = 0; i < N; i++) {
      vector<long long> ndp = dp;
      for (int j = 0; j < K; j++) {
        if (dp[j] == -1) continue;
        int k = (j - 1 + K) % K;
        ndp[k] = max(ndp[k], dp[j] + (S[i] - '0') * pw[k]);
      }
      swap(ndp, dp);
    }
    cout << dp[0] << endl;
  }
}
accuracy: 1
time_ms: 180
memory_kb: 3532
final_rank: 2