Submission #1258825

#TimeUsernameProblemLanguageResultExecution timeMemory
1258825phtungFeast (NOI19_feast)C++20
51 / 100
1094 ms25924 KiB
#include <bits/stdc++.h>

using namespace std;

#define name "IO"
#define int long long 

const int inf = 1e18 + 7; 
const int maxn = 3e5 + 5;
int n, k, a[maxn]; 

int Pow(int x, int y) 
{
    int ans = 1;
    while (y > 0) 
    {
        if (y & 1) ans = ans * x;
        y /= 2;
        x = x * x;
    }
    return ans;
}

void sub45()
{
    vector<int> pre(n + 1, 0); 

    for(int i = 1; i <= n; i++) 
    {
        pre[i] = pre[i - 1] + a[i];
    } 

    vector<vector<int>> dp(k + 1, vector<int>(n + 1, 0)); 

    for(int i = 1; i <= k; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            int mx = 0; 
            for(int l = 0; l < j; l++)
            {
                mx = max(mx, dp[i - 1][l] - pre[l]);
            }

            dp[i][j] = max(dp[i][j - 1], pre[j] + mx); 
        }
    }

    int res = 0;
    for(int i = 1; i <= n; i++) res = max(res, dp[k][i]);

    cout << res << "\n"; 
}

void sub1()
{
    int sum = 0;
    for(int i = 1; i <= n; i++) sum += a[i];

    cout << sum << "\n"; 
}

void sub2()
{
    int sum1 = 0, pos, sum2 = 0;

    for(int i = 1; i <= n; i++)
    {
        if(a[i] < 0)
        {
            pos = i;
            break; 
        }
        sum1 += a[i]; 
    }

    for(int i = pos + 1; i <= n; i++) sum2 += a[i]; 

    if(k >= 2) cout << sum1 + sum2 << "\n"; 

    else if(k == 1)
    {
        int res = max({sum1, sum2, sum1 + sum2 + a[pos]}); 
        cout << res << "\n"; 
    }
}

void sub3()
{
    int pre = 0, mn = 0, ans = 0; 

    for(int i = 1; i <= n; i++)
    {
        pre += a[i];
        mn = min(mn, pre); 

        ans = max(ans, pre - mn); 
    }

    cout << ans << "\n";
}


signed main()
{
    if (fopen (name".INP", "r"))
    {
        freopen (name".INP", "r", stdin);
        freopen (name".OUT", "w", stdout);
    }

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    clock_t start = clock(); 

    cin >> n >> k;
    int neg = 0; 

    for(int i = 1; i <= n; i++) 
    {
        cin >> a[i];
        if(a[i] < 0) neg++;
    }

    if(neg == 0)
    {
        sub1(); 
        return 0; 
    }

    if(neg == 1)
    {
        sub2(); 
        return 0; 
    }

    if(k == 1)
    {
        sub3(); 
        return 0; 
    }

    if(n <= 2000) 
    {
        sub45();
        return 0; 
    } 





    std::cerr << "Time: " << clock() - start << "ms\n";

    return 0; 

}

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:108:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen (name".INP", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:109:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen (name".OUT", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...