# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
833265 |
2023-08-22T03:52:57 Z |
josanneo22 |
Feast (NOI19_feast) |
C++17 |
|
205 ms |
262144 KB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array
const int inf=1e15;
long long maxSubArraySum(vector<long long> &a, int size)
{
long long max_so_far = LLONG_MIN, max_ending_here = 0;
int starting_index = 0, ending_index = 0, s = 0;
for (int i = 0; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here) {
max_so_far = max_ending_here;
starting_index = s;
ending_index = i;
}
if (max_ending_here < 0) {
max_ending_here = 0;
s = i + 1;
}
}
return max_so_far;
}
void solve(){
int n,k; cin>>n>>k;
vector<int> a(n+1); for(int i=1;i<=n;i++) cin>>a[i];
if(*min_element(a.begin(), a.end())>=0){
cout<< accumulate(a.begin(),a.end(),0LL)<<'\n';
return;
}
if(k==1){
cout<<max(0LL,maxSubArraySum(a,n+1))<<'\n';
return;
}
int cnt=0,pos=-1;
for(int i=1;i<=n;i++){
if(a[i]<0){
cnt++;
pos=i;
}
}
if(cnt<=1){
int ans=max(0LL,accumulate(a.begin(),a.end(),0LL));
if(pos==-1){ cout<<ans<<'\n'; return;}
else{
int S1=0,S2=0;
for(int i=1;i<pos;i++) S1+=a[i];
for(int i=pos+1;i<=n;i++) S2+=a[i];
cout<<S1+S2<<'\n';
return;
}
}
vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1,vector<int>(2,-inf)));
dp[0][0][0]=0; dp[0][0][1]=0; int ans=0;
for(int i=1;i<=n;i++){
dp[i][0][0]=0;
for(int j=1;j<=n;j++){
dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);
dp[i][j][1]=max(dp[i-1][j][1],dp[i-1][j-1][0])+a[i];
if(j<=k) ans=max(ans,max(dp[i][j][0],dp[i][j][1]));
}
}
cout<<ans<<'\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t tStart = clock();
int local=0,multi=0,debug=1,tt=1;
if(local){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
if(multi) cin>>tt;
for(int i=1;i<=tt;i++){
if(debug && multi && local) cout<<"样例 "<<i<<'\n';
solve();
}
fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}
Compilation message
feast.cpp: In function 'long long int maxSubArraySum(std::vector<long long int>&, long long int)':
feast.cpp:16:6: warning: variable 'starting_index' set but not used [-Wunused-but-set-variable]
16 | int starting_index = 0, ending_index = 0, s = 0;
| ^~~~~~~~~~~~~~
feast.cpp:16:26: warning: variable 'ending_index' set but not used [-Wunused-but-set-variable]
16 | int starting_index = 0, ending_index = 0, s = 0;
| ^~~~~~~~~~~~
feast.cpp: In function 'int main()':
feast.cpp:78:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:79:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
2516 KB |
Output is correct |
2 |
Correct |
22 ms |
2644 KB |
Output is correct |
3 |
Correct |
22 ms |
2644 KB |
Output is correct |
4 |
Correct |
23 ms |
2644 KB |
Output is correct |
5 |
Correct |
22 ms |
2644 KB |
Output is correct |
6 |
Correct |
22 ms |
2516 KB |
Output is correct |
7 |
Correct |
22 ms |
2516 KB |
Output is correct |
8 |
Correct |
23 ms |
2644 KB |
Output is correct |
9 |
Correct |
22 ms |
2516 KB |
Output is correct |
10 |
Correct |
24 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
2516 KB |
Output is correct |
2 |
Correct |
15 ms |
2644 KB |
Output is correct |
3 |
Correct |
15 ms |
2516 KB |
Output is correct |
4 |
Correct |
20 ms |
3644 KB |
Output is correct |
5 |
Correct |
31 ms |
5332 KB |
Output is correct |
6 |
Correct |
18 ms |
3624 KB |
Output is correct |
7 |
Correct |
16 ms |
3796 KB |
Output is correct |
8 |
Correct |
25 ms |
5456 KB |
Output is correct |
9 |
Correct |
24 ms |
5324 KB |
Output is correct |
10 |
Correct |
16 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
2644 KB |
Output is correct |
2 |
Correct |
25 ms |
2496 KB |
Output is correct |
3 |
Correct |
25 ms |
2584 KB |
Output is correct |
4 |
Correct |
24 ms |
2516 KB |
Output is correct |
5 |
Correct |
25 ms |
2644 KB |
Output is correct |
6 |
Correct |
25 ms |
2644 KB |
Output is correct |
7 |
Correct |
25 ms |
2644 KB |
Output is correct |
8 |
Correct |
26 ms |
2644 KB |
Output is correct |
9 |
Correct |
25 ms |
2644 KB |
Output is correct |
10 |
Correct |
25 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
3924 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
4 ms |
4440 KB |
Output is correct |
15 |
Correct |
4 ms |
4424 KB |
Output is correct |
16 |
Correct |
3 ms |
3920 KB |
Output is correct |
17 |
Correct |
4 ms |
4668 KB |
Output is correct |
18 |
Correct |
4 ms |
4436 KB |
Output is correct |
19 |
Correct |
4 ms |
4308 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
3924 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
4 ms |
4440 KB |
Output is correct |
15 |
Correct |
4 ms |
4424 KB |
Output is correct |
16 |
Correct |
3 ms |
3920 KB |
Output is correct |
17 |
Correct |
4 ms |
4668 KB |
Output is correct |
18 |
Correct |
4 ms |
4436 KB |
Output is correct |
19 |
Correct |
4 ms |
4308 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
189 ms |
207340 KB |
Output is correct |
22 |
Correct |
188 ms |
205024 KB |
Output is correct |
23 |
Correct |
189 ms |
208424 KB |
Output is correct |
24 |
Correct |
184 ms |
207540 KB |
Output is correct |
25 |
Correct |
198 ms |
219852 KB |
Output is correct |
26 |
Correct |
199 ms |
218136 KB |
Output is correct |
27 |
Correct |
196 ms |
218544 KB |
Output is correct |
28 |
Correct |
205 ms |
217404 KB |
Output is correct |
29 |
Correct |
189 ms |
213580 KB |
Output is correct |
30 |
Correct |
191 ms |
206416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
2516 KB |
Output is correct |
2 |
Correct |
22 ms |
2644 KB |
Output is correct |
3 |
Correct |
22 ms |
2644 KB |
Output is correct |
4 |
Correct |
23 ms |
2644 KB |
Output is correct |
5 |
Correct |
22 ms |
2644 KB |
Output is correct |
6 |
Correct |
22 ms |
2516 KB |
Output is correct |
7 |
Correct |
22 ms |
2516 KB |
Output is correct |
8 |
Correct |
23 ms |
2644 KB |
Output is correct |
9 |
Correct |
22 ms |
2516 KB |
Output is correct |
10 |
Correct |
24 ms |
2516 KB |
Output is correct |
11 |
Correct |
16 ms |
2516 KB |
Output is correct |
12 |
Correct |
15 ms |
2644 KB |
Output is correct |
13 |
Correct |
15 ms |
2516 KB |
Output is correct |
14 |
Correct |
20 ms |
3644 KB |
Output is correct |
15 |
Correct |
31 ms |
5332 KB |
Output is correct |
16 |
Correct |
18 ms |
3624 KB |
Output is correct |
17 |
Correct |
16 ms |
3796 KB |
Output is correct |
18 |
Correct |
25 ms |
5456 KB |
Output is correct |
19 |
Correct |
24 ms |
5324 KB |
Output is correct |
20 |
Correct |
16 ms |
3668 KB |
Output is correct |
21 |
Correct |
25 ms |
2644 KB |
Output is correct |
22 |
Correct |
25 ms |
2496 KB |
Output is correct |
23 |
Correct |
25 ms |
2584 KB |
Output is correct |
24 |
Correct |
24 ms |
2516 KB |
Output is correct |
25 |
Correct |
25 ms |
2644 KB |
Output is correct |
26 |
Correct |
25 ms |
2644 KB |
Output is correct |
27 |
Correct |
25 ms |
2644 KB |
Output is correct |
28 |
Correct |
26 ms |
2644 KB |
Output is correct |
29 |
Correct |
25 ms |
2644 KB |
Output is correct |
30 |
Correct |
25 ms |
2644 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
4 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
3924 KB |
Output is correct |
43 |
Correct |
3 ms |
4180 KB |
Output is correct |
44 |
Correct |
4 ms |
4440 KB |
Output is correct |
45 |
Correct |
4 ms |
4424 KB |
Output is correct |
46 |
Correct |
3 ms |
3920 KB |
Output is correct |
47 |
Correct |
4 ms |
4668 KB |
Output is correct |
48 |
Correct |
4 ms |
4436 KB |
Output is correct |
49 |
Correct |
4 ms |
4308 KB |
Output is correct |
50 |
Correct |
4 ms |
5076 KB |
Output is correct |
51 |
Correct |
189 ms |
207340 KB |
Output is correct |
52 |
Correct |
188 ms |
205024 KB |
Output is correct |
53 |
Correct |
189 ms |
208424 KB |
Output is correct |
54 |
Correct |
184 ms |
207540 KB |
Output is correct |
55 |
Correct |
198 ms |
219852 KB |
Output is correct |
56 |
Correct |
199 ms |
218136 KB |
Output is correct |
57 |
Correct |
196 ms |
218544 KB |
Output is correct |
58 |
Correct |
205 ms |
217404 KB |
Output is correct |
59 |
Correct |
189 ms |
213580 KB |
Output is correct |
60 |
Correct |
191 ms |
206416 KB |
Output is correct |
61 |
Runtime error |
196 ms |
262144 KB |
Execution killed with signal 9 |
62 |
Halted |
0 ms |
0 KB |
- |