TopcoderArchiveVisit Topcoder
SRM22 Dec 2013
SRM 626

SumOfPower

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a int[] array. At any moment, you may choose a nonempty contiguous subsequence of array. Whenever you do so, you will gain power equal to the sum of all elements in the chosen subsequence.

You chose each possible contiguous subsequence exactly once, each time gaining some power. Compute and return the total amount of power you gained.

Definition

Class:
SumOfPower
Method:
findSum
Parameters:
int[]
Returns:
int
Method signature:
int findSum(int[] array)
(be sure your method is public)

Constraints

  • array will contain between 1 and 50 elements, inclusive.
  • Each element in array will be between 1 and 100, inclusive.

Examples

  1. {1,2}
    Returns: 6
    We have the following three contiguous subsequences: {1} => 1 {2} => 2 {1,2} => 3 Thus, the sum of all possible powers is 1+2+3=6.
  2. {1,1,1}
    Returns: 10
    A 3-element sequence has 6 possible nonempty contiguous subsequences. For the sequence {1,1,1} these are the subsequences: {1}, {1}, {1}, {1,1}, {1,1}, and {1,1,1}. Their sums are 1, 1, 1, 2, 2, and 3. If you choose each of them once, the total power you'll gain is 1+1+1+2+2+3 = 10.
  3. {3,14,15,92,65}
    Returns: 1323
  4. {1,2,3,4,5,6,7,8,9,10}
    Returns: 1210
Back to all problems