Submission #9892832


Source Code Expand

# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()
    A = Scanner.map_int()
    s1, s2 = 0, 0
    c1, c2 = 0, 0
    for i in range(N):
        if i % 2 == 0:
            if s1 + A[i] > 0:
                s1 += A[i]
            else:
                c1 += abs(s1 + A[i]) + 1
                s1 = 1
            if s2 + A[i] < 0:
                s2 += A[i]
            else:
                c2 += abs(s2 + A[i])+1
                s2 = -1
        else:
            if s1 + A[i] < 0:
                s1 += A[i]
            else:
                c1 += abs(s1 + A[i]) + 1
                s1 = -1
            if s2 + A[i] > 0:
                s2 += A[i]
            else:
                c2 += abs(s2 + A[i]) + 1
                s2 = 1
    print(min(c1, c2))
    return


if __name__ == "__main__":
    main()

Submission Info

Submission Time
Task C - Sequence
User matsu0112
Language PyPy3 (2.4.0)
Score 300
Code Size 3204 Byte
Status AC
Exec Time 265 ms
Memory 63724 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 18
Set Name Test Cases
Sample 00-00.txt, 00-01.txt, 00-02.txt
All 00-00.txt, 00-01.txt, 00-02.txt, 01-00.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt
Case Name Status Exec Time Memory
00-00.txt AC 203 ms 39916 KB
00-01.txt AC 182 ms 38512 KB
00-02.txt AC 182 ms 38512 KB
01-00.txt AC 265 ms 63724 KB
01-01.txt AC 255 ms 63724 KB
01-02.txt AC 259 ms 63212 KB
01-03.txt AC 256 ms 62316 KB
01-04.txt AC 254 ms 58712 KB
01-05.txt AC 253 ms 56888 KB
01-06.txt AC 265 ms 63084 KB
01-07.txt AC 253 ms 58140 KB
01-08.txt AC 261 ms 63724 KB
01-09.txt AC 257 ms 58720 KB
01-10.txt AC 248 ms 55644 KB
01-11.txt AC 265 ms 63212 KB
01-12.txt AC 261 ms 63724 KB
01-13.txt AC 258 ms 58832 KB
01-14.txt AC 264 ms 63724 KB