Editorial for Inaho V


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Ninjaclasher

The intended solution for this problem was to use unsigned integers.

For the first subtask, it is sufficient to use a signed 64-bit integer, since the final answer will not underflow.

For the second subtask, one can treat the input as positive integers, and negate the final answer at the end while printing. For each of the M nonpositive integers, one can read them in as signed 64-bit integers, negate them, and add them to the unsigned 64-bit integer. Finally, while printing the answer, one can print a - sign in front of the answer to negate it.

It should be noted that printing -0 is invalid and will result in Wrong Answer.

Alternatively, one can implement their own big integer library, though this is overkill.

Time Complexity: O(M)

Sample Solution - C

#include <stdio.h>

int main() 
{
    int M;
    unsigned long long sum = 0;
    long long a;
    scanf("%d", &M);
    for(int x = 0 ; x < M; x++)
    {
        scanf("%ld", &a);
        sum += -a;
    }
    if (sum != 0)
        printf("-");
    printf("%lu\n", sum);
}

Comments

There are no comments at the moment.