#define lli long long int
int Solution:: solve ( vector< int > & A, vector< int > & B, int C) {
vector< int > arr;
sort( A.begin ( ) , A.end ( ) ) ;
int n = ( int ) A.size ( ) ;
int m = ( int ) B.size ( ) ;
for ( int i= 0 ; i< n; i++ ) {
arr.push_back ( A[ i] ) ;
}
for ( int i= 0 ; i< n; i++ ) {
arr.push_back ( C + A[ i] ) ;
}
int p = 2 * n;
int dp[ p] [ p] ;
for ( int i= 0 ; i< p; i++ ) {
for ( int j= 0 ; j< p; j++ ) {
dp[ i] [ j] = 1e7 ;
}
}
int minval = 1e7 ;
for ( int i= 0 ; i< m; i++ ) {
minval = min( minval, B[ i] ) ;
}
for ( int i= 0 ; i< p; i++ ) {
dp[ i] [ i] = minval;
}
for ( int i= 1 ; i< n; i++ ) {
for ( int j= 0 ; j< p; j++ ) {
int y = j + i;
if ( y < p) {
for ( int k= 0 ; k< m; k++ ) {
int z = arr[ j] + B[ k] ;
int l= j,r= p,mid,ans= - 1 ;
while ( l <= r) {
mid = ( l+ r) / 2 ;
if ( arr[ mid] > z) {
ans = mid;
r = mid - 1 ;
} else {
l = mid + 1 ;
}
}
if ( ans == - 1 || ans > y) {
dp[ j] [ y] = min( dp[ j] [ y] , B[ k] ) ;
} else {
dp[ j] [ y] = min( dp[ j] [ y] , B[ k] + dp[ ans] [ y] ) ;
}
}
}
}
}
int fin_ans = 1e7 ;
for ( int i= 0 ; i< n; i++ ) {
fin_ans = min( fin_ans, dp[ i] [ i+ n- 1 ] ) ;
}
return fin_ans;
}
I2RlZmluZSBsbGkgbG9uZyBsb25nIGludAppbnQgU29sdXRpb246OnNvbHZlKHZlY3RvcjxpbnQ+ICZBLCB2ZWN0b3I8aW50PiAmQiwgaW50IEMpIHsKICAgIHZlY3RvcjxpbnQ+IGFycjsKICAgIHNvcnQoQS5iZWdpbigpLCBBLmVuZCgpKTsKICAgIGludCBuID0gKGludClBLnNpemUoKTsKICAgIGludCBtID0gKGludClCLnNpemUoKTsKICAgIGZvcihpbnQgaT0wO2k8bjtpKyspewogICAgICAgIGFyci5wdXNoX2JhY2soQVtpXSk7CiAgICB9CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKXsKICAgICAgICBhcnIucHVzaF9iYWNrKEMgKyBBW2ldKTsKICAgIH0KICAgIGludCBwID0gMipuOwogICAgaW50IGRwW3BdW3BdOwogICAgZm9yKGludCBpPTA7aTxwO2krKyl7CiAgICAgICAgZm9yKGludCBqPTA7ajxwO2orKyl7CiAgICAgICAgICAgIGRwW2ldW2pdID0gMWU3OwogICAgICAgIH0KICAgIH0KICAgIGludCBtaW52YWwgPSAxZTc7CiAgICBmb3IoaW50IGk9MDtpPG07aSsrKXsKICAgICAgICBtaW52YWwgPSBtaW4obWludmFsLCBCW2ldKTsKICAgIH0KICAgIGZvcihpbnQgaT0wO2k8cDtpKyspewogICAgICAgIGRwW2ldW2ldID0gbWludmFsOwogICAgfQogICAgZm9yKGludCBpPTE7aTxuO2krKyl7CiAgICAgICAgZm9yKGludCBqPTA7ajxwO2orKyl7IAogICAgICAgICAgICBpbnQgeSA9IGogKyBpOwogICAgICAgICAgICBpZih5IDwgcCl7CiAgICAgICAgICAgICAgICBmb3IoaW50IGs9MDtrPG07aysrKXsKICAgICAgICAgICAgICAgICAgICBpbnQgeiA9IGFycltqXSArIEJba107CiAgICAgICAgICAgICAgICAgICAgaW50IGw9aixyPXAsbWlkLGFucz0tMTsKICAgICAgICAgICAgICAgICAgICB3aGlsZShsIDw9IHIpewogICAgICAgICAgICAgICAgICAgICAgICBtaWQgPSAobCtyKS8yOwogICAgICAgICAgICAgICAgICAgICAgICBpZihhcnJbbWlkXSA+IHopewogICAgICAgICAgICAgICAgICAgICAgICAgICAgYW5zID0gbWlkOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgciA9IG1pZCAtIDE7CiAgICAgICAgICAgICAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgICAgICAgICAgICAgbCA9IG1pZCArIDE7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaWYoYW5zID09IC0xIHx8IGFucyA+IHkpewogICAgICAgICAgICAgICAgICAgICAgICBkcFtqXVt5XSA9IG1pbihkcFtqXVt5XSwgQltrXSk7CiAgICAgICAgICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgIGRwW2pdW3ldID0gbWluKGRwW2pdW3ldLCBCW2tdICsgZHBbYW5zXVt5XSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgaW50IGZpbl9hbnMgPSAxZTc7CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKXsKICAgICAgICBmaW5fYW5zID0gbWluKGZpbl9hbnMsIGRwW2ldW2krbi0xXSk7CiAgICB9CiAgICByZXR1cm4gZmluX2FuczsKfQo=
compilation info
prog.cpp:2:5: error: ‘Solution’ has not been declared
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
^~~~~~~~
prog.cpp:2:21: error: ‘vector’ was not declared in this scope
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
^~~~~~
prog.cpp:2:28: error: expected primary-expression before ‘int’
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
^~~
prog.cpp:2:37: error: ‘vector’ was not declared in this scope
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
^~~~~~
prog.cpp:2:44: error: expected primary-expression before ‘int’
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
^~~
prog.cpp:2:53: error: expected primary-expression before ‘int’
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
^~~
prog.cpp:2:58: error: expression list treated as compound expression in initializer [-fpermissive]
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
^
stdout