Trains the technique from
LeetCode 1799Maximize Score After N OperationsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A workshop is retiring its cutting blades. There are 2 * n blades in service and values[i] is the tooth count of blade i.
The retirement happens over n sessions, numbered 1, 2, up to n. In session s the workshop picks any two blades that are still in service, takes them out of service, and banks s * g credits, where g is the greatest common divisor of the two tooth counts. After session n no blade remains in service.
The workshop chooses which two blades to retire in each session, and the session number multiplies whatever that session earns. Return the largest total credit the workshop can bank.
Example 1
Retire blades 12 and 4 in session 1, whose greatest common divisor is 4, banking 1 * 4 = 4. Retire blades 30 and 25 in session 2, whose greatest common divisor is 5, banking 2 * 5 = 10. The total is 14.
Example 2
Session 1 retires 4 and 8 for 1 * 4 = 4, session 2 retires 12 and 6 for 2 * 6 = 12, and session 3 retires 9 and 18 for 3 * 9 = 27, totalling 43.
Example 3
Every pair of these blades has greatest common divisor 8, so session 1 banks 1 * 8 and session 2 banks 2 * 8, for 24 in total.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def best_retirement_credit(values: list[int]) -> int:public int bestRetirementCredit(int[] values)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.