Cod sursa(job #2471223)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 10 octombrie 2019 16:46:59
Problema Ferma Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.55 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("ferma.in");
ofstream out("ferma.out");
const int N = 10005;
const int K =  1005;
int dp[2][2][K],v[N];

int main()
{
    int n,k;
    in >> n >> k;
    k++;
    for (int i = 1; i<=n; i++)
        in >> v[i];
    int t = 1;
    for (int i = 1; i<=n; i++, t = !t)
        for (int j = 1; j<=k; j++)
        {
            dp[1][t][j] = v[i]+max(dp[1][!t][j],dp[0][!t][j-1]);
            dp[0][t][j] = max(dp[1][!t][j],dp[0][!t][j]);
        }
    out << max(0,dp[1][!t][k]);
}