Submission #1259127

#TimeUsernameProblemLanguageResultExecution timeMemory
1259127tminhFeast (NOI19_feast)C++20
71 / 100
48 ms30532 KiB
#include "bits/stdc++.h"
using namespace std;

#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>


#define ep emplace_back
#define pb push_back
#define pf push_front


const ll mod = 1e9 + 7;
const int N = 1e6 + 5;
const ll oo = 1e18;

bool START;

int n, k;
bool vis[2005][2005][2];
ll a[N], dp[2005][2005][2];


namespace sub1 {
	void solve() {
		ll ans = 0;
		for (int i = 1; i <= n; ++i) ans += a[i];
		cout << ans;
	}
	bool check() {
		for (int i = 1; i <= n; ++i) if (a[i] < 0) return false;
		return true;
	}
}
namespace sub2 {
	ll pre[N];
	void solve() {
		int pos = 0;
		for (int i = 1; i <= n; ++i) {
			if (a[i] < 0) pos = i;
			pre[i] = pre[i - 1] + a[i];
		}
		if (k > 1) {
			cout << pre[pos - 1] + (pre[n] - pre[pos]);  
		} else {
			cout << max({pre[n], pre[pos - 1], pre[n] - pre[pos]});
		}
	}
	bool check() {
		int cnt = 0;
		for (int i = 1; i <= n; ++i) cnt += (a[i] < 0);
		return (cnt == 1);
	}
}

namespace sub3 {
	void solve() {
		ll ans = 0, premin = 0, cur = 0;
		for (int i = 1; i <= n; ++i) {
			cur += a[i];
			ans = max(ans, cur - premin);
			premin = min(premin, cur);
		}
		cout << ans;
	}
	bool check() {
		return k == 1;
	}
}

namespace sub4 {
	ll f(int pos, int cnt, bool cur) {
		if (pos == n + 1) return (cnt + cur <= k ? 0 : -oo);
		
		ll &res = dp[pos][cnt][cur];
		if (vis[pos][cnt][cur]) return res;
		
	
		vis[pos][cnt][cur] = true;
		res = f(pos + 1, cnt, 1) + a[pos];
		if (cnt + cur <= k) res = max(f(pos + 1, cnt + cur, 0), res); 
		return res;
	}
	void solve() {
		cout << f(1, 0, 0);
	}
	bool check() {
		return (n <= 2000) && (k <= 2000);
	}
}



inline void solve() {
	if (sub1::check()) return sub1::solve();
	if (sub2::check()) return sub2::solve();
	if (sub3::check()) return sub3::solve();
	if (sub4::check()) return sub4::solve();
}


inline void input() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    return solve();
}
bool END;

int main() {
    if(fopen(task ".inp", "r")) {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    input();
    
    
    cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
    cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
    return 0;
}

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |         freopen(task ".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...