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.



Sample input:
3 4
1 2 3


Sample output:
5

Submission status: Time Limit Exceeded

Solution code:
def main():
  e,M=enumerate,10**9+7
  n,k,*a=map(int,open(0).read().split())
  dp=[[0]*(k+1)for _ in range(n+1)]
  dp[0][0]=1
  for b,d,a in zip(dp,dp[1:],a):
    a+=1
    t=0
    for j,v in e(b[:a]):
      t+=v
      t%=M
      d[j]=t
    for j,(w,v)in e(zip(b,b[a:]),a):
      t+=v-w
      t%=M
      d[j]=t
  print(t)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  e,M=enumerate,10**9+7
  n,k,*a=map(int,open(0).read().split())
  dp=[[0]*(k+1)for _ in range(n+1)]
  dp[0][0]=1
  for i,(b,d,a)in e(zip(dp,dp[1:],a),1):
    t=0
    for j,v in e(b[:a+1]):
      t+=v
      t%=M
      d[j]=t
    for j,(w,v)in e(zip(b,b[a+1:]),a+1):
      t+=v-w
      t%=M
      d[j]=t
  print(t)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  e,M=enumerate,10**9+7
  n,k,*a=map(int,open(0).read().split())
  dp=[[0]*(k+1)for _ in range(n+1)]
  dp[0][0]=1
  for i,(b,d,a)in e(zip(dp,dp[1:],a),1):
    t=0
    for j,v in e(b):
      t+=v
      if j>a:t-=b[~a+j]
      t%=M
      d[j]=t
  print(t)
main()

Submission status: Time Limit Exceeded

Solution code:
e,M=enumerate,10**9+7
n,k,*a=map(int,open(0).read().split())
dp=[[0]*(k+1)for _ in range(n+1)]
dp[0][0]=1
for i,(b,d,a)in e(zip(dp,dp[1:],a),1):
  t=0
  for j,v in e(b):
    t+=v
    if j>a:t-=b[~a+j]
    t%=M
    d[j]=t
print(t)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    k, a = read()
    result = think(k, a)
    write(result)


def read():
    n, k = read_int(2)
    return k, read_int(n)


def read_int(n):
    return list(map(int, read_line().split(' ')))[:n]


def read_float(n):
    return list(map(float, read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(k, a):
    mode = 10 ** 9 + 7
    n = len(a)

    # dp[i][j], means ways to count, for from 0th to ith child, candies num is j
    # dp[i][j] = sum(dp[i - 1][j - k]), which k is 0 to a[i]
    dp = [[0 for x in range(k + 1)] for y in range(n)]
    dp[0][0] = 1

    accumulated_a = [0 for x in range(n)]
    accumulated_a[0] = a[0]
    for i in range(1, n):
        accumulated_a[i] = accumulated_a[i - 1] + a[i]

    if accumulated_a[n - 1] < k:
        return 0

    for i in range(n):
        for j in range(min(accumulated_a[i] + 1, k + 1)):
            if i == 0 and j == 0:
                continue
            if i == 0:
                if j <= a[i]:
                    dp[i][j] = 1
            else:
                for c in range(a[i] + 1):
                    if j - c >= 0:
                        dp[i][j] += dp[i - 1][j - c]
                        dp[i][j] %= mode

    return dp[n - 1][k]

def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, raw_input().split())
a = map(int, raw_input().split())

dp = [[0]*(K+1) for _ in xrange(N+1)]
dp[0][0] = 1

for i in xrange(N):
    for j in xrange(K+1):
        for k in xrange(a[i]+1):
            if j-k>=0:
                dp[i+1][j] += dp[i][j-k]

print dp[-1][-1] % 1000000009

Submission status: Time Limit Exceeded

Solution code:
import sys
MAX_INT = int(10e2)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
a = IL()

dp = [[0]*(K+1) for i in range(N+1)] # dp[i人までの子どもが][j個のアメを配る]
Cs = [[0]*(K+1) for i in range(N+1)] # 累積和

aa = a[0]
num = 0
for i in range(K+1):
  if i <= aa:
    dp[1][i] = 1
    num += 1
  Cs[1][i] = num
for i in range(2,N+1):
  num = 0
  for j in range(K+1):
    if j-a[i-1]-1 < 0:
      dp[i][j] = Cs[i-1][j]
    else:
      dp[i][j] = Cs[i-1][j] - Cs[i-1][j-a[i-1]-1]
    num += dp[i][j]
    Cs[i][j] = num

print(dp[N][K] %mod)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random 

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N, K = LI()
a = LI()
"""

dp = np.zeros(K+1, dtype=np.int64)
dp[0:a[0]+1] = 1

for i in range(1,N):
    acc = np.cumsum(dp)
    for j in range(K+1)[::-1]:
        if (j-a[i]<=0):
            dp[j] = acc[j]%mod
        else:
            dp[j] = (acc[j] - acc[j-a[i]-1])%mod

print(dp[K])

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

for i in range(K+1):
    if (i > a[0]):
        break
    dp[i] =  1

#acc = [0]*(K+1)


for i in range(1,N):
    #for i in range(K):
        #acc[i+1] = acc[i] + dp[i]
    #print(acc)
    for j in range(K+1)[::-1]:
        res = 0
        #res = acc[j] - acc[max(j-a[i-1], 0)]
        for k in range(a[i]+1):
            if (k>j):
                break
            res += dp[j-k]
        dp[j] = res%mod

print(dp[K])

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random 
import numpy as np 
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N, K = LI()
a = LI()


dp = np.zeros(K+1, dtype=np.int64)
dp[0:a[0]+1] = 1

for i in range(1,N):
    acc = np.cumsum(dp)
    for j in range(K+1)[::-1]:
        if (j-a[i]<=0):
            dp[j] = acc[j]%mod
        else:
            dp[j] = (acc[j] - acc[j-a[i]-1])%mod

print(dp[K])

Submission status: Time Limit Exceeded

Solution code:
mod = 10**9 + 7

def accmulate(array):
    cs = [0]*(len(array)+1)
    for i in range(len(array)):
        cs[i+1] = cs[i] + array[i]
    return cs

def query(l,r):
    return cs[r+1] - cs[l]

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

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

dp[0][0] = 1

for i in range(1,N+1):
    cs = accmulate(dp[i-1])
    for j in range(K+1):
        dp[i][j] += query(max(j-A[i-1], 0), j)
        dp[i][j] %= mod

ans = dp[N][K]
print(ans)

Submission status: Time Limit Exceeded

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

memo = []
for i in range(K+1):
    l = [None] * N
    memo.append(l)

def rec(remaining, i):
    if i == N:
        if remaining > 0:
            return 0
        else:
            return 1

    if memo[remaining][i] != None:
        return memo[remaining][i]

    a = A[i]
    max_take = min(remaining, a)

    res = 0
    for take in reversed(range(0, max_take+1)):
        assert(remaining >= take)
        res += rec(remaining-take, i+1)

    if memo[remaining][i] == None:
        memo[remaining][i] = res

    return res

print(rec(K, 0) % 1000000007)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

BIG_INT = 10 ** 9 + 7

def dpcalc(N, K):
    dp = [[0] * (K +1) for _ in range(N + 1)]
    dp[0][0] = 1
    rs = [0] * (K + 2)
    
    for j in range(1, N + 1):
        rs[0] = 0
        for i in range(1, K + 2):
            rs[i] = rs[i-1] + dp[j-1][i-1]
        # print(rs)    
        for i in range(K+1):
            dp[j][i] = rs[i+1] -rs[max(0, i-A[j-1])]

    # print(dp)
    return dp[N][K]


N, K = map(int, input().split())
A = [int(x) for x in input().split()]


print(dpcalc(N, K)  % BIG_INT)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

BIG_INT = 10 ** 9 + 7

def dpcalc(n, k):
    if dp[n][k] != -1:
        return dp[n][k]
    
    res = 0
    for i in range(min(k, A[n-1]) + 1):
        res += dpcalc(n - 1, k - i)
    
    dp[n][k] = res
    return res


N, K = map(int, input().split())
A = [int(x) for x in input().split()]

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

print(dpcalc(N, K)  % BIG_INT)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
dp = [[0] * (n + 1) for _ in range(k + 1)]

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

for i in range(1, k + 1):
    for j in range(2, n + 1):
        for w in range(min(a[j - 1], i) + 1):
            dp[i][j] += dp[i - w][j - 1]
        dp[i][j] %= (10 ** 9 + 7)
print(dp[k][n])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, k = map(int, input().split())
a = list(map(int, input().split()))
dp = np.zeros((k + 1, n + 1))

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

for i in range(1, k + 1):
    for j in range(2, n + 1):
        dp[i][j] = sum(dp[max(0, i - a[j - 1]):i + 1, j - 1]) % (10 ** 9 + 7)
print(int(dp[k][n]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def f(n, k, a_s):
    md = 10 ** 9 + 7
    dp=np.zeros((n,k+1),dtype="int64")
    dp[0][0] = 1
    dpi = dp[0]
    for i, a in enumerate(a_s[:-1]):
        dpi1 = dp[i + 1]
        dpi1[0] = s = dp[i][0] % md
        for j in range(1, a + 1):
            dpi1[j] = s = (s + dpi[j]) % md
        for j in range(a + 1, k + 1):
            dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md
        dpi = dpi1
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(n, k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(n, k, a_s):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    dpi = dp[0]
    for i, a in enumerate(a_s[:-1]):
        dpi1 = dp[i + 1]
        dpi1[0] = s = dp[i][0] % md
        for j in range(1, a + 1):
            dpi1[j] = s = (s + dpi[j]) % md
        for j in range(a + 1, k + 1):
            dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md
        dpi = dpi1
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(n, k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(k, a_s):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    dpi = dp[0]
    for dpi1, a in zip(dp[1:],a_s[:-1]):
        dpi1[0] = s = dpi[0] % md
        for j in range(1, a + 1):
            dpi1[j] = s = (s + dpi[j]) % md
        for j in range(a + 1, k + 1):
            dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md
        dpi=dpi1
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(n, k, a_s):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    dpi = dp[0]
    for i, a in enumerate(a_s[:-1]):
        dpi1 = dp[i + 1]
        dpi1[0] = dp[i][0] % md
        for j in range(1, a + 1):
            dpi1[j] = (dpi1[j - 1] + dpi[j]) % md
        for j in range(a + 1, k + 1):
            dpi1[j] = (dpi1[j - 1] + dpi[j] - dpi[j - a - 1]) % md
        dpi = dpi1
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(n, k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(k):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    dpi = dp[0]
    for i, a in enumerate(a_s[:-1]):
        dpi1 = dp[i + 1]
        dpi1[0] = s = dp[i][0] % md
        for j in range(1, a + 1):
            dpi1[j] = s = (s + dpi[j]) % md
        for j in range(a + 1, k + 1):
            dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md
        dpi=dpi1
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(k))

Submission status: Time Limit Exceeded

Solution code:
def f(k, a_s):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    dpi = dp[0]
    for i, a in enumerate(a_s[:-1]):
        dpi1 = dp[i + 1]
        dpi1[0] = s = dp[i][0] % md
        for j in range(1, a + 1):
            dpi1[j] = s = (s + dpi[j]) % md
        for j in range(a + 1, k + 1):
            dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md
        dpi=dpi1
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(k, a_s):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    for i, a in enumerate(a_s[:-1]):
        dpi1 = dp[i + 1]
        dpi = dp[i]
        dpi1[0] = s = dp[i][0] % md
        for j in range(1, a + 1):
            dpi1[j] = s = (s + dpi[j]) % md
        for j in range(a + 1, k + 1):
            dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(k, a_s):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    for i, a in enumerate(a_s[:-1]):
        dp[i + 1][0] = s = dp[i][0]% md
        for j in range(1, a + 1):
            dp[i + 1][j] = s = (s + dp[i][j])% md
        for j in range(a + 1, k + 1):
            dp[i + 1][j] = s = (s + dp[i][j] - dp[i][j - a - 1])% md
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(k, a_s):
    md = 10 ** 9 + 7
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][0] = 1
    for i, a in enumerate(a_s[:-1]):
        dp[i + 1][0] = s = dp[i][0]
        for j in range(1, a + 1):
            dp[i + 1][j] = s = s + dp[i][j]
        for j in range(a + 1, k + 1):
            dp[i + 1][j] = s = s + dp[i][j] - dp[i][j - a - 1]
    return sum(dp[-1][k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(k, a_s))

Submission status: Time Limit Exceeded

Solution code:
def f(k, a_s):
    md = 10 ** 9 + 7
    dp = [0] * (k + 1)
    dp[0] = 1
    for a in a_s[:-1]:
        dp[-1], R = sum(dp[k - a:]) % md, dp[-1]
        for j in range(k - 1, a - 1, -1):
            dp[j], R = (dp[j + 1] - R + dp[j - a]) % md, dp[j]
        for j in range(a - 1, -1, -1):
            dp[j], R = (dp[j + 1] - R) % md, dp[j]
    return sum(dp[k - a_s[-1]:]) % md


n, k = map(int, input().split())
a_s = list(map(int, input().split()))
print(f(k, a_s))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
dp = []
for i in range(N):
  L = [0]*(K+1)
  dp.append(L)
for i in range(A[0]+1):
  dp[0][i] = 1
for i in range(1,N):
  cur = [0]*(K+2)
  for j in range(K+1):
    cur[j+1] = (cur[j]+dp[i-1][j])%(10**9+7)
  for k in range(K+1):
    if k > A[i]:
      dp[i][k] = cur[k+1]-cur[k-A[i]]
    else:
      dp[i][k] = cur[k+1]
print(dp[N-1][K]%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
dp = []
for i in range(N):
  L = [0]*(K+1)
  dp.append(L)
for i in range(A[0]+1):
  dp[0][i] = 1
for i in range(1,N):
  cur = [0]*(K+2)
  for j in range(K+1):
    cur[j+1] = cur[j]+dp[i-1][j]
  for k in range(K+1):
    if k > A[i]:
      dp[i][k] = cur[k+1]-cur[k-A[i]]
    else:
      dp[i][k] = cur[k+1]
print(dp[N-1][K]%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

from itertools import accumulate as accum
import sys
input = sys.stdin.readline

def f(n, k, ai):
    mod = int(1e9 + 7)
    dp = [0] * (k + 1)
    dp[0] = 1
    for a in ai:
        # csum: Cumulative sum
        # dp[j] = dp[0] + dp[1] + ... + dp[j]
        dp = list(accum(dp, lambda x, y: (x + y) % mod))
        # j = 0 ～ a[i] までは累積和の結果そのままで a[i] + 1 以降は
        # dp[j] -= dp[j] - dp[j - a[i] - 1]
        dp[a + 1:] = [x - y for x, y in zip(dp[a + 1:], dp)]
    return(dp[k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
print(f(n, k, a))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

from itertools import accumulate as accum
import sys
input = sys.stdin.readline

def f(n, k, ai):
    mod = int(1e9 + 7)
    dp = [0] * (k + 1)
    dp[0] = 1
    for a in ai:
        # csum: Cumulative sum
        # dp[j] = dp[0] + dp[1] + ... + dp[j]
        dp = [x % mod for x in accum(dp, lambda x, y: (x + y) % mod)]
        # j = 0 ～ a[i] までは累積和の結果そのままで a[i] + 1 以降は
        # dp[j] -= dp[j] - dp[j - a[i] - 1]
        dp[a + 1:] = [x - y for x, y in zip(dp[a + 1:], dp)]
    return(dp[k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
print(f(n, k, a))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from itertools import accumulate as accum
import sys
input = sys.stdin.readline

def f(n, k, a):
    mod = int(1e9 + 7)
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(n + 1):
        dp[i][0] = 1
    for i in range(1, n + 1):
        # csum: Cumulative sum
        # csum[j + 1] = dp[i-1][0] + dp[i-1][1] + ... + dp[i-1][j]
        csum = [0] + [x % mod for x in accum(dp[i - 1])]
        dpi = dp[i]
        for j in range(1, k + 1):
            l = 0 if j < a[i] else j - a[i]
            dpi[j] = csum[j + 1] - csum[l]
            if dpi[j] < 0:
                dpi[j] += mod
    return(dp[n][k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
a.insert(0, 0)
print(f(n, k, a))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

def f(n, k):
    global a
    mod = int(1e9 + 7)
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(n + 1):
        dp[i][0] = 1
    for i in range(1, n + 1):
        # csum: Cumulative sum
        # csum[j + 1] = dp[i-1][0] + dp[i-1][1] + ... + dp[i-1][j]
        csum = [0] + list(dp[i - 1])
        for j in range(k + 1):
            csum[j + 1] += csum[j]
            csum[j + 1] %= mod
        dpi = dp[i]
        for j in range(1, k + 1):
            l = 0 if j < a[i] else j - a[i]
            dpi[j] = csum[j + 1] - csum[l]
            if dpi[j] < 0:
                dpi[j] += mod
    return(dp[n][k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
a.insert(0, 0)
print(f(n, k))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

def f(n, k):
    global a
    mod = int(1e9 + 7)
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(n + 1):
        dp[i][0] = 1
    for i in range(1, n + 1):
        # csum: Cumulative sum
        # csum[j + 1] = dp[i-1][0] + dp[i-1][1] + ... + dp[i-1][j]
        csum = [0] + list(dp[i - 1])
        for j in range(k + 1):
            csum[j + 1] += csum[j]
            csum[j + 1] %= mod
        dpi = dp[i]
        for j in range(1, k + 1):
            dpi[j] = csum[j + 1] - csum[max(0, j - a[i])]
            if dpi[j] < 0:
                dpi[j] += mod
    return(dp[n][k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
a.insert(0, 0)
print(f(n, k))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

def f(n, k):
    global a
    mod = int(1e9 + 7)
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(n + 1):
        dp[i][0] = 1
    for i in range(1, n + 1):
        # csum: Cumulative sum
        # csum[j + 1] = dp[i-1][0] + dp[i-1][1] + ... + dp[i-1][j]
        csum = [0] + list(dp[i - 1])
        for j in range(k + 1):
            csum[j + 1] += csum[j]
            csum[j + 1] %= mod
        for j in range(1, k + 1):
            dp[i][j] = csum[j + 1] - csum[max(0, j - a[i])] + mod
            dp[i][j] %= mod
    return(dp[n][k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
a.insert(0, 0)
print(f(n, k))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

def f(n, k):
    global a
    mod = int(1e9 + 7)
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(n + 1):
        dp[i][0] = 1
    for i in range(1, n + 1):
        csum = [0] + list(dp[i - 1])
        for j in range(k + 1):
            csum[j + 1] += csum[j]
            csum[j + 1] %= mod
        for j in range(1, k + 1):
            dp[i][j] = csum[j + 1] - csum[max(0, j - a[i])]
            if dp[i][j] < 0:
              dp[i][j] += mod
    return(dp[n][k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
a.insert(0, 0)
print(f(n, k))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

def f(n, k):
    global a
    mod = int(1e9 + 7)
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(n + 1):
        dp[i][0] = 1
    for i in range(1, n + 1):
        csum = [0] + list(dp[i - 1])
        for j in range(k + 1):
            csum[j + 1] += csum[j]
            csum[j + 1] -= mod if csum[j + 1] >= mod else 0
        for j in range(1, k + 1):
            dp[i][j] = csum[j + 1] - csum[max(0, j - a[i])]
            dp[i][j] += mod if dp[i][j] < 0 else 0
    return(dp[n][k])

n, k = map(int, input().split()) # 1 <= n <= 100, 0 <= k <= 10^5
a = list(map(int, input().split()))
a.insert(0, 0)
print(f(n, k))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = tuple(map(int,input().split()))
mod =10**9+7

#dp[i][j]:0~iまでの子がj個分け合う場合の数
dp = [[0]*(k+1) for i in range(n)]
#b[q+1]-b[p] = dp[x][p]+...+dp[x][q]
b = [0]*(k+2)

for i in range(k+1):
    if i <= a[0]:
        dp[0][i] = 1
    b[i+1] = b[i]+dp[0][i] 

for i in range(1,n):
    for j in range(k+1):
        dp[i][j] = b[j+1]-b[max(0,j-a[i])]
    #bの更新
    b[0] = 0
    for j in range(k+1):
        b[j+1] = b[j]+dp[i][j]
        dp[i][j] = dp[i][j]%mod
print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())


mod=10**9+7

T=[1]
for a in A:
	W=[0]		#WA:累積和
	l=len(T)
	for j,x in enumerate(T):
		if j==0:	W.append(x)
		else:	W.append( W[-1]+x )

	T=[]  #Temporary
	for k in range( min (K+1,a+l) ):
		#print k, min( k+1, l), max(0,k-a), W[min( k+1, l)]-W[max(0,k-a)]
		T.append( W[min( k+1, l)]-W[max(0,k-a)] )


try:
	print T[K]%mod
except IndexError:
	print 0

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())


mod=10**9+7

T=[1]
for a in A:
	W=[0]		#WA:累積和
	L=T
	l=len(L)
	for j,x in enumerate(L):
		if j==0:	W.append(x)
		else:	W.append( W[-1]+x )

	T=[]  #Temporary
	for k in range( min (K+1,a+l) ):
		#print k, min( k+1, l), max(0,k-a), W[min( k+1, l)]-W[max(0,k-a)]
		T.append( W[min( k+1, l)]-W[max(0,k-a)] )


try:
	print T[K]%mod
except IndexError:
	print 0

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())


mod=10**9+7

T=[1]
for n in range(N):
	W=[0]		#WA:累積和
	L=T
	l=len(L)
	a=A[n]
	for j,x in enumerate(L):
		if j==0:	W.append(x)
		else:	W.append( W[-1]+x )

	T=[]  #Temporary
	for k in range( min (K+1,a+l) ):
		#print k, min( k+1, l), max(0,k-a), W[min( k+1, l)]-W[max(0,k-a)]
		T.append( W[min( k+1, l)]-W[max(0,k-a)] )


try:
	print T[K]%mod
except IndexError:
	print 0

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

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

mod=10**9+7


for n in range(N):
	W=[0]		#WA:累積和
	L=dp[n]
	l=len(L)
	a=A[n]
	for j,x in enumerate(L):
		if j==0:	W.append(x)
		else:	W.append( W[-1]+x )

	T=[]  #Temporary
	for k in range( min (K+1,a+l) ):
		#print k, min( k+1, l), max(0,k-a), W[min( k+1, l)]-W[max(0,k-a)]
		T.append( W[min( k+1, l)]-W[max(0,k-a)] )

	dp[n+1]=T



try:
	print dp[N][K]%mod
except IndexError:
	print 0

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())


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


for i in range(1,N+1):
	W=[]		#WA:累積和
	U=dp[i-1]	#Unit：累積和を計算する前の単位配列
	l=len(U)+A[i-1]	#i行目の配列の長さ

	for j,x in enumerate(U):
		if j==0:	W.append(x)
		else:	W.append(W[-1]+x)

	len_w=len(W)

	L=[]		#dp[i]の配列
	for k in range(l):
		L.append( W[min(k, len_w-1)] )

	for k in range(l-1,-1,-1):
		L[k]=min( L[k] , W[min(l-k-1, len_w-1)] )

	dp[i]=L


try:
	print dp[N][K]%(10**9+7)
except:
	print 0

Submission status: Time Limit Exceeded

Solution code:
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)]

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

print(dp[n][k])

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

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

mod=10**9+7


for n in range(N):
	a=A[n]  #ame
	for k in range(K+1):
		if dp[n][k]!=0:
			for j in range(a+1):	#ame
				if 0<=k+j<=K:
					dp[n+1][k+j]+=dp[n][k]

print dp[N][K]%mod

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

"""
参考：https://kyopro-friends.hatenablog.com/entry/2019/01/12/231035
　　　https://www.hamayanhamayan.com/entry/2019/01/10/235150
　　　http://anctgcc.hatenablog.com/entry/2014/12/03/194140
　　　https://ikatakos.com/pot/programming_algorithm/data_structure/binary_indexed_tree
・数え上げDP
・累積和の代わりにBIT(Binary Indexed Tree)を使ってみる。
・添字の使い方を累積和の方と合わせた版
"""

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

class BIT:
    def __init__(self, n):
        # 0-indexed
        self.size = n
        self.tree = [0] * n
 
    def sum(self, i):
        s = 0
        i += 1
        while i > 0:
            s += self.tree[i-1]
            i -= i & -i
        return s
    
    # 区間和の取得 [l, r)
    def get(self, l, r):
        res = 0
        if r: res += self.sum(r-1)
        if l: res -= self.sum(l-1)
        return res

    # 値の追加：添字, 値
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i-1] += x
            i += i & -i

N, K = MAP()
aN = LIST()

# dp[この子までの範囲で][飴を配る総数] = 場合の数
# dpの2次元目をBITで構築する
dp = [BIT(K+1) for i in range(N+1)]
dp[0].add(0, 1)

for i in range(N):
    for j in range(K+1):
        # iが送る予定の長さに応じた区間和を取る
        sm = dp[i].get(max(j-aN[i], 0), j+1) % MOD
        dp[i+1].add(j, sm)

print(dp[N].get(K, K+1) % MOD)