APIO '10 P1 - Commando

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type

You are the commander of a troop of n soldiers, numbered from 1 to n. For the battle ahead, you plan to divide these n soldiers into several commando units. To promote unity and boost morale, each unit will consist of a contiguous sequence of soldiers of the form (i, i+1, \dots, i+k).

Each soldier i has a battle effectiveness rating x_i. Originally, the battle effectiveness x of a commando unit (i, i+1, \dots, i+k) was computed by adding up the individual battle effectiveness of the soldiers in the unit. In other words, x = x_i + x_{i+1} + \dots + x_{i+k}.

However, years of glorious victories have led you to conclude that the battle effectiveness of a unit should be adjusted as follows: the adjusted effectiveness x' is computed by using the equation x' = ax^2 +bx+c, where a, b, c are known coefficients (a < 0), x is the original effectiveness of the unit.

Your task as commander is to divide your soldiers into commando units in order to maximize the sum of the adjusted effectiveness of all the units.

For instance, suppose you have 4 soldiers, x_1 = 2, x_2 = 2, x_3 = 3, x_4 = 4. Further, let the coefficients for the equation to adjust the battle effectiveness of a unit be a = -1, b = 10, c = -20. In this case, the best solution is to divide the soldiers into three commando units: The first unit contains soldiers 1 and 2, the second unit contains soldier 3, and the third unit contains soldier 4. The battle effectiveness of the three units are 4, 3, 4 respectively, and the adjusted effectiveness are 4, 1, 4 respectively. The total adjusted effectiveness for this grouping is 9 and it can be checked that no better solution is possible.

Input Specification

The input consists of three lines. The first line contains a positive integer n, the total number of soldiers. The second line contains 3 integers a, b, and c, the coefficients for the equation to adjust the battle effectiveness of a commando unit. The last line contains n integers x_1, x_2, \dots, x_n, separated by spaces, representing the battle effectiveness of soldiers 1, 2, \dots, n, respectively.

Output Specification

A single line with an integer indicating the maximum adjusted effectiveness achievable.

Sample Input

-1 10 -20
2 2 3 4

Sample Output



  • In 20\% of the test cases, n \le 1000;
  • In 50\% of the test cases, n \le 10\,000;
  • In 100\% of the test cases, n \le 1\,000\,000, -5 \le a \le -1, |b| \le 10\,000\,000, |c| \le 10\,000\,000 and 1 \le x_i \le 100.


There are no comments at the moment.