# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1103121 |
2024-10-20T07:23:17 Z |
khoaha123 |
Feast (NOI19_feast) |
C++14 |
|
524 ms |
12880 KB |
#include <bits/stdc++.h>
#define endl '\n'
#define ii pair<int,int>
#define task ""
#define all(x) x.begin(),x.end()
#define MASK(x) (1 << (x))
#define int long long
using namespace std;
const int N = 3e5 + 5;
const int LOG = 20;
const int mod = 998244353;
const long long INF = 1e18;
int n,a[N],dp[85][85][85],s[N];
int calc(int l,int r,int k){
if (k == 0) return 0;
if (l == r) return max(a[l],0LL);
int &res = dp[l][r][k];
if (res != -1) return res;
res = 0;
for (int i = l; i < r; ++i){
for (int j = 0; j <= min(k,(i-l+1)); ++j){
res = max(res,calc(l,i,j) + calc(i+1,r,k-j));
}
}
res = max(res,s[r] - s[l-1]);
return res;
}
int k;
void subtask1(void){
int sum = 0;
for (int i = 1; i <= n; ++i) sum += a[i];
cout << sum;
}
void subtask2(void){
int pos = -1;
for (int i = 1; i <= n; ++i){
if (a[i] < 0){
pos = i;break;
}
}
if (pos == -1){
subtask1();return;
}
int sum = 0;
for (int i = 1; i <= n; ++i) sum += a[i];
if (k == 1){
int sum1 = 0,sum2 = 0;
for (int i = 1; i < pos; ++i) sum1 += a[i];
for (int i = pos+1; i <= n; ++i) sum2 += a[i];
cout << max(sum1,sum2);
}
else {
cout << sum - a[pos];
}
}
void subtask3(void){
int best = 0;
int sum = 0;
for (int i = 1; i <= n; ++i){
sum = max(a[i],sum + a[i]);
best = max(best,sum);
}
cout << best;
}
void subtask4(void){
memset(dp,-1,sizeof(dp));
cout << calc(1,n,k) << endl;
}
long long Rand(long long l, long long r) {
long long res = 0;
for (int i= 1; i <= 4; ++i) res = (res << 15) ^ (rand() & ((1 << 15) - 1));
return l + res % (r-l+1);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
freopen(task".OUT","w",stdout);
}
srand(time(NULL));
cin >> n >> k;
int sub1 = 0,am = 0;
for (int i = 1; i <= n; ++i){
cin >> a[i];
//a[i] = Rand(-1000000000,1000000000);
if (a[i] >= 0) sub1++;
else am++;
s[i] = s[i-1] + a[i];
}
if (sub1 == n){
subtask1();
return 0;
}
if (k == 1){
subtask3();
return 0;
}
if (am == 1){
subtask2();
return 0;
}
subtask4();
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
feast.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(task".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
7760 KB |
Output is correct |
2 |
Correct |
20 ms |
7760 KB |
Output is correct |
3 |
Correct |
21 ms |
7760 KB |
Output is correct |
4 |
Correct |
20 ms |
7812 KB |
Output is correct |
5 |
Correct |
20 ms |
7760 KB |
Output is correct |
6 |
Correct |
20 ms |
7680 KB |
Output is correct |
7 |
Correct |
20 ms |
7816 KB |
Output is correct |
8 |
Correct |
20 ms |
7760 KB |
Output is correct |
9 |
Correct |
20 ms |
7760 KB |
Output is correct |
10 |
Correct |
21 ms |
7760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
7760 KB |
Output is correct |
2 |
Correct |
14 ms |
7760 KB |
Output is correct |
3 |
Correct |
13 ms |
7760 KB |
Output is correct |
4 |
Correct |
13 ms |
7804 KB |
Output is correct |
5 |
Correct |
20 ms |
7820 KB |
Output is correct |
6 |
Correct |
13 ms |
7664 KB |
Output is correct |
7 |
Correct |
13 ms |
7772 KB |
Output is correct |
8 |
Correct |
21 ms |
7760 KB |
Output is correct |
9 |
Correct |
20 ms |
7760 KB |
Output is correct |
10 |
Correct |
14 ms |
7824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
7880 KB |
Output is correct |
2 |
Correct |
22 ms |
7760 KB |
Output is correct |
3 |
Correct |
22 ms |
7816 KB |
Output is correct |
4 |
Correct |
27 ms |
7760 KB |
Output is correct |
5 |
Correct |
25 ms |
7760 KB |
Output is correct |
6 |
Correct |
25 ms |
7760 KB |
Output is correct |
7 |
Correct |
26 ms |
7852 KB |
Output is correct |
8 |
Correct |
31 ms |
7760 KB |
Output is correct |
9 |
Correct |
26 ms |
7844 KB |
Output is correct |
10 |
Correct |
25 ms |
7760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
524 ms |
6480 KB |
Output is correct |
3 |
Correct |
7 ms |
6480 KB |
Output is correct |
4 |
Correct |
11 ms |
6648 KB |
Output is correct |
5 |
Correct |
59 ms |
6480 KB |
Output is correct |
6 |
Correct |
49 ms |
6480 KB |
Output is correct |
7 |
Correct |
32 ms |
6480 KB |
Output is correct |
8 |
Correct |
12 ms |
6480 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
13 ms |
6648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
524 ms |
6480 KB |
Output is correct |
3 |
Correct |
7 ms |
6480 KB |
Output is correct |
4 |
Correct |
11 ms |
6648 KB |
Output is correct |
5 |
Correct |
59 ms |
6480 KB |
Output is correct |
6 |
Correct |
49 ms |
6480 KB |
Output is correct |
7 |
Correct |
32 ms |
6480 KB |
Output is correct |
8 |
Correct |
12 ms |
6480 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
13 ms |
6648 KB |
Output is correct |
11 |
Runtime error |
20 ms |
12880 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
524 ms |
6480 KB |
Output is correct |
3 |
Correct |
7 ms |
6480 KB |
Output is correct |
4 |
Correct |
11 ms |
6648 KB |
Output is correct |
5 |
Correct |
59 ms |
6480 KB |
Output is correct |
6 |
Correct |
49 ms |
6480 KB |
Output is correct |
7 |
Correct |
32 ms |
6480 KB |
Output is correct |
8 |
Correct |
12 ms |
6480 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
13 ms |
6648 KB |
Output is correct |
11 |
Runtime error |
20 ms |
12880 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
7760 KB |
Output is correct |
2 |
Correct |
20 ms |
7760 KB |
Output is correct |
3 |
Correct |
21 ms |
7760 KB |
Output is correct |
4 |
Correct |
20 ms |
7812 KB |
Output is correct |
5 |
Correct |
20 ms |
7760 KB |
Output is correct |
6 |
Correct |
20 ms |
7680 KB |
Output is correct |
7 |
Correct |
20 ms |
7816 KB |
Output is correct |
8 |
Correct |
20 ms |
7760 KB |
Output is correct |
9 |
Correct |
20 ms |
7760 KB |
Output is correct |
10 |
Correct |
21 ms |
7760 KB |
Output is correct |
11 |
Correct |
13 ms |
7760 KB |
Output is correct |
12 |
Correct |
14 ms |
7760 KB |
Output is correct |
13 |
Correct |
13 ms |
7760 KB |
Output is correct |
14 |
Correct |
13 ms |
7804 KB |
Output is correct |
15 |
Correct |
20 ms |
7820 KB |
Output is correct |
16 |
Correct |
13 ms |
7664 KB |
Output is correct |
17 |
Correct |
13 ms |
7772 KB |
Output is correct |
18 |
Correct |
21 ms |
7760 KB |
Output is correct |
19 |
Correct |
20 ms |
7760 KB |
Output is correct |
20 |
Correct |
14 ms |
7824 KB |
Output is correct |
21 |
Correct |
23 ms |
7880 KB |
Output is correct |
22 |
Correct |
22 ms |
7760 KB |
Output is correct |
23 |
Correct |
22 ms |
7816 KB |
Output is correct |
24 |
Correct |
27 ms |
7760 KB |
Output is correct |
25 |
Correct |
25 ms |
7760 KB |
Output is correct |
26 |
Correct |
25 ms |
7760 KB |
Output is correct |
27 |
Correct |
26 ms |
7852 KB |
Output is correct |
28 |
Correct |
31 ms |
7760 KB |
Output is correct |
29 |
Correct |
26 ms |
7844 KB |
Output is correct |
30 |
Correct |
25 ms |
7760 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
524 ms |
6480 KB |
Output is correct |
33 |
Correct |
7 ms |
6480 KB |
Output is correct |
34 |
Correct |
11 ms |
6648 KB |
Output is correct |
35 |
Correct |
59 ms |
6480 KB |
Output is correct |
36 |
Correct |
49 ms |
6480 KB |
Output is correct |
37 |
Correct |
32 ms |
6480 KB |
Output is correct |
38 |
Correct |
12 ms |
6480 KB |
Output is correct |
39 |
Correct |
1 ms |
2384 KB |
Output is correct |
40 |
Correct |
13 ms |
6648 KB |
Output is correct |
41 |
Runtime error |
20 ms |
12880 KB |
Execution killed with signal 11 |
42 |
Halted |
0 ms |
0 KB |
- |