이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 2;
const int mod = 1e9 + 7;
ll a[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
auto Can = [&] (ll penalty) {
vector<ll> dp(n + 1);
vector<int> cnt(n + 1);
vector<pair<ll, int>> p(n + 1);
p[0] = {0, 0};
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
cnt[i] = cnt[i - 1];
p[i] = p[i - 1];
if (-a[i] > p[i].first) p[i] = {-a[i], i};
if (dp[i] < p[i].first + penalty + a[i]) {
dp[i] = p[i].first + penalty + a[i];
cnt[i] = cnt[p[i].second] + 1;
}
if (dp[i] - a[i] > p[i].first) p[i] = {dp[i] - a[i], i};
}
if (cnt[n] > m) return (ll)-1e18;
return dp[n] - (ll) cnt[n] * penalty;
};
ll l = -1e18, r = 0, ans = 0;
while (l <= r) {
ll penalty = l + r >> 1;
if (Can(penalty) != -1e18) {
ans = penalty;
l = penalty + 1;
}
else r = penalty - 1;
}
ll o = Can(ans);
if (o < 0) o = 0;
cout << o << en;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
feast.cpp: In function 'int main()':
feast.cpp:43:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | ll penalty = l + r >> 1;
| ~~^~~
# | 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... |