# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1113176 |
2024-11-16T02:30:12 Z |
huyngodzz |
Feast (NOI19_feast) |
C++14 |
|
47 ms |
61004 KB |
///huynhocute123///
#include<bits/stdc++.h>
using namespace std;
#define S second
#define F first
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define pb push_back
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = b; i >= a; --i)
#define ALL(v) v.begin(),v.end()
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
//random_device rd;
//mt19937 rng(rd());
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
const int maxN = 2009;
const int mod = 1e9 + 7;
const int base = 2309;
const int MAX = 1e9+9;
const double pi = 3.14159265358979323846;
const double rad = 3.14159265358979323846 /180;
bool minimize(int &u, int v){
if(v < u){
u = v;
return 1;
}
return 0;
}
bool maximize(int &u, int v){
if(v > u){
u = v;
return 1;
}
return 0;
}
bool maximizell(long long &u, long long v){
if(v > u){
u = v;
return 1;
}
return 0;
}
bool minimizell(long long &u, long long v){
if(v < u){
u = v;
return 1;
}
return 0;
}
inline int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
int n , k;
long long a[maxN] , dp[maxN][maxN], g[maxN][maxN], res;
bool check1(){
FOR(i, 1, n)if(a[i] <0)return 0;
return 1;
}
void sub1(){
FOR(i,1,n) res += a[i];
cout<<res;
return;
}
bool check2(){
int cnt =0;
FOR(i, 1, n)if(a[i] <0)++cnt;
return (cnt == 1);
}
void sub2(){
int pos = 0;
FOR(i,1,n) if(a[i] < 0){
pos = i;
break;
}
long long sum1 = 0, sum2 = 0;
FOR(i,1,pos - 1) sum1 += a[i];
FOR(i,pos + 1,n) sum2 += a[i];
if(k > 1) cout<<sum1 + sum2;
else{
long long sum = sum1 + sum2 + a[pos];
cout<<max({sum, sum1, sum2});
}
}
void sub3(){
long long sum =0;
FOR(i,1,n){
sum += a[i];
maximizell(res, sum);
maximizell(sum, 0LL);
}
cout<<res;
}
bool check3(){
return (k ==1);
}
inline void solve(){
cin >> n >>k;
FOR(i, 1, n)cin >> a[i];
if(check1())return sub1();
if(check2())return sub2();
if(check3())return sub3();
FOR(i, 1, n){
FOR(j, 1, k){
dp[i][j] = dp[i-1][j] + a[i];
maximizell(dp[i][j] , g[i-1][j-1] + a[i]);
maximizell(g[i][j] , g[i-1][j]);
maximizell(g[i][j] , dp[i][j]);
}
}
FOR(i, 1, k)maximizell(res, g[n][i]);
cout << res;
}
signed main(){
// freopen("name.inp","r",stdin);
// freopen("name.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
inp("task.inp");
int tc = 1;
// cin >> tc;
while( tc-- )solve();
cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:12:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
feast.cpp:127:5: note: in expansion of macro 'inp'
127 | inp("task.inp");
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
1104 KB |
Output is correct |
3 |
Correct |
2 ms |
1020 KB |
Output is correct |
4 |
Correct |
2 ms |
848 KB |
Output is correct |
5 |
Correct |
1 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
1104 KB |
Output is correct |
3 |
Correct |
2 ms |
1020 KB |
Output is correct |
4 |
Correct |
2 ms |
848 KB |
Output is correct |
5 |
Correct |
1 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
11 |
Correct |
2 ms |
2896 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
3 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2808 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
3 ms |
2640 KB |
Output is correct |
17 |
Correct |
3 ms |
2896 KB |
Output is correct |
18 |
Correct |
2 ms |
2640 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
20 |
Correct |
3 ms |
2860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
1104 KB |
Output is correct |
3 |
Correct |
2 ms |
1020 KB |
Output is correct |
4 |
Correct |
2 ms |
848 KB |
Output is correct |
5 |
Correct |
1 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
11 |
Correct |
2 ms |
2896 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
3 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2808 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
3 ms |
2640 KB |
Output is correct |
17 |
Correct |
3 ms |
2896 KB |
Output is correct |
18 |
Correct |
2 ms |
2640 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
20 |
Correct |
3 ms |
2860 KB |
Output is correct |
21 |
Correct |
15 ms |
22096 KB |
Output is correct |
22 |
Correct |
47 ms |
61004 KB |
Output is correct |
23 |
Correct |
20 ms |
27720 KB |
Output is correct |
24 |
Correct |
16 ms |
22608 KB |
Output is correct |
25 |
Correct |
17 ms |
25424 KB |
Output is correct |
26 |
Correct |
16 ms |
21840 KB |
Output is correct |
27 |
Correct |
19 ms |
24912 KB |
Output is correct |
28 |
Correct |
12 ms |
17488 KB |
Output is correct |
29 |
Correct |
12 ms |
17232 KB |
Output is correct |
30 |
Correct |
12 ms |
15952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |