/***************************************************************
* Author : Nguyen Trong Van Viet *
* Age : 17 *
* School : 12T2 Le Khiet High School for the Gifted *
* Hometown : Quang Ngai , Viet Nam . *
* Khanh An is my lover :) the more I code , the nearer I am *
****************************************************************/
#define TASK "text"
#define INPUT TASK".INP"
#define OUTPUT TASK".OUT"
bool mtt = 0 ;
int test = 1 ;
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define db double
#define ve vector
#define vi vector<int>
#define vll vector<ll>
#define str string
#define pb push_back
#define pk pop_back
#define el '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define fi first
#define se second
#define uni(a) sort(all(a)),a.resize(unique(all(a))-a.begin())
#define FOR(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define FORD(i,a,b) for(int i=(int)(a);i>=(int)(b);i--)
#define FORN(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define all(a) a.begin(),a.end()
#define btpc __builtin_popcountll
#define LB lower_bound
#define UB upper_bound
#define tct template<class T>
#define BIT(msk,i) (msk>>(i)&1)
ll lg(ll a){return __lg(a);}
ll sq(ll a){return a*a;}
ll gcd(ll a,ll b){return __gcd(a,b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll rd(ll l , ll r ){return l+1LL*rand()*rand()*rand()%(r-l+1);}
#define prt(a,n) FOR(i,1,n)cout<<a[i]<<" ";cout<<el;
#define prv(a) for(auto v:a)cout<<v<<" "; cout<<el;
tct bool mini(T& a,T b){return (a>b)?a=b,1:0;}
tct bool maxi(T& a,T b){return (a<b)?a=b,1:0;}
int xx[] = {0,0,-1,0,1};
int yy[] = {0,-1,0,1,0};
const db PI = acos(-1) , EPS = 1e-9;
const ll inf = 1e18 , cs = 331 , sm = 1e9+7;
const int N = 3e5+5 , oo = 2e9 , LO = 17 , CH = 26 ;
int n , k;
int a[N] ;
ll s[N] ;
void doc()
{
cin>> n >> k;
FOR(i,1,n)cin>>a[i] , s[i]=s[i-1]+a[i] ;
}
namespace sub1
{
pll f[N] ;
bool CK(ll x)
{
f[0] = {0,0};
// f[i] = s[i]+(f[j]-s[j]+x);
pii curmx = {x,-1};
FOR(i,1,n+1){
f[i] = max(f[i-1],mp(curmx.fi+s[i],curmx.se));
maxi(curmx,mp(f[i].fi-s[i]+x,f[i].se-1));
// trace(i,f[i].fi,f[i].se);
}
return -f[n].se<=k ;
// return mp(f[n].fi,-f[n].se);
}
void xuly()
{
ll l = -1e18 ;
ll r= 0;
ll ans = -1 ;
while(l<=r)
{
ll mid =(l+r)/2 ;
if(CK(mid))
{
ans=mid,l=mid+1;
}
else r=mid-1 ;
}
CK(ans) ;
cout<<f[n].fi+ans*f[n].se ;
}
}
/* DON'T BELIEVE LOVE WILL INSPIRE YOU -> TRAIN HARDER -> YOU WILL GET THE LOVE YOU WANT !!*/
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);srand(time(0));
if(fopen(INPUT,"r"))
{
freopen(INPUT ,"r",stdin);
freopen(OUTPUT,"w",stdout);
}
if(mtt)cin>>test;
FOR(i,1,test)
{
doc() ;
sub1::xuly() ;
}
cerr<<el<<"Love KA : " << clock() <<"ms"<<el;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(INPUT ,"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
feast.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen(OUTPUT,"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
9592 KB |
Output is correct |
2 |
Correct |
63 ms |
9700 KB |
Output is correct |
3 |
Correct |
65 ms |
9812 KB |
Output is correct |
4 |
Correct |
63 ms |
9752 KB |
Output is correct |
5 |
Correct |
65 ms |
9704 KB |
Output is correct |
6 |
Correct |
62 ms |
9452 KB |
Output is correct |
7 |
Correct |
66 ms |
9584 KB |
Output is correct |
8 |
Correct |
64 ms |
9780 KB |
Output is correct |
9 |
Correct |
65 ms |
9568 KB |
Output is correct |
10 |
Correct |
70 ms |
9640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
9476 KB |
Output is correct |
2 |
Correct |
62 ms |
9676 KB |
Output is correct |
3 |
Correct |
59 ms |
9464 KB |
Output is correct |
4 |
Correct |
60 ms |
9468 KB |
Output is correct |
5 |
Correct |
63 ms |
9420 KB |
Output is correct |
6 |
Correct |
59 ms |
9428 KB |
Output is correct |
7 |
Correct |
59 ms |
9592 KB |
Output is correct |
8 |
Correct |
66 ms |
9644 KB |
Output is correct |
9 |
Correct |
62 ms |
9392 KB |
Output is correct |
10 |
Correct |
65 ms |
9632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
9600 KB |
Output is correct |
2 |
Correct |
87 ms |
9448 KB |
Output is correct |
3 |
Correct |
90 ms |
9528 KB |
Output is correct |
4 |
Correct |
85 ms |
9488 KB |
Output is correct |
5 |
Correct |
92 ms |
9564 KB |
Output is correct |
6 |
Correct |
87 ms |
9628 KB |
Output is correct |
7 |
Correct |
89 ms |
9660 KB |
Output is correct |
8 |
Correct |
87 ms |
9600 KB |
Output is correct |
9 |
Correct |
89 ms |
9684 KB |
Output is correct |
10 |
Correct |
87 ms |
9660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
9592 KB |
Output is correct |
2 |
Correct |
63 ms |
9700 KB |
Output is correct |
3 |
Correct |
65 ms |
9812 KB |
Output is correct |
4 |
Correct |
63 ms |
9752 KB |
Output is correct |
5 |
Correct |
65 ms |
9704 KB |
Output is correct |
6 |
Correct |
62 ms |
9452 KB |
Output is correct |
7 |
Correct |
66 ms |
9584 KB |
Output is correct |
8 |
Correct |
64 ms |
9780 KB |
Output is correct |
9 |
Correct |
65 ms |
9568 KB |
Output is correct |
10 |
Correct |
70 ms |
9640 KB |
Output is correct |
11 |
Correct |
62 ms |
9476 KB |
Output is correct |
12 |
Correct |
62 ms |
9676 KB |
Output is correct |
13 |
Correct |
59 ms |
9464 KB |
Output is correct |
14 |
Correct |
60 ms |
9468 KB |
Output is correct |
15 |
Correct |
63 ms |
9420 KB |
Output is correct |
16 |
Correct |
59 ms |
9428 KB |
Output is correct |
17 |
Correct |
59 ms |
9592 KB |
Output is correct |
18 |
Correct |
66 ms |
9644 KB |
Output is correct |
19 |
Correct |
62 ms |
9392 KB |
Output is correct |
20 |
Correct |
65 ms |
9632 KB |
Output is correct |
21 |
Correct |
90 ms |
9600 KB |
Output is correct |
22 |
Correct |
87 ms |
9448 KB |
Output is correct |
23 |
Correct |
90 ms |
9528 KB |
Output is correct |
24 |
Correct |
85 ms |
9488 KB |
Output is correct |
25 |
Correct |
92 ms |
9564 KB |
Output is correct |
26 |
Correct |
87 ms |
9628 KB |
Output is correct |
27 |
Correct |
89 ms |
9660 KB |
Output is correct |
28 |
Correct |
87 ms |
9600 KB |
Output is correct |
29 |
Correct |
89 ms |
9684 KB |
Output is correct |
30 |
Correct |
87 ms |
9660 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
122 ms |
9464 KB |
Output is correct |
62 |
Correct |
118 ms |
9696 KB |
Output is correct |
63 |
Correct |
100 ms |
9456 KB |
Output is correct |
64 |
Correct |
123 ms |
9516 KB |
Output is correct |
65 |
Correct |
125 ms |
9616 KB |
Output is correct |
66 |
Correct |
121 ms |
9568 KB |
Output is correct |
67 |
Correct |
122 ms |
9368 KB |
Output is correct |
68 |
Correct |
87 ms |
9632 KB |
Output is correct |
69 |
Correct |
89 ms |
9448 KB |
Output is correct |
70 |
Correct |
86 ms |
9404 KB |
Output is correct |