CodeForces-1073D Berland Fair 单点更新

在即将到来的XXIBerland年度集市上,Polycarp计划在有限的资金下,遵循特定策略购买尽可能多的糖果。他从第1个摊位开始,如果资金足够就立即购买一颗糖果,然后顺时针移动到下一个摊位,直到无法再购买为止。本文提供了一个算法解决方案,通过建立和更新线段树来计算Polycarp能够购买的糖果总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值