D. Berland Fair

5 篇文章 0 订阅
4 篇文章 0 订阅

题目链接:http://codeforces.com/contest/1073/problem/D

XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nn booths, arranged in a circle. The booths are numbered 11through 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:

  1. Booth 11, buys candy for 55, T=33T=33;
  2. Booth 22, buys candy for 22, T=31T=31;
  3. Booth 33, buys candy for 55, T=26T=26;
  4. Booth 11, buys candy for 55, T=21T=21;
  5. Booth 22, buys candy for 22, T=19T=19;
  6. Booth 33, buys candy for 55, T=14T=14;
  7. Booth 11, buys candy for 55, T=9T=9;
  8. Booth 22, buys candy for 22, T=7T=7;
  9. Booth 33, buys candy for 55, T=2T=2;
  10. Booth 11, buys no candy, not enough money;
  11. Booth 22, buys candy for 22, T=0T=0.

No candy can be bought later. The total number of candies bought is 1010.

In the second example he has 11 burle left at the end of his path, no candy can be bought with this amount.

题意:这道题的意思是有T元然后可以去n家商店买东西,但如果钱少于价格则跳过到下一家商店,问最多可以买多少个。

思路:刚开始想用队列,但极端的情况下会达到o(n*n),所以不行,这个题关键是价格高的商店可以不考虑,那么意味着可以删除

o(1)删除的话就是链表。所以这个题用o(1)的链表即可,然后加个前缀和的处理。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define  LL long long

using namespace std;
const int maxn=2e5+100;

struct node
{
    LL v;
    int pre,Next;
};
node a[maxn];

int main()
{
    LL n;
    LL T;
    scanf("%lld%lld",&n,&T);
    LL sum=0;
    LL ans=0;

    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i].v);
        sum+=a[i].v;
        a[i].pre=i-1;
        a[i].Next=i+1;
    }
    a[1].pre=n;
    a[n].Next=1;
    ans+=T/sum*n;
    T%=sum;
    int i=1;
    while(a[i].Next!=i)
    {
        if(T>=a[i].v)
        {
            T-=a[i].v;
            ans++;
            if(T<=0)
            {
                break;
            }
        }
        else
        {
            a[a[i].pre].Next=a[i].Next;
            a[a[i].Next].pre=a[i].pre;
            sum-=a[i].v;
            n--;
            ans+=T/sum*n;
            T%=sum;
            if(T<=0)
            {
                break;
            }
        }
        i=a[i].Next;
    }
    printf("%lld\n",ans);
    return 0;
}

总结:复杂度算得不够好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值