#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define MASK(x) ((1LL) << (x))
#define BIT(x, i) (((x) >> (i)) & (1LL))
#define eb emplace_back
#define pb push_back
#define endl '\n'
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
const int nmax = 3e5+5;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int base = 31;
ll powmod(ll a, ll b) {
    if(b==1) return a;
    if(b==0) return 1;
    ll res=powmod(a,b/2);
    res=res*res%MOD;
    if(b%2)
        res=res*a%MOD;
    return res;
}
ll divide(ll a, ll b) {
    return a * powmod(b, MOD - 2) % MOD;
}
ll pre[nmax];
ll a[nmax];
int n , k;
namespace sub1{
    bool check(){
       int cnt=0;
       for (int i= 1;i <=n;i++)
        if(a[i] < 0)
        cnt++;
       return cnt <=1;
    }
    void solve(){
       ll sum1=0,sum2=0;
       bool ok = false;
       for (int i= 1;i <=n;i++){
           if(a[i] < 0){
                ok=true;
           }
           if(!ok && a[i] > 0) sum1+=a[i];
           else if ( ok && a[i] > 0 ) sum2+=a[i];
       }
       if(k==1) cout << max(sum1,sum2);
       else cout << sum1 + sum2;
       return;
    }
}
namespace sub2{
    bool check(){
       return k==1;
    }
    void solve(){
       ll res=0;
       ll mx=0;
       for (int i= 1;i <=n;i++){
        mx=max(mx+ a[i] , a[i]);
        res=max(res,mx);
       }
       cout << res;
       return;
    }
}
namespace sub3 {
    bool check(){
       return n <= 2000;
    }
    void solve(){
        sort(a+1, a+1+n);
    
        for (int i=1;i<=n;i++) pre[i] = pre[i-1] + a[i];
        vector<vector<ll>> dp(n+1 , vector<ll>(k+1, -INF));
        for (int i=0;i<=n;i++) dp[i][0] = 0;
        ll res=0;
        for (int i=1;i<=n;i++){
            for (int j=1;j<=k;j++){
             //   dp[i][j] = dp[i-1][j];
                for (int t=0;t<i;t++){
                    dp[i][j] = max(dp[i][j], dp[t][j-1] + pre[i] - pre[t]);
                }
                res=max(res,dp[i][j]);
            }
        }
        cout << res;
        return;
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for (int i= 1;i <=n;i++) cin >> a[i];
    for (int i= 1;i <=n;i++){
        pre[i]=pre[i-1] + a[i];
    }
    if(sub1::check()){
        sub1::solve();
        return 0;
    }
    if(sub2::check()){
        sub2::solve();
        return 0;
    }
    if(sub3::check()){
        sub3::solve();
        return 0;
    }
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |