Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ld long double
- #define pb push_back
- #define p_b pop_back
- #define si stack<int>
- #define sll stack<ll>
- #define sc stack<char>
- #define vi vector<int>
- #define vll vector<ll>
- #define mii map<int, int>
- #define msi map<string, int>
- #define mci map<char, int>
- #define qc queue<char>
- #define qi queue<int>
- #define qll queue<ll>
- using namespace std;
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- ll n, k;
- cin >> n >> k;
- sll s, t, buf;
- for(int i = 0; i < n; i++){
- ll x;
- cin >> x;
- buf.push(x);
- }
- while(buf.size() > 0){
- s.push(buf.top());
- buf.pop();
- }
- t.push(0);
- if(k > n && n > 1){
- while(s.size() > 0){
- if(t.top() <= s.top()){
- t.push(s.top());
- s.pop();
- } else{
- s.pop();
- }
- k--;
- }
- s.push(t.top());
- k--;
- if(k%2 == 0){
- cout << s.top() << "\n";
- } else{
- cout << t.top() << "\n";
- }
- } else if(n == 1){
- if(k%2 == 0){
- cout << s.top() << "\n";
- } else{
- cout << "-1\n";
- }
- } else{
- while(k-- > 1){
- if(t.top() <= s.top()){
- t.push(s.top());
- s.pop();
- } else{
- s.pop();
- }
- }
- s.push(t.top());
- cout << s.top() << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement