#include <stdio.h>
#include <mpi.h>

int main(void) {
    int my_rank;
    int comm_sz;

    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);

    const int n = 10;
    int local_sum = 0;

    for (int i = my_rank + 1; i <= n; i += comm_sz) {
        local_sum += i;
    }

    if (my_rank == 0) {
        int total = local_sum;
        printf("Process 0 partial sum: %d\n", local_sum);

        for (int source = 1; source < comm_sz; source++) {
            int received_sum;
            MPI_Recv(&received_sum, 1, MPI_INT, source, 0,
                     MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            printf("Process %d partial sum: %d\n", source, received_sum);
            total += received_sum;
        }

        printf("Total: %d\n", total);
    } else {
        MPI_Send(&local_sum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
    }

    MPI_Finalize();
    return 0;
}
