best way to run through loop and perform operation

I have a vector of bools that looks like

vector<bool> vals

position: 0 1 2 3 4 5

bool val: 0 1 0 0 1 1

the bool val indexes to a 6x6 matrix A where i have random values stored.

in my following vector, I would want to sum the values of A(1,4),A(4,5),A(5,5)

if i had

position: 0 1 2 3 4 5

bool val: 0 1 1 0 1 0

I would want to sum A(1,2), A(2,4),A(4,4)

what is the fastest way to loop through the bool vector and calculate the values i want?

Code:

`double sum = 0;`

int j = 0;

int k = 0;

for(int i =0; i<vals.size(); ++i)

{

k += -(vals[i]-1);

j= i*vals[i];

//sum +=A[j-k][j]

}

i was thinking something along the lines of the above, but there are problems when my bool vector vals[0]=1, and my matrix A might have non-zero values for A[row][col] that i sum.

thoughts?