Problem:
五等分のケーキ (Divide Cake into Five)
Segtree 君は五つ子の家庭教師をしています。今日はクリスマスイブなので、五つ子のために円形のケーキを五等分しようとしています。
ケーキは中心から扇形状に $N$ 個のピースに分けられており、 $i$ 番目と $i + 1$ 番目($1 \leq i \leq N - 1$) 、 $N$ 番目と $1$ 番目のピースは隣り合っています。
$i$ 番目のピースの大きさは $A_i$ です。全てのピースの大きさの和を $S$ とすると、全ての入力について $S$ が $5$ の倍数であることが保証されます。
ある非負整数 $Y$ が与えられます。以下の条件を満たすようなケーキの五つ子への分け方を、「ケーキの五等分」と呼びます。
全ての人が1つ以上のピースを取る。
ケーキの中でそれぞれが取るピースたちは連結である。つまり、取る人でピースをグループ分けしたとき、同じグループかつ隣り合っているピースに移動することを繰り返して辿り着けないような同じグループ内のピースの組は存在しない。
誰も取らないピースは存在しない。
全ての人について、取るピースの大きさを $X$ としたとき、必ず $X + Y \geq S / 5$ を満たす。
「ケーキの五等分」になるようなケーキの分け方の通り数が何通りあるか求めてください。
入力
入力は以下の形式で標準入力から与えられる。
$N$ $Y$
$A_1$ $A_2$ $\ldots$ $A_N$
出力
「ケーキの五等分」になるようなケーキの分け方の通り数を出力してください。
ただし、最後には改行を入れること。
制約
$5 \leq N \leq 300$
$1 \leq A_i \leq 10^9$
$0 \leq Y \leq 10^9$
入力は全て整数である。
入力例1
5 0
1 1 1 1 1
出力例1
1
入力例2
10 27
3 1 4 1 5 9 2 6 5 4
出力例2
252

Submitted solutions:

code_snippet: // AOJ #3135 Divide Cake into Five
// 2025.3.21

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

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

    int N; ll Y;
    cin >> N >> Y;
    vector<ll> A(N);
    ll S = 0;
    for (int i = 0; i < N; i++){
        cin >> A[i];
        S += A[i];
    }
    ll q = S / 5;
    ll t = q - Y;
    ll lb = (t <= 1 ? 1 : t);

    ll tot = 0;
    for (int s = 0; s < N; s++){
        vector<ll> arr(N);
        for (int i = 0; i < N; i++) arr[i] = A[(s+i) % N];
        vector<ll> P(N+1, 0);
        for (int i = 0; i < N; i++) P[i+1] = P[i] + arr[i];
        vector<vector<ll>> dp(N+1, vector<ll>(6, 0));
        dp[0][0] = 1;
        for (int i = 0; i <= N; i++){
            for (int j = 0; j < 5; j++){
                if(dp[i][j] == 0) continue;
                for (int k = i+1; k <= N; k++){
                    ll sum = P[k] - P[i];
                    if(sum >= lb) dp[k][j+1] += dp[i][j];
                }
            }
        }
        tot += dp[N][5];
    }
    cout << tot / 5 << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3444
final_rank: 8

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

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

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    ll Y; cin >> Y;
    vector<ll> a(n);
    vector<ll> sum(n+1);
    for(int i=0;i<n;i++){
        cin >> a[i];
        sum[i+1] = sum[i] + a[i];
    }
    ll li = sum[n]/5 - Y;
    ll res = 0;
    vector<vector<int>> dp(5,vector<int>(n+1));
    for(int i=0;i<n;i++){
        // [0,i]は1人目が取る
        for(int j=0;j<5;j++){
            for(int k=0;k<=n;k++){
                dp[j][k] = 0;
            }
        }
        dp[0][i] = 1;
        for(int j=0;j<4;j++){
            for(int k=0;k<n;k++){
                if(!dp[j][k]) continue;
                for(int l=k+1;l<n;l++){
                    ll u = sum[l+1] - sum[k+1];
                    if(u >= li){
                        dp[j+1][l] += dp[j][k];
                    }
                }
            }
            for(int k=0;k<n;k++){
                if(!dp[4][k]) continue;
                if(sum[i+1]+(sum[n]-sum[k+1]) >= li){
                    res += dp[4][k];
                }
            }
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3480
final_rank: 5

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


#define SIZE 605

int N;
ll ADD;
ll table[SIZE];
ll dp[SIZE][5][SIZE]; //dp[数列の末尾][何人目(=k)か][k人目の始点] = 場合の数
ll BASE;

ll recursive(int tail,int index,int loc){

	if(dp[tail][index][loc] != -1){

		return dp[tail][index][loc];
	}

	if(index == 4){ //取り分は自動決定

		if(table[tail]-table[loc-1]+ADD >= BASE){

			return dp[tail][index][loc] = 1;
		}else{

			return dp[tail][index][loc] = 0;
		}
	}

	int left = loc,right = tail,mid = (left+right)/2;
	int left_loc = tail+1;

	while(left <= right){

		if(table[mid]-table[loc-1]+ADD >= BASE){

			left_loc = mid;
			right = mid-1;
		}else{

			left = mid+1;
		}
		mid = (left+right)/2;
	}

	ll ret = 0;
	for(int next_start = left_loc+1; next_start <= tail; next_start++){

		ret += recursive(tail,index+1,next_start);
	}

	return dp[tail][index][loc] = ret;
}


int main(){

	scanf("%d %lld",&N,&ADD);

	table[0] = 0;
	ll tmp_sum = 0;

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

		scanf("%lld",&table[i]);
		tmp_sum += table[i];

		table[N+i] = table[i];
	}
	BASE = tmp_sum/5;


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

		table[i] += table[i-1];
	}

	for(int tail = N; tail <= 2*N-1; tail++){
		for(int i = 1; i <= 4; i++){
			for(int k = 1; k <= 2*N-1; k++){

				dp[tail][i][k] = -1;
			}
		}
	}

	ll ans = 0;
	int left,right,mid;
	int left_loc = 2*N,tail;

	for(int start = 1; start <= N; start++){ //0人目の始点

		tail = start+(N-1);
		left = start,right = tail,mid = (left+right)/2;

		while(left <= right){ //和がS/5以上となる最小のインデックスを求める

			if(table[mid]-table[start-1]+ADD >= BASE){

				left_loc = mid;
				right = mid-1;
			}else{

				left = mid+1;
			}
			mid = (left+right)/2;
		}

		for(int next_start = left_loc+1; next_start <= tail; next_start++){

			ans += recursive(tail,1,next_start);
		}
	}

	printf("%lld\n",ans/5); //円順列

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

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int n;
    ll y;
    cin >>n >>y;

    ll s = 0;
    vector<ll> a(n);
    rep(i,n){
        cin >>a[i];
        s += a[i];
    }

    ll ans = 0;
    for(int pre=1; pre<n; ++pre)rep(suf,n){
        if(pre+suf>=n) continue;

        ll tt = 0;
        rep(i,pre) tt += a[i];
        rep(i,suf) tt += a[n-1-i];
        if(tt+y < s/5) continue;

        vector<ll> v;
        for(int i=pre; i<n-suf; ++i) v.pb(a[i]);
        int V = v.size();
        if(V<4) continue;

        vector<ll> p(V+1);
        rep(i,V) p[i+1] = v[i]+p[i];

        vector<ll> dp(V+1);
        dp[0] = 1;
        rep(loop,4){
            vector<ll> nx(V+1);

            ll ss = 0;
            int j = 0;
            rep(i,V){
                while(j<i+1 && p[i+1]-p[j]+y>=s/5){
                    ss += dp[j];
                    ++j;
                }
                nx[i+1] = ss;
            }
            dp = nx;
        }

        ans += dp[V];
    }

    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3184
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>

long long int cal_pattern(int from, int until, int count, std::vector<std::vector<std::vector<long long int>>> &memo, const std::vector<long long int>& pieces, const long long int sum, const long long int y) {
	if (--count < 0) return from == until ? 1 : 0;
	if (memo[from][until][count] >= 0) return memo[from][until][count];
	long long int s = 0;
	long long int result = 0;
	for (auto i = from; i != until; i = (i + 1) % pieces.size()) {
		s += pieces[i];
		if (s + y >= sum / 5) {
			result += cal_pattern((i + 1) % pieces.size(), until, count, memo, pieces, sum, y);
		}
	}
	return memo[from][until][count] = result;
}
long long int solve(const std::vector<long long int>& pieces, const long long int y) {
	const auto sum = std::accumulate(pieces.begin(), pieces.end(), 0LL);
	std::vector<std::vector<std::vector<long long int>>> memo(pieces.size(), std::vector<std::vector<long long int>>(pieces.size(), std::vector<long long int>(5, -1)));
	long long int result = 0;
	for (auto from = 0; from < pieces.size(); ++from) {
		long long int s = pieces[from];
		for (auto size = 1; size < pieces.size(); ++size) {
			if (s + y >= sum / 5) result += cal_pattern((from + size) % pieces.size(), from, 4, memo, pieces, sum, y);
			s += pieces[(from + size) % pieces.size()];
		}
	}
	return result / 5;
}

int main() {
	int n, y; std::cin >> n >> y;
	std::vector<long long int> pieces(n); for (auto& p : pieces) std::cin >> p;
	std::cout << solve(pieces, y) << std::endl;
}
accuracy: 1
time_ms: 930
memory_kb: 9532
final_rank: 20

code_snippet: #include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <utility>
#include <queue>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

llint n, y;
llint a[305], s[305];
llint dp[305][305][5];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> y;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) s[i] = s[i-1] + a[i];
	llint d = s[n]/5-y;
	
	llint ans = 0;
	for(int p = 0; p < n; p++){
		for(int i = p; i <= n; i++){
			for(int j = p; j <= n; j++){
				for(int k = 0; k <= 4; k++){
					dp[i][j][k] = 0;
				}
			}
		}
		dp[p][p][0] = 1;
		for(int i = p; i < n; i++){
			for(int j = p; j <= n; j++){
				for(int k = 0; k <= 4; k++){
					if(k+1 <= 4 && s[i+1]-s[j] >= d) dp[i+1][i+1][k+1] += dp[i][j][k];
					dp[i+1][j][k] += dp[i][j][k];
				}
			}
		}
		for(int j = p; j < n; j++){
			if(s[n]-s[j]+s[p] >= d) ans += dp[n][j][4];
		}
	}
	cout << ans << endl;
	
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 6724
final_rank: 15

code_snippet: //
// Created by yamunaku on 2019/12/29.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

int main(){
    // CFS;
    int n;
    ll y;
    cin >> n >> y;
    vl a(2 * n);
    ll s = 0;
    rep(i, n){
        cin >> a[i];
        s += a[i];
        a[n + i] = a[i];
    }
    s = s / 5 - y;
    vl l(n, n + 1);
    rep(i, n){
        ll sum = 0;
        repl(j, i, n){
            sum += a[j];
            if(sum >= s){
                l[i] = j;
                break;
            }
        }
    }
    ll ans = 0;
    rep(i, n){
        mtl dp(n, vl(5, 0));
        dp[i][0] = 1;
        repl(j, i + 1, n){
            repl(k, i, j){
                if(j >= l[k + 1]){
                    repl(x, 1, 5){
                        dp[j][x] += dp[k][x - 1];
                    }
                }
            }
        }
        ll sum = 0;
        rep(j, i + 1){
            sum += a[j];
        }
        per(j, n){
            if(sum >= s) ans += dp[j][4];
            sum += a[j];
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3168
final_rank: 1

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

int main(){
    int N, Y;
    cin >> N >> Y;
    vector<int64_t> A(2*N), S(2*N+1);
    for(int i=0; i<N; i++){
        cin >> A[i];
        A[i+N] = A[i];
    }
    for(int i=0; i<2*N; i++){
        S[i+1] = S[i] + A[i];
    }
    int64_t L = accumulate(A.begin(), A.end(), 0LL) / 10 - Y;
    int64_t ans = 0;
    for(int s=N; s>=0; s--){
        int t = s+N;
        int64_t dp[601][6] = {0};
        dp[s][0] = 1;
        for(int i=s; i<t; i++) for(int j=i+1; j<=t; j++) if(S[j] - S[i] >= L) for(int k=0; k<5; k++){
            if(k == 0 && j <= N) continue;
            dp[j][k+1] += dp[i][k];
        }
        ans += dp[t][5];
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3144
final_rank: 9

code_snippet: #include<iostream>
using namespace std;
int N;
long Y,A[300];
long dp[6][301];
main()
{
	cin>>N>>Y;
	long S=0,ans=0;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		S+=A[i];
	}
	Y-=S/5;
	for(int i=0;i<N;i++)
	{
		for(int k=0;k<=5;k++)for(int j=0;j<=N;j++)dp[k][j]=0;
		dp[0][0]=1;
		for(int j=0;j<N;j++)
		{
			long sum=Y;
			for(int k=j+1;k<=N;k++)
			{
				sum+=A[(i+k-1)%N];
				if(sum>=0)
				{
					for(int l=0;l<5;l++)dp[l+1][k]+=dp[l][j];
				}
			}
		}
		ans+=dp[5][N];
	}
	cout<<ans/5<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3116
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P std::pair<int,int>
#define PiP std::pair<int,std::pair<int,int>>
#define all(v) v.begin(),v.end()
#define mkp std::make_pair
#define prique(T) std::priority_queue<T,vector<T>,greater<T>>
using namespace std;
template<class T> inline void chmax(T &a, T b) {
	a = std::max(a, b);
}
template<class T> inline void chmin(T &a, T b) {
	a = std::min(a, b);
}

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)
			return false;
	}
	return x != 1;
}
int gcd(int x, int y) {
	if (y == 0)
		return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i;
		res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)
		return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod ;
}
int get_rand(int MIN, int MAX) {
	std::random_device rnd;
	std::mt19937 mt32(rnd());
	std::uniform_int_distribution<int> engine(MIN, MAX);
	return engine(mt32);
}
/*--------Library Zone!--------*/

int N, Y, A[305], sum[700];
signed main() {
	cin >> N >> Y;
	int all=0;
	REP(i,N+1)
	{
		cin >> A[i];
		all+=A[i];
		sum[i]+=A[i];
		sum[i+1]+=sum[i];
	}
	int least=all/5;
	REP(i,N+1){
		sum[i+N]=sum[N]+sum[i];
	}
	int ans = 0;
	REP(i,N+1){
		int DP[705][6];
		rep(j,705)rep(k,6)DP[j][k]=0;
		DP[i][0]=1;
		for(int j=i;j<i+N;j++){
			for(int k=j;k<i+N;k++){
				if(sum[k]-sum[j-1]+Y>=least){
					rep(l,5){
						DP[k+1][l+1]+=DP[j][l];
					}
				}
			}
		}
		ans+=DP[i+N][5];
	}
	cout<<ans/5<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3148
final_rank: 3

code_snippet: #include "bits/stdc++.h"

using namespace std;

using i64 = long long;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;


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

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


i64 solve(i64 n, i64 y, i64 a_sum, vector<i64> a){
    vector<vector<i64>> dp(n + 1, vector<i64>(6, 0));
    dp[0][0] = 1;
    for(i64 i = 0; i < n; ++i){
        i64 sum = 0;
        for(i64 j = i; j < n; ++j){
            sum += a[j];
            if(sum + y >= a_sum / 5){
                for(i64 k = 0; k < 5; ++k)
                    dp[j + 1][k + 1] += dp[i][k];
            }
        }
    }
    return dp.back()[5];
}


signed main(){

    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    i64 n, y;
    cin >> n >> y;
    vector<i64> a(n);
    for(auto& x : a)
        cin >> x;
    i64 sum = 0;
    for(i64 i = 0; i < n; ++i){
        sum += a[i];
        a.push_back(a[i]);
    }
    i64 ans = 0;
    for(i64 i = 0; i < n; ++i){
        vector<i64> b;
        for(i64 j = 0; j < n; ++j)
            b.emplace_back(a[i + j]);
        ans += solve(n, y, sum, b);
    }

    cout << ans / 5 << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3252
final_rank: 4

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

ll dp[606][606][6];

int main(){
  int N;
  ll Y;
  cin>>N>>Y;
  vector<ll> A(N);
  rep(i,N) cin>>A[i];
  ll S=0;
  rep(i,N) S+=A[i];
  vector<ll> sum(2*N+1,0);
  for(int i=1;i<=2*N;i++) sum[i]=sum[i-1]+A[(i-1)%N];

  for(int i=0;i<N;i++){
    for(int j=i;j<i+N;j++){
      ll res=sum[j+1]-sum[i];
      if(res+Y>=S/5) dp[i][j][1]=1;
    }
  }

  for(int k=2;k<=5;k++){
    for(int i=0;i<N;i++){
      for(int j=i;j<i+N;j++){
        for(int l=i;l<j;l++){
          dp[i][j][k]+=dp[i][l][1]*dp[l+1][j][k-1];
        }
      }
    }
  }

  //cout<<dp[0][N-1][5]<<endl;
  ll ans=0;
  for(int i=0;i<N;i++) ans+=dp[i][i+N-1][5];
  cout<<ans<<endl;
  

  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 8516
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000



int main(){
	
	int N,Y;
	cin>>N>>Y;
	
	long long S = 0;
	
	vector<int> A(N);
	for(int i=0;i<N;i++){
		cin>>A[i];
		S += (long long)A[i];
	}
	
	long long X = S/5 - Y;
	
	long long ans = 0;
	
	for(int o=0;o<N;o++){
		vector<vector<long long>> dp(N+1,vector<long long>(6,0));
		dp[0][0] = 1;
		
		for(int i=1;i<=N;i++){
			for(int j=1;j<6;j++){
				long long sum = 0;
				for(int k=i-1;k>=0;k--){
					sum += A[k];
					if(sum < X)continue;
					dp[i][j] += dp[k][j-1];
				}
			}
		}
		ans += dp[N][5];
		int k = A[0];
		A.erase(A.begin());
		A.push_back(k);
	}
	cout<<ans/5<<endl;
	
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3188
final_rank: 11

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

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int n = ri(), y = ri();
	int a[n];
	int64_t min = 0;
	for (auto &i : a) i = ri(), min += i;
	min /= 5;
	min -= y;
	
	int64_t res = 0;
	for (int v = 0; v < n; v++) {
		int64_t dp[6][n + 1];
		for (int j = 0; j < 6; j++) for (int i = 0; i <= n; i++) dp[j][i] = 0;
		dp[0][0] = 1;
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j <= n; j++) {
				if (dp[i][j] == 0) continue;
				int64_t sum = 0;
				for (int k = j + 1; k <= n; k++) {
					sum += a[(v + k - 1) % n];
					if (sum < min) continue;
					dp[i + 1][k] += dp[i][j];
				}
			}
		}
		res += dp[5][n];
	}
	std::cout << res / 5 << std::endl;
	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3232
final_rank: 12

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set> 
#include<cmath>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) lower_bound(h.begin(),h.end(),val)-h.begin()
#define upper(h,val) upper_bound(h.begin(),h.end(),val)-h.begin()
int max_kai = 150000;
vel kai(max_kai, 1);
vel inv_kai;
int rui(int a, int n, int mod) {
    if (n == 0) { return 1 % mod; }
    int x = rui(a, n / 2, mod);
    x *= x; x %= mod;
    if (n % 2 == 1) { x *= a; x %= mod; }
    return x;
}
vel pa;
int root(int x) {
    if (pa[x] == -1) { return x; }
    int ans = root(pa[x]); pa[x] = ans;
    return ans;
}
void marge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) { pa[x] = y; }
}
int gcd(int x, int y) {
    if (x < y) { return gcd(y, x); }
    if (y == 0) { return x; }
    return gcd(y, x % y);
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
vel uni(vel x) {
    if (x.size() == 0) { return x; }
    sor(x);
    int n = x.size();
    vel ans(1, x[0]);
    for (int j = 1; j < n; j++) {
        if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
    }
    x = ans;
    return x;
}
void pr(vel& v) {
    int n = v.size();
    if (n != 0) {
        cout << v[0];
        rep(i, n - 1) {
            cout << " " << v[i + 1];
        }
        cout << endl;
    }
}
int sol(int x, int k) {
    if (x == 0 || k == 0) { return 0; }
    return x + sol(x / 2, k - 1);
}
vel dis(vvel& way, int n, int st) {
    vel dist(n, n + 1);
    dist[st] = 0;
    queue<int> q;
    q.push(st);
    while (!q.empty()) {
        int x = q.front(); q.pop();
        for (auto y : way[x]) {
            if (dist[y] > dist[x] + 1) {
                dist[y] = dist[x] + 1;
                q.push(y);
            }
        }
    }
    return dist;
}
int solve(vel& v, int s) {
    int n = v.size();
    vvel dp(6,vel(n,0));
    dp[0][0] = 1;
    rep(i, 5) {
        rep(j, n) {
            int st = lower_bound(v.begin(), v.end(), v[j] + s) - v.begin();
            if (st == j) { st++; }
            for (int k = st; k < n; k++) {
                dp[i + 1][k] += dp[i][j];
            }
        }
    }
    return dp[5][n - 1];
}
signed main() {
    int n; cin >> n;
    int y; cin >> y;
    int sum = 0;
    vel a(n);
    rep(i, n) { 
        cin >> a[i]; sum += a[i]; 
    }
    sum /= 5; sum -= y;
    if (sum < 0) { sum = 0; }
    int ans = 0;
    rep(i, n) {
        vel b(n);
        rep(j, n) { b[j] = a[(i + j) % n]; }
        vel s(n + 1, 0);
        rep(i, n) {
            s[i + 1] = s[i] + b[i];
        }
        ans += solve(s, sum);
    }
    cout << ans / 5 << endl;
    return 0;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4208
final_rank: 13

code_snippet: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

ll dp[310][8];
int main() {
    ll N, Y; cin >> N >> Y;
    ll target = 0;
    vector<ll> A(2*N), S(2*N + 1);
    for(int i=0; i<N; i++) {
        cin >> A[i];
        A[N+i] = A[i];
        S[i+1] = S[N+i+1] = A[i];
        target += A[i];
    }
    for(int i=1; i<=2*N; i++) S[i] += S[i-1];
    target /= 5;
    
    ll ans = 0;
    for(int ofs=0; ofs<N; ofs++) {
        fill(dp[0], dp[N+1], 0);
        dp[0][0] = 1;
        for(int i=0; i<N; i++) {
            for(int j=0; j<5; j++) {
                for(int k=i+1; k<=N; k++) {
                    ll sum = S[ofs+k] - S[ofs+i];
                    if(sum + Y >= target) {
                        dp[k][j+1] += dp[i][j];
                    }
                }
            }
        }
        ans += dp[N][5];
    }
    cout << ans / 5 << endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3128
final_rank: 7

code_snippet: #include <bits/stdc++.h>
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;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
ll N, Y;
ll sum[305];
ll A[305];
ll dp[305][305][5];

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> Y;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        sum[i+1] = sum[i] + A[i];
    }
    for(int i = 0; i <= N; i++) {
        dp[i][i][0] = 1;
    }
    for(int num = 1; num <= 4; num++) {
        for(int i = 0; i <= N; i++) {
            for(int j = i; j <= N; j++) {
                for(int k = i + 1; k <= j; k++) {
                    if(sum[k] - sum[i] + Y < sum[N] / 5) continue;
                    //cerr << i << " " << k << " " << j << " " << sum[k] << " " << sum[i] << endl;
                    dp[i][j][num] += dp[k][j][num-1];
                }
                //cerr << i << " " << j << " " << num << " " << dp[i][j][num] << endl;
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j <= N; j++) {
            if(i > j) continue;
            if(j == N && i != 0) continue;
            ll now = 0;
            for(int k = 0; k < N; k++) {
                if(k < i or k >= j) now += A[k];
            }
            if(now == 0) continue;
            //cerr << i << " " << j << " " << now << endl;
            if(now + Y >= sum[N] / 5) ans += dp[i][j][4];
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5968
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
const int MOD=1000000007;

ll sum[320];
ll dp[320][5][320];

int main(){
	int n,y;cin>>n>>y;
	deque<int>a;
	ll S=0;
	rep(i,n){
		int b;scanf("%d",&b);
		a.push_back(b);
		S+=b;
	}
	ll ans=0;
	rep(i,n){
		memset(dp,0,sizeof(dp));
		memset(sum,0,sizeof(sum));
		rep(j,a.size()){
			sum[j+1]=sum[j]+a[j];
		}
		dp[0][0][0]=1;
		rep(j,a.size())rep(k,5)rep(t,j+1){
			if(dp[j][k][t]==0)continue;
			ll s=sum[j+1]-sum[t];
			if((s+y)*5>=S&&k<4){
				dp[j+1][k+1][j+1]+=dp[j][k][t];
			}
			dp[j+1][k][t]+=dp[j][k][t];
		}
		ll T=0;
		rep(t,a.size()){
			ll s=sum[a.size()]-sum[t];
			if((s+y)*5>=S)(T+=dp[a.size()][4][t]);
		}
		//~ cerr<<T<<endl;
		ans+=T;
		int d=a.front();a.pop_front();
		a.push_back(d);
	}
	cout<<ans/5<<endl;
}
/*
6 100
1 1 1 1 1 1
*/
accuracy: 1
time_ms: 120
memory_kb: 7252
final_rank: 17

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

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#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 pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-6;
const ld pi = acos(-1.0);
typedef vector<vector<ll>> mat;
typedef vector<ll> vec;

ll dp[301][5];
void solve() {
	int n; cin >> n;
	ll y; cin >> y;
	ll s = 0;
	vector<ll> a(n);
	rep(i, n) {
		cin >> a[i]; s += a[i];
	}
	vector<ll> ra(n + 1);
	rep(i, n) {
		ra[i + 1] = ra[i] + a[i];
	}
	ll ans = 0;
	rep(i, n) {
		if (i == 0)continue;
		rep(j, n + 1)rep(k, 5)dp[j][k] = 0;
		dp[i][0] = 1;
		for (int j = i + 1; j <= n; j++) {
			for (int l = i; l < j; l++) {
				ll sum = ra[j] - ra[l];
				if (sum + y < s / 5)continue;
				for (int k = 1; k <= 4; k++) {
					dp[j][k] += dp[l][k - 1];
				}
			}
		}
		for (int j = i + 4; j <= n; j++) {
			ll sum = ra[i] + (s - ra[j]);
			if (sum + y < s / 5)continue;
			ans += dp[j][4];
			
		}
	}
	cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	//init();
	solve();
	stop
		return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3236
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define whlie while
#define pb push_back
#define eb emplace
#define fi first
#define se second
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repr(i,N) for(int i = (N) - 1; i >= 0; i--)
#define rep1(i,N) for(int i = 1; i <= (N) ; i++)
#define repr1(i,N) for(int i = (N) ; i > 0 ; i--)
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
using namespace std; void solve();
using ll = long long; using vl = vector<ll>;
using vi = vector<int>; using vvi = vector< vector<int> >;
constexpr int inf = 1001001001;
constexpr ll infLL = (1LL << 61) - 1;
struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(10);} } iosetupnya;
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; }
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#ifdef NyaanDebug
  #include "NyaanDebug.h"
  #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
  #define trca(v,N) do { cerr << #v << " = "; array_out(v , N);cout << endl;} while(0)
#else
  #define trc(...)
  #define trca(...)
  int main(){solve();}
#endif
using P = pair<int,int>; using vp = vector<P>;
constexpr int MOD = /** 1000000007; //*/ 998244353;
////////////////

ll dp[8][512];
int ok[512][512];
void solve(){
  ini(N); inl(Y);
  vl a(N); in(a);
  ll S = 0; 
  each(x , a) S += x; 
  S /= 5;
  
  rep(i , N) rep(j , N){
    if(i == j) continue;
    ll cur = 0;
    for(int x = i ; x != j ; x = (x + 1) % N) cur += a[x];
    if(cur + Y >= S) ok[i][j] = 1;
  }

  ll ans = 0;
  // 始点決め
  for(int start = 0 ; start < N ; start++){
    
    // dp初期化
    rep(i , N * 2 + 1) rep(j , 6) dp[j][i] = 0;
    auto idx = [&](int n){return (n + N - start) % N ;};
    auto inv = [&](int n){return (n + start) % N ; };
    dp[0][0] = 1;
    rep(i , 5) rep(j , N){
      if(dp[i][j] == 0) continue;
      rep(k , N){
        int idxk = idx(k); if(idxk == 0) idxk = N;
        if(idxk <= j) continue;
        if(ok[inv(j)][k] == 1)
          dp[i + 1][ idxk ] += dp[i][j];
      }
    }
    //trc(start);
    //rep(i , 6) trca(dp[i] , N + 1);
    ans += dp[5][N];
  }
  out(ans / 5);


}
accuracy: 1
time_ms: 470
memory_kb: 3848
final_rank: 16