#include<mpi.h>

#pragma TASS input 
int B;
#pragma TASS input {n>=0 && n<=B}
int n;
#pragma TASS input 
double a[n];
#pragma TASS output 
double sum;

int myrank;
int nprocs;
double localSum = 0.0;

double computeGlobalSum() {
	double result = localSum;
	double buffer;
	int i;
	
	for (i=1; i<nprocs; i++) {
		MPI_Recv(&buffer, 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
		result += buffer;
	}
	return result;
}

void main() {
	int argc;
	char **argv;
	int first;
	int afterLast;
	int i;
	
	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
	first = n*myrank/nprocs;
	afterLast = n*(myrank+1)/nprocs;
	for (i=first; i<afterLast; i++) localSum += a[i];
	if (myrank == 0)
		sum = computeGlobalSum();
	else
		MPI_Send(&localSum, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
	MPI_Finalize();
}
