can`t understand how this algorithm will give the correct answer i.e. the greatest sum up to index k .

suppose if have

A[] = {1,19,29,2,3}; B[]={1,2,1,2,1}

Now we will start the calculation from dpf(1), which will be dpf(1) = A[0]+A[1] = 20 , now dpf(2) = max(A[2]+A[1], dpf(1)-A[1]+B[1]+A[2]]) => max(48, 32) = 48 dpf(3) = max(A[3]+A[2], dpf(2)-A[2]+B[2]+A[3]) => max(31, 22) = 31 dpf(4) = max(A[4]+A[3], dpf(3)-A[3]+B[3]+A[4]) => max(5, 34) = 34

hence the max value up to k = 4 that we are getting is 34 while it must be 48 as the sum of A[1] and A[2].

Please tell me where i am wrong??