Stack and Queue <Nissan>
Practice
3.5 (63 votes)
Arrays
Data structures
Easy
Implementation
Queue
Stacks
Problem
87% Success 27714 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove exactly K elements such that the sum of the K removed elements is maximised.

Input format :     

  • The first line consists of two space-separated integers N and K.
  • The second line consists of N space-separated integers denoting the elements of the stack.

Output format :

  • Print the maximum possible sum of the K removed elements

Constraints :

  • \(1 \le N \le 10^{5}\)
  • \(1 \le K \le N\)
  • \(1 \le A_{i} \le 10^{9}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
44 votes
Tags:
Data StructuresEasyStacks
Points:20
10 votes
Tags:
StacksBasics of StacksSetsData Structures
Points:20
48 votes
Tags:
Data StructuresEasyHiringQueueStacks