Submission #1017410

#TimeUsernameProblemLanguageResultExecution timeMemory
1017410vjudge1Feast (NOI19_feast)C++17
100 / 100
107 ms12132 KiB
#include<bits/stdc++.h> using namespace std; #define int long long pair<int, int> dp[300005][2]; int n, k, a[300005]; pair<int, int> sigma(int cost) { dp[0][1] = {-1e18, 0}; for (int i = 1; i <= n; i++) { dp[i][0] = max(dp[i-1][0], dp[i-1][1]); pair<int,int> v1 = {dp[i-1][1].first+a[i],dp[i-1][1].second}; pair<int,int> v2 = {dp[i-1][0].first+a[i]-cost, dp[i-1][0].second+1}; dp[i][1] = (v1.first > v2.first ? v1 : v2); //if(cost == 2) cout << i << ' ' << dp[i][0].first << ' ' << dp[i][0].second << ' ' << dp[i][1].first << ' ' << dp[i][1].second << '\n'; } return max(dp[n][0], dp[n][1]); } void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; int l = 0, r = 3e14, mid, ans=0; while(l <= r) { mid = (l + r) / 2; pair<int, int> val = sigma(mid); if(val.second <= k) { // cout << mid << ' ' << val.first << ' ' << val.second << '\n'; ans = max(ans, val.first + mid * val.second); r = mid-1; } else l = mid+1; } cout << ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen("input.inp","r")){ freopen("input.inp", "r", stdin); freopen("output.out", "w", stdout); } int t = 1; // cin >> t; while(t--) { solve(); } }

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen("input.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen("output.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...