Selection of Numbers

3.1

14 votes
Algorithms, Dynamic Programming, Medium, Prefix sum, prefix sum
Problem

You are given N numbers placed in a line. You have to select K numbers from those numbers. The priority level of the numbers is different.
You can select numbers from end only. After selection the number gets erased from the line. You want to maximize the sum of priority level of all the numbers. Your task is to find the maximum sum of the priority values.

Input format

First line contains K and N.
Second line contains N space separated integers denoting the priority of numbers in the list.

Output format

Maximum sum of priority of K numbers

Constraints

1KN105

0Ai109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Sum of priorities will be maximised if he selects 4th chocolate first , followed by 3rd one

Editor Image

?