Problem:
 Score : 100 points    Problem Statement There are N children, numbered 1, 2, \ldots, N .  They have decided to share K candies among themselves.
Here, for each i ( 1 \leq i \leq N ), Child i must receive between 0 and a_i candies (inclusive).
Also, no candies should be left over.  Find the number of ways for them to share candies, modulo 10^9 + 7 .
Here, two ways are said to be different when there exists a child who receives a different number of candies.      Constraints  All values in input are integers.  1 \leq N \leq 100  0 \leq K \leq 10^5  0 \leq a_i \leq K         Input Input is given from Standard Input in the following format:  N  K  a_1  a_2  \ldots  a_N       Output Print the number of ways for the children to share candies, modulo 10^9 + 7 .        Sample Input 1 3 4
1 2 3      Sample Output 1 5  There are five ways for the children to share candies, as follows:   (0, 1, 3)  (0, 2, 2)  (1, 0, 3)  (1, 1, 2)  (1, 2, 1)   Here, in each sequence, the i -th element represents the number of candies that Child i receives.       Sample Input 2 1 10
9      Sample Output 2 0  There may be no ways for the children to share candies.       Sample Input 3 2 0
0 0      Sample Output 3 1  There is one way for the children to share candies, as follows:   (0, 0)        Sample Input 4 4 100000
100000 100000 100000 100000      Sample Output 4 665683269  Be sure to print the answer modulo 10^9 + 7 .

Solution (Python):

N, K = map(int, input().split())
a = tuple(map(int, input().split()))

mod = 1000000007



dp = [[0] * (K + 1) for i in range(N + 1)]

for i in range(N + 1):
  dp[i][0] = 1

for i in range(N):
  for j in range(K):

    if a[i] < j + 1:
      dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j - a[i]]) % mod
    else:
      dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1]) % mod
  
print(dp[N][K])

Solution (Python):
def main():
    MOD = 10**9 + 7
    N, K = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    dp = [[0]*(K+1) for i in range(N+1)]
    dp[0][0] = 1
    S = [0] * (K+2)
    for i in range(1, N+1):
        S[0] = 0
        for j in range(1, K+2):
            S[j] = (S[j-1] + dp[i-1][j-1]) % MOD
        
        for j in range(K+1):
            """
            for k in range(A[i-1]+1):
                
                if j - k >= 0:
                    dp[i][j] = (dp[i][j] + dp[i-1][j-k]) % MOD
            """
            dp[i][j] = (S[j+1] - S[max(0, j-A[i-1])] + MOD) % MOD
    print(dp[N][K])
    
    


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    mod = 7 + 10 ** 9
    DP = [[0 for j in range(K + 1)] for i in range(N)]
    for i in range(A[0] + 1): DP[0][i] = 1
    cum = [0] * (K + 2)
    for i in range(1, N):
        for j in range(1, K + 2): cum[j] = (cum[j-1] + DP[i-1][j-1]) % mod
        for j in range(K + 1): DP[i][j] = (cum[j + 1] - cum[max(0, j - A[i])]) % mod
    print(DP[N-1][K])

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
def main():
    N, K = map(int, input().split())
    if K == 0:
        return 1
    A = list(map(int, input().split()))
    MOD = 10**9+7
    dp = [[0]*(K+1) for _ in range(N+1)]
    dp[0][0] = 1
    for i in range(N):
        a = A[i]
        for j in range(K+1):
            dp[i+1][j] += dp[i][j]
            if j+a+1 <= K:
                dp[i+1][j+a+1] -= dp[i][j]
        for j in range(K):
            dp[i+1][j+1] += dp[i+1][j]
        for j in range(K+1):
            dp[i+1][j] = dp[i+1][j] % MOD

    return dp[N][K]


print(main())

Solution (Python):


N,K=map(int,input().split())
a=list(map(int,input().split()))

mod=10**9+7

dp=[[0]*(K+1) for _ in range(N+1)]
for i in range(N+1):
    dp[i][0]=1


for i in range(N):
    S=1
    for j in range(K):
        S+=dp[i][j+1]
        if j+1>a[i]:
            S-=dp[i][j-a[i]]
        dp[i+1][j+1]=S%mod
        
print(dp[-1][-1])

Solution (Python):
mod = 10**9+7

n,k = map(int, input().split())
a = list(map(int, input().split()))

DP = [0]*(k+1)
DP[0] = 1

for ai in a:
    nxt = [0]*(k+1)
    
    
    s = 0
    for i in range(k+1):
        s += DP[i]
        if i>ai:
            s -= DP[i-ai-1]
        s %= mod
        nxt[i] = s
    DP = nxt
print(DP[-1])

Solution (Python):
MOD = 1000000007

N,K = map(int,input().split())
A = list(map(int,input().split()))

dp = [[0 for j in range(K+1)] for i in range(N+1)]
dp[0][0] = 1

for i in range(1,N+1):
    cum = [0 for j in range(K+2)]
    cum[0] = 0
    for j in range(1,K+2):
        cum[j] = cum[j-1]+dp[i-1][j-1]
        cum[j] %= MOD
    for j in range(K+1):
        dp[i][j] = cum[j+1]-cum[max(0,j-A[i-1])]
        dp[i][j] %= MOD

print(dp[N][K])

Solution (Python):
MOD = 10 ** 9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))

dp = [0 for _ in range(k + 1)]
dp[0] = 1
b = [1 for _ in range(k + 2)]
b[0] = 0

for ai in a:
    for i in range(k, -1, -1):
        dp[i] += (b[i] - b[i - min(ai, i)]) % MOD
        dp[i] %= MOD
    for i in range(1, k + 2):
        b[i] = (b[i - 1] + dp[i - 1]) % MOD
        b[i] %= MOD
print(dp[-1])

Solution (Python):
n,k = map(int,input().split())
al = list(map(int,input().split()))


dp = [[0 for j in range(k+1)] for i in range(n+1)]


for i in range(n+1):
    dp[i][0] = 1
MOD = 10**9 + 7

for i in range(1,n+1):
    for j in range(1,k+1):
        if j-al[i-1]-1 >= 0:
            dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-al[i-1]-1]
            dp[i][j] %= MOD
        else:
            dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
            dp[i][j] %= MOD
print(dp[n][k])

Solution (Python):
n,k=map(int,input().split())
A=list(map(int,input().split()))
mod=10**9+7
dp=[0]*(k+2)
dp[0]=1
for a in A:
    for i in range(1,k+1):
        dp[i]+=dp[i-1]
    for i in range(k,0,-1):
        dp[i]=(dp[i]-dp[max(0,i-a)-1])%mod
print(dp[k])

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
Acum = 0
mod = 10 ** 9 + 7


dp = [0]*(K+1)
dp[0] = 1

for a in A:
    Acum += a
    newDP = [0]*(K+1)
    for i in range(min(Acum, K) + 1):
        if i == 0:
            newDP[i] = 1
            continue
        dp[i] += dp[i - 1]
        newDP[i] = dp[i]
        if i > a:
            newDP[i] -= dp[i - a - 1]
        newDP[i] %= mod
    dp = newDP[::]

ans = dp[K]
print(ans)

Solution (Python):
def examA():
    N = I()
    H = LI()
    dp = [inf]*(N)
    dp[0] = 0
    for i in range(N-1):
        dp[i+1] = min(dp[i+1],dp[i]+abs(H[i+1]-H[i]))
        if i<N-2:
            dp[i + 2] = min(dp[i + 2], dp[i]+abs(H[i + 2] - H[i]))
    ans = dp[-1]
    print(ans)
    return

def examB():
    N, K = LI()
    H = LI()
    dp = [inf]*N
    dp[0] = 0
    for i in range(N):
        for k in range(1,K+1):
            if i+k>=N:
                break
            if dp[i+k]>dp[i]+abs(H[i+k]-H[i]):
                dp[i+k] = dp[i]+abs(H[i+k]-H[i])
    ans = dp[-1]
    print(ans)
    return

def examC():
    N = I()
    A = [0]*N; B = [0]*N; C = [0]*N
    for i in range(N):
        A[i],B[i],C[i] = LI()
    dp = [[0]*3 for _ in range(N+1)]
    for i in range(N):
        dp[i+1][0] = max(dp[i][1],dp[i][2]) + A[i]
        dp[i + 1][1] = max(dp[i][0], dp[i][2]) + B[i]
        dp[i + 1][2] = max(dp[i][1], dp[i][0]) + C[i]
    ans = max(dp[N])
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    
    S = SI(); N = len(S)
    T = SI(); M = len(T)
    dp = [[0] * (M + 1) for _ in range(N + 1)]
    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1)
            dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j], dp[i][j+1])

    i = N; j = M
    ans = []
    while i > 0 and j > 0:
        if dp[i][j] == dp[i-1][j]:
            i -= 1
        elif dp[i][j] == dp[i][j-1]:
            j -= 1
        else:
            ans.append(S[i-1])
            i -= 1
            j -= 1
    print("".join(ans[::-1]))
    return

def examG():
    
    
    def topological_sort(n,outs,ins):
        q = deque(v1 for v1 in range(n) if ins[v1] == 0)
        res = []
        while q:
            v1 = q.popleft()
            res.append(v1)
            for v2 in outs[v1]:
                ins[v2] -= 1
                if ins[v2] == 0:
                    q.append(v2)
        return res
    N, M = LI()
    V = [[]for _ in range(N)]
    start = [0]*N
    for _ in range(M):
        x,y = LI()
        x -= 1; y -= 1
        V[x].append(y)
        start[y] += 1
    Order = topological_sort(N, V, start)

    dp = [0]*N
    for i in Order:
        for v in V[i]:
            dp[v] = max(dp[v],dp[i]+1)
    ans = max(dp)
    print(ans)
    return

def examH():
    H, W = LI()
    A = [SI() for _ in range(H)]
    dp = [[0]*W for _ in range(H)]
    dp[0][0] = 1
    for i in range(H):
        for j in range(W):
            if A[i][j]=="
                continue
            dp[i][j] %=mod
            if i+1<H and dp[i+1][j]!="
                dp[i+1][j] +=dp[i][j]
            if j+1<W and dp[i][j+1]!="
                dp[i][j+1] +=dp[i][j]
    ans = dp[-1][-1]
    print(ans)
    return

def examI():
    N = I()
    P = LFI()
    dp = [[0]*(N+1) for _ in range(N+1)]
    dp[0][0] = 1
    for i in range(N):
        for j in range(i+1):
            dp[i+1][j+1] += dp[i][j]*P[i]
            dp[i+1][j] += dp[i][j]*(1-P[i])
    ans = 0
    for v in dp[N][(1+N//2):]:
        ans += v
    print(ans)
    return

def examJ():
    N = I()
    A = LI()
    dp = [[[0]*(N+1) for _ in range(N+1)]for _ in range(N+1)]
    S = Counter(A)
    print(S)
    dp[S[1]][S[2]][S[3]] = 0
    loop = S[1] + S[2]*2 + S[3]*3
    que = deque()
    que.append((S[1],S[2],S[3]))
    used = defaultdict(set)
    while(que):
        n1,n2,n3 = que.popleft()
        if used[(n1,n2,n3)]:
            continue
        used[(n1,n2,n3)] = True
        now = n1+n2+n3
        if n1>0:
            dp[n1-1][n2][n3] += dp[n1][n2][n3] + N/now * n1/now
            que.append((n1-1, n2, n3))
        if n2>0:
            dp[n1+1][n2-1][n3] += dp[n1][n2][n3] + N/now * n2/now
            que.append((n1+1,n2-1,n3))
        if n3>0:
            dp[n1][n2+1][n3-1] += dp[n1][n2][n3] + N/now * n3/now
            que.append((n1,n2+1,n3-1))

    ans = dp[0][0][0]
    print(ans)
    return

def examK():
    N, K = LI()
    A = LI()
    return

def examM():
    N, K = LI()
    A = LI()
    dp = [[0]*(K+1) for _ in range(N+1)]
    dp[0][0] = 1
    for i in range(N):
        cum = [0]*(K+1)
        cum[0] = dp[i][0]
        for c in range(K):
            cum[c+1] = cum[c] + dp[i][c+1]
        for j in range(K+1):
            if j<=A[i]:
                dp[i+1][j] = cum[j]
            else:
                dp[i+1][j] = cum[j] - cum[j-A[i]-1]
            dp[i+1][j] %=mod

    ans = dp[N][K]%mod
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examM()

"""

"""

Solution (Python):
n, k  = map(int, input().split())
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7


dp = [[0] * (ｋ + 1) for _ in range(n + 1)]

for i in range(n + 1):
  dp[i][0] = 1
  
for i in range(1, n + 1):
  for j in range(1, k + 1):
    if j-1-A[i-1] >= 0:
      dp[i][j] = (dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1-A[i-1]]) % MOD
    else:
      dp[i][j] = (dp[i-1][j] + dp[i][j-1]) % MOD

ans = dp[n][k]
print(ans)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

dp = [[0]*(K+1) for i in range(N)]
for i, a in enumerate(A):
  if i == 0:
    for j in range(a+1):
      dp[0][j] = 1
  
  else:
    dp[i][0] = 1
    for j in range(1, K+1):
      dp[i][j] = dp[i][j-1] + dp[i-1][j]
      if j > a:
        dp[i][j] -= dp[i-1][j-a-1]

      dp[i][j] %= MOD

print(dp[N-1][K])

Solution (Python):
N, K = map(int, input().split())
*a, = map(int, input().split())
MOD = 10**9+7

dp = [0]*(K+1)
sums = [0]*(K+2)

dp[0] = 1

for i in range(1, N+1):
    sums[0] = 0
    for j in range(1, K+2):
        sums[j] = (dp[j-1]+sums[j-1]) % MOD

    for j in range(1, K+1):
        sj = max([0, j-a[i-1]])
        dp[j] = (sums[j+1]-sums[sj]+MOD) % MOD
print(dp[K])

Solution (Python):
N, K = map(int, input().split())
*a, = map(int, input().split())
MOD = 10**9+7

dp = [[0]*(K+1) for _ in range(N+1)]
for i in range(N+1):
    dp[i][0] = 1

sums = [0]*(K+2)
for i in range(1, N+1):
    sums[0] = 0
    for j in range(1, K+2):
        sums[j] = (dp[i-1][j-1]+sums[j-1]) % MOD

    for j in range(1, K+1):
        sj = max([0, j-a[i-1]])
        dp[i][j] = (sums[j+1]-sums[sj]+MOD) % MOD

print(dp[N][K])

Solution (Python):
N, K = map(int, input().split())
M=10**9+7
As = list(map(int, input().split()))
dp = [[0]*(K+1)for _ in range(N+1)]
dp2 = [[1]*(K+1)for _ in range(N+1)]
dp[0][0] = 1

for i in range(1, N+1):
    for j in range(K+1):

        dp[i][j] = 1 if j==0 else (dp[i][j-1] - dp2[i][j-1] + dp[i-1][j])%M
        dp2[i][j] = 0 if j-As[i-1] <0 else dp[i-1][j-As[i-1]]%M




print(dp[N][K])

Solution (Python):
def candies(k, a):
    mod = 10 ** 9 + 7
    cur = [0] * (k + 1)
    cur[0] = 1
    prefix = cur[:]
    nxt = cur[:]
    for x in a:
        for i in range(1, k + 1):
            prefix[i] = (prefix[i - 1] + cur[i]) % mod
        for i in range(1, k + 1):
            
            nxt[i] = (prefix[i] - (prefix[i - x - 1] if i - x - 1 >= 0 else 0)) % mod
        cur, nxt = nxt, cur
    return cur[k]


def main():
    n, k = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    return candies(k, a)


print(main())

Solution (Python):
div = 10**9 + 7

n,k = map(int, input().split())
a = list(map(int, input().split()))

dp = [[0] * (n+1) for _ in range(k+1)]
dp[0] = [1]*(n+1)

for i in range(1,k+1):
    for j in range(1, n+1):
        if(i <= a[j-1]):
            dp[i][j] = (dp[i-1][j] + dp[i][j-1])%div
        else:
            dp[i][j] = (dp[i-1][j] + dp[i][j-1] - dp[i - a[j-1] - 1][j -1])%div

print(dp[k][n] % div)

Solution (Python):
def wczytaj_liste(): 
    wczytana_lista = input()
    lista_znakow = wczytana_lista.split()
    ostateczna_lista = []
    for element in lista_znakow:
        ostateczna_lista.append(int(element))
    return ostateczna_lista 

def ciasteczka(): 
    N, K = wczytaj_liste()
    max_cukierkow = wczytaj_liste()
    przed = [1]
    for dziecko in max_cukierkow:
        po = [None]* min(len(przed)+ dziecko, K+1)
        suma_kroczaca = 0
        for i in range(len(po)):
            if len(przed) > i: 
                suma_kroczaca += przed[i]
            if i-dziecko-1 > -1:
                suma_kroczaca -= przed[i-dziecko-1]
            suma_kroczaca %= 10**9+7
            po[i] = suma_kroczaca
        przed = po
    if K < len(przed):
        print(przed[K])
    else:
        print(0)
    
ciasteczka()

Solution (Python):
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
mod = 10**9 + 7

dp = [[0 for _ in range(k+1)] for __ in range(n)]

sm = 0

for i in range(n):
    sm += a[i]
    for j in range(k+1):
        if j == 0:
            dp[i][j] = 1
            continue
        if sm < j:
            dp[i][j] = 0
        else:
            if j-1-a[i] >= 0:
                dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1-a[i]]
            else:
                dp[i][j] = dp[i][j-1] + dp[i-1][j]
            dp[i][j] %= mod

print(dp[n-1][k])

Solution (Python):
import sys
sys.setrecursionlimit(2 * 10**5)
mod = 10 ** 9 + 7


def main():
    N, K = map(int, input().split())
    A = [int(x) for x in input().split()]
    dp = [0] * (K + 1)
    dp[0] = 1
    for i in range(N):
        acc = [0]
        for x in dp:
            acc.append((acc[-1] + x) % mod)
        dp_ = []
        for j in range(K+1):
            dp_.append((acc[j+1] - acc[max(0, j-A[i])] + mod) % mod)
        dp = dp_
    print(dp[K])


if __name__ == '__main__':
    main()

Solution (Python):

import sys
from itertools import accumulate
INF = float("inf")

MOD = 1000000007  


def solve(N: int, K: int, a: "List[int]"):

    dp = [[0]*(K+1) for _ in range(N+1)]
    dp[0][0] = 1
    for i in range(1, N+1):
        acc = [0] + list(accumulate(dp[i-1]))
        for j in range(K+1):
            dp[i][j] = acc[j+1] - acc[max(j-a[i-1], 0)]
            dp[i][j] %= MOD
    print(dp[N][K])
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, a)


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10**9 + 7

dp = [[0]*(k+1) for i in range(n+1)]
dp[0][0] = 1
ruiseki = [0] * (k+2)
for j in range(k+1):
    ruiseki[j+1] = ruiseki[j] + dp[0][j]

for i in range(n):
    for j in range(k+1):
        dp[i+1][j] = ruiseki[j+1] - ruiseki[max(j-a[i], 0)]
        dp[i+1][j] %= MOD
    ruiseki = [0] * (k+2)
    for j in range(k+1):
        ruiseki[j+1] = ruiseki[j] + dp[i+1][j]

print(dp[n][k])

Solution (Python):
def main(sample_file = ''):

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter 
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)
    def make_divisors(n, reversed=False):
        """create list of divisors
        
        Args:
            number (int): number from which list of divisors is created
            reversed (bool, optional): ascending order if False. Defaults to False.
        
        Returns:
            list: list of divisors
        """
        divisors = set()
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divisors.add(i)
                divisors.add(n//i)
        return sorted(list(divisors),reverse=reversed)
    
    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    
    
    
    
    

    def BellmanFord(N, M, ABC, vertex_start, vertex_end, value_if_inf = -1, find_shortest = False):
        """to calculate furthest or shortest length between vertex_start and vertex_end using BellmanFord algorithm
        
        Args:
            N (int): number of vertices
            M (int): number of edges
            ABC (list): [(ai, bi, ci) for _ in range(N)] where i-th edge is directed from vertex ai to vertex bi and the length is ci 
            vertex_start (int): start vertex. usually use 0.
            vertex_end (int): end vertex. usually use N-1.
            value_if_inf (int or string as you like, optional): value you want when the furthest (or shortest) distance is infinite (or -infinite). Defaults to -1.
            find_shortest (bool, optional): choose False to find furthest path. Defaults to False.
        
        Returns:
            int or string: normally int (but can be str if you set value_if_inf to str)
            
        Example:
                N, M, P = R()
                ABC = [R() for _ in range(M)]
                ABC = [(a-1, b-1, c-P) for a, b, c in ABC]
                print(BellmanFord(N, M, ABC, 0, N-1, value_if_inf = 'inf'))

        """

        def make_reachable_list(N, M, ABC, vertex_start, vertex_end):
            reachable_to_direct = defaultdict(list)
            reachable_from_direct = defaultdict(list)
            reachable_from_start = [False] * N
            reachable_to_end = [False] * N
            reachable_from_start[vertex_start] = True
            reachable_to_end[vertex_end] = True
            reachable_from_both_sides = [False] * N
            dfs_from_start = []
            dfs_to_end = []
            for a, b, c in ABC:
                reachable_to_direct[a].append(b)
                reachable_from_direct[b].append(a)
                if a == vertex_start:
                    dfs_from_start.append(b)
                    reachable_from_start[b] = True
                if b == vertex_end:
                    dfs_to_end.append(a)
                    reachable_to_end[a] = True
            while dfs_from_start:
                v = dfs_from_start.pop()
                for i in reachable_to_direct[v]:
                    if not reachable_from_start[i]:
                        reachable_from_start[i] = True
                        dfs_from_start.append(i)
            while dfs_to_end:
                v = dfs_to_end.pop()
                for i in reachable_from_direct[v]:
                    if not reachable_to_end[i]:
                        reachable_to_end[i] = True
                        dfs_to_end.append(i)
            for i in range(N):
                reachable_from_both_sides[i] = reachable_from_start[i] and reachable_to_end[i]
            return reachable_from_both_sides
        
        reachable_from_both_sides = make_reachable_list(N, M, ABC, vertex_start, vertex_end)
        
        if find_shortest:
            dist = [INF for i in range(N)]
        else:
            dist = [-INF for i in range(N)]
        
        dist[vertex_start] = 0
        for i in range(N):
            updated = False
            for a, b, c in ABC:
                if not reachable_from_both_sides[a]:
                    continue
                elif find_shortest:
                    update_condition = dist[a] + c < dist[b]
                else:
                    update_condition = dist[a] + c > dist[b]
                if dist[a] != INF and update_condition:
                    dist[b] = dist[a] + c
                    updated = True
                    if i == N-1:
                        return value_if_inf
            if not updated:
                break
        return dist[vertex_end]

    """ initialize variables and set inputs
    
        
        
            
            
    
        
        
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    r_float = lambda: float(r())
    R = lambda: list(map(int, r().split()))
    R_map = lambda: map(int, r().split())
    R_float = lambda: list(map(float, r().split()))
    R_tuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    
    if sample_file:
        sys.stdin = open(sample_file)
        
    
    

    N, K = R()
    A = R()
    
    dp = [[0]*(N+1) for _ in range(K+1)]
    for i in range(N+1):
        dp[0][i] = 1
    for j, a in enumerate(A):
        for i in range(K):
            if (i)-a >= 0:
                dp[i+1][j+1] = (dp[i][j+1] + dp[i+1][j] - dp[i-a][j]) % (10**9+7)
            else:
                dp[i+1][j+1] = (dp[i][j+1] + dp[i+1][j]) % (10**9+7)
    print(dp[K][N])

    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """
    
    """memo: how to solve the problem
    creating simple test/answer
    greed
    simple dp
    graph
    """

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9+7

dp = [[0 for _ in range(K+1)] for _ in range(N+1)]
dp[0][0] = 1
dp2 = [[0 for _ in range(K+2)] for _ in range(N)]
for i in range(N):
    for j in range(K+1):
        dp2[i][j+1] = (dp2[i][j] + dp[i][j]) % mod
    for j in range(K+1):
        dp[i+1][j] = (dp[i+1][j] + (dp2[i][j+1] - dp2[i][max(j - a[i], 0)])%mod) % mod
print(dp[-1][-1])

Solution (Python):
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

mod = 10**9+7

dp = [[0]*(K+1) for _ in range(N+1)]
dp[0][0] = 1
    
for i in range(N):
    S = [0]*(K+2)
    for j in range(K+1):
        S[j+1] = S[j] + dp[i][j]
        S[j+1] %= mod
    for j in range(K+1):
        dp[i+1][j] = S[j+1]-S[max(j-A[i], 0)]
        dp[i+1][j] %= mod
        
print(dp[N][K])

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=10**9+7
dp=[0]*(k+1)
for i in range(a[0]+1):
    dp[i]=1
for i in range(1,n):
    u=a[i]
    h=dp[:]
    for j in range(u+1,k+1):
        h[j]=(dp[j]-dp[j-u-1])%mod
    for j in range(1,k+1):
        h[j]=(h[j]+h[j-1])%mod
    dp=h
print(dp[k])