99842024-03-22 23:12:4242Zebra (75 pont)pypy3Accepted 75/75287ms111028 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
base75/75
1Accepted0/059ms93008 KiB
2Accepted0/0170ms108748 KiB
3Accepted5/548ms92872 KiB
4Accepted5/563ms102180 KiB
5Accepted5/5119ms105416 KiB
6Accepted5/5134ms106252 KiB
7Accepted5/5155ms105576 KiB
8Accepted5/5122ms107548 KiB
9Accepted5/5162ms106400 KiB
10Accepted5/5164ms107004 KiB
11Accepted5/5231ms109260 KiB
12Accepted5/5237ms109824 KiB
13Accepted5/5240ms107848 KiB
14Accepted5/5190ms109964 KiB
15Accepted5/5186ms109180 KiB
16Accepted5/5287ms111028 KiB
17Accepted5/5222ms108480 KiB