본문 바로가기

Programming [Python]/백준 알고리즘 솔루션

#241 백준 파이썬 [17390] 이건 꼭 풀어야 해!

https://www.acmicpc.net/problem/17390

 

Python Code

연속된 합을 구해놓고 빼주면 된다.

import sys
N, Q = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))
A = sorted(A)

for i in range(1, len(A)):
    A[i] = A[i] + A[i-1]
    
for _ in range(Q):
    L, R = map(int, sys.stdin.readline().split())
    if L == 1:
        print(A[R-1])
    else:
        print(A[R-1] - A[L-2])