Problem 'Greedy Santa'

Good evening. On Subtask 2 (N <= 20), on three tasks, I get the wrong output. Otherwise all outputs are correct.
Does anyone know what could be the reason for this?

    #include <stdio.h>
    #include <assert.h>
    #include <bits/stdc++.h>
    using namespace std;

    // constraints
    #define MAXN 10000

    // input data
    int N, B, s, i, j, answer, sum;
    int V[MAXN];

    int main() {
    	answer, sum = 0;
        assert(2 == scanf("%d%d", &N, &B));
        for(s = 0; s < N; s++){
        	assert(1 == scanf("%d", &V[s]));
        	answer += V[s];
    	}
        // insert your code here
        if(answer < B){
        	printf("%d", answer); // print result
    	}
        else{
        	sort(V, V + N);
        	sum = V[N-1];
        	for(int j = N-1, i = j-1; j >= 0;){
    			while(V[j] > B){
                    sum = V[j];
        			j--;
    				i--;
    				//cout << "SWITCH  j = "<< j << " i = "<< i << " sum = " << sum << endl;
    			}
    			if(sum == B){
    				answer = sum;
    				break;
    			}
        		sum += V[i];
        		//cout << "SUM = " << sum << endl;
        		if(sum >= B){
        			if(sum < answer){
        				answer = sum;
        				//cout << "NEW HIGHEST = " << answer << endl;
        				if(answer == B){
        					break;
    					}
    				}
    				sum -= V[i];
    				//cout << "SUM = " << sum << endl;
    			}
    			if(i <= 0){
    				j--;
    				i = j-1;
    				sum = V[j];
    				//cout << "SWITCH  j = "<< j << " i = "<< i << " sum = " << sum << endl;
    				//cout << "SUM = " << sum << endl;
    			}else{
    				i--;
    			}
    		}
    		printf("%d", answer); // print result
    	}
        return 0;
    }

On the scoring paragraph, on subtask 2 it say: " The cost of all the gifts is either 1 or 2 ".
Try troubleshooting on this case

Can u explain the code?

Oh sorry, i mean Subtask 3.
How my code works:
Step 1:
First I sort the array. Then I set j to the last (largest) element and i to the last but one. If the last element is larger than B, j and i are decreased until an element is found that is smaller than B or until j is 0. If no element is found that is smaller than B, the result is the lowest number. This saves time.

Step 2:
If a number is smaller than B, the i-th number is added with the found number.
If the sum is a new minimum that is greater than or equal to B, this sum becomes the new minimum. Then the i-th number is subtracted again and i is decreased by 1 to possibly find an even smaller minimum, which is greater than or equal to B.
But if the sum of the j-th element and the i-th element is less than B, the sum is kept and i is decreased again by 1. Then it is asked again if the new sum is greater than or equal to B. If it is, the i-th element is subtracted and i is decreased.
This continues until B is found or i is 0. If i is 0, j is decreased by 1 and i is set j-1.
Step 2 is repeated until B is found or j is 0. If j is 0, the minimum sum greater than B is output.