Please explain how the code works

Question Link : PRLADDU Problem - CodeChef

This is someone else’s code.Just trying to know the logic behind this code. Why it is working??

#include<stdio.h>
inline void read(int &n)
{

n=0;
int ch=getchar();
int sign=1;
while(ch<'0' || ch>'9') {
	if(ch=='-')sign=-1; ch=getchar();
}
while(ch>='0' && ch<='9')
       n = (n<<3)+(n<<1) + ch-'0', ch=getchar();
n=n*sign;

}

int main()
{

int t,n,k,i;
long long r , s;
read(t);
while(t--){
	read(n);
	r=s=0;
	for(i=0;i<n;i++){
    	read(k);
    	s=s+k;
    	if(s>0){
        	r=r+s;
        }
    	else{
    		r=r-s;
		}
	}
	printf("%lld\n",r);
}
return 0;

}

The read() function here is used to increase the speed of the input. The first thing in the read function is getchar() which helps to take input character by character. Then the number is created in an integer format using bitwise operators, also considering the negative sign.

Then the program just takes in the test cases and does as the question demands it to. If you have any issues regarding the logic, please refer to the well written editorial here,

PRLADDU-Editorial - editorial - CodeChef Discuss

Which part do you not understand?

He is asking about the logic of the code inside the for loop. What is the logic behind using these two variables r and s in that way. ??