Pagini recente » Cod sursa (job #2714386) | Cod sursa (job #3224991) | Cod sursa (job #1444296) | Cod sursa (job #2911292) | Cod sursa (job #3173664)
#ifndef LOCAL
#pragma GCC optimize("Ofast")
#endif
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;
using ci = const int;
using cll = const long long;
using namespace std;
/*******************************/
// INPUT / OUTPUT
#ifndef LOCAL
ifstream in("deque.in");
ofstream out("deque.out");
#define cin in
#define cout out
#endif
/*******************************/
/// GLOBAL DECLARATIONS
int N, K;
ll rasp;
struct TwoStacks
{
stack <pii> st, dr;
inline void push_back(int x)
{
if (st.empty())
st.push({x, x});
else
st.push({x, min(st.top().second, x)});
}
inline void pop_front()
{
if (dr.empty())
{
while (!st.empty())
{
pii top = st.top();
st.pop();
if (dr.empty())
dr.push({top.first, top.first});
else
dr.push({top.first, min(top.first, dr.top().second)});
}
}
dr.pop();
}
inline int get_min()
{
if (st.empty() && dr.empty()) return -1;
if (st.empty()) return dr.top().second;
if (dr.empty()) return st.top().second;
return min(st.top().second, dr.top().second);
}
};
/*******************************/
/// FUNCTIONS
void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
cin >> N >> K;
}
///-------------------------------------
inline void Solution()
{
int x;
TwoStacks ts;
for (int i = 1 ; i <= K ; ++ i)
{
cin >> x;
ts.push_back(x);
}
rasp += 1LL * ts.get_min();
for (int i = K + 1 ; i <= N ; ++ i)
{
cin >> x;
ts.push_back(x);
ts.pop_front();
rasp += 1LL * ts.get_min();
}
}
///-------------------------------------
inline void Output()
{
cout << rasp;
}
///-------------------------------------
int main()
{
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#endif
ReadInput();
Solution();
Output();
return 0;
}