Basic Problem (Codeforces A) Mathematical proof

Problem:
(Problem - 347A - Codeforces)
Solution:
(Submission #87311735 - Codeforces)

Can you explain why is this optimal?

(a_1 - a_2) + (a_2 - a_3) + (a_3 - a_4) + ... + (a_{n - 1} - a_n) cancels to a_1 - a_n (sometimes this is called “telescoping”), so that’s the only thing that really matters; to optimize that, you want the largest thing at the beginning and the smallest thing at the end

5 Likes

Oh actually I by mistake read question wrong I thought it’s saying (a1 - a2) + (a3 - a4)+ (a5 - a6)…
I went on thinking how is this even possible solution, Thanks. @galencolin