D. Berland Fair
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nn booths, arranged in a circle. The booths are numbered 11 through nn clockwise with nn being adjacent to 11 . The ii -th booths sells some candies for the price of aiai burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most TT burles at the fair. However, he has some plan in mind for his path across the booths:
- at first, he visits booth number 11 ;
- 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 nn and TT (1≤n≤2⋅1051≤n≤2⋅105 , 1≤T≤10181≤T≤1018 ) — the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109 ) — the price of the single candy at booth number ii .
Output
Print a single integer — the total number of candies Polycarp will buy.
Examples
Input
Copy
3 38 5 2 5
Output
Copy
10
Input
Copy
5 21 2 4 100 2 6
Output
Copy
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
- Booth 11 , buys candy for 55 , T=33T=33 ;
- Booth 22 , buys candy for 22 , T=31T=31 ;
- Booth 33 , buys candy for 55 , T=26T=26 ;
- Booth 11 , buys candy for 55 , T=21T=21 ;
- Booth 22 , buys candy for 22 , T=19T=19 ;
- Booth 33 , buys candy for 55 , T=14T=14 ;
- Booth 11 , buys candy for 55 , T=9T=9 ;
- Booth 22 , buys candy for 22 , T=7T=7 ;
- Booth 33 , buys candy for 55 , T=2T=2 ;
- Booth 11 , buys no candy, not enough money;
- Booth 22 , buys candy for 22 , T=0T=0 .
题目大意:糖果商店买糖果(顺时针环形)给n,T分别表示 价值个数,金钱总额,买得起的店必须一定会买,买不起就到下一个,直到所有的都买不起,问最多可以买多少糖果。
这题可以用(BIT)树状数组,也可以通过Set 做。
这里熟悉一下set的用法。
注意这里 记录 前点、后点的方法,pre,nex。这样的好处就是每次O(1)时间选取下一个元素进行判断。
code:
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i = j; i < k; ++i)
using namespace std;
typedef long long LL;
const int N = 2e5+5;
int n;
LL T,ans,num,sum;
int pre[N],nex[N],a[N];
LL mn;
struct node{
int id,v;
bool operator<(const node o)const{
return v < o.v;
}
};
set<node> S;
/*
Set::erase
(1)
iterator erase (const_iterator position);
(2)
size_type erase (const value_type& val);
(3)
iterator erase (const_iterator first, const_iterator last);
Set::find
const_iterator find (const value_type& val) const;
iterator find (const value_type& val);
Set
C++ 11 结构体赋值方式 (node){v1,v2}
set 用迭代器取元素值 (*S.begin()).v
*/
int main()
{
scanf("%d%I64d",&n,&T);
rep(i,1,n+1){
scanf("%d",&a[i]);
nex[i] = i%n + 1;
S.insert((node){i,a[i]});
sum += a[i];
}
rep(i,1,n+1) pre[nex[i]] = i;
int cur = 1;
//printf("%d\n",(*S.begin()).v);
mn = (*S.begin()).v; // 最小值
ans = 0;
num = n;
while(T >= mn){
if(T >= sum){
ans += T/sum*num,T %= sum;
}
else if(T >= a[cur]){
T -= a[cur],++ans,cur = nex[cur];
}
else{ //删除 cur 对应的元素 并调整cur 前、后元素的 后、前元素位置
--num;
sum -= a[cur];
pre[nex[cur]] = pre[cur];
nex[pre[cur]] = nex[cur];
S.erase((node){cur,a[cur]});
cur = nex[cur];
}
}
printf("%I64d\n",ans);
return 0;
}