forked from Aaryan-kapur/lets_see_how_this_goes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
competitiveQ4.py
66 lines (48 loc) · 1.98 KB
/
competitiveQ4.py
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
63
64
65
66
# Hulk is a good young dude from Kasna village. He goes to Haldiram to buy N types of sweets for celebrating Diwali with his friends. Initially he bought sweets in kilos S = [s1, s2, s3…sN]. After paying bills he gets a call from his friends saying that he has to buy sweets in the following order. Every sweet Si has to be lesser or equal to next sweet Si+1. So he tells the Haldiram bhayya to make it in that way. Now bhayya can either add a kilo or remove a kilo of sweet from Si at a time. Now Hulk needs to tell Bhayya the minimum number of times T required to make the sweets in the specific order. For example S = [1, 2, 1, 4, 3] then T = 2. First we can add a kilo to 3rd sweet and subtract a kilo from 4th sweet to get [1, 2, 2, 3, 3]. If S = [1, 2, 2, 100] then T = 0.
# Input Format
# 5 1 2 1 4 3
# Constraints
# NA
# Output Format
# 2
# Sample Input 0
# 5
# 1 2 1 4 3
# Sample Output 0
# 2
# Explanation 0
# First line is the N Second line is the array
# k = input()
# ar = list(map(int,input().split()))
# n = len(ar)
# min = min(ar)
# max = max(ar)
# arr = [[ 0 for i in range(max + 1)] for i in range(n)]
# for j in range(min, max + 1):
# arr[0][j] = abs(ar[0] - j)
# for i in range(1, n):
# minimum = 10**9
# for j in range(min, max + 1):
# minimum = min(minimum, arr[i - 1][j])
# arr[i][j] = minimum + abs(ar[i] - j)
# val = 10**9
# for j in range(min, max + 1):
# val = min(val, arr[n - 1][j])
# print(val)
n = int(input())
ar = list(map(int,input().split()))
smallest = min(ar)
largest = max(ar)
array = [[ 0 for i in range(largest + 1)]
for i in range(n)]
for j in range(smallest, largest + 1):
array[0][j] = abs(ar[0] - j)
for i in range(1, n):
minimum = 10**9
for j in range(smallest, largest + 1):
minimum = min(minimum, array[i - 1][j])
array[i][j] = minimum + abs(ar[i] - j)
result = 10**9
for j in range(smallest, largest + 1):
result = min(result, array[n - 1][j])
print(result)