Walls Reconstruction - Strictly Increasing Order
Walls Reconstruction - Strictly Increasing Order --TCS NQT
Problem:
There are N walls constructed in increasing order of height. A mason wants to reconstruct the walls with minimal changes in height so that the height of the N walls are in strictly increasing order. The program must accept N integers representing the height of the N walls as the input. The program must print the total height of the N walls after the reconstruction as the output.
Boundary Condition(s):
2 <= N <= 100
1 <= Each integer value <= 1000
Input Format:
The first line contains N. The second line contains N integers separated by a space.
Output Format:
The first line contains the total height of the N walls after the reconstruction.
Example Input/Output 1:
Input:
5
2 2 2 5 8
Output:
22
Explanation:
Here N = 5, the heights of the 5 walls are 2, 2, 2, 5 and 8. After reconstruction of the 2nd wall and 3rd wall (with minimal changes in height), the heights of the 5 walls become 2, 3, 4, 5 and 8. Now the heights of the 5 walls are in strictly increasing order. The total height of the 5 walls after the reconstruction is 22 (2+3+4+5+8). So 22 is printed as the output.
Example Input/Output 2:
Input:
6
1 3 3 3 3 4
Output:
26
Program:
n = int(input())
l = list(map(int,input().split()))
ans = [l[0]]
for i in range(1,n):
if ans[-1] < l[i]:
ans.append(l[i])
else:
ans.append(max(ans[-1],l[i])+1)
print(sum(ans))
Comments
Post a Comment