Problem 3: Inquiry

https://open.kattis.com/problems/inquiryi

Trivial approach:

calculate the value for all kk separately

Hints:

Do we have to calculate (a12+a22++ak2)(ak+1++an)(a_1^2 + a_2^2 + \cdots + a_k^2) \cdot (a_{k+1} + \cdots + a_n)every single time?

Can we store some intermediate value in (a12+a22++ak2)(ak+1++an)(a_1^2 + a_2^2 + \cdots + a_k^2) \cdot (a_{k+1} + \cdots + a_n) so that we we can save ourselves some work for the next product?

Key idea:

  1. Store a12++ak2 a_1^2 + \cdots + a_k^2as S1 S_1. Store ak+1++ana_{k+1} + \cdots + a_n as S2S_2

  2. For k= 1 ... n: we add ak+12a_{k+1}^2 to S1S_1 and subtract ak+1a_{k+1}from S2S_2.

Remember to use type long long for this problem.

Last updated