File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
Hacker Earth/Algorithms/Graphs/Shortest Path Algorithms Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change
1
+ n ,t = map (int ,input ().split ())
2
+ a = list (map (int ,input ().split ()))
3
+ b = list (map (int ,input ().split ()))
4
+ matrix = [[0 for i in range (n )] for i in range (n )]
5
+ product = 0
6
+ original = []
7
+ for i in range (n ):
8
+ for j in range (n ):
9
+ matrix [i ][j ] = a [i ]* b [j ]
10
+ if (i == j ):
11
+ product = product + matrix [i ][i ]
12
+ original .append ([i ,i ])
13
+ isFound = False
14
+ isPossible = True
15
+ while (isFound == False and isPossible == True ):
16
+ mi = - 1
17
+ mj = - 1
18
+ mp = - 1
19
+ mpt = - 1
20
+ if (product == t ):
21
+ break
22
+ elif (product > t ):
23
+ isProduct = False
24
+ for i in range (n ):
25
+ for j in range (i + 1 ,n ):
26
+ temp = product - matrix [original [i ][0 ]][original [i ][1 ]] - matrix [original [j ][0 ]][original [j ][1 ]]+ matrix [original [i ][0 ]][original [j ][1 ]]+ matrix [original [j ][0 ]][original [i ][1 ]]
27
+ if (temp < t and temp > mpt ):
28
+ mpt = temp
29
+ mi = i
30
+ mj = j
31
+ isProduct = True
32
+ elif (temp > t and temp < product and temp > mp and isProduct == False ):
33
+ mp = temp
34
+ mi = i
35
+ mj = j
36
+ elif (temp == t ):
37
+ mi = i
38
+ mj = j
39
+ isFound = True
40
+ break
41
+ if (isFound ):
42
+ break
43
+ else :
44
+ for i in range (n ):
45
+ for j in range (i + 1 ,n ):
46
+ temp = product - matrix [original [i ][0 ]][original [i ][1 ]] - matrix [original [j ][0 ]][original [j ][1 ]]+ matrix [original [i ][0 ]][original [j ][1 ]]+ matrix [original [j ][0 ]][original [i ][1 ]]
47
+ if (temp < t and temp > mpt ):
48
+ mpt = temp
49
+ mi = i
50
+ mj = j
51
+ elif (temp == t ):
52
+ mi = i
53
+ mj = j
54
+ isFound = True
55
+ break
56
+ if (isFound ):
57
+ break
58
+
59
+ if (mj == - 1 and mi == - 1 ):
60
+ isPossible = False
61
+ else :
62
+ product = product - matrix [original [mi ][0 ]][original [mi ][1 ]] - matrix [original [mj ][0 ]][original [mj ][1 ]]+ matrix [original [mi ][0 ]][original [mj ][1 ]]+ matrix [original [mj ][0 ]][original [mi ][1 ]]
63
+ temp = original [mi ][1 ]
64
+ original [mi ][1 ] = original [mj ][1 ]
65
+ original [mj ][1 ] = temp
66
+
67
+ for i in range (n ):
68
+ print (a [original [i ][0 ]],end = " " )
69
+ print ("" )
70
+ for i in range (n ):
71
+ print (b [original [i ][1 ]],end = "" )
You can’t perform that action at this time.
0 commit comments