Pagini recente » Cod sursa (job #2820122) | Cod sursa (job #2715923) | Cod sursa (job #1939787) | Cod sursa (job #2612581) | Cod sursa (job #3236236)
#include <iostream>
#include <fstream>
#include <bitset>
#include <cmath>
using namespace std;
ifstream fin("recyclebin.in");
ofstream fout("recyclebin.out");
int n;
int a[1001];
void reading() {
fin >> n;
for(int i = 1; i <= n; i++){
fin >> a[i];
}
}
int big(int x){
int p = 1, k = 0;
while(p - 1 < x){
p <<= 1;
k++;
}
return k;
}
int dp[1001][1001];
int main() {
reading();
int put = big(n);
int limit = (1 << put) - 1;
for(int i = 1; i <= n; i++){
for(int masc = 0; masc <= limit && masc <= i; masc++) {
dp[i][masc] = dp[i - 1][masc] + a[i];
bitset<10> biti(masc);
for(int bit = 0; bit <= 9; bit++)
if(biti[bit] == 1)
dp[i][masc] = max(dp[i][masc], dp[i - (1 << bit)][masc - (1 << bit)]);
}
}
int ma = 0;
for(int i = 0; i <= limit;i++) {
ma = max(ma, dp[n][i]);
}
fout << ma;
}