Codeforces 839A Arya and Bran

Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.

At first, Arya and Bran have 0 Candies. There are n days, at the i-th day, Arya finds ai candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candies. If she don't give him the candies at the same day, they are saved for her and she can give them to him later.

Your task is to find the minimum number of days Arya needs to give Bran k candies before the end of the n-th day. Formally, you need to output the minimum day index to the end of which k candies will be given out (the days are indexed from 1 to n).

Print -1 if she can't give him k candies during n given days.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10000).

The second line contains n integers a1, a2, a3, ..., an (1 ≤ ai ≤ 100).

Output

If it is impossible for Arya to give Bran k candies within n days, print -1.

Otherwise print a single integer — the minimum number of days Arya needs to give Bran k candies before the end of the n-th day.

Examples
input
2 3
1 2
output
2
input
3 17
10 10 10
output
3
input
1 9
10
output
-1
Note

In the first sample, Arya can give Bran 3 candies in 2 days.

In the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day.

In the third sample, Arya can't give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1 day.


  题目大意 有n天,在第i天Arya能够神奇地得到ai颗糖,(为了防止Bran吃糖吃多了蛀牙,所以)每天Arya最多能给Bran 8颗糖,问在最早在哪一天,Bran总共得到k颗糖。

  每天能给多少就给多少。

Code

 1 /**
 2  * Codeforces
 3  * Problem#839D
 4  * Accepted
 5  * Time: 171ms
 6  * Memory: 15400k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 
11 const int lim = 1e6 + 1;
12 const int moder = 1e9 + 7;
13 
14 int n;
15 int *a;
16 int *pow2;
17 int cnt[lim], counter[lim];
18 int f[lim];
19 int res = 0;
20 
21 inline void init() {
22     scanf("%d", &n);
23     a = new int[(n + 1)];
24     pow2 = new int[(n + 1)];
25     pow2[0] = 1;
26     for(int i = 1; i <= n; i++) {
27         scanf("%d", a + i);
28         counter[a[i]]++;
29         pow2[i] = (pow2[i - 1] << 1) % moder;
30     }
31 }
32 
33 inline void solve() {
34     for(int i = 1; i < lim; i++)
35         for(int j = i; j < lim; j += i)
36             cnt[i] += counter[j];
37     
38     for(int i = lim - 1; i > 1; i--) {
39         if(!cnt[i])    continue; 
40         f[i] = (cnt[i] * 1LL * pow2[cnt[i] - 1]) % moder;
41         for(int j = i << 1; j < lim; j += i)
42             f[i] = (f[i] - f[j]) % moder;
43         if(f[i] < 0)    f[i] += moder;
44         res = (res + (f[i] * 1LL * i) % moder) % moder;
45     }
46     
47     printf("%d\n", res);
48 }
49 
50 int main() {
51     init();
52     solve();
53     return 0;
54 }
posted @ 2017-08-15 14:29  阿波罗2003  阅读(219)  评论(0编辑  收藏  举报