HackWithInfy 2021 Solutions | Adventure question | 3 pm | 9 May

HackWithInfy 2021 Solutions | Adventure question | 3 pm | 9 May

Question: Adventure question

Solution: In Python 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
n = int(input())
    m = int(input())
    typelst = []
    for i in range(n): typelst.append(int(input()))
    timelst = []
    for i in range(n): timelst.append(int(input()))
    lst1 = []
    lst2 = []
    lst3 = []
    for i in range(n):
        if typelst[i] == 1:
            lst1.append(timelst[i])
        elif typelst[i] == 2:
            lst2.append(timelst[i])
        elif typelst[i] == 3:
            lst3.append(timelst[i])
    lst1.sort()
    lst2.sort()
    lst3.sort()
    
    for i in range(1,len(lst1)):
        lst1[i] += lst1[i-1]
    
    for i in range(1,len(lst2)):
        lst2[i] += lst2[i-1]
    for i in range(1,len(lst3)):
        lst3[i] += lst3[i-1]
    ans = math.inf
    
    try:
        ans = lst1[m-1] + lst2[m-1]
    except:
        pass
   
    for i in range(m):
        if i < len(lst3):
            if i==m-1:
                try:
                    val = lst3[i]
                    if val < ans: ans = val
                except:
                    pass
            try:
                indx = m-i-2
                val = lst3[i] + lst2[indx] + lst1[indx]
                if val < ans: ans = val 
            except:
                pass
    
    if ans==math.inf:
        print(-1)
    else:
        print(ans)

Be the first to comment

Leave a Reply

Your email address will not be published.


*