Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y ≠ x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).

The first line of the input contains four space-separated integers n, a, b, k (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n, a ≠ b).

Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).

NoteTwo sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≤ j ≤ k), that pj ≠ qj.Notes to the samples:  In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.  In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.  In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.

#include <stdio.h>
int N,a,b,K;
//      k     n
int dp[5005][5005];
long long int p[5005];
#define max(a,b) ((a) > (b)?(a):(b))
#define min(a,b) ((a) < (b)?(a):(b))
#define mod(a) ((a) % 1000000007)
int main(void)
{
	int count,k,sum,m,n;
	scanf("%d%d%d%d",&N,&a,&b,&K);

	for(count = 0;count <= N;count++)
		dp[0][count] = 1;
	dp[0][b] = 0;
	for(k = 1;k <= K;k++){
		for(p[0] = 0,count = 1;count <= N;count++)
			p[count] = p[count - 1] + dp[k - 1][count];
		if(a < b){
			for(count = 1;count < b - 1;count++)
				dp[k][count] = mod(mod(p[b - 1] - p[count]) + mod(p[count - 1] - p[max(count - (b - count),0)]));
		}else{
			for(count = b + 2;count <= N;count++)
				dp[k][count] = mod(mod(p[count - 1] - p[b]) + mod(p[min(count - b - 1 + count,N)] - p[count]));
		}
	}
	printf("%d",dp[K][a]);
	return 0;
}

#include <stdio.h>
int N,a,b,K;
//      k     n
int dp[2][5005];
long long int p[5005];
#define max(a,b) ((a) > (b)?(a):(b))
#define min(a,b) ((a) < (b)?(a):(b))
#define mod(a) ((a) % 1000000007)
int main(void)
{
	int count,k,sum,m,n;
	scanf("%d%d%d%d",&N,&a,&b,&K);

	for(count = 0;count <= N;count++)
		dp[0][count] = 1;
	dp[0][b] = 0;
	for(k = 1;k <= K;k++){
		dp[k % 2][b  - 1] = 0;
		dp[k % 2][b  + 1] = 0;
		for(p[0] = 0,count = 1;count <= N;count++)
			p[count] = p[count - 1] + dp[(k - 1)%2][count];
		if(a < b){
			for(count = 1;count < b - 1;count++)
				dp[k % 2][count] = mod(mod(p[b - 1] - p[count]) + mod(p[count - 1] - p[max(count - (b - count),0)]));
		}else{
			for(count = b + 2;count <= N;count++)
				dp[k % 2][count] = mod(mod(p[count - 1] - p[b]) + mod(p[min(count - b - 1 + count,N)] - p[count]));
		}
	}
	printf("%d",dp[K % 2][a]);
	return 0;
}

#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <stdlib.h>

#define MAX_N (5000)
#define MAX_K (5000)
#define MOD   (1000000007)

int dp[MAX_K+1][MAX_N+1];

int max(int x, int y) { return x > y ? x : y; }

int min(int x, int y) { return x < y ? x : y; }

int range(int r, int i, int j) {
  int ret = dp[r][j] - dp[r][i-1];

  while ( ret < 0 )
    ret += MOD;
  return ret;
}

int build_dp(int n, int a, int b, int k) {
  int i,j,prev;

  for ( j=1; j <= n; ++j )
    if ( j != b )
      dp[0][j] = dp[0][j-1] + 1;
  for ( i=1; i <= k; ++i ) {
    for ( j=1,prev=0; j <= n; ++j ) {
      if ( j != b )
        dp[i][j] = ((range(i-1, max(1, j-abs(j-b)+1), j-1) +
                   range(i-1, j+1, min(n, j+abs(j-b)-1))) % MOD + prev) % MOD;
        prev = dp[i][j];
    }
  }
  return range(k, a, a);
}

int main() {
  int n,a,b,k;

  scanf("%d %d %d %d", &n, &a, &b, &k);
  printf("%d\n", build_dp(n, a, b, k));
  return EXIT_SUCCESS;
}

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX_N (5000)
#define MAX_K (5000)
#define MOD   (1000000007)

int max(int x, int y) { return x > y ? x : y; }

int min(int x, int y) { return x < y ? x : y; }

int dp[MAX_K+1][MAX_N+2];

int build_dp(int n, int a, int b, int k) {
  int fq[MAX_N+1];
  int i,j,acc;

  dp[0][a] = 1, dp[0][a+1] = -1;
  for ( i=0; i < k; ++i ) {
    for ( j=1; j <= n; ++j )
      fq[j] = (fq[j-1] + dp[i][j]) % MOD;
    for ( j=1; j <= n; ++j ) {
      dp[i+1][max(1, j-abs(j-b)+1)] += fq[j];
      dp[i+1][max(1, j-abs(j-b)+1)] %= MOD;
      dp[i+1][j] -= fq[j];
      while ( dp[i+1][j]  < 0 )
        dp[i+1][j] += MOD;
      dp[i+1][j+1] += fq[j];
      dp[i+1][j+1] %= MOD;
      dp[i+1][min(n+1, j+abs(j-b))] -= fq[j];
      while ( dp[i+1][min(n+1, j+abs(j-b))] < 0 )
        dp[i+1][min(n+1, j+abs(j-b))] += MOD;
    }
  }
  for ( j=1,acc=0; j <= n; ++j ) {
    fq[j] = (fq[j-1] + dp[i][j]) % MOD;
    acc = (acc + fq[j]) % MOD;
  }
  return acc;
}

int main() {
  int n,a,b,k;

  scanf("%d %d %d %d", &n, &a, &b, &k);
  printf("%d\n", build_dp(n, a, b, k));
  return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int mem [5010][5010];
int N, b, a, K;
int MOD = 1000000007;

int min(int a, int b)	{
	return	a < b ? a : b;
}
int max(int a, int b)	{
	return	a > b ? a : b;
}

int solve(int id, int restan)	{
	if	( restan == 0 )
		return	1;

	if	( mem[id][restan] != -1 )
		return	mem[id][restan];

	int dist = abs(b - id);
	int ans = 0;
	int y;
	for (y = max(1, id - dist + 1); y <= min(N, id + dist - 1); y++)	{
		if	( y != b && y != id )	{
			ans += solve(y, restan - 1);
			if	( ans >= MOD )
				ans -= MOD;
		}
	}
	return	mem[id][restan] = ans;
}

int sum(int a, int b, int k)	{
	int ans = mem[b][k];
	ans -= mem[a - 1][k];
	if	( ans < 0 )
		ans += MOD;
	return	ans;
}

int main(int argc, char *argv[])	{
	scanf("%d %d %d %d", &N, &a, &b, &K);

	int i, j, id, k, dist, x, y, ans;

	for (i = 0; i <= N; i++)	{
		for (j = 0; j <= K; j++)	{
			mem[i][j] = -1;
		}
	}

	for (id = 1; id <= N; id++)
		mem[id][0] = 1;
	mem[b][0] = 0;
	for (id = 1; id <= N; id++)	{
		mem[id][0] += mem[id - 1][0];
		if	( mem[id][0] >= MOD )
			mem[id][0] -= MOD;
	}

	for (k = 1; k <= K; k++)	{
		for (id = 1; id <= N; id++)	{
			dist = abs(id - b);
			x = max( 1, id - dist + 1 );
			y = min( N, id + dist - 1 );

			ans = sum(x, y, k - 1);

			ans -= sum(b, b, k - 1);
			if	( ans < 0 )
				ans += MOD;

			ans -= sum(id, id, k - 1);

			if	( ans < 0 )
				ans += MOD;

			mem[id][k] = ans;
		}

		for (id = 1; id <= N; id++)	{
			mem[id][k] += mem[id - 1][k];
			if	( mem[id][k] >= MOD )
				mem[id][k] -= MOD;
		}
	}
	/*
	for (i = 1; i <= N; i++)	{
		for (j = 0; j <= K; j++)	{
			printf( "mem(%d, %d) = %d\n", i, j, sum(i, i, j) );
		}
		printf("\n");
	}
	*/
	printf( "%d\n", sum(a, a, K) );

    return 0;
}

/* practice with Dukkha */
#include <stdio.h>
#include <string.h>

#define N	5000
#define MD	1000000007

int main() {
	static int dp[N], dq[N];
	int n, a, b, k, i, ans;

	scanf("%d%d%d%d", &n, &a, &b, &k), a--, b--;
	if (a < b) {
		n = b + 1;
		a = b - a;
	} else {
		n -= b;
		a -= b;
	}
	dp[a] = 1;
	while (k--) {
		memset(dq, 0, n * sizeof *dq);
		for (i = 2; i < n; i++) {
			int x = dp[i];

			if (x) {
				dq[1] = (dq[1] + x) % MD;
				dq[i] = (dq[i] - x) % MD;
				if (i + 1 < n)
					dq[i + 1] = (dq[i + 1] + x) % MD;
				if (i + i < n)
					dq[i + i] = (dq[i + i] - x) % MD;
			}
		}
		for (i = 1; i < n; i++)
			dp[i] = (dq[i] + dp[i - 1]) % MD;
	}
	ans = 0;
	for (i = 1; i < n; i++)
		ans = (ans + dp[i]) % MD;
	if (ans < 0)
		ans += MD;
	printf("%d\n", ans);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int max(int a , int b){
	if(a  >b)
		return a ;
	return b ;
}
int min(int a, int b){
	if(a  < b)
		return a ;
	return b ;
}

long long dpp[5002][5002];
int n , a , b , k ;

#define MOD 1000000007

void build(long long arr[] , long long rmq []){
	int i ;
	for(i = 1 ; i <= n ; i++){
		rmq[i] = arr[i];
		rmq[i] += rmq[i - 1];
		rmq[i] %= MOD ;
	}
	//cout << endl ;
}

long long query(long long rmq [] , int to){
	return rmq[to];
}

int main(){
	scanf("%d%d%d%d" , & n , &a , &b , &k);
	int i ;
	for(i = 1 ; i <= n ; i ++){
		dpp[0][i] = 1 ;
	}
	dpp[0][b] = 0 ;
	for(i =  1 ; i <= k ; i++){
		// cout << i << " : " << endl ;
		long long rmq [10003] ={0};
		build(dpp[i-1] , rmq);
		int j ;
		for(j = 1 ; j <= n; j++){
			if(j == b)
				continue ;
			int diff = abs(b - j) - 1;
			dpp[i][j] = query(rmq , min(n , j + diff));
			dpp[i][j] -= query(rmq , max(1 , j - diff) -1 );
			dpp[i][j] -= dpp[i-1][j];
			dpp[i][j] %= MOD ;
			dpp[i][j] += MOD ;
			dpp[i][j] %= MOD ;
		}
		// cout << i << " :  => " << endl ;
		// for(int j = 1 ; j <= n ; j++){
		// 	cout << dpp[i][j] << " ";
		// }
		//cout << endl ;
	}
	printf("%d" , dpp[k][a]);
	return 0 ;
}

#include <stdio.h>
int N,a,b,K;
//      k     n
int dp[5005][5005];
int p[5005];
#define max(a,b) ((a) > (b)?(a):(b))
#define min(a,b) ((a) < (b)?(a):(b))
#define mod(a) ((a) % 1000000007)
int main(void)
{
	int count,k,sum,m,n;
	scanf("%d%d%d%d",&N,&a,&b,&K);

	for(count = 0;count <= N;count++)
		dp[0][count] = 1;
	dp[0][b] = 0;
	for(k = 1;k <= K;k++){
		for(p[0] = 0,count = 1;count <= N;count++)
			p[count] = mod(p[count - 1] + dp[k - 1][count]);
		if(a < b){
			for(count = 1;count < b - 1;count++)
				dp[k][count] = mod(p[b - 1] - p[count]) + mod(p[count - 1] - p[max(count - (b - count),0)]);
		}else{
			for(count = b + 2;count <= N;count++)
				dp[k][count] = mod(p[count - 1] - p[b]) + mod(p[min(count - b - 1 + count,N)] - p[count]);
		}
	}
	printf("%d",dp[K][a]);
	return 0;
}

#include <stdio.h>
int N,a,b,K;
//      k     n
int dp[5005][5005];
int p[5005];
#define max(a,b) ((a) > (b)?(a):(b))
#define min(a,b) ((a) < (b)?(a):(b))
int main(void)
{
	int count,k,sum,m,n;
	scanf("%d%d%d%d",&N,&a,&b,&K);

	for(count = 0;count <= N;count++)
		dp[0][count] = 1;
	dp[0][b] = 0;
	for(k = 1;k <= K;k++){
		for(p[0] = 0,count = 1;count <= N;count++)
			p[count] = p[count - 1] + dp[k - 1][count];
		if(a < b){
			for(count = 1;count < b - 1;count++)
				dp[k][count] = p[b - 1] - p[count] + p[count - 1] - p[max(count - (b - count),0)];
		}else{
			for(count = b + 2;count <= N;count++)
				dp[k][count] = p[count - 1] - p[b] + p[min(count - b - 1 + count,N)] - p[count];
		}
	}
	printf("%d",dp[K][a]);
	return 0;
}

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX_N (5000)
#define MAX_K (5000)
#define MOD   (1000000007)

int n,b;
int memo[MAX_N+1][MAX_K+1];

int min(int x, int y) { return x < y ? x : y; }

int max(int x, int y) { return x > y ? x : y; }

int f(int x, int t) {
  int i,acc=0;

  if ( !t )
    return 1;
  if ( (x-b)*(x-b) == 1 )
    return 0;
  if ( memo[x][t] )
    return memo[x][t];
  for ( i=max(1, x-abs(x-b)-1); i < min(n, x+abs(x-b)); ++i ) {
    if ( i == x ) continue;
    acc = (acc + f(i, t-1)) % MOD;
  }
  return memo[x][t] = acc;
}

int main() {
  int a,k;

  scanf("%d %d %d %d", &n, &a, &b, &k);
  printf("%d\n", f(a, k));
  return EXIT_SUCCESS;
}

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX_N (5000)
#define MAX_K (5000)
#define MOD   (1000000007)

int max(int x, int y) { return x > y ? x : y; }

int min(int x, int y) { return x < y ? x : y; }

int dp[MAX_K+1][MAX_N+2];

int build_dp(int n, int a, int b, int k) {
  int fq[MAX_N+1];
  int i,j,acc;

  dp[0][a] = 1, dp[0][a+1] = -1;
  for ( i=0; i < k; ++i ) {
    for ( j=1; j <= n; ++j )
      fq[j] = (fq[j-1] + dp[i][j]) % MOD;
    for ( j=1; j <= n; ++j ) {
      dp[i+1][max(1, j-abs(j-b)+1)] += fq[j];
      dp[i+1][max(1, j-abs(j-b)+1)] %= MOD;
      dp[i+1][j] -= fq[j];
      dp[i+1][j] %= MOD;
      dp[i+1][j+1] += fq[j];
      dp[i+1][j+1] %= MOD;
      dp[i+1][min(n+1, j+abs(j-b))] -= fq[j];
      dp[i+1][min(n+1, j+abs(j-b))] %= MOD;
    }
  }
  for ( j=1,acc=0; j <= n; ++j ) {
    fq[j] = (fq[j-1] + dp[i][j]) % MOD;
    acc = (acc + fq[j]) % MOD;
  }
  return acc;
}

int main() {
  int n,a,b,k;

  scanf("%d %d %d %d", &n, &a, &b, &k);
  printf("%d\n", build_dp(n, a, b, k));
  return EXIT_SUCCESS;
}


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.

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

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.

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

#include <stdio.h>

int main() {
    int n,x[100],k;
    scanf("%d %d",&n,&k);
    int i;
    for(i=0;i<n;i++){
        scanf("%d",&x[i]);
    }
    int s=0,count=1;
    for(i=0;i<n;i++){
        if(x[i]<=8) s=s+x[i];
        else{
            s=s+8;
            x[i+1]=x[i+1]+x[i]-8;
        }
        if(s>=k) {
            printf("%d",count);
            return 0;
        }
        count++;
    }
    printf("-1");
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    int sum = 0;
    int sum2 = 0;
    int i;
    for ( i = 0; i < a; i++)
    {
        int k;
        scanf("%d", &k);
        if (k >= 8)
        {
            sum2 += k - 8;
            sum += 8;
        }
        else
        {
            if (sum2 > (8-k))
            {
                sum2 -= 8-k;
                sum += 8;
            }
            else
            {
                sum +=sum2 + k;
                sum2 = 0;
            }
        }
        //printf("sum = %d   sum2 = %d\n",sum,sum2);
        if ( sum >= b)
        {
            sum =0;
            break;
        }
    }
    if (sum != 0 )
    {
        printf("-1");
    }
    else
    {
        printf("%d",++i);
    }
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int a=0,b=0,i,x;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&x);
        a+=x;
        if(a>7)
        {
            b+=8;
            a-=8;
        }
        else
        {
            b+=a;
            a=0;
        }
        if(b>=k)
            {
               printf("%d",i);
               return 0;
            }
    }
    printf("-1");
    return 0;
}

#include <stdio.h>

int main ()
{
    int arr[105], n, k, i, sum = 0, sum1 = 0;
    scanf("%d %d", &n, &k);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for(i = 0; i < n; i++)
    {
        sum1 = sum1 + arr[i];
        if(sum1 > 8)
        {
            sum = sum + 8;
            sum1 = sum1 - 8;
        }
        else
        {
            sum = sum + sum1;
            sum1 = sum1 - sum1;
        }
        if(sum >= k)
        {
            printf("%d\n", (i + 1));
            return 0;
        }
    }

    printf("-1\n");

    return 0;
}

#include<stdio.h>
int main()
{
    int i,n,k,left=0,count=0,flag=0,h=0;
    int a[1010];
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(i=0;;i++){
        if(a[i]<=8){
            h=h+a[i];
            count++;
        }
        else {
            a[i+1]+=(a[i]-8);
            h=h+8;
            count++;
        }
        if(h>=k)break;
    }
    if(count<=n)printf("%d\n",count);
    else printf("-1\n");

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, k, candy, days=0, i, ans=0, temp=0, remain=0;
    scanf("%d%d", &n, &k);
    for(i=0; i<n; i++){
        scanf("%d", &candy);
        candy+=remain;
        if(candy<=8){
            ans+=candy;
            remain = 0;
        }
        else{
            remain = candy-8;
            ans+=8;
        }
        days++;
        if(ans>=k){
            temp=1;
            break;
        }
    }
    if(k==0) days = 0;
    if(temp) printf("%d\n", days);
    else printf("-1\n");
    return 0;
}

#include<stdio.h>

int floor(float x)
{
    int floor;
    if(x>=0) floor = (int)x;
    else {
        floor = (int)x;
        if(x - (float)floor ) floor--;
    }

    return floor;
}

int ceil(float x)
{
        int ceil;
    if(x>=0) {
        ceil = (int)x;
        if(x - (float)ceil) ceil++;

    }
    else ceil = (int)x;

    return ceil;

}

int round(float x)
{
    int round;
    if(x<0) x -= .5;
    else x += .5;
    round = (int)x;

    return round;
}

int main()
{
    int i, j, n, k;
    scanf("%d %d", &n, &k);
    int a[n];
    for(i=0; i<n; ++i) {
        scanf("%d", &a[i]);
    }
    int given = 0, remain = 0;
    i = 0;

    while(given<k && i<n) {

//        remain = 0;
//        for(j=0; j<i; ++j) remain += a[j];
        if(a[i]<8) {
            if(remain+a[i]-8 >= 0) {
                given += 8;
                remain -= 8 - a[i];
//                a[i] = 0;
            }
            else if(remain+a[i]-8 < 0) {
                given += remain+a[i];
                remain = 0;
//                a[i] = 0;
            }
        }
        else if(a[i]>8) {
            given += 8;
            remain += a[i]-8;
        }
        else {
            given += a[i];
//            a[i] = 0;
        }
//        printf("%d %d %d\n", a[i], given, remain);
        i++;

    }
    if(given<k) printf("-1");
    else printf("%d", i);

    return 0;
}

#include<stdio.h>
#include<conio.h>
int main()
{
	int SoNgay, SoKeoCho, Keo, SoKeoDu = 0;
	scanf("%d", &SoNgay);
	scanf("%d", &SoKeoCho);
	int i;
	for ( i = 1; i <= SoNgay; i++)
	{
		scanf("%d", &Keo);
		SoKeoDu = SoKeoDu + Keo;
		if (SoKeoDu > 8)
		{
			SoKeoCho = SoKeoCho - 8;
			SoKeoDu = SoKeoDu - 8;
		}
		else
		{
			SoKeoCho = SoKeoCho - SoKeoDu;
			SoKeoDu = 0;
		}
		if (SoKeoCho <= 0)
			break;
	}
	if (SoKeoCho <= 0)
	{
		printf("%d", i);
	}
	else
	{
		printf("-1");
	}
	return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

int main() {
	int n, k;
	while(scanf("%d %d", &n, &k)==2) {
		int s=0, g=0, i;
		for(i=0; i<n && g<k; i++) {
			int x;
			scanf("%d", &x);
			s += x;
			if (s>=8) g+=8, s-=8;
			else g+=s, s=0;
		}
		if (g<k) printf("%d\n", -1);
		else printf("%d\n", i);
		break;
	}
}

#include <stdio.h>

int main()
{
	int n,k,s,g=0,t=0;
	scanf("%d %d",&n,&k);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&s);
		t+=s;
		if(t<=8) {k-=t;t=0;}
		else {t-=8; k-=8;}
		g++;
		if(k<=0) break;

	}
	if(k>0) printf("%d\n", -1);
	else printf("%d\n", g);
	return 0;
}

//http://codeforces.com/contest/839/problem/A
#include<stdio.h>
#include<math.h>
#include<string.h>
#define False 0
#define True 1
// int small(char ch)
// int capital(char ch);
// int prime(int number);
// void bubble_sort(int arr[], int n);
//void upper(char *ptr);
//void lower(char *ptr);
// void sort(char *ptr, int n);
// void RemoveDuplicates(char * arr, char * new_str, int arr_size);
// int Contains(char * arr, char elem, int arr_size);
// int Times_of_shapes(char * str, int len);

int main(void)
{

    int days, order, result=0, i;
    scanf("%d %d", &days, &order);
    int arr[days];
    for(i=0;i<days;i++)
    {
    	scanf("%d", &arr[i]);
    }
    for(i=0;i<days;i++)
    {
    	if(arr[i]>=8)
    	{
    		order-=8;
    		arr[i+1]+=(arr[i]-8);
    		result++;
    	}
    	else
    	{
    		order-=arr[i];
    		result++;

    	}
    	if(order<=0)
    	{
    		break;
    	}
    }
    if(order<=0)
    {
    	printf("%d\n", result);
    }
    else
    	printf("-1\n");
	return 0;
}

// int Times_of_shapes(char * str, int len)
// {

// 	int j, i, times=0, result=0;
// 	str[len]='\0';
// 	char temp[len][len+1];
// 	for(i=0;i<=len;i++)
// 	{
// 		temp[0][i]=str[i];
// 	}
// 	for(i=1;i<len;i++)
// 	{
// 		temp[i][0]=temp[i-1][len-1];
// 		for(j=1;j<len;j++)
// 		{
// 			temp[i][j]=temp[i-1][j-1];
// 		}
// 		temp[i][len]='\0';
// 	}

// 	for(i=0;i<len;i++)
// 	{
// 		times=0;
// 		for(j=i+1;j<len;j++)
// 		{
// 			if(strcmp(temp[i], temp[j])==False)
// 			{
// 				times++;
// 			}
// 		}
// 		if(times==False)
// 		{
// 			result++;
// 		}
// 	}
// 	return result;

// }

// int Contains(char * arr, char elem, int arr_size)
// {

//     int index = 0;

//     for(index = 0; index < arr_size; index++)
//     {

//         if(arr[index] == elem)
//         {
//             return True;
//         }
//     }

//     return False;

// }

// void RemoveDuplicates(char * arr, char * new_str, int arr_size)
// {

//     int index = 0, new_index = 0;

//     //clear temp string
//     for(index = 0; index < arr_size; index++)
//     {
//         new_str[index] = 0;
//     }

//     for(index = 0; index < arr_size; index++)
//     {

//         if(Contains(new_str, arr[index], arr_size) == False)
//         {
//             new_str[new_index] = arr[index];
//             new_index++;
//         }
//         if(index==arr_size-1)
//         {
//             new_str[new_index]='\0';
//         }
//     }

// }

// void sort(char *ptr, int n)
// {
// 	int i, j, temp;
// 	for(i=0;i<n-1;i++)
// 	{
// 		for(j=0;j<n-i-1;j++)
// 		{
// 			if(ptr[j]>ptr[j+1])
// 				{
// 					temp=ptr[j];
// 					ptr[j]=ptr[j+1];
// 					ptr[j+1]=temp;
// 				}

// 		}
// 	}

// }

/*
void upper(char *ptr)
{
	int i=0;
	while(ptr[i]!='\0')
	{
		if(ptr[i]>='a'&&ptr[i]<='z')
			ptr[i]-=32;
		i++;
	}

}*/

/*
void lower(char *ptr)
{
	int i=0;
	while(ptr[i]!='\0')
	{
		if(ptr[i]>='A'&&ptr[i]<='Z')
			ptr[i]+=32;
		i++;
	}
}*/

// void bubble_sort(int ptr[], int n)
// {
// 	int i, j, temp;
// 	for(i=0;i<n-1;i++)
// 	{
// 		for(j=0;j<n-i-1;j++)
// 		{
// 			if(ptr[j]>ptr[j+1])
// 				{
// 					temp=ptr[j];
// 					ptr[j]=ptr[j+1];
// 					ptr[j+1]=temp;
// 				}

// 		}
// 	}

//  }

// int capital(char ch)
// {
// 	int flag=0;
// 	if(ch>=65&&ch<=90)
// 		flag=1;
// 	return flag;

// }

// int small(char ch)
// {
// 	int flag=0;
// 	if(ch>='a'&&ch<='z')
// 		flag=1;
// 	return flag;

// }
// int prime(int number)
// {
// 	int flag=1, i;
// 	for(i=2;i<=number/2;i++)
// 	{
// 		if(number%i==0)
// 		{
// 			flag=0;
// 			break;
// 		}
// 	}
// 	if(number<=1)
// 		flag=1;
// 	return flag;
// }

#include<stdio.h>

int main()
{
    int n=0;
	int	k=0;
	int last=0;
	int sum=0;
	int i;
	int a[20000];

	scanf("%d %d ",&n,&k);
	for (i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if (a[i]+last>=8)
		{
			sum+=8;
            last=last+a[i]-8;
		}
		else if (a[i]+last<8)
		{
			 sum=sum+last+a[i];
             last=0;
		}
		if (sum >= k)
		{
			printf("%d",i);
			return 0;
		}
	}

	printf("-1");
	return 0;

}

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n,k,*a,i,count=0;
	scanf("%d%d",&n,&k);
	a = (int *)malloc(sizeof(int)*n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	i = 0;
	while((k>0)&&(i<n))
	{
		if(a[i]<=8)
			k = k-a[i];
		else
		{
			k = k-8;
			a[i+1] = a[i+1]+(a[i]-8);
		}
		//printf("%d\n",k);
		count++;
		i++;

	}
	if(k<=0)
		printf("%d\n",count);
	else
		printf("-1\n");
	return 0;
}

/*
 * A. Arya and Bran
 * time limit per test
 * 1 second
 * memory limit per test
 * 256 megabytes
 * input
 * standard input
 * output
 * standard output
 *
 * 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.
 */

#include <stdio.h>
int main() {
    int i, n, k, num, a_candy, b_candy;
    scanf("%d %d", &n, &k);
    for(i = 0, a_candy = b_candy = 0; i < n && b_candy < k; i++) {
        scanf("%d", &num);
        a_candy += num;
        if(a_candy >= 8) {
           a_candy -= 8;
           b_candy += 8;
        } else {
           b_candy += a_candy;
           a_candy = 0;
        }
    }
    if(i == n && b_candy < k)
        printf("-1");
    else
        printf("%d", i);
    return 0;
}

#include <stdio.h>

int main(){
	int n, k;
	scanf("%d %d", &n, &k);

	int give = 0, save = 0, a;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a);
		if (a > 8) {
			save += (a - 8);
			give += 8;
		} else {
			give += a;
			if (save < (8 - a))	{
				give += save;
				save = 0;
			} else {
				give += (8 - a);
				save -= 8 - a;
			}
		}

		if (give >= k)
		{
			printf("%d", i);
			return 0;
		}
	}

	printf("-1");

	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <string.h>
int main(int argc, char const *argv[])
{
	int n,k;
	scanf("%d %d",&n,&k);
    int a[n];
	for(int i=0;i<n;i=i+1){
		scanf("%d",&a[i]);
	}
	if(k>n*8){
		printf("-1");
		return 0;
	}
	int count=0;
	for(int i=0;i<n-1;i=i+1){
		if(a[i]<=8&&k>0){
			k=k-a[i];
    		count+=1;
		}else if(a[i]>8&&k>0){
			k=k-8;
			a[i+1]+=a[i]-8;
    		count+=1;
		}
	}
	if(a[n-1]>8){
		a[n-1]=8;
	}
	if(k>0){
		k=k-a[n-1];
		count+=1;
	}
	if(k<=0){
		printf("%d",count);
		return 0;
	}
	printf("-1");
	return 0;
}

#include <stdio.h>
main ()
	 {
	 int ans=0,n,k,a[101],sum=0,i;
	 scanf("%d %d",&n,&k);
	 for (i=0;i<n;i++)
	 	 scanf("%d",&a[i]);
	 for (i=0;i<n;i++)
	 	 {
	 	  sum+=a[i];
	 	  if (sum>=8)
		  	 {ans+=8;sum=sum-8;}
	 	  else {ans+=sum;sum=0;}

		  if (k<=ans) {printf ("%d",i+1);return 0;}
		 }
	 printf("-1");
	 return 0;
	 }

#include <stdio.h>

int main()
{
	int n,k,a[105],i,given_candies=0,stored_candies=0,count_days=0,j=0;

	scanf("%d",&n);
	scanf("%d",&k);

	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}

	while(j<n && given_candies<k)
	{
		if(a[j]>8)
		{
			stored_candies=stored_candies+a[j]-8;
			given_candies=given_candies+8;
			count_days++;
		}
		else
		{
			if(stored_candies>8-a[j])
			{
				given_candies=given_candies+8;
				stored_candies=stored_candies-(8-a[j]);
				count_days++;
			}
			else
			{
				given_candies=given_candies+a[j]+stored_candies;
				stored_candies=0;
				count_days++;
			}
		}
		j++;

	}

	if(given_candies<k)
		printf("-1\n");
	else
		printf("%d",count_days);

}

#include<stdio.h>
#define N 100
int main(){
    int n,k;
    int a[N];
    scanf("%d%d",&n,&k);
    int i;
    int give=0;
    int receive=0;
    int day=0;
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        give+=a[i];
        if(receive>=k){
            break;
        }else{
            if(give>=8){
                receive+=8;
                give-=8;
            }else{
                receive+=give;
                give=0;
            }
            day++;
        }
    }
    if(receive<k){
        printf("%d\n",-1);
    }else{
        printf("%d\n",day);
    }
    return 0;
}

#include<stdio.h>

int main(void)
{
    int N,K,Count,Day=0,Total=0;
    scanf("%d%d",&N,&K);
    int Array[N];
    for(Count=0;Count<N;Count++)
        scanf("%d",&Array[Count]);
    for(Count=0;Count<N;Count++)
    {
        if(K<=0)
            break;
        if(Array[Count]>8)
        {
            K-=8;
            Total+=(Array[Count]-8);
        }
        else
        {
            K-=Array[Count];
            if(Total>=(8-Array[Count]))
            {
                K-=(8-Array[Count]);
                Total-=(8-Array[Count]);
            }
            else
            {
                K-=Total;
                Total=0;
            }
        }
        Day++;
    }
    if(K<=0)
        printf("%d\n",Day);
    else
        printf("%d\n",-1);
    return 0;
}

#include <stdio.h>
int main()
{
	int n,k,i,a[1000];
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int sum=0;
	for(i=0;i<n;i++)
	{
		if(sum<k&&a[i]<8)
		{
			sum+=a[i];
		}
		else if(sum<k&&a[i]>=8)
		{
			sum+=8;
			a[i+1]+=a[i]-8;
		}
		if(sum>=k)
			break;
	}
	if(i==n)
		printf("-1\n");
	else
		printf("%d\n",++i);
	return 0;
}

#include<stdio.h>
#include<string.h>
int cmpfunc (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

main()
{
    int n, k, i, j;
    scanf("%d %d", &n, &k);
    int sum = 0, cnt = 0;
    int a[n];

    for(i = 0; i<n; i++)
    {
        scanf("%d", &a[i]);
        sum += a[i];
        if(sum>=8)
        {
            cnt +=  8;
            sum = sum - 8;
        }
        else
        {
            cnt += sum;
            sum = 0;
        }

        if(cnt>=k)
        {
            printf("%d", i+1);
            return 0;
        }
    }

    printf("-1");

}

#include <stdio.h>
int main()
{
    int n,k,a,i=0,j,x=0;
    scanf("%d%d",&n,&k);
    while(n--)
    {
        scanf("%d",&a);
        x=x+a;
        if(x<=8)
           {
            k=k-x;
            x=0;}
        else if(x>8)
            {
                k=k-8;
                x=x-8;
                }
            i++;
        if(k<=0)
        {
            printf("%d",i);
            return 0;
        }
        }
        printf("-1");
        return 0;
}

#include <stdio.h>
#include <string.h>
int main(){
	int n,k,num=0,t,tmp=0,f=0;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++){
		scanf("%d",&t);
		t+=tmp;
		tmp=0;
		if(t>8){
			num+=8;
			tmp=t-8;
			f++;
			if(num>=k)
			break;
		}
		else{
		num+=t;
		f++;
		if(num>=k)
		break;
    }
	}
	if(num<k){
		printf("-1");
	}
	else{
		printf("%d",f);
		}
		}

#include<stdio.h>
int main()
{
    int a[100],i,j,n,m,count=0,sum=0,b,k,l=0;
    scanf("%d %d",&n,&m);
    k=m;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        a[i]=a[i]+l;
        if(a[i]==m&&a[i]==8)
        {
            count=count+1;
            b=8;
           // printf("%d %d\n",count,b);
        }
        else if(a[i]>=8)
        {
            count=count+1;
            b=8;
            l=a[i]-8;
         // printf("d=%d %d\n",count,b);
        }
        else
        {
            m=m-a[i];
            count=count+1;
            b=a[i];
            l=0;
           // printf("e=%d %d\n",count,b);
        }

        sum=sum+b;
        if(sum>=k)
        {
            break;
        }
    }
    //printf("%d",sum);
    if(count==0||sum<k)
    {
        printf("-1");
    }
    else{
    printf("%d\n",count);}

}

#include<stdio.h>
int main()
{
    int n,k,i,j,giv=0;
    scanf("%d %d",&n,&k);
    int a[n+1];
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    a[n]=0;
    for(i=0;i<n;i++)
    {
        if(a[i]<8)
        giv+=a[i];
        else {
            giv+=8;
            a[i+1]+=a[i]-8;
        }
        if(giv>=k)
        break;
    }
    if(i==n)
    printf("-1");
    else printf("%d",i+1);
     return 0;

}

#include <stdio.h>

int main() {
    int n,x[100],k;
    scanf("%d %d",&n,&k);
    int i;
    for(i=0;i<n;i++){
        scanf("%d",&x[i]);
    }
    int s=0,count=1;
    for(i=0;i<n;i++){
        if(x[i]<=8) s=s+x[i];
        else{
            s=s+8;
            x[i+1]=x[i+1]+x[i]-8;
        }
        if(s>=k) {
            printf("%d",count);
            return 0;
        }
        count++;
    }
    printf("-1");
	return 0;
}

#include <stdio.h>

int main(void)
{
    int i, n, k, a[100], bram = 0;

    scanf("%d %d", &n, &k);
    for(i = 0; i < n; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < n; i++)
    {
        if(a[i] < 8)
            bram += a[i];
        else
        {
            bram += 8;
            if(i < n)
                a[i+1] += a[i] - 8;
        }

        if(bram >= k)
            break;
    }

    (i < n)? printf("%d\n", i+1) : printf("%d\n", -1);
}

#include <stdio.h>
#define MIN(x, y) ((x) < (y)? (x): (y))

int main()
{
    int n, k, box = 0, day;
    scanf("%d%d", &n, &k);
    for (day = 1; day <= n; day++) {
        int a;
        scanf("%d", &a);
        box += a;
        int give = MIN(8, box);
        box -= give;
        k -= give;
        if (k <= 0) break;
    }
    printf("%d\n", (k > 0)? -1: day);
    return 0;
}

#include <stdio.h>

int main(){
	int n , k , a , i=0 , j = 0 ;
	scanf("%d", &n) ;
	scanf("%d", &k) ;
	while (i < n && k > 0 ) {
        scanf("%d", &a) ;
        a = a + j ;
        j = 0 ;
        if ( a > 8 ) {
            k = k - 8 ;
            j = a - 8 ;
        }
        else {
            k = k - a ;
        }
        ++i ;
	}
	if ( k <= 0 ) printf("%d", i) ;
	else printf("%d", -1) ;
return 0 ;
}

#include<stdio.h>
#include<math.h>
int main()
{
    int n,k,i,a=0,b=0,c=0,d=0;
    scanf("%d %d",&n,&k);
    int ar[n];
    for(i=0;i<n;i++)
    scanf("%d",&ar[i]);

    for(i=0;i<n;i++)
    {
        while(a<k)
        {
            if(ar[i]==8)
            {
                a+=ar[i];
                b+=1;
                break;
            }
            else if(ar[i]<8)
            {
                if(ar[i]+c>=8)
                {
                a+=8;
                c=c-(8-ar[i]);
                b+=1;
                break;
                }
                else
                {
                a+=(ar[i]+c);
                c=0;
                b+=1;
                break;
                }
            }
            else
            {
                a+=8;
                c+=ar[i]-8;
                b+=1;
                break;
            }
        }
    }
    if(a<k)
    printf("-1");
    else
    printf("%d",b);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int cmp(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;
}

int main()
{
	int i,n,k,a[1005],sum=0,temp=0;
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);

	}
	for(i=0;i<n;i++)
	{
		if(a[i]>=8)
		{
			sum+=8;
			temp+=(a[i]-8);
		}
		else
		{
			temp+=a[i];
			if(temp>=8)
			{
				temp-=8;
				sum+=8;
			}
			else
			{
				sum+=temp;
				temp=0;
			}
		}
		if(sum>=k)
			break;
	}
	if(sum>=k)
	{
		printf("%d\n",i+1);
	}
	else
	{
		printf("-1\n");
	}

	return 0;

}

#include<stdio.h>
#include<math.h>

int main()
{
    int num,k,i;
    scanf("%d%d",&num,&k);
    int ara[num],sum=0;
    for(i=0;i<num;i++)
       {
           scanf("%d",&ara[i]);
           sum=sum+ara[i];
       }
       if(sum<k) printf("-1\n");
       else if(num*8<k) printf("-1\n");
       else
       {
           for(i=1;i<=num;i++)
           {
               if(ara[i-1]<=8) k=k-ara[i-1];
               else if(ara[i-1]>8)
               {
                   k=k-8;
                   ara[i]+=ara[i-1]-8;
               }
               if(k<=0)
                break;
           }
           if(i>num) printf("-1\n");
           else printf("%d\n",i);
       }

    return 0;
}

#include<stdio.h>
int main()
{
int n,k,i,count=0,l=0,m,mul=0,save=0;
scanf("%d",&n);
int arr[n];
scanf("%d",&k);
m=k;
for (i=0 ;i<n ;i++)
{
    scanf("%d",&arr[i]);
    mul +=arr[i];
    }
    for (i=0 ; i<n ; i++)
        {

if(n*8<k || k>mul)
{
printf("-1\n");
break;
}
else
    {
if(arr[i]>8 && m>8)
{
m= m-8;
count +=8;
save =arr[i]-8;
arr[i+1] +=save;
l++;
}
else
{
if(arr[i]>m)
{
count +=m;
l++;
}
else
{
    if(m>arr[i]){
m= m-arr[i];
count +=arr[i];
l++;}
else
{
    count +=m;
    l++;
}
}
}}
if(m==0 || count==k)
{
printf("%d\n",l);
l = 0;
break;
}
}
if (l!=0)
    printf("-1");

return 0;
 }

#include <stdio.h>
int min(int a, int b) { if(a > b) return b; return a; }
int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    int d[n];
    for(int i = 0; i < n; i++)
        scanf("%d", &d[i]);
    int lend = 0, addition = 0;
    for(int i = 0; i < n; i++)
    {
        int x = min(8, d[i]);
        if(x < 8)
        {
            lend += 8-x;
            int v = min(8-x, addition);
            x += v;
            addition -= v;
            lend -= v;
        }
        else
            addition += d[i]-8;
        k -= x;
        if(k <= 0)
            return 0*printf("%d\n", i+1);
    }
    puts("-1");
}

#include <stdio.h>

int main()
{
	int n,k,a[105],candies_given=0,store=0,i,count=0,j=0;

	scanf("%d",&n);
	scanf("%d",&k);

	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}

	while(candies_given<k && j<n)
	{
		if(a[j]>8)
		{
			candies_given=candies_given+8;
			store=store+a[j]-8;
			j++;
			count++;
		}
		else
		{
			a[j]=a[j]+store;
			if(a[j]>8)
			{
				candies_given=candies_given+8;
				store=a[j]-8;
				j++;
				count++;
			}
			else
			{
				candies_given=candies_given+a[j];
				j++;
				store=0;
				count++;
			}

		}
	}

	if(candies_given<k && j==n)
		printf("-1\n");
	else
		printf("%d",count);
}

#include <stdio.h>
int main()
{
    int n,k,i,sum,c,remain,flag;
    scanf("%d %d",&n,&k);
    int x[n];
    for(i=0; i<n; i++)
    {
        scanf("%d",&x[i]);
    }
    flag=0;
    sum=0;
    c=0;
    for(i=0; i<n; i++)
    {
        if(x[i]<=8)
        {
            sum=sum+x[i];
            c++;
            if(sum>=k)
            {
                flag=1;
                break;
            }
            else
            {
                flag=0;
            }
        }
        else if(x[i]>8)
        {
            c++;
            sum=sum+8;
            remain=x[i]-8;
            if(sum>=k)
            {
                flag=1;
                break;
            }
            else if(sum<k)
            {
                flag=0;
            }
            if(i+1<n && flag==0)
            {
                x[i+1]=x[i+1]+remain;
            }
        }
    }
    if(flag==1)
    {
        printf("%d",c);
    }
    else if(flag==0)
    {
        printf("-1");
    }
}

#include <stdio.h>
#include <stdlib.h>
int main(){
	int n,k,c=0,c1;
	scanf ("%d%d", &n,&k);

	for (int i=1;i<=n;i++){
		scanf ("%d", &c1);
		c +=c1;
		if (c >=8){
		k = k-8;
		c-=8;
		}
		else{ k-=c; c =0;}

		if (k<=0){
			printf ("%d", i);
		break;
		}

	}
	if (k>0)
		printf ("-1");

return 0;
}

#include <stdio.h>
#define N 100

void give(int *caramelleArya, int k, int *caramelleBran)
{
    int d=k-*caramelleBran, caramelleDonabili; //caramelle mancanti a Bran

    if (*caramelleArya>8) caramelleDonabili=8;
    else caramelleDonabili=*caramelleArya;

    if (caramelleDonabili-d>=0){
        *caramelleBran+=d;
        return;
    }

    *caramelleBran+=caramelleDonabili;
    *caramelleArya-=caramelleDonabili;
}

int main() {

    int n, k, a[N], caramelleArya=0, caramelleBran=0, giorni=0;

    scanf("%d %d", &n, &k);

    for (int i=0; i<n && caramelleBran!=k; ++i){

        scanf("%d", &a[i]);

        caramelleArya+=a[i];

        give(&caramelleArya, k, &caramelleBran);

        ++giorni;
    }

    if (caramelleBran!=k) printf("%d", -1);
    else printf("%d", giorni);

    return 0;
}

#include <stdio.h>

int main(int argc, char** argv) {
	int n, i, ukupno = 0, k, a, tacno = -1;
	scanf("%d %d", &n, &k);
	for(i = 0 ; i < n ; i++){
		scanf("%d", &a);
		if(tacno == -1){

		ukupno += a;
		if(ukupno <= 8){
			k  -= ukupno;
			ukupno = 0;
		}else{
			k -= 8;
			ukupno -= 8;
		}
		if( k <= 0){
			tacno = i+1;
		}
	}
	}
	printf("%d", tacno);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int n,k;
   scanf("%d %d",&n,&k);
   int i,p[n],sum=0;
   for(i=0;i<n;i++){
    scanf("%d",p+i);
       sum+=p[i];
   }
    if(sum>=k){
        sum=0; int sum2=0,flag=0;
        for(i=0;i<n-1;i++){
           if(p[i]>=8){
            p[i+1]+=p[i]-8;
            p[i]=8;
           }
        }
        for(i=0;i<n;i++){
          if(p[i]<=8)sum+=p[i];
          else sum+=8;
          if(sum>=k){
            printf("%d",i+1);
            flag=1;
            break;
          }
        }
        if(flag==0)printf("-1");

    }

    else printf("-1\n");

}

#include <stdio.h>
#define MAXGIVE 8
int min(int x, int y) {
    return x*(x<=y)+y*(x>y);
}
int main() {
    int len,candy,curr,bran=0,saved=0,i=0,keep=1,dar;
    scanf("%d %d\n",&len,&candy);
    while (i<len && keep) {
        scanf("%d",&curr);
        dar=min(curr+saved,MAXGIVE);
        bran+=dar;
        saved=(curr+saved-dar);
        if (bran>=candy) {
            keep=0;
            printf("%d",i+1);
        }
        i++;
    }
    if (keep) printf("-1");
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,k,i,j,r=0,c=0,d=0,m=0;
    scanf("%d%d",&n,&k);
    int ar[n];
    for(i=0;i<n;i++)
    scanf("%d",&ar[i]);

    for(i=0;i<n;i++)
    {
            ar[i]+=r;
            if(ar[i]>8)
            {r=abs(ar[i]-8);
            c+=8;}
            else
            {
               r=0;
               c+=ar[i];
            }
            d++;
        if(c>=k)
        {
            m=1;
            break;
        }
    }
    if(m!=1)
    printf("-1");
    else
    printf("%d",d);
}

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

int main() {
	int n, k;
	while(scanf("%d %d", &n, &k)==2) {
		int s=0, g=0, i;
		for(i=0; i<n && g<k; i++) {
			int x;
			scanf("%d", &x);
			s += x;
			if (s>=8) g+=8, s-=8;
			else g+=s, s=0;
		}
		if (g<k) printf("%d\n", -1);
		else printf("%d\n", i);
		break;
	}
}

#include<stdio.h>

int main()
{
    int n, k, i, day=0, total=0;
    scanf("%d%d", &n, &k);
    int ara[n];

    for(i=0; i<n; i++){
        scanf("%d", &ara[i]);
    }

    for(i=0; i<n; i++){
        if(ara[i]+day<=8){
            total=total+ara[i]+day;
            day=0;
        }
        if(ara[i]+day>8){
            total=total+8;
            day=ara[i]+day-8;
        }
        if(total>=k){
            printf("%d\n", i+1);
            return 0;
        }
    }

    printf("-1\n");
    return 0;
}

//https://codeforces.com/problemset/problem/839/A

#include<stdio.h>

int main()
{
    int n,k,i,sum=0;
    scanf("%d %d",&n,&k);

    int a[n],total=0;
    for(i=0;i<n;i++)
      {
            scanf("%d",&a[i]);
            total=total+a[i];
      }

      if(k>total)
        printf("-1");
       else if(k>(n*8))
        printf("-1\n");
       else
       {
           for(i=0;i<n;i++)
           {
               if(a[i]<8)
             {
                  sum=sum+a[i];
               if(sum>=k)
               {
                   printf("%d\n",i+1);
                   break;
               }

             }
             else if(a[i]>=8)
             {
                 a[i+1]=a[i+1]+a[i]-8;
                 sum=sum+8;

                 if(sum>=k)
                 {
                      printf("%d\n",i+1);
                      break;
                 }
                 if(i==(n-1) && sum<k)
                 {
                     printf("-1");
                     break;
                 }

             }
           }
       }

    return 0;
}

#include<stdio.h>
#define N 100
int main(){
    int n,k;
    int a[N];
    scanf("%d%d",&n,&k);
    int i;
    int give=0;
    int receive=0;
    int day=0;
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        give+=a[i];
        if(receive>=k){
            break;
        }else{
            if(give>=8){
                receive+=8;
                give-=8;
            }else{
                receive+=give;
                give=0;
            }
            day++;
        }
    }
    if(receive<k){
        printf("%d\n",-1);
    }else{
        printf("%d\n",day);
    }
    return 0;
}

#include<stdio.h>

int main()
{
	int n, k, i, a, s, b, c;
	scanf("%d %d",&n,&k);

	s = 0;
	b = 0;
	c = -1;

		for (i = 1;i<=n;i++)
		{
			scanf("%d", &a);

			s = s + a;
			if (s > 8)
			{
				b = b + 8;
				s = s - 8;
			}
			else
			{
				b = b + s;
				s = 0;
			}

			if (b >= k)
			{
				c = i;
				break;
			}
		}

		printf("%d", c);
	return 0;
}

#include <stdio.h>

int main(){
	int n , k , a , i=0 , j = 0 ;
	scanf("%d", &n) ;
	scanf("%d", &k) ;
	while (i < n && k > 0 ) {
        scanf("%d", &a) ;
        a = a + j ;
        j = 0 ;
        if ( a > 8 ) {
            k = k - 8 ;
            j = a - 8 ;
        }
        else {
            k = k - a ;
        }
        ++i ;
	}
	if ( k <= 0 ) printf("%d", i) ;
	else printf("%d", -1) ;
return 0 ;
}

#include<stdio.h>

int main()

{
    int n,k,sum=0,days=0,i,count;

    scanf("%d%d",&n,&k);
    int a[n];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++){
        if(a[i]>=8){
            sum=sum+8;
            if(sum<k)
                days++;
            if(sum>=k && days<=n){
                days++;
                printf("%d\n",days);
                break;
            }
            if(n==days){
                printf("-1");
                break;
            }

        count=a[i]-8;
        a[i+1]+=count;
        }

    if(a[i]<8){
        sum=sum+a[i];
        if(sum<k)
                days++;
            if(sum>=k && days<=n){
                days++;
                printf("%d\n",days);
                break;
            }
            if(n==days){
                printf("-1");
                break;
            }
    }
    }
return 0;
}

#include<stdio.h>
int a[100005];
int main()
{
    int n,k,i;
    int flag=0;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(i=1;i<=n;i++)
    {
         if(a[i]>8)
        {
            k-=8;
            a[i+1]+=a[i]-8;
        }
        else k-=a[i];
        if(k<=0)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
        printf("%d\n",i);
    else printf("-1\n");

    return 0;

}

#include<stdio.h>

int main()
{
    int n,k,f=0,sum=0;
    scanf("%d%d",&n,&k);
    int ar[n];
    for(int i=0;i<n;i++)
    scanf("%d",&ar[i]);
    for(int i=0;i<n;i++)
    {
    if(ar[i]<=8)
    {
    sum+=ar[i];
    }
    else
    {
    sum+=8;
    ar[i+1]+=ar[i]-8;
    }
    if(sum>=k)
    {
    k=i;
    f=1;
    break;
    }
    }
    if(f==1)
    printf("%d",k+1);
    else
    printf("%d",-1);
    return 0;
}

#include<stdio.h>
int main()
{
    int n,k,s,a,q=-1,m=0,p=1;
    scanf("%d %d",&n,&k);
    s=0;
    while(p<=n)
    {
        scanf("%d",&a);
        s=s+a;
        if(s>8) { m=m+8;  s=s-8;  }
        else { m=m+s; s=0; }
        if( m>=k ) { q=p; break; }
        p++;
    }
    printf("%d",q);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <string.h>
int main(int argc, char const *argv[])
{
	int n,k;
	scanf("%d %d",&n,&k);
    int a[n];
	for(int i=0;i<n;i=i+1){
		scanf("%d",&a[i]);
	}
	int count=0;
	for(int i=0;i<n-1;i=i+1){
		if(a[i]<=8&&k>0){
			k=k-a[i];
			count+=1;
		}else if(a[i]>8&&k>0){
			k=k-8;
			a[i+1]+=a[i]-8;
			count+=1;
		}
	}
	if(a[n-1]>8){
		a[n-1]=8;
	}
	if(k>0){
		k=k-a[n-1];
		count+=1;
	}
	if(k<=0){
		printf("%d",count);
		return 0;
	}
	printf("-1");
	return 0;
}

#include<stdio.h>
int main()
{
	int n,k,i,s=0,b,j=0;
	scanf("%d %d",&n,&k);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&b);
		j=j+b;
		if(j>=8)
		{
			s=s+8;
			j=j-8;
		}
		else
		{
			s=s+j;
			j=0;
		}
		if(s>=k)
		{
			printf("%d\n",i);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
 }

#include<stdio.h>

int main()
{
    int n,k,i;
    scanf("%d %d",&n,&k);
    int arr[100];
    for(i=0; i<n; i++) scanf("%d",&arr[i]);

    for(i=0; i<=n; i++)
    {
        if(i==n)
        {
            printf("-1");
            break;
        }

        if(arr[i]<=8)
        {
            if(k<=arr[i])
            {
                printf("%d",i+1);
                break;
            }
            else k=k-arr[i];
        }
        else
        {
            if(k<=8)
            {
                printf("%d",i+1);
                break;
            }
            else
            {
                k=k-8;
                arr[i+1]+=arr[i]-8;
            }
        }
    }

}

#include <stdio.h>
int main()
{
    int n,k,a,i=0,j,x=0;
    scanf("%d%d",&n,&k);
    while(n--)
    {
        scanf("%d",&a);
        x=x+a;
        if(x<=8)
           {
            k=k-x;
            x=0;}
        else if(x>8)
            {
                k=k-8;
                x=x-8;
                }
            i++;
        if(k<=0)
        {
            printf("%d",i);
            return 0;
        }
        }
        printf("-1");
        return 0;
}

#include <stdio.h>

int candy;
int n,k;

int main()
{
    int i,a;

    scanf("%d %d",&n,&k);

    for(i=0;i<n;i++){
        scanf("%d",&a);
        candy+=a;

        if(candy>8){
            k -= 8;
            candy -= 8;
        }
        else{
            k -= candy;
            candy = 0;
        }

        if(k<=0) {
            printf("%d",i+1);
            return 0;
        }
    }

    printf("-1");
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	int n,k,t,brian=0,arya=0;
	scanf("%d %d",&n,&k);
	int a[n];
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	int i;
	t=n;
	for(i=0;i<n;i++)
	{
		arya+=a[i];
		if(arya>=8)
			{
				k-=8;
				arya-=8;
			}
		else
			{
				k-=arya;
				arya=0;
			}
		if(k<=0)
			{
				printf("%d",i+1);
				return 0;
			}
	}
	printf("-1");
	return 0;
}

#include <stdio.h>
#include <string.h>
int main(){
	int n,k,num=0,t,tmp=0,f=0;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++){
		scanf("%d",&t);
		t+=tmp;
		tmp=0;
		if(t>8){
			num+=8;
			tmp=t-8;
			f++;
			if(num>=k)
			break;
		}
		else{
		num+=t;
		f++;
		if(num>=k)
		break;
    }
	}
	if(num<k){
		printf("-1");
	}
	else{
		printf("%d",f);
		}
		}

#include<stdio.h>

int main()
{
	int n, k, i, a, s, b, c;
	scanf("%d %d",&n,&k);

	s = 0;
	b = 0;
	c = -1;

		for (i = 1;i<=n;i++)
		{
			scanf("%d", &a);

			s = s + a;
			if (s > 8)
			{
				b = b + 8;
				s = s - 8;
			}
			else
			{
				b = b + s;
				s = s - s;
			}

			if (b >= k)
			{
				c = i;
				break;
			}
		}

		printf("%d", c);
	return 0;
}

#include<stdio.h>
int main()
{
	int i,j,n,k,arr[100]={0},sum=0,val=0,ans=0;
	scanf("%d %d",&n, &k);
	for(i=0; i<n; i++)
	{
		scanf("%d",&arr[i]);
	}
	for(i=0; i<n; i++)
	{
		if(sum >= k)
		{
			ans = i;
			break;
		}
		arr[i] = arr[i] + val;
		if(arr[i] > 8)
		{
		val = arr[i] - 8;
		sum = sum + 8;
		}
		else
		{
			sum = sum +arr[i];
			val = 0;
		}
	//	printf("%d\n",sum);

	}
	if(sum >= k)
		{
			ans = i;
		}
	if(sum < k)
	{
		ans = -1;
	}
	printf("%d\n",ans);

}

#include<stdio.h>

int main()
{
	int n, k, i, a, s, b, c;
	scanf("%d %d",&n,&k);

	s = 0;
	b = 0;
	c = -1;

		for (i = 1;i<=n;i++)
		{
			scanf("%d", &a);

			s = s + a;
			if (s > 8)
			{
				b = b + 8;
				s = s - 8;
			}
			else
			{
				b = b + s;
				s = 0;
			}

			if (b >= k)
			{
				c = i;
				break;
			}
		}

		printf("%d", c);
	return 0;
}

#include <stdio.h>

int main()
{
     int n,k;
 int i=1,sum=0,same=0;
 int ara[100001],bra[100001];
 scanf("%d %d",&n,&k);
  while(n--)
  {
      int a;
      scanf("%d",&a);
      a=a+same;
      if(a<9)
      {
           sum=sum+a;
           same=0;
      }
     else
     {
         sum=sum+8;
         same=a-8;

     }
     if(sum>=k)
     {
          printf("%d\n",i);
       return 0;
     }
     i++;
  }
  printf("-1\n");

}

#include<stdio.h>
#include<stdlib.h>

int main(){
    int n,k;
    scanf("%d",&n);
    scanf("%d",&k);
    int* arr = (int*)malloc(sizeof(int)*n);
    int i=0,x=0,y=0;
    for (i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    i =0;
    int sent =0;
    while(i<n){
        x = arr[i] + y;
        if(x<=8){
            y = 0;
            sent+=x;

        }
        else {
            y = x-8;
            sent +=8;
        }
        if(sent >= k){
            printf("%d",i+1);
            return 0;
        }
        i++;
    }
    if(y > 0 || y<=0){
        printf("-1");
    }

}

#include<stdio.h>

int main()
{
    int n, k, i, day=0, total=0;
    scanf("%d%d", &n, &k);
    int ara[n];

    for(i=0; i<n; i++){
        scanf("%d", &ara[i]);
    }

    for(i=0; i<n; i++){
        if(ara[i]+day<=8){
            total=total+ara[i]+day;
            day=0;
        }
        if(ara[i]+day>8){
            total=total+8;
            day=ara[i]+day-8;
        }
        if(total>=k){
            printf("%d\n", i+1);
            return 0;
        }
    }

    printf("-1\n");
    return 0;
}

#include<stdio.h>

int main(void)
{
	int n, k;
	int i, j;
	int in;
	int day = 0;
	int candy = 0;
	int sum = 0;

	scanf("%d %d", &n, &k);
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &in);
		candy += in;
		if (candy >= 8)
		{
			sum += 8;
			candy -= 8;
			day++;
			if (sum >= k)
				break;
		}
		else
		{
			sum += candy;
			candy = 0;
			day++;
			if (sum >= k)
				break;
		}

	}
	if (i == n + 1)
		printf("-1");
	else
		printf("%d", day);

	return 0;
}

#include<stdio.h>

int main()
{
    int a[101],i,sum=0,s=0,n,m,count=0;
    scanf("%d %d",&n,&m);

    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);

    }
    for(i=0;i<n;i++)
    {   if(m>s)
    {   if(a[i]>=8)
        {
            count++;
        }
        else
        {
            if(a[i]>=8)
                continue;

            else
                sum+=a[i];
        }

    }
        else
            break;

    }
    if((sum+(count*8))>=m)
    printf("%d",i);

   else
       printf("-1");
return 0;

   // printf("%d %d",count,sum);

}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <string.h>
int main(int argc, char const *argv[])
{
	int n,k;
	scanf("%d %d",&n,&k);
    int a[n];
	for(int i=0;i<n;i=i+1){
		scanf("%d",&a[i]);
	}
	int count=0;
	for(int i=0;i<n-1;i=i+1){
		if(a[i]<=8&&k>0){
			k=k-a[i];
		}else if(a[i]>8&&k>0){
			k=k-8;
			a[i+1]+=a[i]-8;
		}
		count+=1;
	}
	if(a[n-1]>8){
		a[n-1]=8;
	}
	if(k>0){
		k=k-a[n-1];
		count+=1;
	}
	if(k<=0){
		printf("%d",count);
		return 0;
	}
	printf("-1");
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,k,i,sum=0;
    scanf("%d %d",&n,&k);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n;i++)
    {
        if(sum<k)
        {
            if(a[i]>8)
            {
                a[i+1] = a[i+1]+(a[i]-8);
                sum = sum +8;
            }
            else
            {
                sum = a[i];
            }
        }
        else
        {
            break;
        }
    }

    if(sum<k)
        printf("%d",-1);
    else
    printf("%d",i);
    return 0;
}

#include<stdio.h>
int main()
{
    int n,k,i,c=0,t=0;
    scanf("%d %d",&n,&k);
    int a[n];
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
     for(i=0;i<n,t<k;i++){
            if(a[i]>8){
                t=t+8;
                a[i+1]=a[i+1]+a[i]-8;
        }
            else
                t=t+a[i];
    }
if(k>t)
    printf("-1");
else
    printf("%d",i);
    return 0;
}

#include<stdio.h>
int main()
{
     int n,k,i,r=0,x=0,p=0;
     scanf("%d %d",&n,&k);
     int ara[n];
     for(i=0; i<n ;i++)
     {
          scanf("%d",&ara[i]);
          ara[i]=x+ara[i];
         if(ara[i]>8 )
          {
               ara[i]=8;
               x=ara[i]-8;

          }
          else x=0;
          p=ara[i]+p;

          r++;
          if(p>=k)
               break;
     }
     if(r<=n && p>=k)
          printf("%d",i+1);
     else
          printf("-1");
     return 0;
}

#include<stdio.h>
int main()
{
    int n,k,s,a,c=0,m=0,p;
    scanf("%d %d",&n,&k);
    s=k;
    while(n--)
    {
        scanf("%d",&a);
        if(a>8) s=s-8;
        else s=s-a;
        c++;
        if(s<=0&&c>m) { m=c; c=0; }
    }
    if(s>0) printf("-1");
    else printf("%d",m);
}

#include <stdio.h>

int main()
{
    int n, k, count = 0, a;
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &a);
        count += a > 8 ? 8 : a;
        if(count >= k) {
            printf("%d\n", i + 1);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

#include<stdio.h>

int main(){
    int n,k,i,j,count=0,x=-1;
    scanf("%d %d",&n,&k);
    int a[n];
    for(i=0;i<n;i++){
        scanf("%d ",&a[i]);
    }

    j=0;
    while(k>0 && (j<n)){
        if(a[j]>8){
            k=k-8;
            count++;
            j++;
        }

        else if(a[j]<=8){
            k=k-a[j];
            count++;
            j++;
        }

    }
    if(j=n){

    if(k<=0){
    printf("%d",count);
    }

    else printf("%d",x);

    i=j=count=0;

}
}

#include<stdio.h>
int main()
{
    int c[100];
    int k,i,n,sum=0,count=0;
    float r,f;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++)
    {
        scanf("%d",&c[i]);
    }
    if(n*8<k)
    {
        printf("-1");
    }
    else
    {
        for(i=0;i<n;i++)
        {
            if(sum<k)
            {
                sum=sum+c[i];
                count=count+1;

            }
        }

        if(count*8<=k)
        {
            r=k/8;
            f=(float)k/8;
            if(f>r)
            {
                printf("%d",(int)f+1);
            }
            else
            {
                printf("%d",(int)r);
            }

        }
        else
        {
            printf("%d",count);
        }

    }
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
	int n, k, i, sum = 0;
	scanf("%d%d", &n, &k);
	int x;
	for(i = 1; i <= n; i++){
		scanf("%d", &x);
		if(x >= 8)
			x = 8;
		sum = sum + x;
		if(sum >= k){
			printf("%d\n", i);
			exit(0);
		}
	}
	printf("-1\n");
	return 0;
}

#include <stdio.h>

int main()
{
	int n, k;
	scanf("%d%d", &n, &k);
	int s = 0;
     int i;
	for (i=1; i<=n; i++)
	{
	     int a;
	     scanf("%d", &a);
	     s = (a>8)?(s+8):(s+a);
	     if (s>=k) break;
	}
	if (s>=k)
	     printf("%d", i);
	else
	     printf("-1");
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef long long int ll;
typedef long double ld;

#define FOR(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define rep(i,n)   for(int (i) = 0; (i) < (n); ++(i))
int MAX(int a, int b) {
	return (a>b)?a:b;
}
int main(int argc, char const *argv[]) {
	int n,k;
	scanf("%d%d",&n,&k);
	int *arr = (int*)calloc(n,sizeof(int));
	rep(i,n)scanf("%d",&arr[i]);
	int ans = 0, rem = 0;
	rep(i,n) {
		ans = i;
		if (arr[i]>8) {
			rem+=arr[i]-8;
			k-=8;
		} else {
			rem+=arr[i];
			if (rem>8) {
				rem-=8;
				k-=8;
			} else {
				rem=0;
				k-=rem;
			}
		}
		if (k<=0)
			break;
	}
	if (k>0)
		printf("-1\n");
	else
		printf("%d\n", ans+1);
	return 0;
}

#include<stdio.h>
int main()
{
    int n,k,temp=-1;
    scanf("%d %d",&n,&k);
    int arr[n];
    int i,sum=0;
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
        if(arr[i]>8) sum+=8;
        else sum+=arr[i];
        if(sum>=k) temp=i+1;
    }

    printf("%d",temp);
}

#include<stdio.h>
int main()
{
    int c[100];
    int k,i,n,sum=0,count=0;
    float r,f;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++)
    {
        scanf("%d",&c[i]);
    }
    if(n*8<k)
    {
        printf("-1");
    }
    else
    {
        for(i=0;i<n;i++)
        {
            if(sum<k)
            {
                sum=sum+c[i];
                count=count+1;

            }
        }
        printf("%d ",count);
        if(count*8<k)
        {
            r=k/8;
            f=(float)k/8;
            if(f>r)
            {
                printf("%d",(int)f+1);
            }
            else
            {
                printf("%d",(int)r);
            }

        }
        else
        {
            printf("%d",count);
        }

    }
}

#include<stdio.h>

int main(void)
{
    int N,K,Count,Day=0;
    scanf("%d%d",&N,&K);
    int Array[N];
    for(Count=0;Count<N;Count++)
        scanf("%d",&Array[Count]);
    for(Count=0;Count<N;Count++)
    {
        if(K<=0)
            break;
        if(Array[Count]>8)
            K-=8;
        else
            K-=Array[Count];
        Day++;
    }
    if(K<=0)
        printf("%d\n",Day);
    else
        printf("%d\n",-1);
    return 0;
}

#include<stdio.h>
int main()
{
    int n,k,a,i,sum=0,x=0,r,c=0;
    scanf("%d %d",&n,&k);

    for(i=1; i<=n; i++)
    {

        scanf("%d",&a);
        sum=sum+a+x;
         if(n*8<k)
    {
        printf("-1");
        return 0;
    }

        else if(sum<=k)
        {
            if(sum>8)
            {
                x=sum-8;
            }
            else
            {
                x=0;
            }
        }
c++;

    }
    printf("%d",c);
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	int n,k,t;
	scanf("%d %d",&n,&k);
	int a[n];
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	int i;
	t=n;
	for(i=0;i<n;i++)
	{
		if(a[i]>=8)
			k-=8;
		else
			k-=a[i];
		if(k<=0)
			{
				printf("%d",i+1);
				return 0;
			}
	}
	printf("-1");
	return 0;
}

#include<stdio.h>
int main ()
{
    int n,k,a[1010],count=0,rem=0,i,flag=0;
    scanf("%d%d",&n,&k);
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0; i<n; i++)
    {
        if((rem+a[i])>=8)
        {
            rem=(rem+a[i])-8;
            k=k-8;
            count++;

        }
        else if(k==(rem+a[i])) {
            count++;
            flag=count;
            break;
        }
        else
        {
            k=k-a[i];
            count++;

        }

        if(k<=0)
        {
            flag=count;
            break;
        }

       // printf("DEBUG ==>\nday left =%d\nk left=%d\ncount = %d\nflag =%d\n",n,k,count,flag);

    }
     //printf("%d\n\n\n",flag);
    if(flag>0)printf("%d\n",flag);
    else printf("-1\n");

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int n,k;
   scanf("%d %d",&n,&k);
   int i,p[n],sum=0;
   for(i=0;i<n;i++){
    scanf("%d",p+i);
       sum+=p[i];
   }
    if(8*n>=k){
            int l=0,c=0;
        while(1){
            l+=8;c++;
            sum+=-8;
            if(l>=k){
                printf("%d",c);
                break;
            }
        }
    }

    else printf("-1\n");

}

#include <stdio.h>
#include <stdlib.h>
int main(){
	int n,k,*A;
	scanf ("%d%d", &n,&k);
	A = (int *) malloc ((n+1)*sizeof(int));
	for (int i=1;i<=n;i++)
		scanf ("%d", &A[i]);
	if ((k%8) +1 <= n)
		printf ("%d",n);

return 0;
}

#include <stdio.h>
int main()
{
    int n,k,a,i=0,j,m=0,x;
    scanf("%d%d",&n,&k);
    while(n--)
    {
        scanf("%d",&a);
        x=x+a+m;
        if(x<=8)
            k=k-x;
        else if(x>=8)
            {
                k=k-8;
                m=x-8;
                }
            i++;
        if(k<=0)
        j=i;
        }
        if(k<=0) printf("%d",j);
        else
        printf("-1");
}

#include<stdio.h>
#include<string.h>
int cmpfunc (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

main()
{
    int n, k, i, j;
    long long sum = 0, cnt = 0;
    int a[n];
    scanf("%d %d", &n, &k);
    for(i = 0; i<n; i++)
    {
        scanf("%d", &a[i]);
        sum += a[i];
        if(sum>8)
        {
            cnt +=  8;
            sum = sum - 8;
        }
        else
        {
            cnt += a[i];
            sum -= a[i];
        }

        if(cnt>=k)
        {
            printf("%d", i+1);
            return 0;
        }
    }

    printf("-1");

}

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	int n, k, a, nDay=0;
	// k = numero de doces desejado
	// n = numero de dias
	// a = qtde de doce do dia

	int i, tot=0, sobra=0;

	scanf("%d", &n);
	scanf("%d", &k);

	for(i=0; i<n; i++) {
		scanf("%d", &a);
		a = a+sobra;
		if(a>8) {
			a=a-8;
			sobra+=a;
			tot += 8;
		}
		else {
			tot += a;
		}
		nDay++;
		if(tot==k) break;
	}
	if(tot>=k)
		printf("%d\n", nDay);
	else
		printf("-1\n");

	return 0;
}

#include<stdio.h>
int main ()
{

    int n,k,i,j,sum,d;
    while(scanf("%d %d",&n,&k)!=EOF)
    {
        sum=d=0;
        int array[n];
    for(i=0;i<n;i++)
    {

        scanf("%d",&array[i]);
    }
    for(i=0;i<n;i++)
    {
        if(array[i]>=8)
          sum+=8;
        else
         sum+=array[i];
        if(sum>=k)
        {
            d=i+1;
            break;
        }

    }
    if(d)
        printf("%d\n",d);
    else
        printf("-1\n");

    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main(){
	int n,k,A[10000];
	scanf ("%d%d", &n,&k);

	for (int i=1;i<=n;i++){
		scanf ("%d", &A[i]);
		if (A[i] >=8) A[i] = 8;
		k-=A[i];

		if (k<=0)
			printf ("%d", i);

	}
	if (k>0)
		printf ("-1");

return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,k,t,i;
    int s=0;
    int a [100];
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    for(i=0;i<n;i++)
    {

        if(a[i]>=8)
            {
                s=s+8;
            a[i+1]+= a[i]-8;
    }
    else
        s=s+a[i];
    if(s>=k)
    {
        t=i+1;
        break;
    }

     }

    printf ("%d",t);
}

#include <stdio.h>

#define N 100

int main() {
	static int aa[N];
	int n, k, i, b, r, cnt, yes;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	yes = cnt = b = r = 0;
	for (i = 0; i < n; i++) {
		b += (aa[i] + r <= 8 ? aa[i] : 8);
		if (aa[i] > 8)
			r += aa[i] - 8;
		cnt++;
		if (b >= k) {
			yes = 1;
			break;
		}
	}
	if (!yes)
		if ((b + r) >= k) {
			yes = 1;
			cnt += (r + 7) / 8;
		}
	printf("%d\n", yes && cnt <= n ? cnt : -1);
	return 0;
}

#include <stdio.h>
#include <math.h>
#define N 1000

int main(){
	int n,k,a[N],can,i;
	//can=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
		//can+=a[i];
		//printf("%d\n",a[i] );
		if(a[i]>8) k=k-8;
		else k=k-a[i];
		if(k<=0) {
			printf("%d\n",i+1);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}

#include <stdio.h>

int main()
{
	int n,k,a[105],candies_given=0,store=0,i,count=0,j=0;

	scanf("%d",&n);
	scanf("%d",&k);

	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}

	while(candies_given<=k && j<n)
	{
		if(a[j]>8)
		{
			candies_given=candies_given+8;
			store=store+a[j]-8;
			j++;
			count++;
		}
		else
		{
			a[j]=a[j]+store;
			if(a[j]>8)
			{
				candies_given=candies_given+8;
				store=a[j]-8;
				j++;
				count++;
			}
			else
			{
				candies_given=candies_given+a[j];
				j++;
				store=0;
				count++;
			}

		}
	}

	if(candies_given<k && j==n)
		printf("-1\n");
	else
		printf("%d",count);
}

#include <stdio.h>

int main()

{
    int n,k,i,a,count=0,c=0,b=0;
    scanf(" %d %d",&n,&k);

    for(i=0;i<n;i++)
    {
        scanf(" %d",&a);
        a=a+b;
        printf(" %d %d\n",a,c);

        if(a<8 && c<k){
            c+=a;
            b=0;
            count++;
        }
        else if(c<k){
            c+=8;
            b=a-8;
            count++;
        }
    }

    if(c>=k)
        printf("%d\n",count);
    else
        printf("-1");

    return 0;
}

#include <stdio.h>

int main()
{
	int n,k,a[105],i,given_candies=0,stored_candies=0,count_days=0,j=0;

	scanf("%d",&n);
	scanf("%d",&k);

	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}

	while(j<n)
	{
		if(a[j]>8)
		{
			stored_candies=stored_candies+a[j]-8;
			given_candies=given_candies+8;
			count_days++;
		}
		else
		{
			if(stored_candies>8-a[j])
			{
				given_candies=given_candies+8;
				stored_candies=stored_candies-(8-a[j]);
				count_days++;
			}
			else
			{
				given_candies=given_candies+a[j]+stored_candies;
				stored_candies=0;
				count_days++;
			}
		}
		j++;

	}

	if(given_candies<k)
		printf("-1\n");
	else
		printf("%d",count_days);

}

#include<stdio.h>
int main()
{
int n,k,r=0;
scanf("%d %d",&n,&k);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<=8)
{
r=a[i]+r;
printf("day :%d r=%d\n",i+1,r);
}
else
{
r=8+r;
printf("day :%d r=%d\n",i+1,r);
}
if(r>=k)
{
int rem=r-k;
if(rem!=0)
{
for(int j=0;j<n;j++)
{
if(a[j]==rem)
{
printf("%d\n",i);
return 0;
}
}
}
printf("R=%d\n",r);
printf("%d\n",i+1);
return 0;
}
}

printf("-1\n");
}

#include<stdio.h>
int main ()
{
    int n,k,a[1010],count=0,rem=0,i,flag=0;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
       }
    for(i=0;i<n;i++){
        if((rem+a[i])>8){
        rem=a[i]-8;
        k=k-8;
        count++;

       }
       else{
        k=k-a[i];
         count++;

       }
       if(k<=0){flag=count;
       break;
       }

    }
    if(flag>0)printf("%d\n",flag);
    else printf("-1\n");

    return 0;
}

#include<stdio.h>
int main()
{
    int n,k,i,j,m,ar[10000];
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++)
    {
        scanf("%d",&ar[i]);
    }
    if(n*8 <k)
        printf("-1\n");
    else
    {
        for(i=0;i<n;i++)
        {
            if(ar[i]<8)
                k=k-ar[i];
            else
                k=k-8;
        }
        if(k<=0)
            printf("%d\n",i);
        else
            printf("-1\n");

    }

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int n,k;
   scanf("%d %d",&n,&k);
   int i,p[n],sum=0;
   for(i=0;i<n;i++){
    scanf("%d",p+i);
       sum+=p[i];
   }
    if(8*n>=k){
            int l=0,c=0;
        while(c<n&&sum!=0){
            l+=8;c++;
            sum+=-8;
            if(l>=k)break;
        }
        if(l>=k)printf("%d",c);
        else printf("-1");
    }

    else printf("-1\n");

}

#include<stdio.h>
int main()
{
int n,k,i,count=0,l=0,m;
scanf("%d",&n);
int arr[n];
scanf("%d",&k);
m=k;
for (i=0 ;i<n ;i++)
{
    scanf("%d",&arr[i]);
    }
    for (i=0 ; i<n ; i++)
        {

if(n*8<k)
{
printf("-1\n");
break;
}
else
    {
if(arr[i]>8 && m>8)
{
m= m-8;
count +=8;
l++;
}
else
{
if(arr[i]>m)
{
count +=m;
l++;
}
else
{
    if(m>arr[i]){
m= m-arr[i];
count +=arr[i];
l++;}
else
{
    count +=m;
    l++;
}
}
}}
if(m==0 || count==k)
{
printf("%d\n",l);
break;
}

}
return 0;
 }

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <string.h>
int main(int argc, char const *argv[])
{
	int n,k;
	scanf("%d %d",&n,&k);
    int a[n];
	for(int i=0;i<n;i=i+1){
		scanf("%d",&a[i]);
	}
	int count=0;
	for(int i=0;i<n-1;i=i+1){
		if(a[i]>8){
			a[i]=8;
			a[i+1]+=a[i]-8;
		}
	}
	if(a[n-1]>8){
		a[n-1]=8;
	}
	for(int i=0;i<n;i=i+1){
		if(k>0){
			k-=a[i];
			count+=1;
		}
	}
	if(k>0){
		printf("-1\n");
		return 0;
	}
	printf("%d",count);
	return 0;
}

#include<stdio.h>
int main()
{
	int n, k, i, a, b;
	b = 0;
	scanf("%d%d", &n, &k);
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		a = a + b;
		if (a > 8)
		{
			b = a - 8;
			k = k - 8;
		}
		else k=k-a;
		if (k <= 0) break;
	}
	if (i <= n)
		printf("%d", i);
	else if (i==n,k>0) printf("-1");
	return 0;
}

#include<stdio.h>
int main ()
{
	int n,k,x,sum=0,m=0,b=0;
	scanf("%d%d",&n,&k);
	int i;

	for (i=1;i<=n;i++){
		scanf("%d",&x);
		if (x==0)
		break;
		x+=b;
		if (x>8){
			b=x-8;
			 x=8;
		}
		sum+=x;
		m++;
		if (sum>=k) break;
	}

	if (sum>=k) printf("%d\n",m);
	else printf("-1");
	return 0;

}

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n,k,*a,i,count=0;
	scanf("%d%d",&n,&k);
	a = (int *)malloc(sizeof(int)*n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	i = 0;
	while((k>0)&&(i<n))
	{
		if(k<=8)
			k = k-a[i];
		else
		{
			k = k-8;
			a[i+1] = a[i+1]+(a[i]-8);
		}
		//printf("%d\n",k);
		count++;
		i++;

	}
	if(k<=0)
		printf("%d\n",count);
	else
		printf("-1\n");
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	int n, k, a=0, nDay=0;
	// k = numero de doces desejado
	// n = numero de dias
	// a = qtde de doce do dia

	int i, tot=0, sobra=0;

	scanf("%d", &n);
	scanf("%d", &k);

	for(i=0; i<n; i++) {
		scanf("%d", &a);
		sobra += a;
		if(sobra>=8) {
			sobra-=8;
			k-=8;
		}
		else {
			k -= a;
			sobra = 0;
		}
		nDay++;
		if(k<=0) break;
	}
	if(k<=0) printf("%d\n", nDay);
	else printf("-1\n");

	return 0;
}

#include<stdio.h>
int main()
{
    int a[100],i,j,n,m,count=0,sum=0;
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==m&&a[i]==8)
        {
            count=count+1;
        }
        else if(a[i]>=8&&n!=1)
        {
            m=m-8;
            count=count+1;

        }
        else if(a[i]<8)
        {
            m=m-a[i];
            count=count+1;
        }
        sum=sum+a[i];
    }
    if(count==0||sum<m)
    {
        printf("-1");
    }
    else{
    printf("%d\n",count);}

}

#include<stdio.h>
main()
{
    int n,k,a[10000],i,j,sum=0,flag=0;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>8)a[i]=8;
        }
        for(i=0;i<n;i++)
        {
            sum=sum+a[i];
            if(sum>=k)
            {
                printf("%d\n",i+1);flag=1;break;
            }
        }
    if(flag==0)printf("-1\n");
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, k, candy, days=0, i, ans=0, temp=0, remain=0;
    scanf("%d%d", &n, &k);
    for(i=0; i<n; i++){
        scanf("%d", &candy);
        candy+=remain;
        if(candy<=8) ans+=candy;
        else{
            remain = candy-8;
            ans+=8;
        }
        days++;
        if(ans>=k){
            temp=1;
            break;
        }
    }
    if(k==0) days = 0;
    if(temp) printf("%d\n", days);
    else printf("-1\n");
    return 0;
}

#include<stdio.h>
int main()
{
     int n,k,i,r=0,x=0,p=0;
     scanf("%d %d",&n,&k);
     int ara[n];
     for(i=0; i<n ;i++)
     {
          scanf("%d",&ara[i]);
          ara[i]=x+ara[i];
         if(ara[i]>8 )
          {
               x=ara[i];
               ara[i]=8;

          }
          else x=0;
          p=ara[i]+p;

          r++;
          if(p>=k)
               break;
     }
     if(r<=n && p>=k)
          printf("%d",i+1);
     else
          printf("-1");
     return 0;
}

//Name: Bui Minh Khoa
//Code: 1753005
//Ex 839A

#include <stdio.h>

int main()
{
	int day, candy, a,give=0,spare=0,time=1;
	scanf("%d %d", &day, &candy);

	for (; day > 0; day--)
	{
		scanf("%d", &a);
		if (a >= 8)
		{
			give = give + 8;
			spare = spare + a - 8;
		}
		else
			give = give + a;
		if (give < candy)
			time++;
		else
			break;
	}
	if (give < candy)
		printf("-1");
	else
		printf("%d", time);

	return 0;
}

#include <stdio.h>

int main()

{
    int n,k,i,a,count=0,c=0,b=0;
    scanf(" %d %d",&n,&k);

    for(i=0;i<n;i++)
    {
        scanf(" %d",&a);
        a=a+b;

        if(a<8 && c<k){
            c+=a;
            count++;
        }
        else if(c<k){
            c+=8;
            b=a-8;
            count++;
        }
    }

    if(c>=k)
        printf("%d\n",count);
    else
        printf("-1");

    return 0;
}

#include<stdio.h>
int main()
{
int n,k,a[100],i=0,sum=0;
scanf("%d %d",&n,&k);
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
while(n!=0)
{
    sum=sum+8;
    if(sum>=k)
    {
        printf("%d",i);
        break;
    }n--;
}
if(sum<k)
{
    printf("-1");
}
return 0;
}

#include<stdio.h>
int main ()
{
    int n,k,a[1010],count=0,rem=0,i,flag=0;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
       }
    for(i=0;i<n;i++){
        if((rem+a[i])>=8){
        rem=(rem+a[i])-8;
        k=k-8;
        count++;

       }
       else{
        k=k-a[i];
         count++;

       }
       if(k<=0){flag=count;
       break;
       }

    }
    if(flag>0)printf("%d\n",flag);
    else printf("-1\n");

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,k,i,sum=0;
    scanf("%d %d",&n,&k);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n;i++)
    {
        if(sum<k)
        {
            if(a[i]>8)
            {
                a[i+1] = a[i+1]+(a[i]-8);
                sum = sum +8;
            }
            else
            {
                sum = a[i];
            }
        }
        else
        {
            break;
        }
    }

    if(sum<k)
        printf("%d",-1);
    else
    printf("%d",i);
    return 0;
}

#include<stdio.h>
int a[100005];
int main()
{
    int n,k,i;
    int flag=0;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);

    for(i=1;i<=n;i++)
    {
         if(a[i]>8)
        {
            k-=8;
        }
        else k-=a[i];
        if(k<=0)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
        printf("%d\n",i);
    else printf("-1\n");

    return 0;

}

#include <stdio.h>
int main()
{
    int n,k,a,i=0,j,m=0,x;
    scanf("%d%d",&n,&k);
    while(n--)
    {
        scanf("%d",&a);
        x=x+a+m;
        if(x<=8)
            k=k-x;
        else if(x>=8)
            {
                k=k-8;
                m=x-8;
                }
            i++;
        if(k<=0)
        j=i;
        }
        if(k<=0) printf("%d",j);
        else
        printf("-1");
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int cmp(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;
}

int main()
{
	int i,n,k,a[1005],sum=0,temp=0;
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);

	}
	for(i=0;i<n;i++)
	{
		if(a[i]>=8)
		{
			sum+=8;
			temp+=(a[i]-8);
		}
		else
		{
			temp+=a[i];
			if(a[i]>=8)
			{
				temp-=8;
				sum+=8;
			}
			else
			{
				sum+=temp;
				temp=0;
			}
		}
		if(sum>=k)
			break;
	}
	if(sum>=k)
	{
		printf("%d\n",i+1);
	}
	else
	{
		printf("-1\n");
	}

	return 0;

}

//Name: Bui Minh Khoa
//Code: 1753005
//Ex 839A

#include <stdio.h>

int main()
{
	int day, candy, a, give = 0, spare = 0, time = 1;
	scanf("%d %d", &day, &candy);

	for (; day > 0; day--)
	{
		scanf("%d", &a);
		if (a > 8)
		{
			give = give + 8;
			spare = spare + a - 8;
		}
		else
		{
			give = give + a;
			if (spare+a > 8)
				give = give + 8-a;
			else
				give = give + spare;
		}
		if (give < candy)
			time++;
		else
			break;
	}
	if (give < candy || time<day)
		printf("-1");
	else
		printf("%d", time);

	return 0;
}

#include <stdio.h>

int main(void)
{
	int n, k, a, sum, d, i;
	scanf("%d %d", &n, &k);

	d = -1;
	for(i=1; i<=n; i++)	{
		scanf("%d", &a);
		if(d==-1)	{
		if(a<=8)	{
			k -= a;
			a = 8-a;
			if(a<=sum)	{
				k -= a;
				sum -= a;
			}
			else {
				k -= sum;
				sum = 0;
			}
		}
		else {
			k -= 8;
			a -= 8;
			sum += a;
		}
		if(k<=0)	d = i;
	}
	}

	printf("%d\n", d);

	return 0;
}

#include<stdio.h>

int main()
{
    int a[101],i,sum=0,s=0,n,m,count=0;
    scanf("%d %d",&n,&m);

    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);

    }
    for(i=0;i<n;i++)
    {   if(m>s)
    {   if(a[i]>=8)
        {
            count++;
        }
        else
        {
            if(a[i]>=8)
                continue;

            else
                sum+=a[i];
        }

    }
        else
            break;

    }
    if((sum+(count*8))>=m)
    printf("%d",i);

   else
       printf("-1");
return 0;

   // printf("%d %d",count,sum);

}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    int sum = 0;
    int sum2 = 0;
    int i;
    for ( i = 0; i < a; i++)
    {
        int k;
        scanf("%d", &k);
        if (k > 8)
        {
            sum2 = k - 8;
            sum += 8;
        }
        else
        {
            if (sum2 > (8-k))
            {
                sum2 -= 8-k;
                sum += 8;
            }
            else
            {
                sum +=sum2 + k;
                sum2 = 0;
            }
        }
        //printf("sum = %d\n",sum);
        if ( sum >= b)
        {
            sum =0;
            break;
        }
    }
    if (sum != 0 )
    {
        printf("-1");
    }
    else
    {
        printf("%d",++i);
    }
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,k,i,NoOfCandy[100],FlagDone=0,DailyCandy=0;
    scanf("%d %d ",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&NoOfCandy[i]);
    }
    for(i=0;i<n;i++){
      if(NoOfCandy[i]<8){
        DailyCandy+=NoOfCandy;
        }
       else {
        DailyCandy+=8;
        }
        if(DailyCandy>=k){
            printf("%d",i+1);
            FlagDone=1;
            break;
        }
    }
    if(FlagDone==0){
        printf("-1");
    }
    return 0;
}

#include<stdio.h>
#include<conio.h>

int main() {

	int n, k, a[100];
	scanf("%d", &n);
	scanf("%d", &k);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	int temp = n;
	int dem = 0;
	for (int i = 0; i < n; i++)
	{
		if (k > 0 && temp > 0)
		{
			if (a[i] > 8) {
				k = k - 8;
				temp--;
				if (temp == 0 && k > 0) {
					dem = 0;
				}
				else
				{
					dem++;
				}
			}
			else
			{
				k = k - a[i];
				temp--;
				if (temp == 0 && k > 0) {
					dem = 0;
				}
				else
				{
					dem++;
				}
			}
		}
	}
	if (dem == 0)
	{
		printf("-1");
	}
	else
	{
		printf("%d", dem);
	}
	return 0;
}

#include <stdio.h>

int main()
{
    int n, k, count = 0, a, remain = 0;
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &a);
        a += remain;
        if(a >= 8) {
            count += 8;
            a -= 8;
        } else {
            count += a;
            a = 0;
        }
        if(count >= k) {
            printf("%d\n", i + 1);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

#include <stdio.h>
int main()
{
    int i, j, n, k;
    scanf("%d %d", &n, &k);
    int a[n];
    for(i=0; i<n; ++i) {
        scanf("%d", &a[i]);
    }
    int given = 0, remain = 0;
    i = 0;

    while(given<k && i<n) {

        remain = 0;
        for(j=0; j<i; ++j) remain += a[j];
        if(a[i]<8) {
            if(remain+a[i]-8 >= 0) {
                given += 8;
                remain -= 8 - a[i];
                a[i] = 0;
            }
            else if(remain+a[i]-8 < 0) {
                given += remain+a[i];
                remain = 0;
                a[i] = 0;
            }
        }
        else if(a[i]>8) {
            given += 8;
            a[i] -= 8;
        }
        else {
            given += a[i];
            a[i] = 0;
        }
        i++;

    }
    if(given<k) printf("-1");
    else printf("%d", i);

    return 0;
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define forn(i,n) for (i = 0; i<n; i++)

int lol(const void *x1, const void *x2)
{
	return *(int*)x1 - *(int*)x2;
}

int gcd(int a, int b)
{
	int c;
	while (b)
	{
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}
int main(void)
{
	int n, k, i, t, sber = 0;
	int day, temp;
	scanf("%i%i", &n, &k);
	forn(i, n)
	{
		scanf("%i", &t);
		if (t > 8)
		{
			sber += t - 8;
			t = 8;
		}
		else
		{
			temp = 8 - t;
			if (sber >= temp)
			{
				t += temp;
				sber -= temp;
			}
			else
			{
				t += sber;
				sber = 0;
			}
		}
		k -= t;
		if (k <= 0)
			day = i + 1;
	}
	if (k > 0)
		day = -1;
	printf("%i", day);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define maxn 105
int a[maxn];
int main()
{
    int n,k,i,count_k=0,days=0,temp=0;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {

        if(a[i]>=8)
        {
            count_k+=8;
            temp=temp+a[i]-8;
        }
        else if(a[i]+temp>=8)
        {
            temp=temp-8;
            count_k+=8;
        }
        else count_k+=a[i];
        days++;
        if(count_k>=k)
        {
            printf("%d\n",days);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}

#include <stdio.h>
int min(int a,int b){return (a<b)?a:b;}
int main()
{
    int i,n,k;
    scanf("%d%d",&n,&k);
    int left=k;
    int total=0;
    for(i=0;i<n;i++)
    {
        int ele;
        scanf("%d",&ele);
        total+=ele;
        left-=min(total,8);
        if(left<=0) break;
    }
    if(left>0) printf("-1\n");
    else printf("%d\n",i+1);
    return 0;
}

#include <stdio.h>
int main()
{
	int n,k,i,a[1000];
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int sum=0;
	for(i=0;i<n;i++)
	{
		if(sum<k&&a[i]<8)
		{
			sum+=a[i];
		}
		else if(sum<k&&a[i]>=8)
		{
			sum+=8;
		}
		if(sum>=k)
			break;
	}
	if(i==n)
		printf("-1\n");
	else
		printf("%d\n",++i);
	return 0;
}

#include<stdio.h>

int main(void) {
	int n, k, sum = 0, rem = 0;
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++) {
		int box;
		scanf("%d", &box);
		if (box > 8) {
			sum += 8;
			rem += box - 8;
		}
		else {
			sum += box;
		}

		if (sum >= k) {
			printf("%d", i);
			return 0;
		}
	}
	printf("-1");
}

#include<stdio.h>
#include<math.h>

int main()
{
    int num,k,i;
    scanf("%d%d",&num,&k);
    int ara[num],sum=0;
    for(i=0;i<num;i++)
       {
           scanf("%d",&ara[i]);
           sum=sum+ara[i];
       }
       if(sum<k) printf("-1\n");
       else if(num*8<k) printf("-1\n");
       else
       {
           for(i=1;i<=num;i++)
           {
               if(ara[i-1]<=8) k=k-ara[i-1];
               else if(ara[i-1]>8)
               {
                   k=k-8;
                   ara[i]+=ara[i-1]%8;
               }
               if(k<=0)
                break;
           }
           printf("%d\n",i);
       }

    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
long long int cmpfunc( const void *a, const void *b) {
  return *(long long int*)a - *(long long int *)b;
}

int main()
{
 long long int test,d,n,x,i,sum=0,count=0,j,flag=0,p,y,m,max=0,o,g,r,k, a[1000000],sum1=0;
scanf("%lld%lld",&n,&m);
count=0;
sum=0;
sum1=0;
for(i=0;i<n;i++){
  scanf("%lld",&a[i]);
  if(a[i]>=8){
      sum+=8;
      sum1+=a[i]-8;
      count++;
  }else{

      sum+=a[i];
      if(8-a[i]<=sum1){
          sum+=8-a[i];
          sum1-=8-a[i];
      }
      count++;
  }
  if(sum>=m){
      break;
  }

}

if(sum>=m){
    printf("%lld",count);
}else{
    printf("-1");
}
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
	int n, k, i, sum = 0;
	scanf("%d%d", &n, &k);
	int x;
	int r = 0;
	for(i = 1; i <= n; i++){
		scanf("%d", &x);
		x = x + r;
		if(x >= 8){
			r = x - 8;
			x = 8;
		}
		sum = sum + x;
		if(sum >= k){
			printf("%d\n", i);
			exit(0);
		}
	}
	printf("-1\n");
	return 0;
}