BALLOONS - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

There are N types of balloons.
You bought A_i balloons of the i-th type, and each of them has a cost of exactly i.
How much did you pay in total?

EXPLANATION:

Buying i balloons of type i has a total cost of i\cdot A_i.

Thus, the answer is obtained by summing this up across all i from 1 to N, i.e.

\sum_{i=1}^N (i\cdot A_i)

TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    n = int(input())
    a = [0] + list(map(int, input().split()))
    
    ans = 0
    for i in range(1, n+1):
        ans += i * a[i]
    print(ans)