Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
ldp=[[0]*k for _ in xrange(n)]
rdp=[[0]*k for _ in xrange(n)]
for i in xrange(n):
    ldp[i][0]=True
    rdp[i][0]=True
    if a[i]<k:
        ldp[i][a[i]]=True
    if a[n-i-1]<k:
        rdp[i][a[n-1-i]]=True
for i in xrange(1,n):
    for j in xrange(k):
        if j-a[i]>=0:
            ldp[i][j]|=ldp[i-1][j-a[i]]|ldp[i-1][j]
        else:
            ldp[i][j]|=ldp[i-1][j]
        if j-a[n-1-i]>=0:
            rdp[i][j]|=rdp[i-1][j-a[n-1-i]]|rdp[i-1][j]
        else:
            rdp[i][j]|=rdp[i-1][j]
rsum=[[0]*k for _ in xrange(n)]
for i in xrange(n):
    for j in xrange(k):
        if j==0:
            rsum[i][j]=rdp[i][j]
        else:
            rsum[i][j]=rsum[i][j-1]+rdp[i][j]
cnt=0
for i in xrange(n):
    if k-a[i]<=0:
        cnt+=1
        continue
    for j in xrange(k):
        if i==0:
            left=0
        elif i==n-1:
            if ldp[i-1][j] and a[i]+j>=k:
                cnt+=1
                break
            continue
        else:
            if ldp[i-1][j]:
                left=j
            else:
                continue
        if k-a[i]-left<=0:
            cnt+=1
            break
        if rsum[n-2-i][k-1-left]-rsum[n-2-i][k-a[i]-left-1]:
            cnt+=1
            break
print n-cnt

Fixed submission:
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
used=[False]*n
idx=n-1
cnt=0
while True:
    if idx-cnt<0:
        break
    tmp=sum(a[idx-cnt:idx+1])
    if tmp<k:
        cnt+=1
        continue
    for i in xrange(idx-cnt,idx+1):
        used[i]=True
    tmp-=a[idx-cnt]
    for i in xrange(idx-cnt-1,-1,-1):
        if tmp+a[i]>=k:
            used[i]=True
        else:
            break
    idx-=1
    cnt=0
ans=0
for i in xrange(n):
    if not used[i]:
        ans+=1
print ans

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
used=[False]*n
idx=n-1
cnt=0
while True:
    if idx-cnt<0:
        break
    tmp=sum(a[idx-cnt:idx+1])
    if tmp<k:
        cnt+=1
        continue
    for i in xrange(idx-cnt,idx+1):
        used[i]=True
    tmp-=a[idx-cnt]
    now=idx-cnt-1
    f=0
    while now>=0:
        if tmp+a[now]>=k:
            used[now]=True
            now-=1
        else:
            pre=now
            tmp1=a[now]
            if tmp+sum(a[0:now+1])<k:
                f=1
                break
            while now>=0 and tmp+tmp1<k:
                now-=1
                tmp1+=a[now]
            for j in xrange(now,pre+1):
                used[j]=True
            now=pre-1
    idx-=1
    cnt=0
ans=0
for i in xrange(n):
    if not used[i]:
        ans+=1
print ans


Fixed submission:
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
used=[False]*n
idx=n-1
cnt=0
while True:
    if idx-cnt<0:
        break
    tmp=sum(a[idx-cnt:idx+1])
    if tmp<k:
        cnt+=1
        continue
    for i in xrange(idx-cnt,idx+1):
        used[i]=True
    tmp-=a[idx-cnt]
    for i in xrange(idx-cnt-1,-1,-1):
        if tmp+a[i]>=k:
            used[i]=True
        else:
            break
    idx-=1
    cnt=0
ans=0
for i in xrange(n):
    if not used[i]:
        ans+=1
print ans

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
used=[False]*n
idx=n-1
cnt=0
while True:
    if idx-cnt<0:
        break
    tmp=sum(a[idx-cnt:idx+1])
    if tmp<k:
        cnt+=1
        continue
    for i in xrange(idx-cnt,idx+1):
        used[i]=True
    tmp-=a[idx-cnt]
    now=idx-cnt-1
    f=0
    while now>=0:
        if tmp+a[now]>=k:
            used[now]=True
            now-=1
        else:
            pre=now
            tmp1=a[now]
            if tmp+sum(a[0:now+1])<k:
                f=1
                break
            while now>=0 and tmp+tmp1<k:
                now-=1
                tmp1+=a[now]
            for j in xrange(now,pre+1):
                used[j]=True
            now=pre-1
    idx-=1
    cnt=0
ans=0
for i in xrange(n):
    if not used[i]:
        ans+=1
print ans


Fixed submission:
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
used=[False]*n
idx=n-1
cnt=0
while True:
    if idx-cnt<0:
        break
    tmp=sum(a[idx-cnt:idx+1])
    if tmp<k:
        cnt+=1
        continue
    for i in xrange(idx-cnt,idx+1):
        used[i]=True
    tmp-=a[idx-cnt]
    for i in xrange(idx-cnt-1,-1,-1):
        if tmp+a[i]>=k:
            used[i]=True
        else:
            break
    idx-=1
    cnt=0
ans=0
for i in xrange(n):
    if not used[i]:
        ans+=1
print ans