Problem:
Peculiar Protocol
The Kingdom of
Icpca
has a peculiar protocol in wedding ceremonies. That is, amounts of monetary gifts should be a multiple of a fixed quantity plus a fixed extra. When the fixed quantity is $d$ and the fixed extra is $r$, courteous amounts of wedding gifts are $k \times d + r$ for any non-negative integer multipliers $k$.
Initially, you have a pile of $n$ banknotes. Every time you attend a wedding ceremony, you draw out a contiguous portion from your current banknote pile as your gift that sums up to a courteous amount, that is, a multiple of $d$ plus additional $r$. If no contiguous portion sums up to such an amount, you cannot attend wedding ceremonies any more. After drawing out, the remaining banknotes are squeezed to form a single pile, keeping their relative order. The resultant pile of banknotes may have portions with such an amount, which allows you to attend more ceremonies.
Your monetary gifts are expected to raise your social reputation. As the additional amount r is considered mandatory, the multiplier $k$ is considered significant. Your reputation is raised in proportion to $k$ at each of the ceremonies you attend.
For example, assume $d = 5$ and $r = 1$, and you have banknotes whose values are 2, 2, 2, 4, and 4, piled up in this order. When you attend a wedding ceremony, there are following two possible ways to give courteous monetary gifts.
Give a monetary gift consisting of the first three banknotes from the top, totaling $2+2+2 = 6 = 1 \times d+r$. After drawing them out, you have banknotes with values 4 and 4. No contiguous portion of your remaining banknote pile sums up to a courteous amount. Thus, you cannot attend wedding ceremonies anymore.
Give a monetary gift consisting of the third and the fourth banknotes, totaling $2+4 = 6 = 1 \times d+r$. After drawing them out, you have banknotes with values 2, 2, and 4, in this order. You can attend another wedding ceremony because the second and the third banknotes total $2+4 = 6 = 1 \times d+r$, which is courteous.
In this example, the second way can maximize your social reputation by attending two ceremonies, because the total of the multipliers is $1 + 1 = 2$, which achieves the maximum possible.
In contrast, if the value of the first banknote is 12, giving the first three banknotes at a ceremony prevents you from attending more ceremonies. That, however, maximizes your social reputation because the total of the multipliers is 3, which achieves the maximum possible.
Compute the maximum possible total of the multipliers with your monetary gifts at wedding ceremonies. You may assume that you have so many unmarried relatives and friends that you can attend any number of wedding ceremonies as long as you can give courteous monetary gifts.
Input
The input consists of a single test case in the following format.
$n$ $d$ $r$
$a_1$ ... $a_n$
The first line has three integers $n$, $d$, and $r$. The integer $n$ ($1 \leq n \leq 500$) is the number of banknotes you have. Th
...(truncated)

Submitted solutions:

code_snippet: // AOJ #1486 Peculiar Protocol
// 2025.5.1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

ll dp1[505][505], dp[505][505];
int main(){
    int n = Cin();
    ll d = Cin(), r = Cin();
    vector<ll>a(n+1), S(n+1);
    for(int i=1;i<=n;i++){
        a[i] = Cin();
        S[i] = S[i-1] + a[i];
    }
    auto sum = [&](int l,int r){ return S[r] - S[l-1]; };


    for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dp1[i][j] = -INF;

    for(int len=1; len<=n; len++){
        for(int l=1; l+len-1<=n; l++){
            int rgt = l+len-1;
            ll A = sum(l, rgt);

            if(A >= r && (A - r) % d == 0) dp1[l][rgt] = max(dp1[l][rgt], (A - r)/d);
            if(len >= 2){
                int j = rgt-1;
                for(int i=l; i<=j; i++){
                    if(dp1[i][j] < 0) continue;
                    ll innerSum = sum(i,j);
                    ll rem = A - innerSum;
                    if(rem >= r && (rem - r) % d == 0){
                        ll kip = dp1[i][j];
                        ll kroot = (rem - r)/d;
                        dp1[l][rgt] = max(dp1[l][rgt], kip + kroot);
                    }
                }
            }

            ll best = 0;
            for(int m=l; m<rgt; m++) best = max(best, dp[l][m] + dp[m+1][rgt]);
            dp[l][rgt] = max(best, dp1[l][rgt]);
        }
    }
    cout << dp[1][n] << endl;
    return 0;
}
accuracy: 0.07547169811320754
time_ms: 160
memory_kb: 7312
final_rank: 15

code_snippet: //#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
#define rep(i,n) for(int i=0; i<int(n); i++)
using i64 = long long;
template<class T> void chmin(T& l, const T& r){ if(r < l) l = r; }
template<class T> void chmax(T& l, const T& r){ if(l < r) l = r; }

#include <map>

void testcase(){
    i64 N; cin >> N;
    i64 D, R; cin >> D >> R;
    vector<i64> A(N); rep(i,N) cin >> A[i];
    vector<i64> B(N+1); rep(i,N) B[i+1] = B[i] + A[i];
    vector<vector<i64>> dp(N+1, vector<i64>(N+1));
    vector<vector<i64>> fx(N+1, vector<i64>(N+1));
   // cout << N << " " << D << " " << R << endl;

    map<i64,i64> mp;
    for(i64 k=N; k>=1; k--) mp[R*k%D] = k;

    i64 QMAX = 10000;
    for(i64 t=1; t<=N*2; t++) if(R*t%D == 0) QMAX = t;

    for(i64 r=1; r<=N; r++){
        vector<i64> Q(r+1);
        for(i64 t=r-1; t>=0; t--){
            for(i64 s=t+1; s<=r; s++){
                chmax(Q[t], Q[s] + fx[t][s]);
            }
        }
        //for(auto q : Q) cout << q << " ";
        //cout << endl;
        for(i64 l=0; l<r; l++){
            i64 sum = B[r] - B[l];
            if(mp.count(sum % D) == 0) continue;
            i64 ff = 0;
            while(ff <= Q[l] && (sum - ff * R) % D != R) ff++;
            if(ff > Q[l]) continue;
            sum -= R * ff;
            if(sum < R) continue;
            fx[l][r] = ff + 1;
            dp[l][r] = (sum - R) / D;
        }
        //for(i64 l=0; l<r; l++){
        //    cout << "l = "<< l << " , r = " << r << " , dp = " << dp[l][r] << " , fx = " << fx[l][r] << endl;
        //}
    }

    vector<i64> ans(N+1);
    rep(i,N) for(i64 j=i+1; j<=N; j++) chmax(ans[j], ans[i] + dp[i][j]);
    cout << ans[N] << "\n";
}

int main(){
    //rep(t,4){
        testcase();
    //    cout << string(10, '-') << endl;
    //}
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 7460
final_rank: 5

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

long long modinv(long long a, long long MOD) {
    long long b = MOD, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; std::swap(a, b);
        u -= t * v; std::swap(u, v);
    }
    u %= MOD; 
    if (u < 0) u += MOD;
    return u;
}

long long modpow(long long a, long long n, long long MOD) {
    long long res = 1;
    a %= MOD;
    if(n < 0) {
        n = -n;
        a = modinv(a, MOD);
    }
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}
void chmax(ll &a, ll b) { a = max(a, b); }

pair<ll, ll> calc(ll num, ll r, ll d) {
    ll g = __gcd(num, r);
    g = __gcd(g, d);
    num /= g;
    r /= g;
    d /= g;
    if(num % __gcd(r, d)) return {-1, -1};
    if(r == 0) {
        if(num == 0) return {d - 1, d};
        else return {-1, -1};
    }
    num *= modinv(r, d);
    num %= d;
    if(num == 0) num = d - 1;
    else num --;
    return {num, d};
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, d, p;
    cin >> n >> d >> p;
    vector<ll> a(n + 1);
    rep(i, n) cin >> a[i + 1];
    rep(i, n) a[i + 1] += a[i];
    
    vector<vector<ll>> dp(n, vector(n + 1, 0LL)); // r何回引く？
    vector<vector<ll>> can(n, vector(n + 1, -1LL)); // kの総和
    for(int l = n - 1; l >= 0; l --) {
        for(int r = l + 1; r <= n; r ++) {
            if(dp[l][r] != -1) {
                ll num = a[r] - a[l];
                auto [kmin, diff] = calc(num, p, d);
                if(kmin != -1 and kmin <= dp[l][r]) {
                    ll val = dp[l][r] - kmin;
                    val /= diff;
                    val *= diff;
                    val += kmin;
                    val ++;
                    chmax(dp[l][r], val);
                    val = (val % diff == 0 ? diff : val % diff);
                    num = (num - val * p);
                    assert(num % d == 0);
                    chmax(can[l][r], num / d);
                }
            }
            if((a[r] - a[l]) % d == p) {
                
                chmax(dp[l][r], 1LL);
                chmax(can[l][r], (a[r] - a[l]) / d);
            }
             {
                for(int i = r + 1; i <= n; i ++) {
                    chmax(dp[l][i], dp[l][r] + dp[r][i]);
                }
            }
        }
    }
    // for(int i = 0; i < n; i ++) {
    //     for(int j = i + 1; j <= n; j ++) {
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // for(int i = 0; i < n; i ++) {
    //     for(int j = i + 1; j <= n; j ++) {
    //         cout << can[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    vector<ll> ans(n + 1, -INF);
    ans[0] = 0;
    for(int i = 0; i < n; i ++) {
        for(int j = i + 1; j <= n; j ++) {
            chmax(ans[j], ans[i] + max(can[i][j], 0LL));
        }
    }
    cout << ans.back() << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 7296
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(ll i = a; i < (b); i++)
#define all(a) (a).begin(), (a).end()

int main() {
    ll n, d, R;
    cin >> n >> d >> R;

    vector<ll> a(n);
    rep(i, 0, n) cin >> a[i];
    a.insert(a.begin(), 0);
    rep(i, 0, n) a[i + 1] += a[i];

    map<ll, ll> cnt;
    rep(i, 1, n + 1) if(cnt.count(R * i % d) == 0) cnt[R * i % d] = i;

    vector<vector<pair<ll, bool>>> dp(
        n + 1, vector<pair<ll, bool>>(n + 1, {0, false}));

    vector<vector<pair<ll, ll>>> tmp(n + 1);

    rep(r, 0, n + 1) {
        for(ll l = r; l >= 0; l--) {
            ll mx = 0;
            rep(i, l + 1, r) { mx = max(mx, dp[l][i].first + dp[i][r].first); }
            dp[l][r].first = mx;
            if(cnt.count((a[r] - a[l]) % d) &&
               cnt[(a[r] - a[l]) % d] <= mx + 1) {
                dp[l][r].first = max(mx, cnt[(a[r] - a[l]) % d]);
                dp[l][r].second = true;
                tmp[l].emplace_back(
                    r, (a[r] - a[l] - cnt[(a[r] - a[l]) % d] * R) / d);
            }
        }
    }

    vector<ll> dp2(n + 1);
    int idx = 0;
    rep(i, 0, n) {
        dp2[i + 1] = max(dp2[i + 1], dp2[i]);
        for(auto [r, x] : tmp[i]) {
            dp2[r] = max(dp2[r], dp2[i] + x);
        }
    }

    cout << dp2[n] << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 10240
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define si(a) a.size()
#define vec vector
#undef long
#define long long long
using ll = long;
template <typename T> bool chmin(T &x, T y) {
	if (y < x) {
		x = y;
		return true;
	}
	return false;
}
template <typename T> bool chmax(T &x, T y) {
	if (x < y) {
		x = y;
		return true;
	}
	return false;
}
long extgcd(long a, long b, long &x, long &y) {
	if (!b)
		return x = 1, y = 0, a;
	long d = extgcd(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
long inv_mod(long x, long m) {
	long y, z;
	extgcd(x, m, y, z);
	if ((y %= m) < 0)
		y += m;
	return y;
}
void solve() {
	int n;
	long c, mod;
	cin >> n >> mod >> c;
	vec<long> a(n);
	rep(i, n) cin >> a[i];
	vec<vec<long>> rui(n + 1, vec<long>(n + 1));
	rep(i, n + 1) rep(j, n + 1) {
		for (int k = i; k < j; k++)
			rui[i][j] += a[k];
	}
	vec<vec<int>> d(n + 1, vec<int>(n + 1));
	for (int r = 1; r <= n; r++) {
		for (int l = r - 1; l >= 0; l--) {
			for (int k = l + 1; k < r; k++)
				chmax(d[l][r], d[l][k] + d[k][r]);
			if ((rui[l][r] - (d[l][r] + 1) * c) % mod == 0)
				d[l][r]++;
			chmin(d[l][r], r - l);
		}
	}
	const long g = gcd(c, mod);
	const long cc = c / g, mm = mod / g;
	const long cinv = inv_mod(cc, mm);
	vec<long> dp(n + 1);
	rep(i, n + 1) {
		if (i != 0)
			chmax(dp[i], dp[i - 1]);
		for (int j = i + 1; j <= n; j++) {
			if (rui[i][j] % g)
				continue;
			long kk = rui[i][j] / g * cinv % mm;
			if (kk == 0)
				kk += mm;
			if (kk > d[i][j])
				continue;
			chmax(dp[j], dp[i] + (rui[i][j] - kk * c) / mod);
		}
	}
	cout << dp[n] << endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	solve();
}
accuracy: 1
time_ms: 20
memory_kb: 5988
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
typedef pair<ld,ld> pt;
typedef vector<ll> vi;
typedef vector<vi> vvi;
#define FOR(i,l,r) for(ll i=(l);i<(r);i++)
#define REP(i,n) FOR(i,0,n)
#define F first
#define S second
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
int main(){
  ll N,D,R;cin>>N>>D>>R;
  vi A(N);
  REP(i,N)cin>>A[i];
  vi B(N+1);
  REP(i,N)B[i+1]=A[i]+B[i];
  vvi DP(N+1,vi(N+1,1e18));
  vector<vector<bitset<512>>>EP(N+1,vector<bitset<512>>(N+1));
  REP(i,N+1)DP[i][i]=0,EP[i][i][0]=1;
  FOR(d,1,N+1)REP(i,N+1-d){
    FOR(j,1,d+1)if((B[i+d]-B[i])%D==j*R%D&&EP[i][i+d-1][j-1]){DP[i][i+d]=j;break;}
    FOR(j,i+1,i+d)DP[i][i+d]=min(DP[i][i+d],DP[i][j]+DP[j][i+d]);
    EP[i][i+d]=EP[i][i+d-1];
    if(DP[i][i+d]<1e17)EP[i][i+d][DP[i][i+d]]=1;
    FOR(j,i,i+d)if(DP[j][i+d]<1e17)EP[i][i+d]|=EP[i][j]<<DP[j][i+d];
  }
  vi FP(N+1);
  REP(i,N){
    FP[i+1]=max(FP[i],FP[i+1]);
    FOR(j,i+1,N+1)if(DP[i][j]<1e17)FP[j]=max(FP[j],FP[i]+(B[j]-B[i]-R*DP[i][j])/D);
  }
  cout<<FP[N]<<endl;
  return 0;
}
accuracy: 1
time_ms: 440
memory_kb: 20804
final_rank: 6

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

#pragma GCC optimize("O3")

int dp[501][501];
int ep[501];

long long s[501];
long long hp[501];

constexpr int INF = INT_MAX / 4;

int main() {
  int n;
  long long d, r;
  cin >> n >> d >> r;
  s[0] = 0;
  for(int i = 1; i <= n; ++i) {
    cin >> s[i];
    s[i] += s[i - 1];
  }
  for(int p = 0; p <= n; ++p) {
    for(int q = 0; q <= n; ++q) {
      dp[p][q] = -INF;
    }
  }
  for(int k = 1; k <= n; ++k) {
    for(int p = 0, q = k; q <= n; ++p, ++q) {
      for(int i = p; i <= q; i++) {
        ep[i] = 0;
      }
      for(int i = p; i < q; ++i) {
        for(int j = i + 1; j <= q; ++j) {
          ep[j] = max(ep[j], ep[i] + dp[i][j] * (dp[i][j] != -INF));
        }
      }
      for(int c = 0; c <= ep[q]; ++c) {
        if((s[q] - s[p] - r * c) % d == r) {
          dp[p][q] = c + 1;
          break;
        }
      }
    }
  }
  hp[0] = 0;
  for(int p = 0; p < n; ++p) {
    for(int q = p + 1; q <= n; ++q) {
      hp[q] = max(hp[q], hp[p] + (dp[p][q] != -INF) * (s[q] - s[p] - r * dp[p][q]) / d);
    }
  }
  cout << hp[n] << endl;
}
accuracy: 1
time_ms: 1290
memory_kb: 4432
final_rank: 10

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

#pragma GCC optimize("O3")

int dp[501][501];
int ep[501];

long long s[501];
long long hp[501];

constexpr int INF = INT_MAX / 4;

int main() {
  int n;
  long long d, r;
  cin >> n >> d >> r;
  s[0] = 0;
  for(int i = 1; i <= n; ++i) {
    cin >> s[i];
    s[i] += s[i - 1];
  }
  for(int p = 0; p <= n; ++p) {
    for(int q = 0; q <= n; ++q) {
      dp[p][q] = -INF;
    }
  }
  for(int k = 1; k <= n; ++k) {
    for(int p = 0, q = k; q <= n; ++p, ++q) {
      for(int i = p; i <= q; i++) {
        ep[i] = 0;
      }
      for(int i = p; i < q; ++i) {
        ep[i + 1] = max(ep[i + 1], ep[i]);
        for(int j = i + 1; j <= q; ++j) {
          ep[j] = max(ep[j], ep[i] + (dp[i][j] != -INF));
        }
      }
      for(int c = 0; c <= ep[q]; ++c) {
        if(((s[q] - s[p]) - r * c) % d == r) {
          dp[p][q] = c + 1;
          break;
        }
      }
    }
  }
  // for(int l = 0; l <= n; l++) {
  //   for(int r = 0; r <= n; r++) {
  //     if(l > r) cout << " " << " ";
  //     else if(dp[l][r] == -INF) cout << '.' << " ";
  //     else cout << dp[l][r] << " ";
  //   } cout << "\n";
  // }
  hp[0] = 0;
  for(int p = 0; p < n; ++p) {
    hp[p + 1] = max(hp[p + 1], hp[p]);
    for(int q = p + 1; q <= n; ++q) {
      if(dp[p][q] != -INF) {
        hp[q] = max(hp[q], hp[p] + (s[q] - s[p] - r * dp[p][q]) / d);
      }
    }
  }
  cout << hp[n] << endl;
}
accuracy: 0.07547169811320754
time_ms: 780
memory_kb: 4432
final_rank: 17

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

#pragma GCC optimize("O3")

using ll = long long;

int main() {
  int n;
  ll div, rem;
  cin >> n >> div >> rem;
  vector<ll> s(n + 1);
  for(int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] += s[i - 1];
  }
  const ll INF = LONG_MAX / 4;
  vector<int> ep(n + 1);
  vector dp(n + 1, vector<int>(n + 1, -1000000));
  for(int k = 1; k <= n; k++) {
    for(int l = 0, r = k; r <= n; l++, r++) {
      for(int i = 0; i <= n; i++) ep[i] = 0;
      for(int i = l; i < r; i++) {
        ep[i + 1] = max(ep[i + 1], ep[i]);
        for(int j = i + 1; j <= r; j++) {
          if(dp[i][j] != -INF) {
            ep[j] = max(ep[j], ep[i] + 1);
          }
        }
      }
      for(int c = 0; c <= ep[r]; c++) {
        if(((s[r] - s[l]) - rem * c) % div == rem) {
          dp[l][r] = c + 1;
          break;
        }
      }
    }
  }
  // for(int l = 0; l <= n; l++) {
  //   for(int r = 0; r <= n; r++) {
  //     if(l > r) cout << " " << " ";
  //     else if(dp[l][r] == -INF) cout << '.' << " ";
  //     else cout << dp[l][r] << " ";
  //   } cout << "\n";
  // }
  vector<ll> hp(n + 1);
  for(int l = 0; l < n; l++) {
    hp[l + 1] = max(hp[l + 1], hp[l]);
    for(int r = l + 1; r <= n; r++) {
      if(dp[l][r] != -1000000) {
        hp[r] = max(hp[r], hp[l] + (s[r] - s[l] - rem * dp[l][r]) / div);
      }
    }
  }
  cout << hp[n] << endl;
}
accuracy: 0.1320754716981132
time_ms: 1320
memory_kb: 4456
final_rank: 14

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

#pragma GCC optimize("O3")

using ll = long long;

int main() {
  int n;
  ll div, rem;
  cin >> n >> div >> rem;
  vector<ll> s(n + 1);
  for(int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] += s[i - 1];
  }
  const ll INF = LONG_MAX / 4;
  vector<int> ep(n + 1);
  vector dp(n + 1, vector<int>(n + 1, -1000000));
  for(int k = 1; k <= n; k++) {
    for(int l = 0, r = k; r <= n; l++, r++) {
      for(int i = 0; i <= n; i++) ep[i] = 0;
      for(int i = l; i < r; i++) {
        ep[i + 1] = max(ep[i + 1], ep[i]);
        for(int j = i + 1; j <= r; j++) {
          if(dp[i][j] != -INF) {
            ep[j] = max(ep[j], ep[i] + 1);
          }
        }
      }
      for(int c = 0; c <= ep[r]; c++) {
        if(((s[r] - s[l]) - rem * c) % div == rem) {
          dp[l][r] = c + 1;
          break;
        }
      }
    }
  }
  // for(int l = 0; l <= n; l++) {
  //   for(int r = 0; r <= n; r++) {
  //     if(l > r) cout << " " << " ";
  //     else if(dp[l][r] == -INF) cout << '.' << " ";
  //     else cout << dp[l][r] << " ";
  //   } cout << "\n";
  // }
  vector<ll> hp(n + 1);
  for(int l = 0; l < n; l++) {
    hp[l + 1] = max(hp[l + 1], hp[l]);
    for(int r = l + 1; r <= n; r++) {
      if(dp[l][r] != -INF) {
        hp[r] = max(hp[r], hp[l] + (s[r] - s[l] - rem * dp[l][r]) / div);
      }
    }
  }
  cout << hp[n] << endl;
}
accuracy: 0.018867924528301886
time_ms: 1280
memory_kb: 4364
final_rank: 20

code_snippet: #ifdef DEBUG
#include"stdlibrary.h"
#else
// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#endif
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
// #include<boost/multiprecision/cpp_int.hpp>
// namespace multiprecisioninteger = boost::multiprecision;
// using cint=multiprecisioninteger::cpp_int;
using ll=long long;
using datas=std::pair<ll,ll>;
using ddatas=std::pair<long double,long double>;
using tdata=std::pair<ll,datas>;
using vec=std::vector<ll>;
using mat=std::vector<vec>;
using pvec=std::vector<datas>;
using pmat=std::vector<pvec>;
// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i,a,b) if(const std::int64_t _iterate_from=a,_iterate_to=b;_iterate_from<_iterate_to)for(const std::int64_t i:std::views::iota(_iterate_from,_iterate_to))
#define bFor(i,b,a) for(ll i=b-1;i>=(ll)a;--i)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N,0)
#define brep1(i,N) bFor(i,N,1)
#define all(v) std::begin(v),std::end(v)
#define allr(v) std::rbegin(v),std::rend(v)
#define vsort(v) std::sort(all(v))
#define vrsort(v) std::sort(allr(v))
#define uniq(v) vsort(v),(v).erase(std::unique(all(v)),std::end(v))
#define endl "\n"
#define popcount __builtin_popcountll
#define print(x) std::cout<<x<<endl
#define printyes print("Yes")
#define printno print("No")
#define printYES print("YES")
#define printNO print("NO")
#define output(v) do{bool f=0;for(auto outi:v){std::cout<<(f?" ":"")<<outi;f=1;}std::cout<<endl;}while(0)
#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)
// constexpr ll mod=1000000007;
constexpr ll mod=998244353;
constexpr ll inf=1LL<<60;
constexpr long double eps=1e-9;
const long double PI=acosl(-1);
template<class T,auto x=T::mod()> std::ostream& operator<<(std::ostream& os,const T& v){return os<<v.val();}
template<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v);
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v);
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v);
template<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v);
template<class T,class E> std::ostream& operator<<(std::ostream& os,const std::pair<T,E>& p){return os<<"("<<p.first<<","<<p.second<<")";}
template<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T> inline bool chmax(T& a,const T b){bool x=a<b;if(x)a=b;return x;}
template<class T> inline bool chmin(T& a,const T b){bool x=a>b;if(x)a=b;return x;}
#ifdef DEBUG
void debugg(){std::cout<<endl;}
template<class T,class... Args>void debugg(const T& x,const Args&... args){std::cout<<" "<<x;debugg(args...);}
#define debug(...) cout<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debugg(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif

inline void startupcpp(void) noexcept{
  cin.tie(0)->sync_with_stdio(0);
  cout<<fixed<<setprecision(15);
}

ll modinv(ll a,const ll m=mod) noexcept{
  ll b=m,u=1,v=0,t;
  while(b){
    t=a/b;
    a-=t*b; swap(a,b);
    u-=t*v; swap(u,v);
  }
  return (u+m)%m;
}

ll moddevide(const ll a,const ll b) noexcept{return (a*modinv(b))%mod;}

vec modncrlistp,modncrlistm;

ll modncr(const ll n,const ll r){
  if(n<r||r<0)return 0;
  ll i,size=modncrlistp.size();
  if(size<=n){
    modncrlistp.resize(n+1);
    modncrlistm.resize(n+1);
    if(!size){
      modncrlistp[0]=modncrlistm[0]=1;
      size++;
    }
    For(i,size,n+1)modncrlistp[i]=modncrlistp[i-1]*i%mod;
    modncrlistm[n]=modinv(modncrlistp[n]);
    for(i=n;i>size;--i)modncrlistm[i-1]=modncrlistm[i]*i%mod;
  }
  return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;
}

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

constexpr ll gcd(const ll a,const ll b) noexcept{return (!b)?abs(a):(a%b==0)?abs(b):gcd(b,a%b);}
constexpr ll lcm(const ll a,const ll b) noexcept{return a/gcd(a,b)*b;}

// #include<atcoder/all>
// using mint=atcoder::modint998244353;
// void operator>>(istream& is,mint& v){long long x;is>>x;v=x;}

ll solve(const ll D, const ll R, const vec& a){
  const ll N=a.size();
  vec s(N+1);
  rep(i,N)s[i+1]=s[i]+a[i];

  mat dp(N,vec(N+1,0));
  vector<tuple<ll,ll,ll>> cs;
  rep1(j,N+1)brep(i,j){
    // [i,j)
    const ll sum=s[j]-s[i];
    For(k,i,j)chmax(dp[i][j],dp[i][k]+dp[k][j]);
    rep(k,dp[i][j]+1){
      if((sum-k*R)%D!=R)continue;
      chmax(dp[i][j],(ll)k+1);
      cs.emplace_back(i,j,(sum-(k+1)*R)/D);
    }
  }
  pmat g(N);
  for(const auto& [l,r,c]:cs)g[l].emplace_back(c,r);
  vec dp2(N+1,0);
  rep(i,N){
    for(const auto& [c,n]:g[i])chmax(dp2[n],dp2[i]+c);
    chmax(dp2[i+1],dp2[i]);
  }
  return dp2[N];
}

ll naive(const ll D, const ll R, const vec& a){
  map<vec,ll> mp;
  queue<pair<vec,ll>> que;
  que.emplace(a,0);
  mp[a]=0;
  while(!que.empty()){
    const auto [v,cost]=que.front();que.pop();
    if(mp[v]!=cost)continue;
    vec s(v.size()+1);
    rep(i,v.size())s[i+1]=s[i]+v[i];
    rep1(j,a.size()+1)rep(i,j){
      if((s[j]-s[i])%D!=R)continue;
      vec nv=v;
      For(k,i,j)nv[k]=0;
      if(chmax(mp[nv],cost+(s[j]-s[i]-R)/D))
        que.emplace(nv,cost+(s[j]-s[i]-R)/D);
    }
  }
  return ranges::max(mp|ranges::views::values);
}

int main(){
  startupcpp();
  ll N,D,R;
  cin>>N>>D>>R;
  vec a(N);
  rep(i,N)cin>>a[i];

  print(solve(D,R,a));

  // ll N,D,R;
  // vec a;
  // do{
  //   N=1+rand()%10;
  //   D=2+rand()%10;
  //   R=rand()%D;
  //   a.resize(N);
  //   rep(i,N)a[i]=rand()%10;
  //   if(solve(D,R,a)!=naive(D,R,a)){
  //     print(D);
  //     print(R);
  //     output(a);
  //     print(solve(D,R,a));
  //     print(naive(D,R,a));
  //     break;
  //   }
  // }while(1);
}
accuracy: 1
time_ms: 940
memory_kb: 1781060
final_rank: 12

code_snippet: #ifdef DEBUG
#include"stdlibrary.h"
#else
// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#endif
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
// #include<boost/multiprecision/cpp_int.hpp>
// namespace multiprecisioninteger = boost::multiprecision;
// using cint=multiprecisioninteger::cpp_int;
using ll=long long;
using datas=std::pair<ll,ll>;
using ddatas=std::pair<long double,long double>;
using tdata=std::pair<ll,datas>;
using vec=std::vector<ll>;
using mat=std::vector<vec>;
using pvec=std::vector<datas>;
using pmat=std::vector<pvec>;
// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i,a,b) if(const std::int64_t _iterate_from=a,_iterate_to=b;_iterate_from<_iterate_to)for(const std::int64_t i:std::views::iota(_iterate_from,_iterate_to))
#define bFor(i,b,a) for(ll i=b-1;i>=(ll)a;--i)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N,0)
#define brep1(i,N) bFor(i,N,1)
#define all(v) std::begin(v),std::end(v)
#define allr(v) std::rbegin(v),std::rend(v)
#define vsort(v) std::sort(all(v))
#define vrsort(v) std::sort(allr(v))
#define uniq(v) vsort(v),(v).erase(std::unique(all(v)),std::end(v))
#define endl "\n"
#define popcount __builtin_popcountll
#define print(x) std::cout<<x<<endl
#define printyes print("Yes")
#define printno print("No")
#define printYES print("YES")
#define printNO print("NO")
#define output(v) do{bool f=0;for(auto outi:v){std::cout<<(f?" ":"")<<outi;f=1;}std::cout<<endl;}while(0)
#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)
// constexpr ll mod=1000000007;
constexpr ll mod=998244353;
constexpr ll inf=1LL<<60;
constexpr long double eps=1e-9;
const long double PI=acosl(-1);
template<class T,auto x=T::mod()> std::ostream& operator<<(std::ostream& os,const T& v){return os<<v.val();}
template<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v);
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v);
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v);
template<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v);
template<class T,class E> std::ostream& operator<<(std::ostream& os,const std::pair<T,E>& p){return os<<"("<<p.first<<","<<p.second<<")";}
template<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v){
  os<<"{";bool f=false;
  for(auto& x:v){if(f)os<<",";os<<x;f=true;}
  os<<"}";
  return os;
}
template<class T> inline bool chmax(T& a,const T b){bool x=a<b;if(x)a=b;return x;}
template<class T> inline bool chmin(T& a,const T b){bool x=a>b;if(x)a=b;return x;}
#ifdef DEBUG
void debugg(){std::cout<<endl;}
template<class T,class... Args>void debugg(const T& x,const Args&... args){std::cout<<" "<<x;debugg(args...);}
#define debug(...) cout<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debugg(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif

inline void startupcpp(void) noexcept{
  cin.tie(0)->sync_with_stdio(0);
  cout<<fixed<<setprecision(15);
}

ll modinv(ll a,const ll m=mod) noexcept{
  ll b=m,u=1,v=0,t;
  while(b){
    t=a/b;
    a-=t*b; swap(a,b);
    u-=t*v; swap(u,v);
  }
  return (u+m)%m;
}

ll moddevide(const ll a,const ll b) noexcept{return (a*modinv(b))%mod;}

vec modncrlistp,modncrlistm;

ll modncr(const ll n,const ll r){
  if(n<r||r<0)return 0;
  ll i,size=modncrlistp.size();
  if(size<=n){
    modncrlistp.resize(n+1);
    modncrlistm.resize(n+1);
    if(!size){
      modncrlistp[0]=modncrlistm[0]=1;
      size++;
    }
    For(i,size,n+1)modncrlistp[i]=modncrlistp[i-1]*i%mod;
    modncrlistm[n]=modinv(modncrlistp[n]);
    for(i=n;i>size;--i)modncrlistm[i-1]=modncrlistm[i]*i%mod;
  }
  return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;
}

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

constexpr ll gcd(const ll a,const ll b) noexcept{return (!b)?abs(a):(a%b==0)?abs(b):gcd(b,a%b);}
constexpr ll lcm(const ll a,const ll b) noexcept{return a/gcd(a,b)*b;}

// #include<atcoder/all>
// using mint=atcoder::modint998244353;
// void operator>>(istream& is,mint& v){long long x;is>>x;v=x;}

ll solve(const ll D, const ll R, const vec& a){
  const ll N=a.size();
  vec s(N+1);
  rep(i,N)s[i+1]=s[i]+a[i];

  ll ans=0;
  mat dp(N,vec(N+1,0));
  rep1(j,N+1)brep(i,j){
    // [i,j)
    const ll sum=s[j]-s[i];
    For(k,i,j)chmax(dp[i][j],dp[i][k]+dp[k][j]);
    rep(k,dp[i][j]+1){
      if((sum-k*R)%D!=R)continue;
      chmax(dp[i][j],(ll)k+1);
      chmax(ans,(sum-(k+1)*R)/D);
    }
  }
  return ans;
}

int main(){
  startupcpp();
  ll N,D,R;
  cin>>N>>D>>R;
  vec a(N);
  rep(i,N)cin>>a[i];

  print(solve(D,R,a));
}
accuracy: 0.018867924528301886
time_ms: 100
memory_kb: 5376
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
using namespace std;
const int inf = 1000000007;
const ll longinf = 1ll << 60;

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll d, k;
    cin >> d >> k;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    vector dp(n + 1, vector<ll>(n + 1, 0));
    rep(r, n + 1) {
        ll sum = 0;
        for(int l = r - 1; l >= 0; l--) {
            sum += a[l];
            for(int m = l + 1; m < r; m++) {
                dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);
            }
            if((sum - (dp[l][r] + 1) * k) % d == 0) {
                dp[l][r] += 1;
            }
        }
    }
    vector<ll> dp2(n + 1);
    ll g = gcd(d, k);
    REP(r, 1, n + 1) {
        ll sum = 0;
        dp2[r] = dp2[r - 1];
        for(int l = r - 1; l >= 0; l--) {
            sum += a[l];
            if(sum % g != 0) {
                continue;
            }
            ll x, y;
            extGCD(d, k, x, y);
            ll cnt = sum / g;
            y = (y * cnt) % (d / g);
            if(y <= 0) {
                y += d / g;
            }
            if(dp[l][r] >= y) {
                dp2[r] = max(dp2[r], dp2[l] + (sum - y * k) / d);
            }
        }
    }
    cout << dp2[n] << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5376
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
using namespace std;
const int inf = 1000000007;
const ll longinf = 1ll << 60;

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll d, k;
    cin >> d >> k;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    vector dp(n + 1, vector<ll>(n + 1, 0));
    rep(r, n + 1) {
        ll sum = 0;
        for(int l = r - 1; l >= 0; l--) {
            sum += a[l];
            for(int m = l + 1; m < r; m++) {
                dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);
            }
            if((sum - (dp[l][r] + 1) * k) % d == 0) {
                dp[l][r] += 1;
            }
        }
    }
    vector<ll> dp2(n + 1);
    ll g = gcd(d, k);
    REP(r, 1, n + 1) {
        ll sum = 0;
        dp2[r] = dp2[r - 1];
        for(int l = r - 1; l >= 0; l--) {
            sum += a[l];
            if(sum % g != 0) {
                continue;
            }
            ll x, y;
            extGCD(d, k, x, y);
            ll cnt = sum / g;
            y = (y * cnt) % (d / g);
            x = (x * cnt) % (k / g);
            if(y <= 0) {
                y += d / g;
                x -= k / g;
            }
            if(dp[l][r] >= y) {
                dp2[r] = max(dp2[r], dp2[l] + (sum - y * k) / d);
            }
        }
    }
    cout << dp2[n] << endl;
    return 0;
}
accuracy: 0.018867924528301886
time_ms: 10
memory_kb: 5376
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)

int main(){
    int N, D, R;
    cin >> N >> D >> R;
    vector<ll> A(N);
    for (auto &a : A) cin >> a;
    vector<ll> S(N + 1);
    rep(i, 0, N) S[i + 1] = S[i] + A[i];
    vector C(N + 1, vector<ll>(N + 1, 1 << 30));
    rep(l, 0, N) rep(r, l + 1, N + 1){
        ll sum = (S[r] - S[l]) % D;
        rep(c, 1, N + 1){
            sum = (sum + D - R) % D;
            if (sum == 0){
                C[l][r] = c;
                // cout << "C " << l << " " << r << " " << c << endl;
                break;
            }
        }
    }
    vector dp(N + 1, vector<ll>(N + 1));
    vector wei(N + 1, vector<ll>(N + 1, 0));
    for (int l = N - 1; l >= 0; l--) rep(r, l + 1, N + 1){
        rep(k, l + 1, r){
            dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r]);
        }
        if (dp[l][r] >= C[l][r] - 1){
            dp[l][r] = max(dp[l][r], C[l][r]);
            wei[l][r] = (S[r] - S[l] - C[l][r] * R) / D;
            // cout << l << " " << r << " " << wei[l][r] << endl;
        }
    }
    vector<ll> ans(N + 1);
    rep(l, 0, N) rep(r, l + 1, N + 1){
        ans[r] = max(ans[r], ans[l] + wei[l][r]);
    }
    cout << ans.back() << "\n";
}
accuracy: 1
time_ms: 580
memory_kb: 9388
final_rank: 7

code_snippet: //#define _GLIBCXX_DEBUG

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

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


#ifdef LOCAL
#include <debug_print.hpp>
#define OUT(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define OUT(...) (static_cast<void>(0))
#endif

#define endl '\n'
#define lfs cout<<fixed<<setprecision(15)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)

namespace template_tute{
  using ll = long long;
  using ld = long double;
  const ll MOD1 = 1e9+7;
  const ll MOD9 = 998244353;
  const ll INF = 4.1e18;
  using P = pair<ll, ll>;
  template<typename T> using PQ = priority_queue<T>;
  template<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;
  template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
  template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
  ll median(ll a,ll b, ll c){return a+b+c-max<ll>({a,b,c})-min<ll>({a,b,c});}
  void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
  void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
  void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
  template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
  template<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};  
  template<typename T>void debug(const T &v,ll h,ll w,string sv=" "){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};
  template<typename T>void debug(const T &v,ll n,string sv=" "){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};
  template<typename T>void debug(const vector<T>&v){debug(v,v.size());}
  template<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}
  template<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
  template<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop();}cout<<endl;}
  template<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop_front();}cout<<endl;}
  template<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
  template<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
  template<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
  template<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
  template<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<" ";cout<<endl;}
  template<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<"["<<z.first<<"]="<<z.second<<",";cout<<endl;}
  template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
  vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
  template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
  template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
  template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << "(" << p.first << "," << p.second << ")";}
  template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){os<<"[";for(auto &z:v)os << z << ",";os<<"]"; return os;}
  template<typename T>void rearrange(vector<int>&ord, vector<T>&v){
    auto tmp = v;
    for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
  }
  template<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){
    rearrange(ord, head);
    rearrange(ord, tail...);
  }
  template<typename T> vector<int> ascend(const vector<T>&v){
    vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
    sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],i)<make_pair(v[j],j);});
    return ord;
  }
  template<typename T> vector<int> descend(const vector<T>&v){
    vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
    sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],-i)>make_pair(v[j],-j);});
    return ord;
  }
  template<typename T> vector<T> inv_perm(const vector<T>&ord){
    vector<T>inv(ord.size());
    for(int i=0;i<ord.size();i++)inv[ord[i]] = i;
    return inv;
  }
  ll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}
  ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}
  ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}
  ll modulo(ll n,ll d){return (n%d+d)%d;};
  template<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}
  template<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}
  template<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};
  template<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};
  //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
  int popcount(ll x){return __builtin_popcountll(x);};
  int poplow(ll x){return __builtin_ctzll(x);};
  int pophigh(ll x){return 63 - __builtin_clzll(x);};
  template<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};
  template<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};
  template<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};
  template<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};
  ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}
  ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}
  ll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}
  std::ostream &operator<<(std::ostream &dest, __int128_t value) {
    std::ostream::sentry s(dest);
    if (s) {
      __uint128_t tmp = value < 0 ? -value : value;
      char buffer[128];
      char *d = std::end(buffer);
      do {
        --d;
        *d = "0123456789"[tmp % 10];
        tmp /= 10;
      } while (tmp != 0);
      if (value < 0) {
        --d;
        *d = '-';
      }
      int len = std::end(buffer) - d;
      if (dest.rdbuf()->sputn(d, len) != len) {
        dest.setstate(std::ios_base::badbit);
      }
    }
    return dest;
  }
  namespace converter{
    int dict[500];
    const string lower="abcdefghijklmnopqrstuvwxyz";
    const string upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const string digit="0123456789";
    const string digit1="123456789";
    void regi_str(const string &t){
      for(int i=0;i<t.size();i++){
        dict[t[i]]=i;
      }
    }
    void regi_int(const string &t){
      for(int i=0;i<t.size();i++){
        dict[i]=t[i];
      }
    }
    vector<int>to_int(const string &s,const string &t){
      regi_str(t);
      vector<int>ret(s.size());
      for(int i=0;i<s.size();i++){
        ret[i]=dict[s[i]];
      }
      return ret;
    }
    vector<int>to_int(const string &s){
      auto t=s;
      sort(t.begin(),t.end());
      t.erase(unique(t.begin(),t.end()),t.end());
      return to_int(s,t);
    }
    
    vector<vector<int>>to_int(const vector<string>&s,const string &t){
      regi_str(t);
      vector<vector<int>>ret(s.size(),vector<int>(s[0].size()));
      for(int i=0;i<s.size();i++){
        for(int j=0;j<s[0].size();j++){
          ret[i][j]=dict[s[i][j]];
        }
      }
      return ret;
    }
    vector<vector<int>>to_int(const vector<string>&s){
      string t;
      for(int i=0;i<s.size();i++){
        t+=s[i];
      }
      sort(t.begin(),t.end());t.erase(unique(t.begin(),t.end()),t.end());
      return to_int(s,t);
    }
    string to_str(const vector<int>&s,const string &t){
      regi_int(t);
      string ret;
      for(auto z:s)ret+=dict[z];
      return ret;
    }
    vector<string> to_str(const vector<vector<int>>&s,const string &t){
      regi_int(t);
      vector<string>ret(s.size());
      for(int i=0;i<s.size();i++){
        for(auto z:s[i])ret[i]+=dict[z];
      }
      return ret;
    }
  }
  template< typename T = int >
  struct edge {
    int to;
    T cost;
    int id;
    edge():to(-1),id(-1){};
    edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}
    operator int() const { return to; }
  };

  template<typename T>
  using Graph = vector<vector<edge<T>>>;
  template<typename T>
  Graph<T>revgraph(const Graph<T> &g){
    Graph<T>ret(g.size());
    for(int i=0;i<g.size();i++){
      for(auto e:g[i]){
        int to = e.to;
        e.to = i;
        ret[to].push_back(e);
      }
    }
    return ret;
  }
  template<typename T>
  Graph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){
    Graph<T> ret(n);
    for(int es = 0; es < m; es++){
      int u,v;
      T w=1;
      cin>>u>>v;u-=indexed,v-=indexed;
      if(weighted)cin>>w;
      ret[u].emplace_back(v,w,es);
      if(!directed)ret[v].emplace_back(u,w,es);
    }
    return ret;
  }
  template<typename T>
  Graph<T> readParent(int n,int indexed=1,bool directed=true){
    Graph<T>ret(n);
    for(int i=1;i<n;i++){
      int p;cin>>p;
      p-=indexed;
      ret[p].emplace_back(i);
      if(!directed)ret[i].emplace_back(p);
    }
    return ret;
  }
}
using namespace template_tute;

void solve(){
	ll res=0,buf=0;
  bool judge = true;

  ll n,d,rem;cin>>n>>d>>rem;
  vector<ll>a(n);
  rep(i,0,n)cin>>a[i];
  //rep(i,0,n)a[i]=1;
  vector<ll>sa(n+1);
  rep(i,0,n)sa[i+1]=sa[i]+a[i];
  auto c=vec(n+1,n+1,0LL);
  auto dpc=vec(n+1,n+1,INF);
  rrep(l,0,n){
    dpc[l]=dpc[l+1];
    dpc[l][0]=l+1;
    vector<bool>used(n+1,false);
    while(1){
      ll mi=INF,num=-1;
      rep(k,0,n){
        if(!used[k]&&chmin(mi,dpc[l][k])){
          num=k;
        }
      }
      if(num==-1)break;
      used[num]=true;
      rep(nk,0,n+1){
        if(num+nk<=n){
          chmin(dpc[l][num+nk],dpc[mi][nk]);
        }
      }
      rep(r,mi,n+1){
        if(modulo(sa[r]-sa[l]-num*rem,d)==rem){
          //OUT(l,r,num,sa[r]-sa[l],num*rem);
          chmin(dpc[l][num+1],r);
          chmax(c[l][r],(sa[r]-sa[l]-(num+1)*rem)/d);
        }
      }
    }
    OUT(l,dpc[l]);
  }
  vector<ll>dp(n+1,0);
  rep(i,0,n){
    rep(j,i+1,n+1){
      chmax(dp[j],dp[i]+c[i][j]);
    }
    chmax(dp[i+1],dp[i]);
  }
  cout<<dp[n]<<endl;
}

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
  int T = 1;
  //cin>>T;
  while(T--){
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 1410
memory_kb: 7056
final_rank: 11

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef bitset<501> BS;

const int MAXN = 500;

int N, D, R, A[MAXN+10];
ll S[MAXN+10];
int dp[MAXN+10][MAXN+10];
BS dp2[MAXN+10];
ll dp3[MAXN+10];

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

    cin >> N >> D >> R;
    for(int i=1; i<=N; i++) cin >> A[i], S[i]=S[i-1]+A[i];

    for(int l=N; l>=1; l--)
    {
        dp2[l-1][0]=1;
        for(int r=l; r<=N; r++)
        {
            dp2[r]=dp2[r-1];
            for(int i=l; i<r; i++) dp2[r]|=dp2[i]<<dp[i+1][r];
            ll sum=S[r]-S[l-1];
            dp[l][r]=N+1;
            for(int i=N; i>=1; i--) if(dp2[r][i-1] && (ll)i*R%D==sum%D)
            {
                dp2[r][i]=1;
                dp[l][r]=i;
            }
            // printf("%d %d : %d\n", l, r, dp[l][r]);
            // for(int i=0; i<=N; i++) if(dp2[r][i]) printf("%d ", i);
            // printf("\n");
        }
    }

    for(int i=1; i<=N; i++)
    {
        dp3[i]=dp3[i-1];
        for(int j=1; j<=i; j++)
        {
            if(dp[j][i]<=N) dp3[i]=max(dp3[i], dp3[j-1]+(S[i]-S[j-1]-(ll)dp[j][i]*R)/D);
        }
    }
    cout << dp3[N] << "\n";
}
accuracy: 1
time_ms: 790
memory_kb: 4504
final_rank: 9

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef bitset<501> BS;

const int MAXN = 500;

int N, D, R, A[MAXN+10];
ll S[MAXN+10];
int dp[MAXN+10][MAXN+10];
BS dp2[MAXN+10];
ll dp3[MAXN+10];

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

    cin >> N >> D >> R;
    for(int i=1; i<=N; i++) cin >> A[i], S[i]=S[i-1]+A[i];

    for(int l=N; l>=1; l--)
    {
        dp2[l-1][0]=1;
        for(int r=l; r<=N; r++)
        {
            dp2[r]=dp2[r-1];
            for(int i=l; i<r; i++) dp2[r]|=dp2[i]<<dp[i+1][r];
            ll sum=S[r]-S[l-1];
            dp[l][r]=N+1;
            for(int i=N; i>=1; i--) if(dp2[r][i-1] && (ll)i*R%D==sum%D)
            {
                dp2[r][i]=1;
                dp[l][r]=i;
            }
            // printf("%d %d : %d\n", l, r, dp[l][r]);
            // for(int i=0; i<=N; i++) if(dp2[r][i]) printf("%d ", i);
            // printf("\n");
        }
    }

    for(int i=1; i<=N; i++)
    {
        dp3[i]=dp3[i-1];
        for(int j=1; j<=i; j++)
        {
            if(dp[j][i]<=N) dp3[i]=max(dp3[i], dp3[j-1]+(S[i]-S[j-1]-dp[j][i]*R)/D);
        }
    }
    cout << dp3[N] << "\n";
}
accuracy: 0.2830188679245283
time_ms: 560
memory_kb: 4500
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=505;
const ll INF=15LL<<58;

ll S[MAX];
ll mi[MAX][MAX];
ll sub[MAX][MAX];
ll dp[MAX];
ll T[MAX][MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N,A,B;cin>>N>>A>>B;
    for(int i=0;i<N;i++){
        ll x;cin>>x;
        S[i+1]=S[i]+x;
    }
    for(int i=0;i<MAX;i++){
        for(int j=0;j<MAX;j++){
            T[i][j]=INF;
            sub[i][j]=INF;
            mi[i][j]=INF;
        }
    }
    
    for(int l=N-1;l>=0;l--){
        for(int r=l+1;r<=N;r++){
            if((S[r]-S[l])%A==B){
                chmin(T[1][l],(ll)(r));
            }
        }
        for(int len=1;len<=N;len++){
            for(int i=N;i>=0;i--){
                chmin(T[len][i],T[len][i+1]);
            }
        }
        sub[l][0]=l;
        for(int s=0;s<=N;s++){
            if(sub[l][s]==INF) continue;
            for(int ad=1;s+ad<=N;ad++){
                if(T[ad][sub[l][s]]<=N) chmin(sub[l][s+ad],T[ad][sub[l][s]]);
            }
        }
        
        for(int s=0;s<=N;s++){
            if(sub[l][s]==INF) continue;
            if(s){
                chmin(T[s][l],sub[l][s]);
            }
            for(int r=sub[l][s];r<=N;r++){
                if(r==l) continue;
                ll sum=S[r]-S[l];
                if(sum%A==(B*(s+1))%A){
                    chmin(T[s+1][l],(ll)(r));
                    chmin(mi[l][r],s+1LL);
                }
            }
        }
    }
    
    for(int i=0;i<N;i++){
        for(int j=i+1;j<=N;j++){
            if(mi[i][j]!=INF){
                chmax(dp[j],dp[i]+(S[j]-S[i]-mi[i][j]*B)/A);
            }
        }
        chmax(dp[i+1],dp[i]);
    }
    
    cout<<dp[N]<<endl;
}
accuracy: 1
time_ms: 580
memory_kb: 9456
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=505;
const ll INF=15LL<<58;

ll S[MAX];
ll mi[MAX][MAX];
ll sub[MAX][MAX];
ll dp[MAX];
ll T[MAX][MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N,A,B;cin>>N>>A>>B;
    for(int i=0;i<N;i++){
        ll x;cin>>x;
        S[i+1]=S[i]+x;
    }
    for(int i=0;i<MAX;i++){
        for(int j=0;j<MAX;j++){
            T[i][j]=INF;
            sub[i][j]=INF;
            mi[i][j]=INF;
        }
    }
    
    for(int l=N-1;l>=0;l--){
        for(int r=l+1;r<=N;r++){
            if((S[r]-S[l])%A==B){
                chmin(T[1][l],(ll)(r));
            }
        }
        for(int len=1;len<=N;len++){
            for(int i=N;i>=0;i--){
                chmin(T[len][i],T[len][i+1]);
            }
        }
        sub[l][0]=l;
        for(int s=0;s<=N;s++){
            if(sub[l][s]==INF) continue;
            for(int ad=1;s+ad<=N;ad++){
                if(T[ad][sub[l][s]]<=N) chmin(sub[l][s+ad],T[ad][sub[l][s]]);
            }
        }
        
        for(int s=0;s<=N;s++){
            if(sub[l][s]==INF) continue;
            for(int r=sub[l][s];r<=N;r++){
                if(r==l) continue;
                ll sum=S[r]-S[l];
                if(sum%A==(B*(s+1))%A){
                    chmin(mi[l][r],s+1LL);
                }
            }
        }
    }
    
    for(int i=0;i<N;i++){
        for(int j=i+1;j<=N;j++){
            if(mi[i][j]!=INF){
                chmax(dp[j],dp[i]+(S[j]-S[i]-mi[i][j]*B)/A);
            }
        }
        chmax(dp[i+1],dp[i]);
    }
    
    cout<<dp[N]<<endl;
}
accuracy: 0.07547169811320754
time_ms: 300
memory_kb: 9452
final_rank: 16