이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int MAXN = 1e5+5;
ll pref[MAXN], suf[MAXN], in[MAXN];
int main()
{
int n, k;
cin >> n >> k;
vector<int> max_idx;
pref[0] = suf[n+1] = 0;
for(int i = 1; i <= n; i++) {
cin >> in[i];
pref[i] = pref[i - 1] + in[i];
if(max_idx.size() == 0 || in[max_idx[0]] <= in[i])
{
if(!max_idx.empty() && in[max_idx[0]] < in[i])
max_idx.clear();
max_idx.push_back(i);
}
}
/*for(int i = n; i >= 1; i--)
{
suf[i] = suf[i + 1] + in[i];
}*/
ll ans = 1e18;
for(int idx : max_idx)
{
//cout << idx << "\n";
ll temp = 1e18;
int right = n;
ll other = 0;
while(right > max(idx, n - k + 1))
{
other += in[right];
right--;
}
right++;
for(int left = 0; left <= min(k - 1, idx - 1); left++)
{
while((n - right + 1) + left + 1 < k && left <= min(k - 1, idx - 1))
left++;
if((n - right + 1) + left + 1 < k)
break;
temp = min(temp, pref[left] + in[idx] + other);
if(right == n+1)
break;
other -= in[right];
right++;
}
ans = min(temp, ans);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |