• + 1 comment

    Wow, this one just doesn't want to get solved no matter what. First I tried doing it with a recursive function, well that ran into the RuntimeError. No biggie let's read up on DP. So we need to go through all A:s and add possible sums of those into a list. Nope still a runtime error. Ok how about going through numbers from 0 to G and mark those of them that can be easily summed to? Well that leads to counting A[0]+A[0] etc, so I try recording also the "route" for each of them as a list, well that's a MemoryError...

    Amazingly it's the recursive function that has so far managed to solve the most amount of cases(3 - 0,7,11), while both my attempts at dynamic programming solve no more than 2 cases, before they either timeout or raise a memory/runtime error.

    E.g. this code seems to net correct output if I let it run for a couple of minutes on my own computer:

    T=int(input().strip())
    for run in range(T):
        N,G = map(int, input().strip().split())
        A=[int(i) for i in input().strip().split()]
        if max(A)>G or sum(A)>2*G: print('NO')
        else:
            states=[0] #sum of A:s
            for i in A:
                states.sort()
                for x in states[:]:
                    if i+x > G: break
                    elif i+x not in states: states.append(x+i)
            if sum(A)-max(states)<=G:print('YES')
            else:print('NO')
    

    But on the server it just gets terminated before it finishes and I have no idea how to further improve it's performance. Any suggestions?