Under construction

Still being built. Things may change

Forming Groups

Time limit5sMemory limit1024 MB

Summary
Choose where student 1 stands in the line and a divisor k of n (k above 1) so that the largest-to-smallest ratio of round-robin group sums is minimized.
Level

Medium5 of 10

Topics
Array, Prefix sum, Math
Solved
No attempts yet

Problem

There are nn students, numbered from 11 to nn, who need to form groups for an upcoming hackathon. You are student 11, the captain. Student ii has skill level aia_i.

Students 22 to nn stand in a line from left to right, in order. You can stand between any two students, to the left of student 22, or to the right of student nn. The order of the n−1n - 1 students cannot change.

You can also choose the number of groups kk, where k>1k > 1 and kk is a divisor of nn. The groups are numbered from 11 to kk. After you choose your position and kk, the students are grouped as follows.

The first student from the left is assigned to group 11, the second to group 22, and so on up to the kk-th student, who is assigned to group kk. The (k+1)(k+1)-th student is assigned to group 11, the (k+2)(k+2)-th to group 22, and this pattern continues until the nn-th student, who is assigned to group kk.

Formally, for each jj (1≤j≤k1 \le j \le k) and each ii (0≤i<n/k0 \le i < n/k), the (i×k+j)(i \times k + j)-th student from the left is assigned to group jj. Every student is in exactly one group, and all groups have the same number of students.

The skill level of a group is the sum of the skill levels of its students. Choose your position and kk to minimize the ratio xmax⁡/xmin⁡x_{\max}/x_{\min}, where xmax⁡x_{\max} is the largest group skill level and xmin⁡x_{\min} is the smallest.

Examples1

  1. Example 1

    Input
    2
    4 1
    2 1 2
    3 10
    4 3
    
    Expected output
    1 1
    10 3