Color Balls
InterviewTime limit1sMemory limit256 MB
For each ball, add up the sizes of all strictly smaller balls with a different color.
- Level
Medium5 of 10
- Topics
- Sorting, Prefix sum, Hash map
- Solved
- No attempts yet
Problem
Jihoon plays a computer game with several players in it. Each player controls one ball that has a fixed color and a fixed size. A player scores by capturing a ball that is strictly smaller than their own ball and has a different color, and the score gained is the size of the captured ball. A ball of the same size cannot be captured even when its color differs. Capturing another ball does not change the color or the size of your own ball.
Consider four balls. Colors are written as numbers for convenience.
Ball 2 captures all three other balls. Ball 1 cannot capture ball 2, which is larger, and cannot capture ball 3, which has the same color, so it captures only ball 4.
Given the color and the size of every ball, print for each player the total size of all balls that player can capture.
Input
The first line contains the number of balls ().
Each of the next lines contains the color and the size of the -th ball, separated by a space (, ). Several balls may share a color, and several balls may share a size.
Output
Print lines. The -th line contains the total size of all balls that the player controlling the -th ball can capture.