Cod sursa(job #900430)

Utilizator RobertSSamoilescu Robert RobertS Data 28 februarie 2013 19:37:03
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
using namespace std;

int a[2][5010], b[2][5010];
int n, k;

void solve();

ifstream fin ("joc13.in");
ofstream fout ("joc13.out");
int main(){



    fin >> n >> k;
    for(int i=0; i<2; i++){
        for(int j=0; j<n; j++)
            fin >> a[i][j];
    }

    solve();


return 0;
}



void solve(){
    b[0][0] = a[0][0];
    b[1][0] = 0;

    for(int i=1; i<n; i++){
        for(int j=0; j<2; j++){

            int max = (-1)<<30; // - infinit

            int suma = a[j][i];
            for(int l=1; l<=i && l<k; l++){
                suma += a[j][i-l];
                if(suma + b[1-j][i-l] > max ) max = suma + b[1-j][i-l];
            }
            b[j][i] = max;
        }
    }

    if(b[0][n-1] + a[1][n-1] > b[1][n-1] ) fout << a[1][n-1] + b[0][n-1];
    else fout << b[1][n-1];

}