Submission #791940

# Submission time Handle Problem Language Result Execution time Memory
791940 2023-07-24T12:43:39 Z vjudge1 Feast (NOI19_feast) C++17
100 / 100
117 ms 5024 KB
//i_love_aisukiuwu
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
	#include "/home/trucmai/.vim/tools.h"
	#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
	#else
	#define debug(x...)
#endif

void open(){
  if(fopen("i.inp","r")){
    freopen("i.inp","r",stdin);
    freopen("o.out","w",stdout);
  }
}

#define ll long long
#define all(a) (a).begin(), (a).end()
#define vi vector<ll>
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define fi first
#define se second
#define gcd __gcd
#define mset(a,v) memset(a, v, sizeof(a))
#define endl '\n'
#define spc " "

const int MN1 = 1e6 + 5,MN2 = 2e3 + 5,LOG = 27;
const ll MOD = 1e9 + 7,INF = 1e9;
ll n,k,a[MN1],pf[MN1];

void solve(){
  cin>>n>>k;
  for(ll i = 1;i <= n;++i){
    cin>>a[i];
    pf[i] = pf[i-1] + a[i];
  } 
  ll l = 0,r = 3e14,res = 0;
  auto dp=[&](ll x)->pi{
    pi mx = {0,0},f = {0,0};
    for(ll i = 1;i <= n;++i){
      f = max(f,{mx.fi + pf[i] - x,mx.se - 1}); 
      mx = max(mx,{f.fi - pf[i],f.se});
    }
    f.se *= -1;
    return f;
  };
  while(l <= r){
    ll m = (r+l) >> 1; 
    auto [val,cnt] = dp(m);
    debug(l,r,cnt);
    if(cnt <= k){
      r = m - 1;
      res = val + k*m;
    }
    else l = m + 1;
  }
  cout<<res;
}

signed main(){
  cin.tie(0) -> sync_with_stdio(0);
  open();
  ll t = 1; //cin>>t;
  while(t--) solve(); 
  
  #ifdef LOCAL
    cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  #endif
}

Compilation message

feast.cpp: In function 'void open()':
feast.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     freopen("i.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
feast.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen("o.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4860 KB Output is correct
2 Correct 43 ms 4988 KB Output is correct
3 Correct 34 ms 4912 KB Output is correct
4 Correct 34 ms 4948 KB Output is correct
5 Correct 34 ms 4940 KB Output is correct
6 Correct 40 ms 4872 KB Output is correct
7 Correct 33 ms 4892 KB Output is correct
8 Correct 34 ms 4972 KB Output is correct
9 Correct 34 ms 4904 KB Output is correct
10 Correct 33 ms 4920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 4984 KB Output is correct
2 Correct 35 ms 4940 KB Output is correct
3 Correct 30 ms 4864 KB Output is correct
4 Correct 29 ms 4872 KB Output is correct
5 Correct 33 ms 4868 KB Output is correct
6 Correct 29 ms 4852 KB Output is correct
7 Correct 30 ms 5016 KB Output is correct
8 Correct 34 ms 4976 KB Output is correct
9 Correct 38 ms 4864 KB Output is correct
10 Correct 30 ms 4876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 4948 KB Output is correct
2 Correct 58 ms 4904 KB Output is correct
3 Correct 60 ms 4948 KB Output is correct
4 Correct 57 ms 4908 KB Output is correct
5 Correct 57 ms 4948 KB Output is correct
6 Correct 59 ms 4884 KB Output is correct
7 Correct 58 ms 4928 KB Output is correct
8 Correct 57 ms 4948 KB Output is correct
9 Correct 65 ms 5024 KB Output is correct
10 Correct 61 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 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 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 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 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 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
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4860 KB Output is correct
2 Correct 43 ms 4988 KB Output is correct
3 Correct 34 ms 4912 KB Output is correct
4 Correct 34 ms 4948 KB Output is correct
5 Correct 34 ms 4940 KB Output is correct
6 Correct 40 ms 4872 KB Output is correct
7 Correct 33 ms 4892 KB Output is correct
8 Correct 34 ms 4972 KB Output is correct
9 Correct 34 ms 4904 KB Output is correct
10 Correct 33 ms 4920 KB Output is correct
11 Correct 41 ms 4984 KB Output is correct
12 Correct 35 ms 4940 KB Output is correct
13 Correct 30 ms 4864 KB Output is correct
14 Correct 29 ms 4872 KB Output is correct
15 Correct 33 ms 4868 KB Output is correct
16 Correct 29 ms 4852 KB Output is correct
17 Correct 30 ms 5016 KB Output is correct
18 Correct 34 ms 4976 KB Output is correct
19 Correct 38 ms 4864 KB Output is correct
20 Correct 30 ms 4876 KB Output is correct
21 Correct 62 ms 4948 KB Output is correct
22 Correct 58 ms 4904 KB Output is correct
23 Correct 60 ms 4948 KB Output is correct
24 Correct 57 ms 4908 KB Output is correct
25 Correct 57 ms 4948 KB Output is correct
26 Correct 59 ms 4884 KB Output is correct
27 Correct 58 ms 4928 KB Output is correct
28 Correct 57 ms 4948 KB Output is correct
29 Correct 65 ms 5024 KB Output is correct
30 Correct 61 ms 4948 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 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 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 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 85 ms 4896 KB Output is correct
62 Correct 83 ms 5012 KB Output is correct
63 Correct 94 ms 4784 KB Output is correct
64 Correct 87 ms 4976 KB Output is correct
65 Correct 90 ms 4892 KB Output is correct
66 Correct 117 ms 4960 KB Output is correct
67 Correct 99 ms 4816 KB Output is correct
68 Correct 62 ms 4972 KB Output is correct
69 Correct 57 ms 4884 KB Output is correct
70 Correct 72 ms 4776 KB Output is correct