#include <bits/stdc++.h>
#define fi(i, a, b) for( int i = a; i <= b; i++ )
#define fid(i, a, b) for( int i = a; i >= b; i-- )
#define getbit(x, i) ((x>>i)&1)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define st first
#define nd second
#define mp make_pair
#define HTManh "trochoi"
#define maxn 100009
#define endl '\n'
using namespace std;
int n, q;
ll a[100009];
int tv[100009];
pll dp[100009][2];
pll calc(ll x)
{
dp[1][0] = {0,0};
dp[1][1] = {a[1] - x, 1};
fi(i,2,n)
{
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = max(mp(dp[i-1][0].st + a[i] - x, dp[i-1][0].nd + 1),
mp(dp[i-1][1].st + a[i], dp[i-1][1].nd));
}
return max(dp[n][0], dp[n][1]);
}
void sub4()
{
ll d = 0, c = 1e9;
//fi(i,0,10) cout << i << " " << calc(i).st << " " << calc(i).nd << endl;
while(d <= c)
{
int g = (d+c)/2;
if (calc(g).nd >= tv[1]) d = g + 1;
else c = g - 1;
}
cout << calc(c).st + c*calc(c).nd << endl;
}
void sub5()
{
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
if (fopen(HTManh".inp", "r"))
{
freopen(HTManh".inp", "r", stdin);
freopen(HTManh".out", "w", stdout);
}
cin >> n >> tv[1];
fi(i,1,n) cin >> a[i];
sub4();
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(HTManh".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(HTManh".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |