n=int(input())
a=[int(i) for i in input().split()]
k=int(input())
if(sum(a)<k):
    print('-1')
else:
    x,y = 0,n-1
    count = 0
    while(x <= y):
        if(a[x]>=a[y] and k>0):
            if(k-a[x] >= 0):
                k -= a[x]
                count += 1
                x += 1
                # print('1')
            elif(k-a[y] >= 0):
                k -= a[y]
                count += 1
                y -= 1
                # print('11')
        elif(a[x]<a[y] and k>0):
            if(k-a[y] >= 0):
                k -= a[y]
                count += 1
                y -= 1
                # print('111')
            elif(k-a[x] >= 0):
                k -= a[x]
                count += 1
                x += 1
                # print('1111')
        else:
            break
    print(count)                
            

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: