void Divide( const int st, const int fn, int weight, bool printAnswer = true ) {
if ( weight == 0 ) {
return ;
}
if ( st == fn) {
const int object = objects[ st] .first / objects[ st] .second ;
while ( weight >= object) {
printf ( "%d\n " , object) ;
weight - = object;
}
} else {
int mid = ( st + fn) / 2 ;
dp[ 0 ] [ 0 ] = 0 ;
from[ 0 ] [ 0 ] = 0 ;
for ( int i = 1 ; i <= weight; i++ ) {
dp[ 0 ] [ i] = INF;
from[ 0 ] [ i] = 0 ;
}
int now = 0 ;
for ( int i = st; i <= fn; i++ ) {
now ^ = 1 ;
for ( int j = 0 ; j < objects[ i] .first ; j++ ) {
from[ now] [ j] = from[ now ^ 1 ] [ j] ;
dp[ now] [ j] = dp[ now ^ 1 ] [ j] ;
}
for ( int j = objects[ i] .first ; j <= weight; j++ ) {
if ( dp[ now ^ 1 ] [ j] > dp[ now ^ 1 ] [ j - objects[ i] .first ] + objects[ i] .second ) {
dp[ now] [ j] = dp[ now ^ 1 ] [ j - objects[ i] .first ] + objects[ i] .second ;
if ( i <= mid) {
from[ now] [ j] = j;
} else {
from[ now] [ j] = from[ now ^ 1 ] [ j - objects[ i] .first ] ;
}
} else {
dp[ now] [ j] = dp[ now ^ 1 ] [ j] ;
from[ now] [ j] = from[ now ^ 1 ] [ j] ;
}
}
}
int best = weight;
while ( dp[ now] [ best] == INF) {
best-- ;
}
if ( printAnswer) {
printf ( "%d %d\n " , best, dp[ now] [ best] ) ;
}
const int divideWeight = from[ now] [ best] ;
Divide( st, mid, divideWeight, false ) ;
Divide( mid + 1 , fn, best - divideWeight, false ) ;
}
}
int main( ) {
Divide( 0 , objectIndex - 1 , G) ;
}
dm9pZCBEaXZpZGUoY29uc3QgaW50IHN0LCBjb25zdCBpbnQgZm4sIGludCB3ZWlnaHQsIGJvb2wgcHJpbnRBbnN3ZXIgPSB0cnVlKSB7CiAgICBpZiAod2VpZ2h0ID09IDApIHsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBpZiAoc3QgPT0gZm4pIHsKICAgICAgICBjb25zdCBpbnQgb2JqZWN0ID0gb2JqZWN0c1tzdF0uZmlyc3QgLyBvYmplY3RzW3N0XS5zZWNvbmQ7CiAgICAgICAgd2hpbGUgKHdlaWdodCA+PSBvYmplY3QpIHsKICAgICAgICAgICAgcHJpbnRmKCIlZFxuIiwgb2JqZWN0KTsKICAgICAgICAgICAgd2VpZ2h0IC09IG9iamVjdDsKICAgICAgICB9CiAgICB9IGVsc2UgewogICAgICAgIGludCBtaWQgPSAoc3QgKyBmbikgLyAyOwogICAgICAgIGRwWzBdWzBdID0gMDsKICAgICAgICBmcm9tWzBdWzBdID0gMDsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSB3ZWlnaHQ7IGkrKykgewogICAgICAgICAgICBkcFswXVtpXSA9IElORjsKICAgICAgICAgICAgZnJvbVswXVtpXSA9IDA7CiAgICAgICAgfQogICAgICAgIGludCBub3cgPSAwOwogICAgICAgIGZvciAoaW50IGkgPSBzdDsgaSA8PSBmbjsgaSsrKSB7CiAgICAgICAgICAgIG5vdyBePSAxOwogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG9iamVjdHNbaV0uZmlyc3Q7IGorKykgewogICAgICAgICAgICAgICAgZnJvbVtub3ddW2pdID0gZnJvbVtub3cgXiAxXVtqXTsKICAgICAgICAgICAgICAgIGRwW25vd11bal0gPSBkcFtub3cgXiAxXVtqXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IgKGludCBqID0gb2JqZWN0c1tpXS5maXJzdDsgaiA8PSB3ZWlnaHQ7IGorKykgewogICAgICAgICAgICAgICAgaWYgKGRwW25vdyBeIDFdW2pdID4gZHBbbm93IF4gMV1baiAtIG9iamVjdHNbaV0uZmlyc3RdICsgb2JqZWN0c1tpXS5zZWNvbmQpIHsKICAgICAgICAgICAgICAgICAgICBkcFtub3ddW2pdID0gZHBbbm93IF4gMV1baiAtIG9iamVjdHNbaV0uZmlyc3RdICsgb2JqZWN0c1tpXS5zZWNvbmQ7CiAgICAgICAgICAgICAgICAgICAgaWYgKGkgPD0gbWlkKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGZyb21bbm93XVtqXSA9IGo7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgZnJvbVtub3ddW2pdID0gZnJvbVtub3cgXiAxXVtqIC0gb2JqZWN0c1tpXS5maXJzdF07CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBkcFtub3ddW2pdID0gZHBbbm93IF4gMV1bal07CiAgICAgICAgICAgICAgICAgICAgZnJvbVtub3ddW2pdID0gZnJvbVtub3cgXiAxXVtqXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpbnQgYmVzdCA9IHdlaWdodDsKICAgICAgICB3aGlsZSAoZHBbbm93XVtiZXN0XSA9PSBJTkYpIHsKICAgICAgICAgICAgYmVzdC0tOwogICAgICAgIH0KICAgICAgICBpZiAocHJpbnRBbnN3ZXIpIHsKICAgICAgICAgICAgcHJpbnRmKCIlZCAlZFxuIiwgYmVzdCwgZHBbbm93XVtiZXN0XSk7CiAgICAgICAgfQogICAgICAgIGNvbnN0IGludCBkaXZpZGVXZWlnaHQgPSBmcm9tW25vd11bYmVzdF07CiAgICAgICAgRGl2aWRlKHN0LCBtaWQsIGRpdmlkZVdlaWdodCwgZmFsc2UpOwogICAgICAgIERpdmlkZShtaWQgKyAxLCBmbiwgYmVzdCAtIGRpdmlkZVdlaWdodCwgZmFsc2UpOwogICAgfQp9CgppbnQgbWFpbigpIHsKCURpdmlkZSgwLCBvYmplY3RJbmRleCAtIDEsIEcpOwp9
compilation info
prog.cpp: In function 'void Divide(int, int, int, bool)':
prog.cpp:6:28: error: 'objects' was not declared in this scope
const int object = objects[st].first / objects[st].second;
^
prog.cpp:8:34: error: 'printf' was not declared in this scope
printf("%d\n", object);
^
prog.cpp:13:9: error: 'dp' was not declared in this scope
dp[0][0] = 0;
^
prog.cpp:14:9: error: 'from' was not declared in this scope
from[0][0] = 0;
^
prog.cpp:16:24: error: 'INF' was not declared in this scope
dp[0][i] = INF;
^
prog.cpp:22:33: error: 'objects' was not declared in this scope
for (int j = 0; j < objects[i].first; j++) {
^
prog.cpp:26:26: error: 'objects' was not declared in this scope
for (int j = objects[i].first; j <= weight; j++) {
^
prog.cpp:41:33: error: 'INF' was not declared in this scope
while (dp[now][best] == INF) {
^
prog.cpp:45:50: error: 'printf' was not declared in this scope
printf("%d %d\n", best, dp[now][best]);
^
prog.cpp: In function 'int main()':
prog.cpp:54:12: error: 'objectIndex' was not declared in this scope
Divide(0, objectIndex - 1, G);
^
prog.cpp:54:29: error: 'G' was not declared in this scope
Divide(0, objectIndex - 1, G);
^
stdout