1. 程式人生 > >CodeForces - 1073D Berland Fair

CodeForces - 1073D Berland Fair

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

n">nn and TT (1n21051≤n≤2⋅105, 1T10181≤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 (1ai1091≤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
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
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.

 

題目大意:

n種糖果圍成一圈,每種糖果每個ai元。初始時你有t元,接著你從1開始瘋狂地繞圈。一旦你發現有糖果能買,你就買一個。直到一個糖果都買不起。問最後你買了多少個糖果。

 

稍微帶點技巧的模擬。

若一個週期的和小於剩餘的t,就直接買幾個週期,不必一個個模擬。

然後遇到買不起的則將他從週期中刪除。

注意用long long

 

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<iostream>
#define lol long long
#define maxn 200000

using namespace std;

lol a[maxn+5];

int main()
{
    lol n,t;
    scanf("%lld%lld",&n,&t);
    lol sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",a+i);
        sum+=a[i];
    }

    lol ans=0;
    lol cur=n;
    lol num=n;
    while(1)
    {
        ans+=(t/sum)*num;
        t=t%sum;
        //printf("%lld\n",t);
        for(int i=1;;i++)
        {
            //printf("%lld %lld\n",a[(cur+i-1)%n+1],t);
            if(a[(cur+i-1)%n+1]==-1)
                continue;
            if(a[(cur+i-1)%n+1]>t)
            {
                sum-=a[(cur+i-1)%n+1];
                num--;
                a[(cur+i-1)%n+1]=-1;
                cur=(cur+i-1)%n+1;
                break;
            }
            t-=a[(cur+i-1)%n+1];
            ans++;
        }

        if(num==0)
            break;
    }
    printf("%lld\n",ans);

    return 0;
}
View Code

 

好久沒有更新部落格了。

曾經被炒上天的ACM,如今卻有些人走茶涼的味道。

正確的事是要堅持的。