Reassign exam marks as distinct integers no smaller than the originals with the minimum total

Quick Overview

Reassign a list of exam marks so that every mark is a distinct positive integer no smaller than its original value and the total is as small as possible. Tests sorting, a greedy assignment, the argument that proves it optimal, and handling of long runs of repeated values.

Reassign exam marks as distinct integers no smaller than the originals with the minimum total

Company: Worldpay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Mr. Myers is preparing an exam with `N` questions. He has an array `marks` of length `N`, where `marks[i]` is the maximum mark originally assigned to the i-th question. He wants to reassign the marks so that all of the following hold: - Every reassigned mark is a distinct positive integer. - Each reassigned mark is greater than or equal to the original value for that question. - The total sum of the reassigned marks is minimized. Return the minimum possible total sum of the reassigned marks. ### Function Signature ```python def min_total_marks(marks: list[int]) -> int: ``` ### Rules - `marks` is not sorted, and values may repeat. - Return only the minimum total as an integer. The assignment itself is not returned. - The total can exceed the 32-bit integer range (it can reach about `10^14`). ### Constraints - `1 <= N <= 10^5` - `1 <= marks[i] <= 10^9` ### Examples ```text Input: marks = [1, 2, 2, 3] Output: 10 ``` In sorted order the marks become 1, 2, 3 (the second 2 is taken) and 4 (3 is now taken), so the total is 1 + 2 + 3 + 4 = 10. No answer can be lower, because four distinct positive integers always sum to at least 10. ```text Input: marks = [5, 5, 5, 5, 5] Output: 35 ``` Every mark must be at least 5 and all five must differ, so the cheapest choice is 5, 6, 7, 8 and 9, which sum to 35. ```text Input: marks = [3, 1, 3, 10, 2] Output: 20 ``` In sorted order the marks become 1, 2, 3, 4 and 10. The originals already sum to 19, and the two 3s cannot both stay at 3, so 20 is the least possible total.

Overview: Reassign a list of exam marks so that every mark is a distinct positive integer no smaller than its original value and the total is as small as possible. Tests sorting, a greedy assignment, the argument that proves it optimal, and handling of long runs of repeated values.

Worldpay logo
Worldpay
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Mr. Myers is preparing an exam with N questions. He has an array marks of length N, where marks[i] is the maximum mark originally assigned to the i-th question.

He wants to reassign the marks so that all of the following hold:

  • Every reassigned mark is a distinct positive integer.
  • Each reassigned mark is greater than or equal to the original value for that question.
  • The total sum of the reassigned marks is minimized.

Return the minimum possible total sum of the reassigned marks.

Function Signature

def min_total_marks(marks: list[int]) -> int:

Rules

  • marks is not sorted, and values may repeat.
  • Return only the minimum total as an integer. The assignment itself is not returned.
  • The total can exceed the 32-bit integer range (it can reach about 10^14 ).

Constraints

  • 1 <= N <= 10^5
  • 1 <= marks[i] <= 10^9

Examples

Input: marks = [1, 2, 2, 3]
Output: 10

In sorted order the marks become 1, 2, 3 (the second 2 is taken) and 4 (3 is now taken), so the total is 1 + 2 + 3 + 4 = 10. No answer can be lower, because four distinct positive integers always sum to at least 10.

Input: marks = [5, 5, 5, 5, 5]
Output: 35

Every mark must be at least 5 and all five must differ, so the cheapest choice is 5, 6, 7, 8 and 9, which sum to 35.

Input: marks = [3, 1, 3, 10, 2]
Output: 20

In sorted order the marks become 1, 2, 3, 4 and 10. The originals already sum to 19, and the two 3s cannot both stay at 3, so 20 is the least possible total.