欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CodeForces-1073D Berland Fair 单点更新

程序员文章站 2022-05-09 15:08:44
...

XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of ai

burles per item. Each booth has an unlimited supply of candies.

Polycarp has decided to spend at most T

burles at the fair. However, he has some plan in mind for his path across the booths:

  • at first, he visits booth number 1
  • ;
  • if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
  • then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).

Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.

Calculate the number of candies Polycarp will buy.

Input

The first line contains two integers n

and T (1≤n≤2⋅105, 1≤T≤1018

) — the number of booths at the fair and the initial amount of burles Polycarp has.

The second line contains n

integers a1,a2,…,an (1≤ai≤109) — the price of the single candy at booth number i

.

Output

Print a single integer — the total number of candies Polycarp will buy.

Examples

Input

3 38
5 2 5

Output

10

Input

5 21
2 4 100 2 6

Output

6

题解:整体拿,不断去掉第一个不能拿的即可

#include<iostream>
#include<cstdio>
using namespace std;
const int N=2e5+10;
typedef long long ll;
struct node{
	int l,r;
	ll val;
}tree[N<<2];
ll a[N],T;
int n;
void pushup(int cur)
{
	tree[cur].val=tree[cur<<1].val+tree[cur<<1|1].val;
}
void build(int l,int r,int cur)
{
	tree[cur].l=l;
	tree[cur].r=r;
	if(l==r)
	{
		tree[cur].val=a[l];
		return;
	}
	int mid=(r+l)>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur);
}
void update(int cur,ll val)
{
	if(tree[cur].l==tree[cur].r)
	{
		tree[cur].val=0;
		return;
	}
	if(tree[cur<<1].val>val) update(cur<<1,val);
	else update(cur<<1|1,val-tree[cur<<1].val);
	pushup(cur);
}
int main()
{
	cin>>n>>T;
	for(int i=1;i<=n;i++)cin>>a[i];
	build(1,n,1);
	ll ans=0;
	for(int i=0;i<n;i++)
	{
		if(T>=tree[1].val)
		{
			ans+=T/tree[1].val*(n-i);
			T=T%tree[1].val;
		}
		update(1,T);
	}
	cout<<ans<<endl;
	return 0;
}