This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define int long long
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
vector<int> ans;
void maxsum(vector<int> &v){
int n = v.size();
int mx = INT_MIN, mx_ending = 0;
for (int i = 0; i < n;i++){
mx_ending = mx_ending + v[i];
if(mx<mx_ending){
mx = mx_ending;
}
if(mx_ending<0){
if(mx>0)ans.push_back(mx);
mx_ending = 0;
}
}
if (mx_ending >0)
{
ans.push_back(mx);
}
}
void solve(int tt)
{
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n;i++){
int x;
cin >> x;
v.push_back(x);
}
maxsum(v);
int ase = ans.size();
int dorkar = min(ase, k);
sort(ans.rbegin(), ans.rend());
int sum = 0;
for (int i = 0; i <dorkar;i++){
//cout << ans[i] << " ";
sum += ans[i];
}
cout << sum << endl;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
int tt = 1;
while (t--)
{
solve(tt++);
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |