Problem statement
Write function solution(chunks, budget). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. chunks:{tokens:positive integer,priority:nonnegative integer}[], budget:integer 0–2048. Select each whole chunk at most once. Maximize total priority subject to total tokens<=budget. Among equal scores choose fewer tokens. Return {score,tokens}; selecting nothing yields {score:0,tokens:0}. All sums are safe integers. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.
Example input and output
Example 1
Function arguments
[
[
{
"tokens": 3,
"priority": 5
},
{
"tokens": 2,
"priority": 4
},
{
"tokens": 4,
"priority": 6
}
],
5
]Return value
{
"score": 9,
"tokens": 5
}Example 2
Function arguments
[
[
{
"tokens": 1,
"priority": 3
},
{
"tokens": 2,
"priority": 3
}
],
2
]Return value
{
"score": 3,
"tokens": 1
}Example 3
Function arguments
[
[
{
"tokens": 1,
"priority": 2
},
{
"tokens": 2,
"priority": 5
},
{
"tokens": 3,
"priority": 9
}
],
6
]Return value
{
"score": 16,
"tokens": 6
}