99832024-03-22 23:12:0542Zebra (75 pont)python3Time limit exceeded 65/75735ms14500 KiB
from sys import stdin
input=stdin.readline

def pre(X):
    for i in range(1,len(X)):
        X[i]+=X[i-1]
    return X

memo={}

def main():
    N = int(input())
    C = [int(x) for x in input().split()]
    T = [int(x) for x in input().split()]
    B=[]
    J=[]
    for i in range(N):
        if C[i]==0:
            B.append(T[i])
        else:
            J.append(T[i])
    B.sort()
    J.sort()
    preB=pre(B[:])
    preJ=pre(J[:])

    def solv(x,y):
        if (x,y) in memo:
            return memo[(x,y)]
        last=max(B[x-1],J[y-1])
        res=(x+y)*last-preB[x-1]-preJ[y-1]
        if x==1 or y==1:
            return res
       
        for X in range(1,x):
            for Y in range(1,y):
                last=max(B[x-1],J[y-1])
                res=min(res,solv(X,Y)+last*(x-X+y-Y)-preB[x-1]-preJ[y-1]+preB[X-1]+preJ[Y-1])
        memo[(x,y)]=res
        return res
    
    print(solv(len(B),len(J)))
    
main()
SubtaskSumTestVerdictTimeMemory
base65/75
1Accepted0/019ms11588 KiB
2Accepted0/0273ms11992 KiB
3Accepted5/517ms12120 KiB
4Accepted5/520ms12556 KiB
5Accepted5/5128ms12480 KiB
6Accepted5/5172ms12868 KiB
7Accepted5/5293ms13236 KiB
8Accepted5/5224ms13464 KiB
9Accepted5/5391ms13516 KiB
10Accepted5/5395ms13816 KiB
11Accepted5/5500ms13932 KiB
12Accepted5/5564ms14052 KiB
13Time limit exceeded0/5735ms14304 KiB
14Accepted5/5400ms14368 KiB
15Accepted5/5337ms14236 KiB
16Time limit exceeded0/5722ms14440 KiB
17Accepted5/5657ms14500 KiB