답안 #848147

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
848147 2023-09-11T12:40:42 Z hehege Feast (NOI19_feast) C++14
30 / 100
28 ms 11796 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int N = 3e5 + 9;
const int N2 = 5e3 + 9;
const int INF = 1e18 + 9;

int n, k, a[N], ps[N], f[N], pre[N2], dp[N2][N2];

namespace sub1 {
	bool valid (){
		for (int i = 1; i <= n; i++) if (a[i] < 0) return 0;
		return 1;
	}
	
	void solve (){
		int res = 0;
		for (int i = 1; i <= n; i++) res += a[i];
		cout << res;
	}
}

namespace sub2 {
	bool valid (){
		int x = 0;
		for (int i = 1; i <= n; i++){
			if (a[i] < 0) x++;
			if (x >= 2) break;
		}
		return x <= 1;
	}
	
	void solve (){
		int sum1 = 0, sum2 = 0, sumall = 0;
		bool flag = 0;
		for (int i = 1; i <= n; i++){
			if (a[i] < 0) flag = 1;
			else if (flag == 0) sum1 += a[i];
			else sum2 += a[i];
		}
		for (int i = 1; i <= n; i++) sumall += a[i];
		if (k == 1) cout << max (sum1, max (sum2, sumall));
		else cout << sum1 + sum2;
	}
}

namespace sub3 {
	bool valid (){
		return k == 1;
	}
	
	void solve (){
		for (int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i];
		f[n] = ps[n];
		for (int i = n - 1; i >= 1; i--) f[i] = max (f[i + 1], ps[i]);
		int res = 0;
		for (int i = 1; i <= n; i++) res = max (res, f[i] - ps[i - 1]);
		cout << res;
	}
}

void sub6 (){
	dp[1][1] = a[1];
    pre[1] = a[1];
    int res = a[1];
    for (int i = 2; i <= n; i++){
    	for (int j = 1; j <= k; j++){
    		if (j > i) continue;
    		dp[i][j] = max (dp[i][j], max (dp[i - 1][j] + a[i], pre[j - 1] + a[i]));
    		pre[j] = max (pre[j], dp[i][j]);
    		res = max (res, dp[i][j]);
    	}
    }
    cout << res;
}

signed main (){
    ios_base::sync_with_stdio (false);
    cin.tie (NULL);
    cout.tie (NULL);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (sub1::valid ()) sub1::solve ();
    else if (sub2::valid ()) sub2::solve ();
    else if (sub3::valid ()) sub3::solve ();
    else sub6 ();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 4440 KB Output is correct
2 Correct 22 ms 4440 KB Output is correct
3 Correct 22 ms 4552 KB Output is correct
4 Correct 22 ms 4564 KB Output is correct
5 Correct 22 ms 4440 KB Output is correct
6 Correct 22 ms 4444 KB Output is correct
7 Correct 21 ms 4444 KB Output is correct
8 Correct 22 ms 4560 KB Output is correct
9 Correct 21 ms 4444 KB Output is correct
10 Correct 22 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 4444 KB Output is correct
2 Correct 18 ms 4700 KB Output is correct
3 Correct 15 ms 5720 KB Output is correct
4 Correct 15 ms 5724 KB Output is correct
5 Correct 22 ms 7260 KB Output is correct
6 Correct 15 ms 5572 KB Output is correct
7 Correct 15 ms 5720 KB Output is correct
8 Correct 23 ms 7516 KB Output is correct
9 Correct 25 ms 7236 KB Output is correct
10 Correct 15 ms 5700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8532 KB Output is correct
2 Correct 28 ms 11480 KB Output is correct
3 Correct 27 ms 11696 KB Output is correct
4 Correct 28 ms 11580 KB Output is correct
5 Correct 27 ms 11600 KB Output is correct
6 Correct 28 ms 11740 KB Output is correct
7 Correct 28 ms 11600 KB Output is correct
8 Correct 27 ms 11708 KB Output is correct
9 Correct 28 ms 11600 KB Output is correct
10 Correct 28 ms 11796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 4440 KB Output is correct
2 Correct 22 ms 4440 KB Output is correct
3 Correct 22 ms 4552 KB Output is correct
4 Correct 22 ms 4564 KB Output is correct
5 Correct 22 ms 4440 KB Output is correct
6 Correct 22 ms 4444 KB Output is correct
7 Correct 21 ms 4444 KB Output is correct
8 Correct 22 ms 4560 KB Output is correct
9 Correct 21 ms 4444 KB Output is correct
10 Correct 22 ms 4444 KB Output is correct
11 Correct 14 ms 4444 KB Output is correct
12 Correct 18 ms 4700 KB Output is correct
13 Correct 15 ms 5720 KB Output is correct
14 Correct 15 ms 5724 KB Output is correct
15 Correct 22 ms 7260 KB Output is correct
16 Correct 15 ms 5572 KB Output is correct
17 Correct 15 ms 5720 KB Output is correct
18 Correct 23 ms 7516 KB Output is correct
19 Correct 25 ms 7236 KB Output is correct
20 Correct 15 ms 5700 KB Output is correct
21 Correct 26 ms 8532 KB Output is correct
22 Correct 28 ms 11480 KB Output is correct
23 Correct 27 ms 11696 KB Output is correct
24 Correct 28 ms 11580 KB Output is correct
25 Correct 27 ms 11600 KB Output is correct
26 Correct 28 ms 11740 KB Output is correct
27 Correct 28 ms 11600 KB Output is correct
28 Correct 27 ms 11708 KB Output is correct
29 Correct 28 ms 11600 KB Output is correct
30 Correct 28 ms 11796 KB Output is correct
31 Correct 1 ms 4440 KB Output is correct
32 Incorrect 1 ms 4700 KB Output isn't correct
33 Halted 0 ms 0 KB -