You are designing a chat server that needs to optimize message delivery between users. Given n users in a chat room, each user i has a message priority value priorities[i].
The server needs to compute the maximum total priority score achievable by forming non-overlapping pairs of adjacent users for group messaging. Each user can be in at most one pair. The priority score of a pair (i, i+1) is priorities[i] * priorities[i+1].
Users not in any pair contribute 0 to the total score. Return the maximum total priority score the server can achieve.
Function Signature
def max_priority_score(priorities: list[int]) -> int:
Rules
-
A pair is always two neighbors
(i, i+1)
in list order. Users who are not next to each other cannot be paired.
-
Any number of pairs is allowed, including none. With a single user the answer is
0
.
-
The answer can exceed the 32-bit integer range, so an implementation in a fixed-width language needs a 64-bit result.
Constraints
-
1 <= len(priorities) <= 10^5
-
1 <= priorities[i] <= 1000
Examples
Input: priorities = [3,5,2,8,4]
Output: 47
Pairing indices (0, 1) and (3, 4) scores 15 + 32 = 47. The other ways to form two pairs score 15 + 16 = 31 and 10 + 32 = 42, and no single pair scores more than 32.
Input: priorities = [1,2,3,4]
Output: 14
Pairing (0, 1) and (2, 3) scores 2 + 12 = 14. Pairing only the middle users (1, 2) scores 6 and leaves no free neighbor for a second pair.
Input: priorities = [2,3,3,2]
Output: 12
Pairing (0, 1) and (2, 3) scores 6 + 6 = 12. Pairing the middle users gives the largest single product, 9, but then no other pair can be formed.