#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
#define int long long
const int N=3e5+10;
int n, k;
ll a[N], p[N];
pair<ll, int> dp[N];
bool can(ll x)
{
FOR(i, 0, n+1) dp[i]={0,0};
pair<ll, int> best={0,0};
FOR(i, 1, n+1)
{
dp[i]=max(dp[i-1], {best.f+a[i]-x+p[i],best.s+1});
best=max(best, {dp[i].f-p[i],dp[i].s});
}
return dp[n].s<=k;
}
ll build(ll x)
{
FOR(i, 0, n+1) dp[i]={0,0};
pair<ll, int> best={0,0};
FOR(i, 1, n+1)
{
dp[i]=max(dp[i-1], {best.f-x+p[i],best.s+1});
best=max(best, {dp[i].f-p[i],dp[i].s});
}
return dp[n].f+dp[n].s*x;
}
signed main()
{
setIO();
cin >> n >> k;
FOR(i, 1, n+1) cin >> a[i];
FOR(i, 1, n+1) p[i]=p[i-1]+a[i];
int l=-INF, r=INF;
while(l<r){
int m=(l+r)/2;
if(can(m)) r=m;
else l=m+1;
}
cout << build(l);
}
Compilation message
feast.cpp: In function 'void setIO(std::string)':
feast.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
10040 KB |
Output is correct |
2 |
Correct |
107 ms |
10120 KB |
Output is correct |
3 |
Correct |
109 ms |
10320 KB |
Output is correct |
4 |
Correct |
139 ms |
10256 KB |
Output is correct |
5 |
Correct |
136 ms |
10144 KB |
Output is correct |
6 |
Correct |
125 ms |
10132 KB |
Output is correct |
7 |
Correct |
111 ms |
9952 KB |
Output is correct |
8 |
Correct |
111 ms |
10156 KB |
Output is correct |
9 |
Correct |
113 ms |
10064 KB |
Output is correct |
10 |
Correct |
102 ms |
10164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
10092 KB |
Output is correct |
2 |
Correct |
85 ms |
10308 KB |
Output is correct |
3 |
Correct |
94 ms |
10096 KB |
Output is correct |
4 |
Correct |
138 ms |
10104 KB |
Output is correct |
5 |
Correct |
115 ms |
10032 KB |
Output is correct |
6 |
Correct |
84 ms |
10088 KB |
Output is correct |
7 |
Correct |
97 ms |
10320 KB |
Output is correct |
8 |
Correct |
106 ms |
10240 KB |
Output is correct |
9 |
Correct |
132 ms |
10028 KB |
Output is correct |
10 |
Correct |
105 ms |
10192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
10148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
10040 KB |
Output is correct |
2 |
Correct |
107 ms |
10120 KB |
Output is correct |
3 |
Correct |
109 ms |
10320 KB |
Output is correct |
4 |
Correct |
139 ms |
10256 KB |
Output is correct |
5 |
Correct |
136 ms |
10144 KB |
Output is correct |
6 |
Correct |
125 ms |
10132 KB |
Output is correct |
7 |
Correct |
111 ms |
9952 KB |
Output is correct |
8 |
Correct |
111 ms |
10156 KB |
Output is correct |
9 |
Correct |
113 ms |
10064 KB |
Output is correct |
10 |
Correct |
102 ms |
10164 KB |
Output is correct |
11 |
Correct |
76 ms |
10092 KB |
Output is correct |
12 |
Correct |
85 ms |
10308 KB |
Output is correct |
13 |
Correct |
94 ms |
10096 KB |
Output is correct |
14 |
Correct |
138 ms |
10104 KB |
Output is correct |
15 |
Correct |
115 ms |
10032 KB |
Output is correct |
16 |
Correct |
84 ms |
10088 KB |
Output is correct |
17 |
Correct |
97 ms |
10320 KB |
Output is correct |
18 |
Correct |
106 ms |
10240 KB |
Output is correct |
19 |
Correct |
132 ms |
10028 KB |
Output is correct |
20 |
Correct |
105 ms |
10192 KB |
Output is correct |
21 |
Incorrect |
126 ms |
10148 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |