t=int(input())
for i in range(t):
n=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l3=[]
l4=[]
te=[]
j=0
while(j<n):
x=l1[j]
te=[]
for k in range(n):
if x==l1[k]:
te.append(l2[k])
l3.append(te)
j+=1
s=0
for j in l3:
if j not in l4:
l4.append(j)
for j in l4:
m=max(j)
if m<0:
m=0
s=s+m
print(s)
This may not be the optimal solution, but it is one.
The Code
t = int(input())
for _ in range(t):
ans = 0
fn = {}
n = int(input())
fruits = list(map(int,input().split()))
nutrition = list(map(int,input().split()))
for i in range(n):
if fruits[i] not in fn:
fn[fruits[i]] = nutrition[i]
else:
fn[fruits[i]] = max(fn[fruits[i]],nutrition[i])
for j in fn.values():
if j < 0:
continue
else:
ans = ans + j
print(ans)
Approach
First let’s create a dict. having all given types of fruits as it’s key and value as max nutrition it can give.
fn(fruits and nutritions) dict. has been created.
now just add all the +ve element of fn.values() and the problem is solved.
Hope this may help you.
As, for your code:
using list will be much easy in this question as one key and only have one value. I am not able to understand your approach clearly.
Anyway here are some point you can improve in:
First, give logical names to variables i.e. if you woke up and read your code you should hove some idea of it, for example:
You created l1,l2 list but I named them fruits and nutrition even the dict. named fn for a reason that is fruitsNutrition.