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

之前用线段树做的。超时了很难受。因为没有有效的处理无效的点。之后借鉴了网上的答案。改成链表去做。链表有些不熟悉了。比如删除结点那里都搞错了。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
//#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 2e5+; ll a[maxn];
ll t;
int n;
int nex[maxn];
int pre[maxn]; int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif scanf("%d%lld", &n , &t);
int num = ;
for (int i = ; i < n; ++i) {
ll tmp;
scanf("%lld", &tmp);
if (tmp > t) continue;
a[num++] = tmp;
}
n = num-;
if (n == ) {
printf("0\n");
return ;
}
for (int i = ; i < n; ++i) {
nex[i] = i+;
}
nex[n] = -;
for (int i = ; i <= n; ++i)
pre[i] = i-;
ll cnt = ;
while (nex[] != -) {
ll sum = ;
int num = ;
ll rem = t;
for (int i = nex[]; i != -; i = nex[i]) {
if (a[i] <= rem) {
sum += a[i];
num++;
rem -= a[i];
} else {
nex[pre[i]] = nex[i];
pre[nex[i]] = pre[i];
}
}
if (sum == ) break;
cnt += t/sum*num;
t %= sum;
}
printf("%lld\n", cnt);
#ifdef local
fclose(stdin);
#endif
return ;
}

最新文章

  1. android四大组件(简单总结)
  2. select制作分层级目录,让select显示和可下拉选择的&quot;不一样&quot;
  3. Spring中的ApplicationContext事件机制
  4. Memcached使用小记
  5. LUA 模块化编程例子
  6. Nim语言:Pascal的语法,Python的缩进
  7. js数字验证
  8. Effective C++ Item 46 当需要投你非成员函数定义模板
  9. 201521123107 《Java程序设计》第8周学习总结
  10. hdu-5786(补图最短路)
  11. 寻找遗失的tags
  12. rem和em学习笔记及CSS预处理(未完待续)
  13. java结合testng,利用mysql数据库做数据源的数据驱动实例
  14. Pycharm常用操作方法
  15. SPOJ 694 Distinct Substrings(不相同子串个数)
  16. 在linux机器上面安装anaconda和相关软件
  17. python-appium520-3引入unittest,编写自动化用例
  18. 最简单的VS-Qt-CMake项目框架
  19. Spring boot——logback.xml 配置详解(三)&lt;appender&gt;
  20. 冲刺ing-6

热门文章

  1. 记一次 SSM 分页
  2. 使用mybatis-generator工具自动生成mybatis代码
  3. SpringMVC详细学习笔记
  4. jmeter的学习路线
  5. Linux服务器管理神器-IPython
  6. 自学python之路(day4)
  7. Fasttext原理
  8. webSocket的 原理 及 实现
  9. sessionFactory中的openSession和getCurrentSession的一些注意事项
  10. Redux的工作流程