Cooperation score
Statement
You want to split N students into K groups. Each student has a cooperation score and can belong to only one group. Every student is placed in exactly one group.
Once every student has been assigned to a group, you can compute the combat power of each group. The combat power of a group is the sum, taken over every student in that group, of (the number of members in that group X that student's cooperation score). A student's cooperation score can be negative (-), so a group's combat power can end up negative as well.
Considering every way to form K groups out of N students, find the smallest difference in combat power between the group with the highest combat power and the group with the lowest. At least one student must be placed in every one of the K groups.
For example, let the cooperation scores of N=4 students be as in the table below, with the number of groups K=2.
| Student number | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Cooperation score | -1 | 0 | 3 | -2 |
First, suppose two groups were formed as follows.
| Group number | 1 | 2 |
|---|---|---|
| Member student numbers | [1, 2] | [3, 4] |
The combat power is computed as below, so the difference between the two groups is 4.
(1) Combat power of group 1: -2 = 2 * (-1) + 2 * 0 (2) Combat power of group 2: 2 = 2 * 3 + 2 * (-2)
Or suppose two groups were formed like this instead.
| Group number | 1 | 2 |
|---|---|---|
| Member student numbers | [2] | [1, 3, 4] |
The combat power is computed as below, so the difference between the two groups is 0.
(1) Combat power of group 1: 0 = 1 * 0 (2) Combat power of group 2: 0 = 3 * (-1) + 3 * 3 + 3 * (-2)
So the best answer for this input example is 0.
Input and Output Examples
Example 1
Input
4, 2, [-1, 0, 3, -2]
Output
0
Example 2
Input
5, 4, [2, 1, 1, 2, 3]
Output
2
Input
First the number of students N is given as a natural number. N is a natural number from 2 to 7.
Next the number of groups K is given as a natural number. K is a natural number from 2 to N.
After that an array arr is given, holding the cooperation score of each student. Each student's cooperation score is an integer from -10 to 10.
Output
Considering every way to form K groups out of N students, return the smallest difference in combat power between the group with the highest combat power and the group with the lowest.
Requirements
- You must write a solution(N, K, arr) function and return the answer
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 20 cases, and 5 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 20 cases to solve this.
Examples
5 public cases. Judging also uses hidden cases. You have to pass all 20 cases to solve this.
4, 2, [-1, 0, 3, -2]
0
5, 4, [2, 1, 1, 2, 3]
2
6, 3, [-8, 4, -2, 11, 0, -4]
3
3, 2, [5, 7, -8]
7
3, 2, [0, -6, 3]
6
Notes
Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.
This is problem 3 of practice test 5 in the original.
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 5 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Input read time 5ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.