Submission

Status:
[PPP][-SSSSS][TS]

Score: 30

User: getsuki

Problemset: ช่างไฟ

Language: cpp

Time: 1.077 second

Submitted On: 2024-09-30 14:29:18

#include <stdio.h>
#include <math.h>

int main() {
    int N;
    long long int P = 0;
    scanf("%d", &N);
    int a[N + 5];
    a[N + 1] = 0;
    for (int i = 0; i < N; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < N; i++) {
        // printf("%d\n", P);
        long long int nonAbsoluteValue = 0;
        long long int absoluteValue = 0;
        nonAbsoluteValue = P + a[i];
        absoluteValue = abs(P + a[i]);
        long long int nextnonAbsValue = nonAbsoluteValue;
        long long int nextAbsValue = absoluteValue;
        for (int j = i + 1; j < N; j++) {
            if (nextnonAbsValue < 0) {
                nextnonAbsValue += abs(a[j]);
            } else nextnonAbsValue += a[j];
            if (nextAbsValue < 0) {
                nextAbsValue += abs(a[j]);
            } else nextAbsValue += a[j];
        }
        if (abs(nextnonAbsValue) == nextAbsValue) {
            P = nonAbsoluteValue;
        } else if (nextAbsValue > abs(nextnonAbsValue)) {
            P = absoluteValue;
        } else {
            P = nonAbsoluteValue;
        }
    }
    printf("%lld", abs(P));
}