#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FOD(i,b,a) for (int i = (b), _a = (a); i >= _a; i--)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define CNTBIT(x) __builtin_popcount(x)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define left ___left
#define right ___right
#define pii pair<int, int>
#define pll pair<ll, ll>
#define DEBUG(n, a) FOR (i, 1, n) cout << a[i] << ' '; cout << endl;
#define lob lower_bound // >=
#define upb upper_bound // >
#define endl "\n"
#define NAME "main"
using namespace std;
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y){ x = y; return true; } return false; }
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y){ x = y; return true; } return false; }
#define gogobruhbruh ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
const int MOD = 1e9 + 7;
void add(int &a, int b){ if ((a += b) >= MOD) a -= MOD; }
int sub(int a, int b){ if ((a -= b) < 0) a += MOD; return a; }
int muti(int a, int b){ return (1LL * a * b) % MOD; }
int Pow(int x, int y){ int res = 1; for (; y; y >>= 1){ if (y & 1) res = muti(res, x); x = muti(x, x); } return res; }
const int MAX = 3e5 + 7;
const ll INF = 1e18 + 5;
int num, lim;
ll pre[MAX], dp[MAX], left, right;
int trace[MAX];
bool ok[MAX];
int Trace(){
int res = 0;
for (int i = num; i; i = trace[i]){
while (!ok[i])
i--;
res += i > 0;
}
return res;
}
pair<ll, int> calc(ll cost){
ok[0] = 1;
ll Max = 0, res = 0;
int idx = 0;
// dp[i] = max(dp[j] + pre[i] - pre[j]);
FOR (i, 1, num){
ok[i] = maximize(res, Max + pre[i] + cost);
trace[i] = idx;
if (maximize(Max, res - pre[i]))
idx = i;
}
return {res, Trace()};
}
void solve(){
// cout << pre[num] << endl;
left = -right;
ll res = 0;
// cout << right << endl;
// DEBUG(num, pre);
while (left <= right){
ll mid = (left + right) >> 1;
auto[cost, k] = calc(mid);
if (k >= lim){
res = cost - k * mid;
right = mid - 1;
}else
left = mid + 1;
}
cout << max(0LL, res);
}
void read(){
cin >> num >> lim;
ll Min = 0;
FOR (i, 1, num)
cin >> pre[i],
pre[i] += pre[i - 1],
maximize(right, pre[i] - Min),
minimize(Min, pre[i]);
}
int main(){
gogobruhbruh;
file("main");
int test = 1;
// cin >> test;
while (test--)
read(),
solve();
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:101:5: note: in expansion of macro 'file'
101 | file("main");
| ^~~~
feast.cpp:30:95: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:101:5: note: in expansion of macro 'file'
101 | file("main");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
4756 KB |
Output is correct |
2 |
Incorrect |
66 ms |
4760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
4776 KB |
Output is correct |
2 |
Correct |
33 ms |
4892 KB |
Output is correct |
3 |
Correct |
31 ms |
4804 KB |
Output is correct |
4 |
Correct |
45 ms |
4740 KB |
Output is correct |
5 |
Correct |
49 ms |
4784 KB |
Output is correct |
6 |
Correct |
27 ms |
4684 KB |
Output is correct |
7 |
Correct |
46 ms |
4888 KB |
Output is correct |
8 |
Correct |
60 ms |
4876 KB |
Output is correct |
9 |
Correct |
54 ms |
4684 KB |
Output is correct |
10 |
Correct |
56 ms |
4880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
4852 KB |
Output is correct |
2 |
Correct |
59 ms |
4812 KB |
Output is correct |
3 |
Correct |
68 ms |
4836 KB |
Output is correct |
4 |
Correct |
64 ms |
4772 KB |
Output is correct |
5 |
Correct |
79 ms |
4852 KB |
Output is correct |
6 |
Correct |
66 ms |
4764 KB |
Output is correct |
7 |
Correct |
60 ms |
4880 KB |
Output is correct |
8 |
Correct |
59 ms |
4856 KB |
Output is correct |
9 |
Correct |
76 ms |
4892 KB |
Output is correct |
10 |
Correct |
68 ms |
4884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
4756 KB |
Output is correct |
2 |
Incorrect |
66 ms |
4760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |