Getting Segmentation Fault in DP solution


#1
vector<int> Solution::solve(int A, vector<int> &B) {
vector<int> out;
if(B.size()==0) return out;
if(A==0) return out;
int dpi[A+1];
int dpmax[A+1];
dpi[0] = -1;
dpmax[0] = 0;
int ti,tm;
for(int i=1;i<=A;i++){
    ti =-1;
    tm =0;
    for(int j=0;j<B.size();j++){
        if(i>=B[j]){
            if(tm<(dpmax[i-B[j]]+1)){
                tm=dpmax[i-B[j]]+1;
                ti =j;
            }
        }
    }
    dpi[i] = ti;
    dpmax[i] = tm;
}
while(dpi[A]!=-1){
    out.push_back(dpi[A]);
    A = A-B[dpi[A]];
}
return out;
}