n = int(input())
As =list(map(int,input().split()))

sum = 0
step =0

try:
    for i in range(n):
        if As[i] > As[i + 1]:
            step = As[i] - As[i + 1]
            As[i + 1] += step
            sum += step 
except IndexError:
    pass


for i in range(len(As)):
    print(As[i],end=" ")
else:
    print("")
print(sum)