Problem:
C: mod reap
問題
hara 君は、夏休みに農家でアルバイトをすることになりました。
hara 君の仕事は、会津人参の収穫と、パック詰めです。
畑には現在 $N$ 本の会津人参がなっていて、
$i$ ($1 \leq i \leq N$) 番目の会津人参の重さは $A_i$ グラムです。
hara 君は畑から何本かの会津人参を収穫し、 1 パックに
ちょうど
$K$ グラムずつ入るように分けます。
このとき、会津人参をカットして複数のパックに分けてもかまいません。
また、全部収穫しても、あるいは
1 本も収穫しなくてもよい
ものとします。
hara 君は几帳面なので、以下の条件により hara 君の「満足度」が変化します。
出来た $K$ グラム入りのパック 1 つにつき満足度が 1 増加します。
最終的に余った (パックに入らなかった) 会津人参 1 グラムにつき満足度が 1 減少します。
より形式的には、収穫した会津人参の合計量を $S$ グラムとしたとき、 hara 君の満足度 $f(S)$ は以下の式で計算されます。
$$f(S) = \lfloor S / K \rfloor - (S \bmod K)$$
ここで、$\lfloor S / K \rfloor$ とは $S \div K$ の商、
$S \bmod K$ とは $S$ を $K$ で割った余りを意味します。
このとき、 hara 君の満足度の最大値を求めてください。
入力形式
$N$ $K$
$A_1$ … $A_N$
制約
入力はすべて整数
$1 \leq N \leq 50000$
$1 \leq K \leq 100$
$1 \leq A_i \leq K$
出力形式
hara 君の満足度の最大値を一行に出力してください。
入力例 1
3 5
2 3 4
出力例 1
1
1 番目と 2 番目の会津人参を収穫すると収穫量が 5 グラムとなり、 5 グラム入りのパックを 1 つ作ることができます。また、余りはないので満足度は 1 となり、これが最大です。
入力例 2
6 7
1 2 3 4 5 6
出力例 2
3
すべての会津人参を収穫すると合計で 21 グラムとなり、このとき満足度を 3 にできます。これが最大値となります。
入力例 3
5 100
1 1 1 1 1
出力例 3
0
この場合、 1 本も収穫しないと満足度は 0 となり、これが最大です。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,K;
    cin>>N>>K;
    vvll DP(N+1,vll(K,-1e18));
    DP[0][0]=0;
    rep(i,N){
        ll A;
        cin>>A;
        rep(k,K){
            chmax(DP[i+1][k],DP[i][k]);
            ll nk=k+A;
            chmax(DP[i+1][nk%K],DP[i][k]+nk/K);
        }
    }
    ll an=0;
    rep(i,K)chmax(an,DP[N][i]-i);
    cout<<an<<endl;

}
accuracy: 1
time_ms: 50
memory_kb: 44008
final_rank: 16

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)

using namespace std;
using ll = long long ;
using P = pair<int,int>;
const int mod = 998244353;
const int inf = (1<<30);

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};

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

    vector<int> a(n);
    rep(i,n) cin>>a[i];

    vector<vector<int>> dp(n+1,vector<int>(k,-inf));
    dp[0][0] = 0;
    rep(i,n){
        rep(j,k){
            if(dp[i][j] == -inf) continue;
            dp[i+1][(j+a[i])%k] = max(dp[i+1][(j+a[i])%k], dp[i][j]+a[i]);
            dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
        }
    }

    int ans = 0;

    rep(i,k){
        ans = max(ans,(dp[n][i]/k) - i);
    }
    cout<<ans<<endl;


}
accuracy: 1
time_ms: 20
memory_kb: 24760
final_rank: 12

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

const int inf = (int)1e9;

int main() {
    int N, K; cin >> N >> K;
    vector dp(K, -inf);
    dp[0] = 0;
    for (int _ = 0 ; _ < N ; _++) {
        int A; cin >> A;
        vector nxt(K, -inf);
        for (int i = 0 ; i < K ; i++) {
            if (dp[i] == -inf) {
                continue;
            }
            int mod = (i + A) % K;
            bool add = ((i + A) >= K);
            nxt[mod] = max(nxt[mod], dp[i] + (int)add);
            nxt[i] = max(nxt[i], dp[i]);
        }
        dp = move(nxt);
    }

    int ans = -inf;
    for (int i = 0 ; i < K ; i++) {
        ans = max(ans, dp[i] - i);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3444
final_rank: 3

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

using namespace std;

int func(){
  int n;
  int k;
  cin >> n >> k;
  vector<int> dp1(k,-1e9);
  vector<int> dp2(k,-1e9);
  dp1[0] = 0;
  for(int i=0;i<n;++i){
    dp2.clear();
    dp2.assign(k,-1e9);
    int v;
    cin >> v;
    for(int j=0;j<k;++j){
      int j2 = (j + v) % k;
      dp2[j2] = max(dp2[j2], dp1[j] + v);
      dp2[j] = max(dp2[j], dp1[j]);
    }
    swap(dp1,dp2);
  }
  int res = -1e9;
  for(int i=0;i<k;++i){
    res = max(res, dp1[i] / k - dp1[i] % k);
  }
  return res;
}
int main(){
  cout << func() << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3452
final_rank: 4

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3187.cc: Mod Reap
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 50000;
const int MAX_K = 100;

/* typedef */

/* global variables */

int as[MAX_N], dp[2][MAX_K];

/* subroutines */

inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  fill(dp[0], dp[0] + k, -1);
  dp[0][0] = 0;
  int cur = 0, nxt = 1;

  for (int i = 0; i < n; i++) {
    copy(dp[cur], dp[cur] + k, dp[nxt]);

    for (int j = 0; j < k; j++)
      if (dp[cur][j] >= 0) {
	int d0 = dp[cur][j], j1 = j + as[i];
	if (j1 >= k) j1 -= k, d0++;
	setmax(dp[nxt][j1], d0);
      }

    swap(cur, nxt);
  }

  int maxd = 0;
  for (int j = 0; j < k; j++) setmax(maxd, dp[cur][j] - j);

  printf("%d\n", maxd);

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3140
final_rank: 1

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

/* alias */
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vf = vector<float>;
using vvf = vector<vf>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vc = vector<char>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using qll = queue<ll>;
//仮想配列map<> https://code-database.com/knowledges/100
//優先度付きque https://atcoder.jp/contests/apg4b/tasks/APG4b_aa

/* define short */
#define MOD 1000000007
#define INF LLONG_MAX/32
#define elif else if
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mp make_pair
#define all(obj) (obj).begin(), (obj).end()
#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
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; }

/* REP macro */
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) reps(i, 1, n + 1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)

//小数点以下出力の時にmainに書く
// cout << fixed << setprecision(10);

#pragma endregion header

ll N;


vvll Graph;/*グラフ*/
// struct edge{ll to, cost;};
// vector<vector<edge>> Graph;
ll H, W; vs HW;/*HW .#*/



#pragma region fanction
#pragma region First Search
/*幅優先探索 vll dist(N,-1); Graphは隣接リスト*/
void bfs(vll &dist, ll fq, vvll Graph){
    dist[fq] = 0;
    deque<ll> que;que.pb(fq);
    ll v;
    while(!que.empty()){
        v = que.front();que.pop_front();

        for(ll nv:Graph[v]){
            if(dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.pb(nv);
        }
    }
}


/*distにsysxからの距離が入る　各重み1*/
/*HW幅優先探索 https://pyteyon.hatenablog.com/entry/2019/03/01/211133#%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4%A2*/
/*vvll dist(H vll(W)); rep(h,H) rep(w,W) dist[h][w] = INF;*/
void HW_bfs(vvll &dist, ll sy, ll sx, vs HW){
    dist[sy][sx] = 0;
    deque<vll> que;
    que.pb({sy,sx});

    vll v;
    ll nowy, nowx, nexty,nextx;
    vvll dydx = {{1,0},{0,1},{-1,0},{0,-1}};

    //重み1ならpbしてd++ 重み0ならpfしてそのままdiseにd入れれば01bfs
    //que.back();que.pop_back()で深さ優先探索

    while(!que.empty()){
        v = que.front();que.pop_front();
        nowy = v[0]; nowx = v[1];
        for(vll dyx:dydx){
            nexty = nowy+dyx[0]; nextx = nowx+dyx[1];
            if(!(0 <= nexty && nexty < H && 0 <= nextx && nextx < W)) continue;
            if(HW[nexty][nextx] == '.' && dist[nexty][nextx] == INF){
                que.pb({nexty,nextx});
                dist[nexty][nextx] = dist[nowy][nowx] + 1;
            }
        }
    }
}

/*深さ優先探索 vll dist(N,-1); Graphは隣接リスト スタートのdistを0にすること！*/
void dfs(vll &dist, ll v, vvll &Graph){
    for(ll nv:Graph[v]){
        if(dist[nv] != -1) continue;
        dist[nv] = dist[v] + 1;
        dfs(dist,nv,Graph);
    }
}

//単一始点最短経路　ダイクストラ　O(MlogN)
//vector<vector<pll>> //Graph; fi:to se:cost
// Graph.resize(N);
// rep(i,M){
//     ll a,b,c; cin >> a >> b >> c;
//     a--;b--;
//     Graph[a].pb(pll(b,c));
//     Graph[b].pb(pll(a,c));
// }

vll dijkstra(ll start,vector<vector<pll>> &Graph){
    vll dist(N,INF);
    priority_queue<pll,vector<pll>,greater<pll>> que;
    dist[start] = 0;
    que.push(pll(dist[start],start));
    while (!que.empty()){
        pll p = que.top(); que.pop();
        ll v = p.second;
        if(dist[v] < p.first) continue;
        for(auto &e:Graph[v]){
            if(dist[e.first] > dist[v] + e.second){
                dist[e.first] = dist[v] + e.second;
                que.push(pll(dist[e.first],e.first));
            }
        }
    }

    return dist;
}

#pragma endregion First Search
#pragma region MOD
const ll nCkMax = 1e6;
vll fac(1,0),finv(1,0),inv(1,0);

void COMinit(){
    fac.resize(nCkMax);
    finv.resize(nCkMax);
    inv.resize(nCkMax);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(ll i=2;i<nCkMax;i++){
        fac[i] = fac[i-1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
/*a^n % mod p O(log N)*/
ll modpow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}
/*a! mod p O(N)*/
ll modfac(ll n){
    ll ret = 1;
    rrep(i,n){
        ret *= i;
        ret %= MOD;
    }
    return ret;
}

/*nCk % mod 前処理O(N) クエリ処理O(1)*/
ll nCk(ll n, ll k){
    if(fac[0] != 1) COMinit();//初期化 O(N)
    if(n<k) return 0;
    if(n<0 || k<0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll factorial(ll n){
    ll ans = 1;
    for (ll i = 1;i<=n;i++){
        ans = ans * i % MOD;
    }
    return ans;
}

ll nHk(ll n, ll k){
    return nCk(n+k-1,k);
}
#pragma endregion MOD
#pragma region enumeration
vvll make_n(vll v){
    vvll ret;
    vll tmp;
    ll n = v.size();
    do{
        tmp = {};
        rep(i,n) tmp.pb(v[i]);
        ret.pb(tmp);
    }while(next_permutation(all(v)));
    return ret;
}
/*nCk列挙 vvllなのに注意！*/
vvll make_C(ll n, ll r){
    vvll ret;
    vll tmp;
    vector<bool> v(n);
    fill(v.end() - r, v.end(), true);
    do {
        tmp = {};
        for (int i = 0; i < n; ++i) { 
            if (v[i]) tmp.pb(i);
        }
        ret.pb(tmp);
    } while (next_permutation(v.begin(), v.end()));
    //reverse(all(ret));
    return ret;
}
/*nPk列挙 nPnでn!列挙 この時順番通りになってる*/
vvll make_P(ll n, ll r){
    vvll C = make_C(n,r);
    vvll ret;
    vvll tmp;
    vll v;
    rep(i,C.size()){
        v = C[i];
        tmp = make_n(v);
        for(vll t:tmp) ret.pb(t);
    }
    return ret;
}

#pragma endregion enumeration
#pragma region array

/*一次元配列最大最小総和*/
ll vmax(vll array){
    ll Max = -INF;
    for(ll a:array) chmax(Max,a);
    return Max;
}

ll vmin(vll array){
    ll Min = INF;
    for(ll a:array) chmin(Min,a);
    return Min;
}

ll sum(vll &array){
    ll Sum = 0;
    for(ll a:array) Sum += a;
    return Sum;
}

/*累積和 先頭に0が追加されてる lからrまではret[r+1]-ret[l]*/
vll cumulatice_sum(vll &array){
    vll ret(array.size()+1,0);
    rep(i,array.size()) ret[i+1] = ret[i] + array[i];
    return ret;
}

/*重複消し*/
void list_set(vll &array){
    sort(all(array));
    array.erase(unique(all(array)),array.end());
}

/*vvllで格納されてる配列をkey番目の要素でソート*/
void vvllsort(vvll &array, ll key){
    sort(all(array),[&key](vll a,vll b){return a[key] < b[key];});
}

/*string Sに何個string Kが含まれるか 一文字でもいいけどcharじゃなくてstringにしてね*/
ll stringinstring(string &S,string K){
    ll ret = 0;
    rep(i,S.size()-K.size()+1){
        bool f = true;
        rep(j,K.size()) if(S[i+j] != K[j]) f=false;
        if(f) ret++;
    }
    return ret;
}
#pragma endregion array
#pragma region UnionFind
class UnionFind{
public:
    vector<ll> parent; //parent[i]はiの親
    vector<ll> siz; //素集合のサイズを表す配列(1で初期化)
    map<ll,vector<ll>> group; //集合ごとに管理する(key:集合の代表元、value:集合の要素の配列)
    ll n; //要素数
 
    //コンストラクタ
    UnionFind(ll n_):n(n_),parent(n_),siz(n_,1){ 
        //全ての要素の根が自身であるとして初期化
        for(ll i=0;i<n;i++){parent[i]=i;}
    }
 
    //データxの属する木の根を取得(経路圧縮も行う)
    ll root(ll x){
        if(parent[x]==x) return x;
        return parent[x]=root(parent[x]);//代入式の値は代入した変数の値なので、経路圧縮できる
    }
 
    //xとyの木を併合
    void unite(ll x,ll y){
        ll rx=root(x);//xの根
        ll ry=root(y);//yの根
        if(rx==ry) return;//同じ木にある時
        //小さい集合を大きい集合へと併合(ry→rxへ併合)
        if(siz[rx]<siz[ry]) swap(rx,ry);
        siz[rx]+=siz[ry];
        parent[ry]=rx;//xとyが同じ木にない時はyの根ryをxの根rxにつける
    }
 
    //xとyが属する木が同じかを判定
    bool same(ll x,ll y){
        ll rx=root(x);
        ll ry=root(y);
        return rx==ry;
    }
 
    //xの素集合のサイズを取得
    ll size(ll x){
        return siz[root(x)];
    }
 
    //素集合をそれぞれグループ化
    void grouping(){
        //経路圧縮を先に行う
        rep(i,n)root(i);
        //mapで管理する(デフォルト構築を利用)
        rep(i,n)group[parent[i]].pb(i);
    }
 
    //素集合系を削除して初期化
    void clear(){
        rep(i,n){parent[i]=i;}
        siz=vector<ll>(n,1);
        group.clear();
    }
};
#pragma endregion UnionFind
#pragma region bisect
/*二分探索*/
ll bisect_left(vll &array, ll key){
    return lower_bound(all(array),key) - array.begin();
}

ll bisect_right(vll &array, ll key){
    return upper_bound(all(array),key) - array.begin();
}

/*最長増加部分列*/
ll LIS(vll a){
    vll dp(a.size(),INF);
    rep(i,a.size()){
        dp[bisect_right(dp,a[i])] = a[i];
    }
    return bisect_left(dp,INF);
}
/*条件を満たす最大のokを返す　最小にしたいならokng逆にする solveを書き換えること!*/
bool solve(ll n){
    return true;
}

ll meguru_bisect(ll ok, ll ng){
    ll mid;
    while(abs(ok-ng) > 1){
        mid = (ok + ng) / 2;
        if(solve(mid)) ok = mid;
        else ng = mid;
    }
    return ok;
}
#pragma endregion bisect
#pragma region others

/* memo

二次元配列の90度回転
// i:j 
// j:N - i - 1


*/



/*普通のa^n*/
ll pow(ll a, ll n){
    ll res = 1;
    while (n > 0) {
        if (n & 1) res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}
/*最大公約数*/
ll gcd(ll x,ll y){
    if (x < y) swap(x,y);
    while (y > 0){
        ll r = x % y;
        x = y;
        y = r;
    }
    return x;
}

/*最小公倍数*/
ll lcm(ll x,ll y){
    return x * y / (gcd(x,y));
}

/*約数列挙*/
vll divisor(ll n){
    vll ret;
    for (ll i = 1; i * i <= n; i++){
        if (n % i == 0){
            ret.pb(i);
            if (i * i != n) ret.pb(n / i);
        }
    }
    sort(all(ret));
    return ret;
}

/*素因数分解*/
vll factorize(ll n){
    vll ret;
    while (n % 2 == 0){
        ret.pb(2);
        n /= 2;
    }
    ll f = 3;
    while (f * f <= n){
        if (n % f == 0){
            ret.pb(f);
            n /= f;
        }else f += 2;
    }
    if (n != 1) ret.pb(n);
    return ret;
}
const ll Eramax = 1e6+1;
vll Eratos(1,1);
void Erainit(){
    Eratos.resize(Eramax+1);
    rep(i,Eramax+1) Eratos[i] = i;
    for(ll i=2;i<Eramax;i++){
        if(Eratos[i] != i) continue;
        for(ll j=2*i;j<Eramax;j+=i) Eratos[j] = i;
    }
    Eratos[1] = -1;
}
//前処理型素因数分解　1e6以下を複数回やるならこっち
vll Era_factorize(ll n){
    if(Eratos[0] != 0) Erainit();
    vll ret = {};
    if(n==1) return ret;
    while(n!=1){
        ret.pb(Eratos[n]);
        n /= Eratos[n];
    }
    return ret;
}

#pragma endregion others
#pragma endregion fanction




int main() {
    ll K; cin >> N >> K;
    vll cnt(K+1,0);
    rep(i,N){
        ll a; cin >> a;
        cnt[a]++;
    }

    // vvll dp(K+1,vll(N*K+1,-1));
    vll dp(N*K+1,-1);
    // vector<vector<bool>> dp(K+1,vector<bool>(N*K+1,false));
    dp[0] = 0;
    rep(i,K){
        
        rep(j,N*K+1){
            ll g = i + 1;
            ll cn = cnt[i+1];
            if(dp[j] >= 0) dp[j] = cn;
            elif(j-g < 0 || dp[j-g] <= 0) dp[j] = -1;
            else dp[j] = dp[j-g] - 1;
        }
    }

    vll List;
    rep(j,N*K+1){
        if(dp[j] != -1) List.pb(j);
    }

    ll Ans = -INF;
    for(ll S:List){
        ll get = S/K - S%K;
        chmax(Ans,get);
    }

    cout << Ans << endl;
    
}
accuracy: 1
time_ms: 520
memory_kb: 77264
final_rank: 20

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

int dp[50050][101];

int main(){
	int n,k; cin >> n >> k;
	vector<int> a(n);
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<k;j++){
			dp[i][j]=-1;
		}
	}
	dp[0][0]=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<k;j++){
			if(dp[i][j]<0)continue;
			dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
			dp[i+1][(j+a[i])%k]=max(dp[i+1][(j+a[i])%k],dp[i][j]+(j+a[i])/k);
		}
	}
	int res=-1e9;
	for(int i=0;i<k;i++){
		res=max(res,dp[n][i]-i);
	}
	cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 22820
final_rank: 10

code_snippet: #line 2 "/home/yuruhiya/programming/library/template/template.cpp"
#include <bits/stdc++.h>
#line 4 "/home/yuruhiya/programming/library/template/constants.cpp"
#include <string_view>
#line 7 "/home/yuruhiya/programming/library/template/constants.cpp"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;
using LD = long double;
using VB = vector<bool>;
using VVB = vector<VB>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VD = vector<LD>;
using PII = pair<int, int>;
using VP = vector<PII>;
using PLL = pair<ll, ll>;
using VPL = vector<PLL>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000000;
constexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846, EPS = 1e-12;
namespace CharacterClass {
	constexpr string_view
	    digit = "0123456789",
	    xdigit = "0123456789ABCDEFabcdef", lower = "abcdefghijklmnopqrstuvwxyz",
	    upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
	    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    alnum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    word = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz",
	    punct = R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)",
	    graph =
	        R"(!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    print =
	        R"( !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    blank = " \t", space = " \t\n\r\f\v";
}  // namespace CharacterClass
#line 7 "/home/yuruhiya/programming/library/template/Input.cpp"
using namespace std;

#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Scanner {
	static int gc() {
		return getchar_unlocked();
	}
	static char next_char() {
		char c;
		read(c);
		return c;
	}
	template <class T> static void read(T& v) {
		cin >> v;
	}
	static void read(char& v) {
		while (isspace(v = gc()))
			;
	}
	static void read(bool& v) {
		v = next_char() != '0';
	}
	static void read(string& v) {
		v.clear();
		for (char c = next_char(); !isspace(c); c = gc()) v += c;
	}
	static void read(int& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(long long& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(double& v) {
		v = 0;
		double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	static void read(long double& v) {
		v = 0;
		long double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	template <class T, class U> static void read(pair<T, U>& v) {
		read(v.first);
		read(v.second);
	}
	template <class T> static void read(vector<T>& v) {
		for (auto& e : v) read(e);
	}
	template <size_t N = 0, class T> static void read_tuple_impl(T& v) {
		if constexpr (N < tuple_size_v<T>) {
			read(get<N>(v));
			read_tuple_impl<N + 1>(v);
		}
	}
	template <class... T> static void read(tuple<T...>& v) {
		read_tuple_impl(v);
	}
	struct ReadVectorHelper {
		size_t n;
		ReadVectorHelper(size_t _n) : n(_n) {}
		template <class T> operator vector<T>() {
			vector<T> v(n);
			read(v);
			return v;
		}
	};
	struct Read2DVectorHelper {
		size_t n, m;
		Read2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}
		template <class T> operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m));
			read(v);
			return v;
		}
	};

public:
	string read_line() const {
		string v;
		for (char c = gc(); c != '\n' && c != '\0'; c = gc()) v += c;
		return v;
	}
	template <class T> T read() const {
		T v;
		read(v);
		return v;
	}
	template <class T> vector<T> read_vector(size_t n) const {
		vector<T> a(n);
		read(a);
		return a;
	}
	template <class T> operator T() const {
		return read<T>();
	}
	int operator--(int) const {
		return read<int>() - 1;
	}
	ReadVectorHelper operator[](size_t n) const {
		return ReadVectorHelper(n);
	}
	Read2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {
		return Read2DVectorHelper(nm);
	}
	void operator()() const {}
	template <class H, class... T> void operator()(H&& h, T&&... t) const {
		read(h);
		operator()(forward<T>(t)...);
	}

private:
	template <template <class...> class, class...> struct Multiple;
	template <template <class...> class V, class Head, class... Tail>
	struct Multiple<V, Head, Tail...> {
		template <class... Args> using vec = V<vector<Head>, Args...>;
		using type = typename Multiple<vec, Tail...>::type;
	};
	template <template <class...> class V> struct Multiple<V> { using type = V<>; };
	template <class... T> using multiple_t = typename Multiple<tuple, T...>::type;
	template <size_t N = 0, class T> void multiple_impl(T& t) const {
		if constexpr (N < tuple_size_v<T>) {
			auto& vec = get<N>(t);
			using V = typename remove_reference_t<decltype(vec)>::value_type;
			vec.push_back(read<V>());
			multiple_impl<N + 1>(t);
		}
	}

public:
	template <class... T> auto multiple(size_t h) const {
		multiple_t<T...> result;
		while (h--) multiple_impl(result);
		return result;
	}
} in;
#define inputs(T, ...) \
	T __VA_ARGS__;     \
	in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(long long, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)
#line 7 "/home/yuruhiya/programming/library/template/Output.cpp"
#include <charconv>
#line 10 "/home/yuruhiya/programming/library/template/Output.cpp"
using namespace std;

struct BoolStr {
	const char *t, *f;
	BoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char *d, *l;
	DivStr(const char* _d, const char* _l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
    no_endl(" ", "");
class Printer {
	BoolStr B{Yes};
	DivStr D{spc};

public:
	void print(int v) const {
		char buf[12]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(long long v) const {
		char buf[21]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(bool v) const {
		print(v ? B.t : B.f);
	}
	void print(vector<bool>::reference v) const {
		print(v ? B.t : B.f);
	}
	void print(char v) const {
		putchar_unlocked(v);
	}
	void print(const char* v) const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void print(double v) const {
		printf("%.20f", v);
	}
	void print(long double v) const {
		printf("%.20Lf", v);
	}
	template <class T> void print(const T& v) const {
		cout << v;
	}
	template <class T, class U> void print(const pair<T, U>& v) const {
		print(v.first);
		print(D.d);
		print(v.second);
	}
	template <class InputIterater>
	void print_range(const InputIterater& begin, const InputIterater& end) const {
		for (InputIterater i = begin; i != end; ++i) {
			if (i != begin) print(D.d);
			print(*i);
		}
	}
	template <class T> void print(const vector<T>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T, size_t N> void print(const array<T, N>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T> void print(const vector<vector<T>>& v) const {
		for (size_t i = 0; i < v.size(); ++i) {
			if (i) print(D.l);
			print(v[i]);
		}
	}

	Printer() = default;
	Printer(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}
	Printer& operator()() {
		print(D.l);
		return *this;
	}
	template <class H> Printer& operator()(H&& h) {
		print(h);
		print(D.l);
		return *this;
	}
	template <class H, class... T> Printer& operator()(H&& h, T&&... t) {
		print(h);
		print(D.d);
		return operator()(forward<T>(t)...);
	}
	template <class InputIterator>
	Printer& range(const InputIterator& begin, const InputIterator& end) {
		print_range(begin, end);
		print(D.l);
		return *this;
	}
	template <class T> Printer& range(const T& a) {
		range(a.begin(), a.end());
		return *this;
	}
	template <class... T> void exit(T&&... t) {
		operator()(forward<T>(t)...);
		std::exit(EXIT_SUCCESS);
	}
	Printer& flush() {
		fflush_unlocked(stdout);
		return *this;
	}
	Printer& set(const BoolStr& b) {
		B = b;
		return *this;
	}
	Printer& set(const DivStr& d) {
		D = d;
		return *this;
	}
	Printer& set(const char* t, const char* f) {
		B = BoolStr(t, f);
		return *this;
	}
} out;
#line 3 "/home/yuruhiya/programming/library/template/Step.cpp"
using namespace std;

template <class T> struct Step {
	using value_type = T;

	class iterator {
		value_type a, b, c;

	public:
		constexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}
		constexpr iterator(value_type _b, value_type _c, value_type _s)
		    : a(_b), b(_c), c(_s) {}
		constexpr iterator& operator++() {
			--b;
			a += c;
			return *this;
		}
		constexpr iterator operator++(int) {
			iterator tmp = *this;
			--b;
			a += c;
			return tmp;
		}
		constexpr const value_type& operator*() const {
			return a;
		}
		constexpr const value_type* operator->() const {
			return &a;
		}
		constexpr bool operator==(const iterator& i) const {
			return b == i.b;
		}
		constexpr bool operator!=(const iterator& i) const {
			return !(b == i.b);
		}
		constexpr value_type start() const {
			return a;
		}
		constexpr value_type size() const {
			return b;
		}
		constexpr value_type step() const {
			return c;
		}
	};
	constexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}
	constexpr iterator begin() const {
		return be;
	}
	constexpr iterator end() const {
		return en;
	}
	constexpr value_type start() const {
		return be.start();
	}
	constexpr value_type size() const {
		return be.size();
	}
	constexpr value_type step() const {
		return be.step();
	}
	constexpr value_type sum() const {
		return start() * size() + step() * (size() * (size() - 1) / 2);
	}
	operator vector<value_type>() const {
		return to_a();
	}
	auto to_a() const {
		vector<value_type> result;
		result.reserve(size());
		for (auto i : *this) {
			result.push_back(i);
		}
		return result;
	}

private:
	iterator be, en;
};
template <class T> constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template <class T> constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template <class T> constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}
#line 8 "/home/yuruhiya/programming/library/template/Ruby.cpp"
using namespace std;

template <class F> struct Callable {
	F func;
	Callable(const F& f) : func(f) {}
};
template <class T, class F> auto operator|(const T& v, const Callable<F>& c) {
	return c.func(v);
}

struct Sort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {
		sort(begin(v), end(v));
		return v;
	}
} Sort;
struct SortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) < f(j); });
			return v;
		});
	}
} SortBy;
struct RSort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(rbegin(v), rend(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {
		sort(rbegin(v), rend(v));
		return v;
	}
} RSort;
struct RSortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) > f(j); });
			return v;
		});
	}
} RSortBy;
struct Reverse_impl {
	template <class T> friend auto operator|(T v, const Reverse_impl& c) {
		reverse(begin(v), end(v));
		return v;
	}
} Reverse;
struct Unique_impl {
	template <class T> friend auto operator|(T v, const Unique_impl& c) {
		v.erase(unique(begin(v), end(v), end(v)));
		return v;
	}
} Unique;
struct Uniq_impl {
	template <class T> friend auto operator|(T v, const Uniq_impl& c) {
		sort(begin(v), end(v));
		v.erase(unique(begin(v), end(v)), end(v));
		return v;
	}
} Uniq;
struct Rotate_impl {
	auto operator()(int&& left) {
		return Callable([&](auto v) {
			int s = static_cast<int>(size(v));
			assert(-s <= left && left <= s);
			if (0 <= left) {
				rotate(begin(v), begin(v) + left, end(v));
			} else {
				rotate(begin(v), end(v) + left, end(v));
			}
			return v;
		});
	}
} Rotate;
struct Max_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *max_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Max_impl& c) {
		return *max_element(begin(v), end(v));
	}
} Max;
struct Min_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *min_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Min_impl& c) {
		return *min_element(begin(v), end(v));
	}
} Min;
struct MaxPos_impl {
	template <class T> friend auto operator|(T v, const MaxPos_impl& c) {
		return max_element(begin(v), end(v)) - begin(v);
	}
} MaxPos;
struct MinPos_impl {
	template <class T> friend auto operator|(T v, const MinPos_impl& c) {
		return min_element(begin(v), end(v)) - begin(v);
	}
} MinPos;
struct MaxBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_it = begin(v);
			auto max_val = f(*max_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_it = it;
					max_val = val;
				}
			}
			return *max_it;
		});
	}
} MaxBy;
struct MinBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_it = begin(v);
			auto min_val = f(*min_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_it = it;
					min_val = val;
				}
			}
			return *min_it;
		});
	}
} MinBy;
struct MaxOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_val = val;
				}
			}
			return max_val;
		});
	}
} MaxOf;
struct MinOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_val = val;
				}
			}
			return min_val;
		});
	}
} MinOf;
struct Count_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return count(begin(v), end(v), val); });
	}
} Count;
struct CountIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return count_if(begin(v), end(v), f); });
	}
} CountIf;
struct Index_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(begin(v), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} Index;
struct IndexIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} IndexIf;
struct FindIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<typename decltype(v)::value_type> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(*result) : nullopt;
		});
	}
} FindIf;
struct Sum_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			return accumulate(next(begin(v)), end(v), f(*begin(v)),
			                  [&](const auto& a, const auto& b) { return a + f(b); });
		});
	}
	template <class T> friend auto operator|(T v, const Sum_impl& c) {
		return accumulate(begin(v), end(v), typename T::value_type{});
	}
} Sum;
struct Includes {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });
	}
} Includes;
struct IncludesIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });
	}
} IncludesIf;
struct RemoveIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) {
			v.erase(remove_if(begin(v), end(v), f), end(v));
			return v;
		});
	}
} RemoveIf;
struct Each_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				f(i);
			}
		});
	}
} Each;
struct Select_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using value_type = typename decltype(v)::value_type;
			vector<value_type> result;
			for (const auto& i : v) {
				if (f(i)) result.push_back(i);
			}
			return result;
		});
	}
} Select;
struct Map_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using result_type = invoke_result_t<F, typename decltype(v)::value_type>;
			vector<result_type> result;
			result.reserve(size(v));
			for (const auto& i : v) {
				result.push_back(f(i));
			}
			return result;
		});
	}
} Map;
struct Indexed_impl {
	template <class T> friend auto operator|(const T& v, Indexed_impl& c) {
		using value_type = typename T::value_type;
		vector<pair<value_type, int>> result;
		result.reserve(size(v));
		int index = 0;
		for (const auto& i : v) {
			result.emplace_back(i, index++);
		}
		return result;
	}
} Indexed;
struct AllOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (!f(i)) return false;
			}
			return true;
		});
	}
} AllOf;
struct AnyOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return true;
			}
			return false;
		});
	}
} AnyOf;
struct NoneOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return false;
			}
			return true;
		});
	}
} NoneOf;

struct Tally_impl {
	template <class F> auto operator()(size_t max_val) {
		return Callable([&](auto v) {
			vector<size_t> result(max_val);
			for (const auto& i : v) {
				result[static_cast<size_t>(i)]++;
			}
			return result;
		});
	}
	template <class T, class value_type = typename T::value_type>
	friend auto operator|(const T& v, Tally_impl& c) {
		map<value_type, size_t> result;
		for (const auto& i : v) {
			result[i]++;
		}
		return result;
	}
} Tally;

template <class T> auto operator*(const vector<T>& a, size_t n) {
	T result;
	for (size_t i = 0; i < n; ++i) {
		result.insert(result.end(), a.begin(), a.end());
	}
	return result;
}
auto operator*(string a, size_t n) {
	string result;
	for (size_t i = 0; i < n; ++i) {
		result += a;
	}
	return result;
}
template <class T, class U> auto& operator<<(vector<T>& a, const U& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T> auto& operator<<(string& a, const T& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T, class U> auto operator+(vector<T> a, const U& b) {
	a << b;
	return a;
}
template <class T> auto operator+(string a, const T& b) {
	a << b;
	return a;
}
#line 7 "/home/yuruhiya/programming/library/template/functions.cpp"
using namespace std;

template <class T = long long> constexpr T TEN(size_t n) {
	T result = 1;
	for (size_t i = 0; i < n; ++i) result *= 10;
	return result;
}
template <class T, class U,
          enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>
constexpr auto div_ceil(T n, U m) {
	return (n + m - 1) / m;
}
template <class T, class U> constexpr auto div_ceil2(T n, U m) {
	return div_ceil(n, m) * m;
}
template <class T> constexpr T triangle(T n) {
	return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
}
template <class T> constexpr T nC2(T n) {
	return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
}
template <class T, class U> constexpr auto middle(const T& l, const U& r) {
	return l + (r - l) / 2;
}
template <class T, class U, class V>
constexpr bool in_range(const T& v, const U& lower, const V& upper) {
	return lower <= v && v < upper;
}
template <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>
constexpr bool is_square(T n) {
	T s = sqrt(n);
	return s * s == n || (s + 1) * (s + 1) == n;
}
template <class T = long long> constexpr T BIT(int b) {
	return T(1) << b;
}
template <class T> constexpr int BIT(T x, int i) {
	return (x & (1 << i)) ? 1 : 0;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Pow(T a, U n) {
	assert(n >= 0);
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result *= a;
			n--;
		} else {
			a *= a;
			n >>= 1;
		}
	}
	return result;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Powmod(T a, U n, T mod) {
	assert(n >= 0);
	if (a > mod) a %= mod;
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result = result * a % mod;
			n--;
		} else {
			a = a * a % mod;
			n >>= 1;
		}
	}
	return result;
}
template <class T> bool chmax(T& a, const T& b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> int sz(const T& v) {
	return v.size();
}
template <class T, class U> int lower_index(const T& a, const U& v) {
	return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int upper_index(const T& a, const U& v) {
	return upper_bound(all(a), v) - a.begin();
}
template <class T> auto Slice(const T& v, size_t i, size_t len) {
	return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
}
template <class T, class U = typename T::value_type> U Gcdv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);
}
template <class T, class U = typename T::value_type> U Lcmv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);
}
namespace internal {
	template <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {
		if constexpr (N == 1) {
			return vector(sizes[0], init);
		} else {
			int size = sizes[N - 1];
			sizes.pop_back();
			return vector(size, make_vector<T, N - 1>(sizes, init));
		}
	}
}  // namespace internal
template <class T, size_t N>
auto make_vector(const int (&sizes)[N], const T& init = T()) {
	vector s(rbegin(sizes), rend(sizes));
	return internal::make_vector<T, N>(s, init);
}
#line 9 "/home/yuruhiya/programming/library/template/template.cpp"
#if __has_include(<library/dump.hpp>)
#include <library/dump.hpp>
#define LOCAL
#else
#define dump(...) ((void)0)
#endif

template <class T> constexpr T oj_local(const T& oj, const T& local) {
#ifndef LOCAL
	return oj;
#else
	return local;
#endif
}
#line 2 "a.cpp"

int main() {
	ini(n, k);
	VI a = in[n];

	VI dp(k, -inf);
	dp[0] = 0;
	rep(i, n) {
		VI dp2 = dp;
		rep(j, k) {
			chmax(dp2[(j + a[i]) % k], dp[j] + (j + a[i] >= k));
		}
		dp = dp2;
	}

	int ans = 0;
	rep(i, k) {
		chmax(ans, dp[i] - i);
	}
	out(ans);
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define irep(i,a,b) for(int i=int(a);i<(int)b;++i)
#define rrep(i,a,b) for(int i=int(a);i>=(int)b;--i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll,ll>>>
#define vpl vector<pair<ll,ll>>
#define vpi vector<pair<int,int>>
#define pb push_back
#define se second
#define fi first
#define all(v) v.begin(),v.end()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

using namespace std;

template<typename T> istream& operator>>(istream&i,v(T)&v){rep(j,v.size())i>>v[j];return i;}
template<typename T> string join(const v(T)&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T> ostream& operator<<(ostream&o,const v(T)&v){if(v.size())o<<join(v);return o;}



using ll = long long;
const ll INF = 1e18;
const double PI = acos(-1);

const ll mod = 1e9 + 7; //998244353;

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;
}


ll modpow(ll a,ll b){
    if(b == 0){
        return 1;
    }
    if(b%2 == 0){
        ll tmp = modpow(a,b/2);
        return tmp*tmp%mod;
    }else{
        return modpow(a,b-1)*a%mod;
    }

}


int main(void)
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int n,k;
    cin >> n >> k;
    vl a(n),dp(k,-INF);
    dp[0] = 0;
    rep(i,n){
        cin >> a[i];
        auto sub = dp;
        rep(j,k){
            dp[(j+a[i])%k] = max(sub[(j+a[i])%k],sub[j] + a[i]);
        }
    }
    ll ans = 0;
    rep(i,k)ans = max(ans,dp[i]/k-i);

    cout << ans << endl;

    
    //cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3612
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P = pair<int,int>;
const int INF=1001001001;
const int mod =1e9+7;

int main() {
  int N,K;cin>>N>>K;
  vector<int>A(N);
  for(int i=0;i<N;i++){cin>>A[i];}
  vector<vector<ll>>dp(N+1,vector<ll>(K,-1));
  dp[0][0]=0;
  for(int i=0;i<N;i++){
    for(int j=K-1;j>=0;j--){
      if(dp[i][j]==-1){continue;}
      chmax(dp[i+1][(j+A[i])%K],dp[i][j]+A[i]);
      chmax(dp[i+1][j],dp[i][j]);
    }
  }
  ll ans=0;
  for(int i=0;i<K;i++){chmax(ans,dp[N][i]/K-i);}
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 44296
final_rank: 13

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 50005

int N,K;
int A[SIZE];
int dp[SIZE][105];

int main(){

	scanf("%d %d",&N,&K);

	for(int i = 1; i <= N; i++){

		scanf("%d",&A[i]);
	}

	for(int i = 0; i <= N; i++){
		for(int a = 0; a <= K-1; a++){
			dp[i][a] = -BIG_NUM;
		}
	}

	dp[0][0] = 0;
	for(int i = 1; i <= N; i++){
		for(int a = 0; a <= K-1; a++){
			if(dp[i-1][a] == -BIG_NUM)continue;

			//足さない
			dp[i][a] = max(dp[i][a],dp[i-1][a]);

			//足す
			int next = a+A[i];
			int add = 0;

			if(next >= K){

				next -= K;
				add++;
			}
			dp[i][next] = max(dp[i][next],dp[i-1][a]+add);
		}
	}

	int ans = -BIG_NUM;
	for(int a = 0; a <= K-1; a++){

		ans = max(ans,dp[N][a]-a);
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 23920
final_rank: 9

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

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

const long long INF = 1e18;
//const ll mod = 1000000007;
ll dp[50100][105];
ll N, K;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> K;
    for(int i = 0; i <= K; i++) {
        dp[0][i] = -INF;
    }
    dp[0][0] = 0;
    for(int i = 0; i < N; i++) {
        ll A;
        cin >> A;
        for(int j = 0; j <= K; j++) {
            dp[i+1][j] = -INF;
        }
        for(int j = 0; j < K; j++) {
            chmax(dp[i+1][j], dp[i][j]);
            ll val = dp[i][j];
            ll nxt = (j + A) % K;
            if(j + A >= K) val++;
            chmax(dp[i+1][nxt], val);
        }
    }
    ll ans = 0;
    for(int i = 0; i <= K; i++) {
        chmax(ans, dp[N][i] - i);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 44460
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)

using namespace std;
typedef long long ll;

const int INTINF = INT_MAX >> 1;

ll dp[50100][110];

int main(){

	rep(i, 50010){
		rep(j, 110) dp[i][j] = -INTINF;
	}

	int N, K; cin >> N >> K;
	vector<ll> v(N);
	rep(i, N) cin >> v[i];

	ll ans = 0;
	dp[0][0] = 0;
	rep(i, N){
		rep(j, K){
			if(dp[i][j]  == -INTINF) continue;
			dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
			dp[i+1][(j+v[i])%K] = max(dp[i][(j+v[i])%K], dp[i][j] + (j + v[i]) / K);
			// cout << dp[i+1][j] << " " << dp[i+1][(j+v[i])%K] << endl;
			ans = max(ans, dp[i+1][j] - j);
			ans = max(ans, dp[i+1][(j+v[i])%K] - (j+v[i])%K);
		}
	}
	cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 46148
final_rank: 19

code_snippet: #pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <numeric>
#define IINF 1000000000
#define INF 3223372036854775807
#define MOD 1000000007
#define mod 1000000007
#define INT_MAX_ 2147483647
#define EPS (1e-10)
#define REP(i, a, n) fo-r (ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
//#define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
#define bit(n,k) ((n>>k)&1)
#define lg length()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invp(a,p)pom(a,p-2,p)
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp1(pair<Pll,ll> a,pair<Pll,ll> b){
        return a.fi.se>b.fi.se;
}
ll cmp2(pair<ll,ll> a,pair<ll,ll> b){
    if(a.fi!=b.fi)
        return  a.se<b.se;
    else
        return a.se>b.se;
}
//----------------------------------------------------------------------
ll dp[50001][101];;
//----------------------------------------------------------------------
int main(int argc, char * argv[]){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------  
    //ll begin_t=clock();
    //freopen("big.txt", "r", stdin);
    //freopen("out3.txt", "w", stdout);
    //------------------------------
    ll n,k;cin>>n>>k;
    V<ll>a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    sort(all(a));
    reverse(all(a));

    ll ans=0;
    for(ll i=0;i<=n;i++){
        for(ll j=0;j<=100;j++){
            dp[i][j]=-INF;
        }
    }
    dp[0][0]=0;
    for(ll i=0;i<n;i++){
        for(ll j=0;j<k;j++){
            if(dp[i][j]==-INF)continue;
            chmax(dp[i+1][(j+a[i])%k], dp[i][j] + (j+a[i])/k);
            chmax(dp[i+1][j],dp[i][j]);
        }
    }
    for(ll j=0;j<k;j++){
        chmax(ans, dp[n][j]-j);
    }
    cout<<ans<<endl;
    //------------------------------
    //fclose(stdin);
    //fclose(stdout);
    //ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
    //------------------------------- 
    return 0;
}
//----------------------------------------------------------------------
accuracy: 1
time_ms: 50
memory_kb: 42696
final_rank: 14

code_snippet: #include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr long long mod = 10000019;
constexpr int MAX = 5000000;


int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	ll n, k; cin >> n >> k;
	vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i];
	vector<vector<ll>> dp(2, vector<ll>(k + 1, -INF));
	int cur = 0;
	int nxt = 1;
	dp[0][0] = 0;
	for (int i = 0; i < n; i++) {
		fill(dp[nxt].begin(), dp[nxt].end(), -INF);
		for (int j = 0; j < k; j++) {
			if (dp[cur][j] == -INF) continue;
			chmax(dp[nxt][j], dp[cur][j]);
			chmax(dp[nxt][(j + a[i]) % k], dp[cur][j] + a[i]);
		}
		swap(cur, nxt);
	}
	ll res = 0;
	for (int i = 0; i < k; i++) {
		chmax(res, dp[cur][i] / k - i);
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3604
final_rank: 8

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

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << '\n';
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const T &a) {
    for (auto e : a) cout << e << ' ';
    cout << '\n';
}

//------------------------- Libraries -------------------------//

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

//--------------------------- Solve ---------------------------//

void solve() {
    int N, K; cin >> N >> K;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    vector<vector<int> > dp(N+1, vector<int>(K, -1));
    dp[0][0] = 0;
    for (int i = 1; i <= N; i++) {
        for (int j = 0; j < K; j++) {
            if (dp[i-1][j] == -1) continue;
            chmax(dp[i][j], dp[i-1][j]);
            chmax(dp[i][(j+A[i-1])%K], dp[i-1][j] + (j+A[i-1])/K);
        }
    }

    int ans = 0;
    for (int i = 0; i < K; i++) {
        chmax(ans, dp[N][i] - i);
    }

    cout << ans << '\n';
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 24724
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
using namespace std;
using P = pair<int, int>;
const int inf = 2e18;
const int mod = 1e9 + 7;

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

template <class T>
vector<T> make_vec(size_t a) {
  return vector<T>(a);
}

template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}

template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t) fill(e, v);
}

signed main() {
  
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;

  vector<int> dp(k, -1);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    auto nxt = dp;
    for (int j = 0; j < k; j++) {
      if (dp[j] == -1) continue;
      nxt[(j + a[i]) % k] = max(nxt[(j + a[i]) % k], dp[j] + (j + a[i] >= k));
    }
    swap(dp, nxt);
  }
  int ans = 0;
  for(int i = 0; i < k; i++){
    ans = max(ans, dp[i] - i);
  }
  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3604
final_rank: 5

code_snippet: #ifdef LOCAL
    #define _GLIBCXX_DEBUG
    #define __clock__
#else
    #pragma GCC optimize("Ofast")
#endif
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;
using PII = pair<ll, ll>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

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; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())
#define MP make_pair
#define p_yes() p("YES")
#define p_no() p("NO")
#define possible() p("Possible")
#define impossible() p("Impossible")

ll SUM(VI& V){
  return accumulate(ALL(V), 0LL);
}

ll MIN(VI& V){return *min_element(ALL(V));}
ll MAX(VI& V){return *max_element(ALL(V));}

void print_vector(VI& V){
  ll n = V.size();
  rep(i, n){
    if(i) cout << ' ';
    cout << V[i];
  }
  cout << endl;
}

ll gcd(ll a,ll b){
    if(b == 0) return a;
    return gcd(b,a%b);
}

ll lcm(ll a,ll b){
    ll g = gcd(a,b);
    return a / g * b;
}

// long double
using ld = long double;
#define EPS (1e-14)
#define equals(a,b) (fabs((a)-(b)) < EPS)

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);

// snuke's mint
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
// const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

ll dp[55000][200];
// i個まで見て
// 余りがjのときの最大箱数

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

    // input
    ll N,K; 
    cin>>N>>K;

    VI A(N);
    rep(i,N)cin>>A[i];

    VV dp(N+10, VI(K, -1));
    dp[0][0]=0;

    rep(i,N){
      ll a = A[i];
      rep(j,K){
        if(dp[i][j]==-1) continue;

        // とる
        ll sum = j+a;
        chmax(dp[i+1][sum%K], dp[i][j] + sum/K);

        // とらない
        chmax(dp[i+1][j], dp[i][j]);
      }
    }
    ll ans = 0;
    rep(i,K){
      chmax(ans, dp[N][i] - i);
    }
    p(ans);

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 44348
final_rank: 17

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
	int n, k; cin >> n >> k;
    vector<int> a(n);
    rep(i, n) {
        cin >> a[i];
    }
    vector<int> dp(k, -INF);
    vector<int> ndp(k, -INF);
    dp[0] = 0;
    rep(i, n) {
        ndp.assign(k, -INF);
        rep(j, k) {
            if(dp[j] == -INF) continue;
            chmax(ndp[j], dp[j]);
            int now = k * dp[j] + j;
            now += a[i];
            int r = now % k, t = now / k;
            chmax(ndp[r], t);
        }
        swap(dp, ndp);
    }
    int ans = -INF;
    rep(j, k) {
        chmax(ans, dp[j] - j);
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3304
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;  
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define Rep(i, a, b) for(ll i = a; i <= b; i++)
#define repr(i, a, b) for(ll i = b-1; i >= a; i--)
// #define _GLIBCXX_DEBUG
template <class T> using V = vector<T>;
using ll = long long;
#define ALL(v) (v).begin(),(v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>> 
// #define fi first
// #define se second
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};

/*--------------------------------------------------------------------------------
                                    ・ω・
--------------------------------------------------------------------------------*/

void solve(){
    ll n, k;
    cin >> n >> k;
    V<ll> A(n);
    rep(i, 0, n) cin >> A[i];
    V<V<ll>> dp(n+5, V<ll>(k+5, -1));
    dp[0][0] = 0;
    rep(i, 0, n){
        rep(j, 0, k){
            if(dp[i][j]==-1) continue;
            chmax(dp[i+1][j], dp[i][j]);
            if(j+A[i] >= k) chmax(dp[i+1][(j+A[i])%k], dp[i][j]+1);
            else chmax(dp[i+1][j+A[i]], dp[i][j]);
        }
    }
    //debug
    // rep(i, 0, k){
    //     Rep(j, 0, n) cout << dp[j][i] << " \n"[j==n];
    // }
    //
    ll ans = -INF;
    rep(i, 0, k) chmax(ans, dp[n][i]-i);
    cout << ans << endl;

    return;
}

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

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 46112
final_rank: 18