Amazon Online Assessment 2021 (OA) - Number Game

You are playing a card game with your friends. Every card is marked with a positive integer. You start the game with 2N cards on your hand, and the game lasts N rounds. In each round, you have to remove 2 cards from your hand.

Your score in each round is gcd(card, card2) * round_number, where card and card2 are the cards that you removed and round_number is the current round. Your total score will be the sum of the scores that you received in each round.

What is the maximum total score that you can get?

Constraints

1 <= n <= 10, n represents the total number of rounds.

1 <= cards[i] <= 10^9, cards represents the array of the cards on your hand, and cards[i] represents the positive integer marked on card i.

The round_number starts from 1.

Examples

Example 1:

Input:

n = 3

cards = [8, 5, 6, 25, 6, 16]

Output: 41
Explanation:

The game can proceed as follow:

  • round_number 1: remove card = 5, card2 = 25, so gcd(5, 25) * round_number = 5 * 1 = 5.
  • round_number 2: remove card = 6, card2 = 6, so gcd(6, 6) * round_number = 6 * 2 = 12.
  • round_number 3: remove card = 8, card2 = 16, so gcd(8, 16) * round_number = 8 * 3 = 24.

The maximum total score is 5 + 12 + 24 = 41.

Try it yourself

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade