#1799
Maximize Score After N Operations
expert · 1230 · lc hard +32 · verified · 58% accepted · 1,712 likes · top 54%
Description
You are given nums of size 2 * n. Perform n operations: in operation i (1-indexed), pick two elements x and y, add i * gcd(x, y) to your score, and remove both. Return the maximum total score.
Example 1:
Input: nums = [1,2]
Output: 1
Explanation: The optimal choice of operations is:
(1 * gcd(1, 2)) = 1
Example 2:
Input: nums = [3,4,6,8]
Output: 11
Explanation: The optimal choice of operations is:
(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
Example 3:
Input: nums = [1,2,3,4,5,6]
Output: 14
Explanation: The optimal choice of operations is:
(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
Code
1
2
3