Christmas Presents
InterviewTime limit1sMemory limit128 MB
Choose a subset of children whose total price is at most p, maximizing excitement of chosen minus frustration of unchosen, and output the lexicographically smallest optimal 0/1 string.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
The local Santa Claus needs your help. He can no longer afford to give every child a present, so he must decide which children receive one. For each child he has recorded two values: the child's excitement if they receive a present, and their frustration if they do not.
Santa wants to maximize the total satisfaction without letting his total spending exceed his budget. The total satisfaction is the sum of the excitement values of the children who receive a present, minus the sum of the frustration values of the children who do not.
Input
The first line contains two integers and : the number of children () and the spending limit ().
Each of the next lines contains three nonnegative integers: the price, the excitement level, and the frustration level of one child. The total price of the children who receive a present must not exceed .
Output
On the first line, print the maximum total satisfaction.
On the second line, print a string of characters made of 0s and 1s: the -th character is 1 if the -th child receives a present and 0 otherwise. If several selections achieve the maximum satisfaction, print the lexicographically smallest such string (a string that has 0 in an earlier position is considered smaller).