<s>[INST] import java.lang.*;
import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        long[] v = new long[n];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            v[i] = Long.parseLong(st.nextToken());
        }
        Arrays.sort(v);
        int p = 0;
        int q = 0;
        long sum = 0;
        for (int i = 0; i < m; i++) {
            while (p < n) {
                if (v[p] < 0) {
                    sum += v[p];
                    p++;
                } else break;
            }
            if (p >= n) break;
            sum += v[p];
            p++;
        }
        out.println(sum);
        out.close();
    }

}

I have written this code in Java for the following problem:

There are n integers in an array A. Add up all the integers in A. If the sum is even,
return 0. If the sum is odd, return the index of the first odd number in A. The sum of
all integers in A is not greater than 2^63 - 1.

However, this doesn't solve the problem correctly. The problem requires that the first
odd number is found only after calculating the sum, but my code finds the first odd
number while calculating the sum.

How do I fix this code to correctly solve the problem?[/INST]