-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstack basic operations
More file actions
58 lines (44 loc) · 1.32 KB
/
stack basic operations
File metadata and controls
58 lines (44 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
You are given a stack of N integers. In one operation, you can either pop an element from the stack or push any popped element into the
stack. You need to maximize the top element of the stack after performing exactly K operations. If the stack becomes empty after performing
K operations and there is no other way for the stack to be non-empty, print -1.
Input format :
The first line of input consists of two space-separated integers N and K.
The second line of input consists N space-separated integers denoting the elements of the stack. The first element represents the top of the
stack and the last element represents the bottom of the stack.
Output format :
Print the maximum possible top element of the stack after performing exactly K operations.
Constraints :
1≤N≤2∗106
1≤Ai≤1018
1≤K≤109
SAMPLE INPUT
6 4
1 2 4 3 3 5
SAMPLE OUTPUT
4
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,k; cin>>n>>k; long long a[n],i;
// stack<int> s;
for(i=0;i<n;i++)
{
cin>>a[i];
//s.push(a[i]);
}
if((n==1) && (k%2==1)) cout<<"-1";
else if(k==n) cout<<*max_element(a,a+n-1);
else if(k<n)
{
long long m=*max_element(a,a+k-1);
if(m>a[k]) cout<<m;
else cout<<a[k];
}
else
{
cout<<*max_element(a,a+n);
}
}