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

3 385 2 5

Output

10

Input

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

好久没有更新博客了。

曾经被炒上天的ACM,如今却有些人走茶凉的味道。

正确的事是要坚持的。

原文地址:https://www.cnblogs.com/acboyty/p/9902419.html

时间: 2024-11-13 07:35:00

CodeForces - 1073D Berland Fair的相关文章

codeforces 897A Scarborough Fair 暴力签到

codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: #include <iostream> #include <stdio.h> #include <string.h> using namespace std; typedef long long ll; int n,m; string s; int main() { ios

CodeForces 567B Berland National Library

Description Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room. Today was the pilot launch of an automated

CodeForces 567B Berland National Library(模拟)(简单)

B. Berland National Library time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Berland National Library has recently been built in the capital of Berland. In addition, in the library you can t

CodeForces 1084D The Fair Nut and the Best Path

The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我们需要明白如果可以从u -> v  那么一定可以从 v -> u, 当然 指的是 u->v是路径上的最大和. u->e1->v; 假如:val[u] = 100, val[e1] = 50, val[v] = 60, 那么我们发现可以从 u -> v 也可以从v ->

CodeForces 1084A The Fair Nut and Elevator 题解

A. The Fair Nut and Elevator time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : standard output The Fair Nut lives in ?? story house. ???? people live on the ??-th floor of the house. Every person use

CodeForces 567B Berland National Library hdu

这类题一个操作增加多少,一个操作减少多少,求最少刚开始为多少,在中途不会出现负值,模拟一遍,用一个数记下最大的即可 1 #include<cstdio> 2 #include<cstring> 3 4 const int HASH=57; 5 6 int n,num[110],head[HASH],next[110]; 7 8 void insert(int s) 9 { 10 int h=num[s]%HASH; 11 int u=head[h]; 12 while(u) u=n

[Codeforces 440D]Berland Federalization

题目大意:给你一棵n个节点的树,现在要你删除尽可能少的边,使得剩余一个节点数刚好为k的子树.你需要输出节点数和删除的边的编号. 解题思路:树形dp. 设dp[i][j]和v[i][j]表示以i为根的子树中删除j个节点最少删的边数,和其所需删除的边对应的(点,删除的节点个数),用一个pair存储. 那么转移状态的时候类似于背包问题. dp[i][j]=min{dp[s][k]+dp[i][j-k]}(s为i的儿子). 更新答案的同时暴力更新v即可. 最后搜索根,注意如果根不为1,则需要把根与它的父

Codeforces 1005F Berland and the Shortest Paths 【最短路树】【性质】

其实是一道裸题,如果没学过最短路树的话会比较难做,要想很久想到关键性质才能做出来. 最短路树顾名思义,就是从一个图中生成出来一棵树,使得每个顶点到root的距离是单源最短路.如果有这样的树的话,那可见这样的树是符合题意的. 怎么生成这样的树呢?关键在于记录前驱father,一个距离root最短路是6的点必定从一个距离root最短路是5的点到达(这两个点之间一定会有一条边).所以我们对于所有顶点 2-n,每个顶点u我们找dis[u] = dis[v]+1的情况,这样的话v就是u的前驱.若v,u之间

CodeFores Round53(div.2) D.Berland Fair

1 #include <iomanip> 2 #include <iostream> 3 #include <cstdio> 4 #include <cmath> 5 #include <cstring> 6 #include <algorithm> 7 #include <queue> 8 #include <stack> 9 #include <vector> 10 #include <m