Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is v_{i}. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions:  She will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her $\sum_{i = l}^{r} v_{i}$.  Let u_{i} be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her $\sum_{i = l}^{r} u_{i}$. 

For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy.


-----Input-----

The first line contains an integer n (1 ≤ n ≤ 10^5). The second line contains n integers: v_1, v_2, ..., v_{n} (1 ≤ v_{i} ≤ 10^9) — costs of the stones. 

The third line contains an integer m (1 ≤ m ≤ 10^5) — the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and r (1 ≤ l ≤ r ≤ n; 1 ≤ type ≤ 2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one.


-----Output-----

Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input.


-----Examples-----
Input
6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6

Output
24
9
28

Input
4
5 5 2 3
10
1 2 4
2 1 4
1 1 1
2 1 4
2 1 2
1 1 1
1 3 3
1 1 3
1 4 4
1 2 2

Output
10
15
5
15
5
5
2
12
3
5



-----Note-----

Please note that the answers to the questions may overflow 32-bit integer type.

Difficulty: EASY

Solutions:
a = int(input())
z = list(map(int, input().split()))
dp = [0]
for i in range(len(z)):
	dp.append(dp[-1] + z[i])
r = sorted(z)
rp = [0]
for i in range(len(z)):
	rp.append(rp[-1] + r[i])
l = int(input())
for i in range(l):
	(x, y, p) = map(int, input().split())
	if x == 1:
		print(dp[p] - dp[y] + z[y - 1])
	else:
		print(rp[p] - rp[y] + r[y - 1])


import sys
input = sys.stdin.readline
n = int(input())
a = [0]
a.extend(list(map(int, input().split())))
s = sorted(a)
for i in range(1, n + 1):
	a[i] += a[i - 1]
	s[i] += s[i - 1]
for i in range(int(input())):
	(x, l, r) = map(int, input().split())
	if x == 1:
		print(a[r] - a[l - 1])
	else:
		print(s[r] - s[l - 1])


n = int(input())
pedras = input().split()
pedras_ordenadas = [int(x) for x in pedras]
pedras_ordenadas.sort()
m = int(input())
saida = []
soma_acumulada = []
soma_acumulada_ord = []
soma = 0
soma_ord = 0
for s in pedras:
	soma += int(s)
	soma_acumulada.append(soma)
for s_ord in pedras_ordenadas:
	soma_ord += int(s_ord)
	soma_acumulada_ord.append(soma_ord)
for i in range(m):
	entrada = input().split()
	q = entrada[0]
	l = int(entrada[1])
	r = int(entrada[2])
	if q == '1':
		if l > 1:
			saida.append(soma_acumulada[r - 1] - soma_acumulada[l - 2])
		else:
			saida.append(soma_acumulada[r - 1])
	elif l > 1:
		saida.append(soma_acumulada_ord[r - 1] - soma_acumulada_ord[l - 2])
	else:
		saida.append(soma_acumulada_ord[r - 1])
for k in saida:
	print(k)


from collections import defaultdict
import sys

def find_cum(numbers):
	cumsum = [0]
	sums = 0
	for n in numbers:
		sums += n
		cumsum.append(sums)
	return cumsum

def solve(cumsum, query_l, query_r):
	l = query_l - 1
	r = query_r
	ans = cumsum[r] - cumsum[l]
	print(ans)
trash = sys.stdin.readline()
numbs = [int(x) for x in sys.stdin.readline().strip().split(' ')]
sorted_numbers = sorted(numbs)
numbs = find_cum(numbs)
num_questions = int(sys.stdin.readline().strip())
sorted_numbers = find_cum(sorted_numbers)
for i in range(0, num_questions):
	question = [int(x) for x in sys.stdin.readline().strip().split(' ')]
	inp = []
	if question[0] == 1:
		inp = numbs
	else:
		inp = sorted_numbers
	solve(inp, question[1], question[2])


n = int(input())
l = list(map(int, input().split()))
temp = [i for i in l]
temp.sort()
m = int(input())
x = []
for i in range(m):
	u = list(map(int, input().split()))
	sum = 0
	x.append(u)
prefix = [0, l[0]]
for i in l[1:]:
	prefix.append(prefix[-1] + i)
sorted_prefix = [0, temp[0]]
for i in temp[1:]:
	sorted_prefix.append(sorted_prefix[-1] + i)
for i in x:
	if i[0] == 1:
		print(-prefix[i[1] - 1] + prefix[i[2]])
	else:
		print(-sorted_prefix[i[1] - 1] + sorted_prefix[i[2]])


n = int(input())
A = list(map(int, input().split()))
m = int(input())
S = 0
X = [0]
for i in A:
	S = S + i
	X.append(S)
A.sort()
S = 0
Y = [0]
for i in A:
	S = S + i
	Y.append(S)
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(X[r] - X[l - 1])
	else:
		print(Y[r] - Y[l - 1])


from sys import stdin
input = stdin.readline
n = int(input())
stones = [int(item) for item in input().split(' ')]
q = int(input())
u = [0]
v = [0]
s = 0
for item in stones:
	s += item
	v.append(s)
s = 0
for item in sorted(stones):
	s += item
	u.append(s)
while q:
	q -= 1
	[type, l, r] = [int(item) for item in input().split(' ')]
	arr = v if type == 1 else u
	print(arr[r] - arr[l - 1])


def single_integer():
	return int(input())

def multi_integer():
	return map(int, input().split())

def string():
	return input()

def multi_string():
	return input().split()
n = single_integer()
integers = [0] + list(multi_integer())
sorted_integers = sorted(integers)
ans = 0
for i in range(n + 1):
	ans += sorted_integers[i]
	sorted_integers[i] = ans
ans = 0
for i in range(n + 1):
	ans += integers[i]
	integers[i] = ans
m = single_integer()
for i in range(m):
	(t, l, r) = multi_integer()
	if t == 2:
		print(sorted_integers[r] - sorted_integers[l - 1])
	else:
		print(integers[r] - integers[l - 1])


n = int(input())
a = [0, *map(int, input().split())]
m = [0]
for i in range(n + 1):
	m.append(m[-1] + a[i])
t = a[:]
t.sort()
s = [0]
for i in range(n + 1):
	s.append(s[-1] + t[i])
s = s[1:]
m = m[1:]
q = int(input())
for i in range(q):
	(qu, l, r) = map(int, input().split())
	if qu == 1:
		if l == r:
			print(a[r])
		else:
			print(m[r] - m[l - 1])
	elif l == r:
		print(t[r])
	else:
		print(s[r] - s[l - 1])


import sys
import os

def get_int():
	return map(int, input().split())

def get_array():
	return list(map(int, input().split()))

def str_replacer(old_str, new_str, index):
	if index in range(len(old_str)):
		return old_str[:index] + new_str + old_str[index + 1:]
	else:
		return old_str
if os.environ.get('DEBUGSTER_PYTHON'):
	sys.stdin = open('in.txt', 'r')
	sys.stdout = open('out.txt', 'w')

def cum_sum(data):
	n = len(data)
	sums = dict()
	sums[0] = 0
	for i in range(1, n + 1):
		sums[i] = sums[i - 1] + data[i - 1]
	return sums
n = int(input())
all_v = get_array()
sum1 = cum_sum(all_v)
all_v.sort()
sum2 = cum_sum(all_v)
m = int(input())
for _ in range(m):
	(t, l, r) = get_int()
	if t == 1:
		ans = sum1[r] - sum1[l - 1]
	else:
		ans = sum2[r] - sum2[l - 1]
	print(ans)


def summation(t, l, r):
	if t == 1 and l == 1:
		return a_s_a[r - 1]
	elif t == 2 and l == 1:
		return a_so_a[r - 1]
	if t == 1:
		return a_s_a[r - 1] - a_s_a[l - 2]
	else:
		return a_so_a[r - 1] - a_so_a[l - 2]
n = int(input())
stones = list(map(int, input().split()))
stones_ordenad = sorted(stones)
a_s_a = [stones[0]]
a_so_a = [stones_ordenad[0]]
for i in range(1, n):
	a_s_a.append(a_s_a[i - 1] + stones[i])
	a_so_a.append(a_so_a[i - 1] + stones_ordenad[i])
m = int(input())
for i in range(m):
	(typ, l, r) = map(int, input().split())
	print(summation(typ, l, r))


n = int(input())
v = list(map(int, input().split()))
lst = []
m = int(input())
for q in range(m):
	lst.append(list(map(int, input().split())))
u = v.copy()
u.sort()
dpv = []
dpu = []
sum1 = 0
sum2 = 0
for i in range(n):
	sum1 += v[i]
	sum2 += u[i]
	dpv.append(sum1)
	dpu.append(sum2)
for q in range(m):
	if lst[q][0] == 1:
		print(dpv[lst[q][2] - 1] - dpv[lst[q][1] - 1] + v[lst[q][1] - 1])
	else:
		print(dpu[lst[q][2] - 1] - dpu[lst[q][1] - 1] + u[lst[q][1] - 1])


n = int(input())
l = list(map(int, input().split()))
l1 = [0]
l2 = [0]
for i in range(n):
	l1.append(l1[i] + l[i])
l.sort()
for i in range(n):
	l2.append(l2[i] + l[i])
l1.append(l1[-1])
l2.append(l2[-1])
for i in range(int(input())):
	(a, l, r) = map(int, input().split())
	if a == 1:
		print(l1[r] - l1[l - 1])
	else:
		print(l2[r] - l2[l - 1])


n = int(input())
a = [int(i) for i in input().split()]
ans1 = [0] * n
ans2 = [0] * n
for i in range(n):
	if i == 0:
		ans1[i] = a[i]
	else:
		ans1[i] = a[i] + ans1[i - 1]
a.sort()
for i in range(n):
	if i == 0:
		ans2[i] = a[i]
	else:
		ans2[i] = a[i] + ans2[i - 1]
ans1.insert(0, 0)
ans2.insert(0, 0)
q = int(input())
for w in range(q):
	(c, l, r) = map(int, input().split())
	if c == 1:
		print(ans1[r] - ans1[l - 1])
	else:
		print(ans2[r] - ans2[l - 1])


n = int(input())
ar = [int(i) for i in input().split()]
p1 = [0] * n
p2 = [0] * n
p1[0] = ar[0]
for i in range(1, n):
	p1[i] = p1[i - 1] + ar[i]
ar.sort()
p2[0] = ar[0]
for i in range(1, n):
	p2[i] = p2[i - 1] + ar[i]
p1.insert(0, 0)
p2.insert(0, 0)
m = int(input())
for i in range(m):
	(a, b, c) = map(int, input().split())
	if a == 1:
		print(p1[c] - p1[b - 1])
	else:
		print(p2[c] - p2[b - 1])


from typing import List

class Solution:

	@classmethod
	def sum_array(cls, numbers: List[int]) -> List[int]:
		sum_numbers = [0]
		for (index, num) in enumerate(numbers):
			prev = sum_numbers[index]
			next = numbers[index]
			sum_numbers.append(prev + next)
		return sum_numbers

	@classmethod
	def sorted_sum_array(cls, numbers: List[int]) -> List[int]:
		sorted_numbers = sorted(numbers)
		sorted_sum_numbers = cls.sum_array(sorted_numbers)
		return sorted_sum_numbers

	@classmethod
	def query(cls, sum_numbers: List[int], sorted_sum_numbers: List[int], qtype: int, low: int, high: int) -> int:
		if qtype == 1:
			return sum_numbers[high] - sum_numbers[low - 1]
		else:
			return sorted_sum_numbers[high] - sorted_sum_numbers[low - 1]
num = int(input())
numbers = list(map(lambda x: int(x), input().split()))
sum_numbers = Solution.sum_array(numbers)
sorted_sum_numbers = Solution.sorted_sum_array(numbers)
num_questions = int(input())
for i in range(num_questions):
	[qtype, low, high] = list(map(lambda x: int(x), input().split()))
	answer = Solution.query(sum_numbers, sorted_sum_numbers, qtype, low, high)
	print(answer)


stones = int(input())
costs = list(map(int, input().split(' ')))
reg = costs[:]
costs.sort()
a = [0] + [0] * stones
b = [0] + [0] * stones
for i in range(1, stones + 1):
	a[i] = a[i - 1] + costs[i - 1]
	b[i] = b[i - 1] + reg[i - 1]
for i in range(int(input())):
	(x, y, z) = map(int, input().split(' '))
	if x == 2:
		print(a[z] - a[y - 1])
	else:
		print(b[z] - b[y - 1])


n = int(input())
l = [int(i) for i in input().split()]
p = l.copy()
p.sort()
o = [0 for i in range(n)]
q = [0 for i in range(n)]
o[0] = l[0]
q[0] = p[0]
for i in range(1, n):
	o[i] = l[i] + o[i - 1]
	q[i] = p[i] + q[i - 1]
o.insert(0, 0)
q.insert(0, 0)
m = int(input())
for _ in range(m):
	(a, b, c) = [int(i) for i in input().split()]
	if a == 2:
		print(q[c] - q[b - 1])
	else:
		print(o[c] - o[b - 1])


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	v = list(map(int, input().split()))
	v = [0] + v
	u = v.copy()
	u.sort()
	for i in range(1, n + 1):
		v[i] += v[i - 1]
		u[i] += u[i - 1]
	m = int(input())
	for _ in range(m):
		(t, l, r) = map(int, input().split())
		if t == 1:
			print(v[r] - v[l - 1])
		else:
			print(u[r] - u[l - 1])

def nouse0():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse1():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse2():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


inp = lambda : map(int, input().rstrip().split())
n = int(input())
v = list(inp())
u = sorted(v.copy())
vs = [0]
us = [0]
for i in range(n):
	vs.append(vs[-1] + v[i])
	us.append(us[-1] + u[i])
for _ in range(int(input())):
	(t, l, r) = inp()
	if t == 1:
		print(vs[r] - vs[l - 1])
	else:
		print(us[r] - us[l - 1])


n = int(input())
stones = [int(num) for num in input().split()]
list1 = [0]
list2 = [0]
for i in range(len(stones)):
	list1.append(stones[i] + list1[i])
stones.sort()
for i in range(len(stones)):
	list2.append(stones[i] + list2[i])
for i in range(int(input())):
	l1 = [int(y) for y in input().split()]
	t = l1[0]
	l = l1[1]
	r = l1[2]
	if t == 1:
		print(list1[r] - list1[l - 1])
	else:
		print(list2[r] - list2[l - 1])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = []
for i in range(1, len(a)):
	a[i] += a[i - 1]
for i in range(1, len(b)):
	b[i] += b[i - 1]
for i in range(int(input())):
	(x, y, z) = map(int, input().split())
	if x == 1:
		if y == 1:
			c.append(a[z - 1])
		else:
			c.append(a[z - 1] - a[y - 2])
	elif y == 1:
		c.append(b[z - 1])
	else:
		c.append(b[z - 1] - b[y - 2])
for i in c:
	print(i)


R = lambda : map(int, input().split())
n = int(input())
a = [0] + list(R())
s = [a, sorted(a)]
for i in range(n):
	s[0][i + 1] += s[0][i]
	s[1][i + 1] += s[1][i]
m = int(input())
print('\n'.join(map(str, (s[t - 1][r] - s[t - 1][l - 1] for (t, l, r) in (R() for _ in range(m))))))


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
p1 = [a[0]]
p2 = [b[0]]
for i in range(1, n):
	p1.append(p1[-1] + a[i])
	p2.append(p2[-1] + b[i])
for i in range(int(input())):
	(x, y, z) = map(int, input().split())
	if x == 1:
		if y == 1:
			print(p1[z - 1])
		else:
			print(p1[z - 1] - p1[y - 2])
	elif y == 1:
		print(p2[z - 1])
	else:
		print(p2[z - 1] - p2[y - 2])


from itertools import accumulate
n = int(input())
a = [0]
a += [int(x) for x in input().split()]
one = list(accumulate(a))
b = sorted(a)
two = list(accumulate(b))
q = int(input())
for i in range(q):
	(t, l, r) = [int(x) for x in input().split()]
	if t == 1:
		print(one[r] - one[l - 1])
	else:
		print(two[r] - two[l - 1])


def get_sum_array(arr):
	sums = [0] * len(arr)
	for i in range(len(arr)):
		if i == 0:
			sums[i] = arr[i]
		else:
			sums[i] = sums[i - 1] + arr[i]
	return sums
n = int(input())
costs = list(map(int, input().split()))
costs_sorted = sorted(costs)
costs_sum_array = get_sum_array(costs)
costs_sorted_sum_array = get_sum_array(costs_sorted)
for _ in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 2:
		print(costs_sorted_sum_array[r - 1] - (costs_sorted_sum_array[l - 2] if l > 1 else 0))
	else:
		print(costs_sum_array[r - 1] - (costs_sum_array[l - 2] if l > 1 else 0))


(n, u) = (int(input()), list(map(int, input().split())))
v = list(sorted(u))
for i in range(n - 1):
	u[i + 1] += u[i]
	v[i + 1] += v[i]
(u, v) = ([0] + u, [0] + v)
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	print(u[r] - u[l - 1] if t == 1 else v[r] - v[l - 1])


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	dp1 = [0] * n
	dp1[0] = arr[0]
	for i in range(1, n):
		dp1[i] = dp1[i - 1] + arr[i]
	arr.sort()
	dp2 = [0] * n
	dp2[0] = arr[0]
	for i in range(1, n):
		dp2[i] = dp2[i - 1] + arr[i]
	ans = []
	m = int(input())
	for i in range(m):
		(t, l, r) = map(int, input().split())
		if t == 1:
			dp = dp1
		else:
			dp = dp2
		if l == 1:
			ans.append(dp[r - 1])
		else:
			ans.append(dp[r - 1] - dp[l - 2])
	for i in range(m):
		print(ans[i])
main()


I = lambda : list(map(int, input().split()))
n = int(input())
v = I()
vcum = [0 for i in range(n + 1)]
for i in range(n):
	vcum[i + 1] = vcum[i] + v[i]
sv = sorted(v)
svcum = [0 for i in range(n + 1)]
for i in range(n):
	svcum[i + 1] = svcum[i] + sv[i]
m = int(input())
for i in range(m):
	(t, l, r) = I()
	if t == 1:
		print(vcum[r] - vcum[l - 1])
	else:
		print(svcum[r] - svcum[l - 1])


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def pre(a):
	ans = [0, a[0]]
	for i in a[1:]:
		ans.append(ans[-1] + i)
	return ans
n = Int()
a = array()
q1 = pre(a)
q2 = pre(sorted(a))
for _ in range(Int()):
	(q, l, r) = value()
	if q == 1:
		print(q1[r] - q1[l - 1])
	else:
		print(q2[r] - q2[l - 1])


n = int(input())
costs = list(map(int, input().split()))
t = int(input())
sum = []
sum.append(costs[0])
for i in range(1, n):
	sum.append(sum[i - 1] + costs[i])
mod = sorted(costs)
sum_ = []
sum_.append(mod[0])
for i in range(1, n):
	sum_.append(sum_[i - 1] + mod[i])
for _ in range(t):
	(type, l, m) = map(int, input().split())
	if type == 1:
		if l > 1:
			print(sum[m - 1] - sum[l - 2])
		else:
			print(sum[m - 1] - 0)
	elif l > 1:
		print(sum_[m - 1] - sum_[l - 2])
	else:
		print(sum_[m - 1] - 0)


n = int(input())
arr = list(map(int, input().split()))
arranged = sorted(arr)
sum1 = sum(arr)
sum2 = sum(arranged)
prefix_sum1 = [arr[0]]
for i in range(1, n):
	prefix_sum1.append(arr[i] + prefix_sum1[-1])
suffix_sum1 = [arr[-1]]
for i in range(n - 2, -1, -1):
	suffix_sum1.append(arr[i] + suffix_sum1[-1])
prefix_sum2 = [arranged[0]]
for i in range(1, n):
	prefix_sum2.append(arranged[i] + prefix_sum2[-1])
suffix_sum2 = [arranged[-1]]
for i in range(n - 2, -1, -1):
	suffix_sum2.append(arranged[i] + suffix_sum2[-1])
m = int(input())
for i in range(m):
	(type, l, r) = map(int, input().split())
	if type == 1:
		if r - l + 1 == n:
			print(sum1)
		elif l == 1:
			print(sum1 - suffix_sum1[n - r - 1])
		elif r == n:
			print(sum1 - prefix_sum1[l - 2])
		elif r == n:
			print(sum1 - (prefix_sum1[l - 2] + suffix_sum1[n - r - 1]))
		else:
			print(sum1 - (prefix_sum1[l - 2] + suffix_sum1[n - r - 1]))
	elif r - l + 1 == n:
		print(sum2)
	elif l == 1:
		print(sum2 - suffix_sum2[n - r - 1])
	elif r == n:
		print(sum2 - prefix_sum2[l - 2])
	elif r == n:
		print(sum2 - (prefix_sum2[l - 2] + suffix_sum2[n - r - 1]))
	else:
		print(sum2 - (prefix_sum2[l - 2] + suffix_sum2[n - r - 1]))


n = int(input())
l = list(map(int, input().split()))
s = sorted(l)
for i in range(1, n):
	l[i] += l[i - 1]
	s[i] += s[i - 1]
l.reverse()
l.append(0)
l.reverse()
s.reverse()
s.append(0)
s.reverse()
for _ in range(int(input())):
	(ty, li, r) = map(int, input().split())
	if ty == 1:
		print(l[r] - l[li - 1])
	else:
		print(s[r] - s[li - 1])


def prefix(nums):
	if len(nums) == 0:
		return []
	ret = [nums[0]]
	for i in range(1, len(nums)):
		ret.append(nums[i] + ret[i - 1])
	ret.append(0)
	return ret
n = int(input())
v = list(map(int, input().split(' ')))
vp = prefix(v)
u = sorted(v)
up = prefix(u)
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split(' '))
	if t == 1:
		print(vp[r - 1] - vp[l - 2])
	else:
		print(up[r - 1] - up[l - 2])


from itertools import accumulate
n = int(input())
v = list(map(int, input().split()))
v2 = list(accumulate(v))
w = sorted(v)
w2 = list(accumulate(w))
for i in range(int(input())):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		if l != 1:
			print(v2[r - 1] - v2[l - 2])
		else:
			print(v2[r - 1])
	elif l != 1:
		print(w2[r - 1] - w2[l - 2])
	else:
		print(w2[r - 1])


n = int(input())
arr = [0]
for x in input().split():
	arr.append(int(x))
ac = [0]
for i in range(1, n + 1):
	ac.append(arr[i] + ac[i - 1])
arr.sort()
acSorted = [0]
for i in range(1, n + 1):
	acSorted.append(arr[i] + acSorted[i - 1])
m = int(input())
for i in range(m):
	(op, l, r) = map(int, input().split())
	if op == 1:
		print(ac[r] - ac[l - 1])
	else:
		print(acSorted[r] - acSorted[l - 1])


n = int(input())
arr = list(map(int, input().split()))
cumm = []
count = 0
cumm.append(count)
for i in range(n):
	count += arr[i]
	cumm.append(count)
arr.sort()
cumm_sort = []
count = 0
cumm_sort.append(count)
for i in range(n):
	count += arr[i]
	cumm_sort.append(count)
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(cumm[r] - cumm[l - 1])
	else:
		print(cumm_sort[r] - cumm_sort[l - 1])


input()
ins = list(map(int, input().split(' ')))
inns = [ins[0]]
for i in range(1, len(ins)):
	inns.append(inns[-1] + ins[i])
sins = [0]
for num in sorted(ins):
	sins.append(num + sins[-1])
sins = sins[1:]
for i in range(int(input())):
	(a, b, c) = map(int, input().split(' '))
	b -= 2
	c -= 1
	if a == 1:
		if b < 0:
			print(inns[c])
			continue
		print(inns[c] - inns[b])
	else:
		if b < 0:
			print(sins[c])
			continue
		print(sins[c] - sins[b])


from itertools import accumulate as am
n = int(input())
s = [*map(int, input().split())]
l = [0] + [*am(s)]
s.sort()
l2 = [0] + [*am(s)]
m = int(input())
for _ in ' ' * m:
	(x, p, r) = map(int, input().split())
	print([l[r] - l[p - 1], l2[r] - l2[p - 1]][x != 1])


n = int(input())
a1 = list(map(int, input().split()))
s = []
for i in range(n):
	if i == 0:
		s.append(a1[i])
	else:
		s.append(s[-1] + a1[i])
a2 = sorted(a1)
sd = []
for i in range(n):
	if i == 0:
		sd.append(a2[i])
	else:
		sd.append(a2[i] + sd[-1])
for i in range(int(input())):
	(t, l, r) = list(map(int, input().split()))
	if t == 1:
		print(s[r - 1] - s[l - 1] + a1[l - 1])
	else:
		print(sd[r - 1] - sd[l - 1] + a2[l - 1])


n = int(input())
v = list(map(int, input().split()))
u = list(v)
u.sort()
for i in range(1, n):
	v[i] += v[i - 1]
	u[i] += u[i - 1]
u.insert(0, 0)
v.insert(0, 0)
for _ in range(int(input())):
	(ty, l, r) = map(int, input().split())
	print(v[r] - v[l - 1] if ty == 1 else u[r] - u[l - 1])


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	a = LI()
	q = I()
	aa = [LI() for _ in range(q)]
	b = sorted(a)
	t = [0]
	u = [0]
	for i in range(n):
		t.append(t[-1] + a[i])
		u.append(u[-1] + b[i])
	r = []
	for (k, c, d) in aa:
		if k == 1:
			r.append(t[d] - t[c - 1])
		else:
			r.append(u[d] - u[c - 1])
	return '\n'.join(map(str, r))
print(main())


n = int(input())
v = input().split()
for (index, item) in enumerate(v):
	v[index] = int(item)
s = sorted(v)
a = [0, v[0]]
b = [0, s[0]]
for i in range(1, n):
	a.append(v[i] + a[i])
	b.append(s[i] + b[i])
line = [0, a, b]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	print(line[t][r] - line[t][l - 1])


n = int(input())
t = list(map(int, input().split()))
g = sorted(t)
one = [t[0]]
two = [g[0]]
if n > 1:
	for j in range(1, n):
		one.append(one[-1] + t[j])
	for k in range(1, n):
		two.append(two[-1] + g[k])
for i in range(int(input())):
	(a, l, r) = map(int, input().split())
	l -= 1
	r -= 1
	if a == 1:
		if l == 0:
			print(one[r])
		else:
			print(one[r] - one[l - 1])
	elif l == 0:
		print(two[r])
	else:
		print(two[r] - two[l - 1])


import sys
input = sys.stdin.readline
I = lambda : map(int, input().split())
n = int(input())
v = [0] + list(I())
u = sorted(v)
for i in range(1, n + 1):
	v[i] += v[i - 1]
	u[i] += u[i - 1]
for _ in range(int(input())):
	(t, l, r) = I()
	if t == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])


n = int(input())
arr = [*map(int, input().split())]
brr = arr.copy()
brr.sort()
dp1 = [0] * (n + 1)
dp2 = [0] * (n + 1)
for i in range(0, n):
	dp1[i + 1] = dp1[i] + arr[i]
for i in range(0, n):
	dp2[i + 1] = dp2[i] + brr[i]
for query in range(int(input())):
	(t, l, r) = input().split()
	if int(t) == 1:
		print(dp1[int(r)] - dp1[int(l) - 1])
	else:
		print(dp2[int(r)] - dp2[int(l) - 1])


def inp():
	return map(int, input().split())

def arr_inp():
	return [int(x) for x in input().split()]

def arr_sum(arr):
	new_arr = [0, arr[0]]
	for (i, j) in enumerate(arr[1:]):
		new_arr.append(new_arr[i + 1] + j)
	return new_arr
(n, v, m) = (int(input()), arr_inp(), int(input()))
(v_sum, v_sort_sum) = (arr_sum(v), arr_sum(sorted(v)))
for i in range(m):
	(type, l, r) = inp()
	if type == 1:
		print(v_sum[r] - v_sum[l - 1])
	else:
		print(v_sort_sum[r] - v_sort_sum[l - 1])


import sys
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')
n = int(input())
v = list(map(int, input().split()))
u = sorted(v)
prefv = []
prefu = []
c1 = 0
c2 = 0
for i in range(n):
	c1 += v[i]
	c2 += u[i]
	prefv.append(c1)
	prefu.append(c2)
q = int(input())
for _ in range(q):
	(t, l, r) = map(int, input().split())
	l = l - 1
	r = r - 1
	if t == 1:
		if l == 0:
			print(prefv[r])
		else:
			print(prefv[r] - prefv[l - 1])
	elif l == 0:
		print(prefu[r])
	else:
		print(prefu[r] - prefu[l - 1])


n = int(input())
l = [int(i) for i in input().split()]
pre = [0] * (n + 1)
for i in range(1, n + 1):
	pre[i] = pre[i - 1] + l[i - 1]
l.sort()
pre1 = [0] * (n + 1)
for i in range(1, n + 1):
	pre1[i] = pre1[i - 1] + l[i - 1]
q = int(input())
for _ in range(q):
	(a, l, r) = map(int, input().split())
	if a == 1:
		print(pre[r] - pre[l - 1])
	else:
		print(pre1[r] - pre1[l - 1])


def cumSum(arr, start, end):
	total = arr[end]
	minus = arr[start - 1]
	total -= minus
	return total
n = input()
n = int(n) + 1
stones = list(map(int, input().split()))
stones.insert(0, 0)
sotnesSum = [0] * n
sortedStones = sorted(stones)
sortedStonesSum = [0] * n
for i in range(1, n):
	sotnesSum[i] = sotnesSum[i - 1] + stones[i]
for i in range(1, n):
	sortedStonesSum[i] = sortedStonesSum[i - 1] + sortedStones[i]
m = int(input())
out = []
for i in range(m):
	(type, start, end) = map(int, input().split())
	if type == 1:
		out.append(cumSum(sotnesSum, start, end))
	else:
		out.append(cumSum(sortedStonesSum, start, end))
for i in out:
	print(i)


from itertools import accumulate
n = int(input())
l1 = list(map(int, input().split()))
l2 = list(accumulate(l1))
l3 = sorted(list(l1))
l4 = list(accumulate(l3))
m = int(input())
for i in range(m):
	(x, y, z) = map(int, input().split())
	if x == 1:
		if y == 1:
			print(l2[z - 1])
		else:
			print(l2[z - 1] - l2[y - 2])
	elif y == 1:
		print(l4[z - 1])
	else:
		print(l4[z - 1] - l4[y - 2])


S = sorted
M = lambda : map(int, input().split())
m = int(input())
a = list(M())
p = S(a)

def pre_sum(arr):
	s_arr = [0]
	for i in range(1, len(arr) + 1):
		s_arr += [s_arr[i - 1] + arr[i - 1]]
	return s_arr
(sa, sp) = (pre_sum(a), pre_sum(p))
n = int(input())
for i in range(n):
	(k, l, r) = M()
	if k == 1:
		print(sa[r] - sa[l - 1])
	else:
		print(sp[r] - sp[l - 1])


n = int(input())
l = [int(i) for i in input().split()]
d = sorted(l)
arr1 = [0] * len(l)
arr2 = [0] * len(l)
s = 0
for i in range(len(l)):
	s += l[i]
	arr1[i] = s
s = 0
for j in range(len(l)):
	s += d[j]
	arr2[j] = s
for _ in range(int(input())):
	(a, b, c) = input().split()
	bc = int(b + c)
	(a, b, c) = (int(a), int(b), int(c))
	if a == 1:
		print(arr1[c - 1] - arr1[b - 1] + l[b - 1])
	else:
		print(arr2[c - 1] - arr2[b - 1] + d[b - 1])


def stones(n, arr):
	sorted_list = sorted(arr)
	prefixSum = [0] * n
	prefixSum[0] = arr[0]
	prefixSum_sorted = [0] * n
	prefixSum_sorted[0] = sorted_list[0]
	for i in range(1, n):
		prefixSum[i] = prefixSum[i - 1] + arr[i]
		prefixSum_sorted[i] = prefixSum_sorted[i - 1] + sorted_list[i]
	t = int(input())
	l = []
	for i in range(t):
		l.append(list(map(int, input().split())))
	for i in l:
		if i[0] == 1:
			if i[1] == 1:
				print(prefixSum[i[2] - 1])
			else:
				print(prefixSum[i[2] - 1] - prefixSum[i[1] - 2])
		elif i[1] == 1:
			print(prefixSum_sorted[i[2] - 1])
		else:
			print(prefixSum_sorted[i[2] - 1] - prefixSum_sorted[i[1] - 2])
n = int(input())
arr = list(map(int, input().split()))
stones(n, arr)


n = int(input())
L = list(map(int, input().split()))
S = list(L)
for i in range(1, n):
	S[i] += S[i - 1]
X = list(sorted(L))
for i in range(1, n):
	X[i] += X[i - 1]
q = int(input())
while q:
	q -= 1
	(t, x, y) = list(map(int, input().split()))
	if t == 1:
		ans = S[y - 1]
		if x > 1:
			ans -= S[x - 2]
	else:
		ans = X[y - 1]
		if x > 1:
			ans -= X[x - 2]
	print(ans)


n = int(input())
a = list(map(int, input().split()))
t = int(input())
b = sorted(a)
for i in range(n - 1):
	a[i + 1] += a[i]
	b[i + 1] += b[i]
(a, b) = ([0] + a, [0] + b)
for i in range(t):
	(qt, l, r) = map(int, input().split())
	print(b[r] - b[l - 1] if qt == 2 else a[r] - a[l - 1])


n = int(input().strip())
v = list(map(int, input().split()))
m = int(input().strip())
f1 = [0] * n
f2 = [0] * n
u = sorted(v)
for i in range(n):
	f1[i] = v[i]
	if i > 0:
		f1[i] += f1[i - 1]
for i in range(n):
	f2[i] = u[i]
	if i > 0:
		f2[i] += f2[i - 1]
ans = []
for i in range(m):
	(t, l, r) = map(int, input().split())
	l -= 1
	r -= 1
	if t == 1:
		an = f1[r]
		if l > 0:
			an -= f1[l - 1]
		ans.append(an)
	else:
		an = f2[r]
		if l > 0:
			an -= f2[l - 1]
		ans.append(an)
print(*ans, sep='\n')


from itertools import accumulate as ac
n = int(input())
a = list(map(int, input().split()))
s = sorted(a)
m = int(input())
lis = [0] + list(ac(a))
lis_sort = [0] + list(ac(s))
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(lis[r] - lis[l - 1])
	else:
		print(lis_sort[r] - lis_sort[l - 1])


def main():
	n = int(input())
	stones = list(map(int, input().split()))
	sorted_stones = sorted(stones)
	for i in range(1, n):
		stones[i] += stones[i - 1]
	for i in range(1, n):
		sorted_stones[i] += sorted_stones[i - 1]
	m = int(input())
	for _ in range(m):
		(question, l, r) = map(int, input().split())
		if question == 1:
			desired_stones = stones
		else:
			desired_stones = sorted_stones
		if l > 1:
			print(desired_stones[r - 1] - desired_stones[l - 2])
		else:
			print(desired_stones[r - 1])
main()


n = int(input())
I = list(map(int, input().split()))
P = sorted(I)
sI = [0] * (n + 1)
sP = [0] * (n + 1)
for i in range(1, n + 1):
	sI[i] = sI[i - 1] + I[i - 1]
	sP[i] = sP[i - 1] + P[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = list(map(int, input().split()))
	if t == 1:
		print(sI[r] - sI[l - 1])
	else:
		print(sP[r] - sP[l - 1])


n = int(input())
arr = [int(i) for i in input().split()]
brr = arr[:]
brr.sort()
sarr = []
sbrr = []
s1 = 0
s2 = 0
for i in range(n):
	s1 += arr[i]
	s2 += brr[i]
	sarr.append(s1)
	sbrr.append(s2)
m = int(input())
for i in range(m):
	(a, l, r) = map(int, input().split())
	if a == 1:
		if l == 1:
			print(sarr[r - 1])
		else:
			print(sarr[r - 1] - sarr[l - 2])
	elif l == 1:
		print(sbrr[r - 1])
	else:
		print(sbrr[r - 1] - sbrr[l - 2])


n = int(input())
a = list(map(int, input().split()))
b = [0]
c = [0]
for x in a:
	b.append(b[-1] + x)
a.sort()
for x in a:
	c.append(c[-1] + x)
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(b[r] - b[l - 1])
	else:
		print(c[r] - c[l - 1])


n = int(input())
l = list(map(int, input().split()))
t = []
t = t + l
l.sort()
c1 = [t[0]]
c2 = [l[0]]
for i in range(1, n):
	c1.append(c1[i - 1] + t[i])
	c2.append(c2[i - 1] + l[i])
m = int(input())
for i in range(m):
	(p, le, r) = map(int, input().split())
	if p == 1:
		if le == 1:
			print(c1[r - 1])
		else:
			print(c1[r - 1] - c1[le - 2])
	elif le == 1:
		print(c2[r - 1])
	else:
		print(c2[r - 1] - c2[le - 2])


from collections import Counter
import string
import math
import sys
from fractions import Fraction
from itertools import product

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))

def printDivisors(n):
	divisors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				divisors.append(i)
			else:
				divisors.extend((i, n // i))
		i = i + 1
	return divisors

def countTotalBits(num):
	binary = bin(num)[2:]
	return len(binary)

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
mod = 10 ** 9 + 7

def comp(x):
	return x[1]

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			primes.append(p * p)
	primes = []

def binary_search(arr, x):
	low = 0
	high = len(arr) - 1
	mid = 0
	while low <= high:
		mid = (high + low) // 2
		if arr[mid] < x:
			low = mid + 1
		elif arr[mid] > x:
			high = mid - 1
		else:
			return mid
	return -1

def lcm(a, b):
	return a * b // math.gcd(a, b)

def primeFactors(n):
	factors = []
	while n % 2 == 0:
		factors.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n = n // i
	if n > 2:
		factors.append(n)
	return factors
mod = 10 ** 9 + 7
testCases = 1
for _ in range(testCases):
	n = vary(1)
	num = array_int()
	q = vary(1)
	preSum = [0 for i in range(len(num))]
	for i in range(n):
		preSum[i] += preSum[i - 1] + num[i]
	preSum += [0]
	num.sort()
	postSum = [0 for i in range(len(num))]
	for i in range(n):
		postSum[i] += postSum[i - 1] + num[i]
	postSum += [0]
	for i in range(q):
		(typ, l, r) = vary(3)
		if typ == 2:
			print(postSum[r - 1] - postSum[l - 2])
		else:
			print(preSum[r - 1] - preSum[l - 2])


n = int(input())
costs = list(map(int, input().split()))
soma = [[] for e in range(n + 2)]
soma2 = [[] for e in range(n + 2)]
costs2 = sorted(costs)
soma[0] = 0
soma2[0] = 0
soma[-1] = 0
soma2[-1] = 0
for i in range(n, 0, -1):
	soma[i] = soma[i + 1] + costs[i - 1]
	soma2[i] = soma2[i + 1] + costs2[i - 1]
nQuest = int(input())
for quest in range(nQuest):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		print(soma[l] - soma[r + 1])
	else:
		print(soma2[l] - soma2[r + 1])


n = int(input())
lista = list(map(int, input().split()))
listaSort = sorted(lista, key=int)
m = int(input())
sumAux = [0] * (n + 1)
sumAuxSort = [0] * (n + 1)
for i in range(1, n + 1):
	sumAux[i] = sumAux[i - 1] + lista[i - 1]
	sumAuxSort[i] = sumAuxSort[i - 1] + listaSort[i - 1]
for i in range(m):
	tlr = list(map(int, input().split()))
	if tlr[0] == 2:
		print(sumAuxSort[tlr[2]] - sumAuxSort[tlr[1] - 1])
	else:
		print(sumAux[tlr[2]] - sumAux[tlr[1] - 1])


import math as mt
import sys, string, bisect
input = sys.stdin.readline
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
n = I()
l = L()
g = [0, l[0]]
for i in range(1, n):
	g.append(g[-1] + l[i])
d = sorted(l)
s = [0, d[0]]
for i in range(1, n):
	s.append(s[-1] + d[i])
m = I()
for i in range(m):
	(a, l, r) = M()
	if a == 1:
		print(g[r] - g[l - 1])
	else:
		print(s[r] - s[l - 1])


n = int(input())
a = list(map(int, input().split()))
import copy
b = copy.deepcopy(a)
b.sort()
arr_a = [0] * (n + 1)
arr_b = [0] * (n + 1)
for i in range(1, n + 1):
	arr_a[i] = arr_a[i - 1] + a[i - 1]
	arr_b[i] = arr_b[i - 1] + b[i - 1]
m = int(input())
for i in range(m):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		print(arr_a[r] - arr_a[l - 1])
	elif typ == 2:
		print(arr_b[r] - arr_b[l - 1])


n = int(input())
v = [0] + [int(x) for x in input().strip().split()]
u = sorted(v)
for i in range(0, n):
	v[i + 1] = v[i + 1] + v[i]
	u[i + 1] = u[i + 1] + u[i]
m = int(input())
while m > 0:
	(q, l, r) = [int(x) for x in input().strip().split()]
	if q == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])
	m -= 1


alph = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
v = list(map(int, input().split()))
m = int(input())
v_sort = v * 1
v_sort.sort()
vsp = [0]
vp = [0]
for i in range(n):
	vsp.append(vsp[i] + v_sort[i])
	vp.append(vp[i] + v[i])
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(vp[r] - vp[l - 1])
	else:
		print(vsp[r] - vsp[l - 1])


n = int(input())
v = [int(i) for i in input().split()]
u = sorted(v)
d = [[v[0], u[0]]]
for i in range(n):
	d.append([d[i][0] + v[i], d[i][1] + u[i]])
ans = []
for i in range(int(input())):
	(t, l, r) = [int(i) for i in input().split()]
	if t == 1:
		ans.append(d[r][0] - d[l - 1][0])
	else:
		ans.append(d[r][1] - d[l - 1][1])
for i in ans:
	print(i)


n = int(input())
l = list(map(int, input().split()))
s = sorted(l)
q = int(input())
for i in range(1, n):
	s[i] += s[i - 1]
	l[i] += l[i - 1]
for i in range(q):
	(t, le, r) = map(int, input().split())
	if t == 1:
		if le != 1:
			print(l[r - 1] - l[le - 2])
		else:
			print(l[r - 1])
	elif le != 1:
		print(s[r - 1] - s[le - 2])
	else:
		print(s[r - 1])


n = int(input())
arr = [int(num) for num in input().split(' ')]
arr2 = arr.copy()
arr2.sort()
d1 = [0]
for item in arr:
	d1.append(d1[-1] + item)
d2 = [0]
for item in arr2:
	d2.append(d2[-1] + item)
for _ in range(int(input())):
	(t, l, r) = map(int, input().split(' '))
	if t == 1:
		print(d1[r] - d1[l - 1])
	else:
		print(d2[r] - d2[l - 1])


import sys

def main1():
	n = int(input())
	v = [int(i) for i in input().split()]
	m = int(input())
	v_cp = sorted(v)
	idx = 0
	ans = []
	while True:
		if idx >= m:
			break
		idx += 1
		(t, l, r) = [int(i) for i in sys.stdin.readline().split()]
		if t == 1:
			ans.append(sum(v[l - 1:r]))
		elif t == 2:
			ans.append(sum(v_cp[l - 1:r]))
	for i in ans:
		print(i)

def main2():
	n = int(input())
	v = [int(i) for i in input().split()]
	m = int(input())
	v_cp = sorted(v)
	for i in range(1, len(v)):
		v[i] += v[i - 1]
		v_cp[i] += v_cp[i - 1]
	while m > 0:
		m -= 1
		(t, l, r) = [int(i) for i in sys.stdin.readline().split()]
		l -= 1
		r -= 1
		if t == 1:
			print(v[r] - v[l - 1] if l > 0 else v[r])
		else:
			print(v_cp[r] - v_cp[l - 1] if l > 0 else v_cp[r])
main2()


n = int(input())
arr = list(map(int, input().split()))
ans = [arr[0]]
for i in range(1, n):
	ans.append(ans[-1] + arr[i])
temp = arr[:]
temp.sort()
t = [temp[0]]
for i in range(1, n):
	t.append(t[-1] + temp[i])
m = int(input())
for i in range(m):
	(x, l, r) = list(map(int, input().split()))
	if x == 1:
		(l, r) = (l - 1, r - 1)
		q = ans[r] - ans[l] + arr[l]
		print(q)
	else:
		(l, r) = (l - 1, r - 1)
		q = t[r] - t[l] + temp[l]
		print(q)


count = int(input())
v = [int(c) for c in input().split()]
vsort = v[:]
vsort.sort()
sumV = [0, v[0]]
sumVS = [0, vsort[0]]
for i in range(1, count):
	sumV.append(sumV[i] + v[i])
	sumVS.append(sumVS[i] + vsort[i])
qc = int(input())
for x in range(qc):
	q = [int(c) for c in input().split()]
	if q[0] == 1:
		tmp = sumV
	else:
		tmp = sumVS
	print(tmp[q[2]] - tmp[q[1] - 1])


n = int(input())
a = list(map(int, input().strip().split()))
pa = [a[0]]
for i in range(1, n):
	pa.append(pa[i - 1] + a[i])
b = sorted(a)
pb = [b[0]]
for i in range(1, n):
	pb.append(pb[i - 1] + b[i])
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().strip().split())
	if t == 1:
		if l == r:
			print(a[l - 1])
		elif l == 1 and r == n:
			print(pa[r - 1])
		elif l - 2 < 0:
			print(pa[r - 1])
		else:
			print(pa[r - 1] - pa[l - 2])
	if t == 2:
		if l == r:
			print(b[l - 1])
		elif l == 1 and r == n:
			print(pb[r - 1])
		elif l - 2 < 0:
			print(pb[r - 1])
		else:
			print(pb[r - 1] - pb[l - 2])


n = int(input())
a = list(map(int, input().split()))
m = int(input())
s = sorted(a)
pre = [a[0]]
s_pre = [s[0]]
for i in range(1, n):
	pre.append(a[i] + pre[i - 1])
	s_pre.append(s[i] + s_pre[i - 1])
for _ in range(m):
	(a, b, c) = map(int, input().split())
	if a == 1:
		if b == 1:
			print(pre[c - 1])
		else:
			print(pre[c - 1] - pre[b - 2])
	elif b == 1:
		print(s_pre[c - 1])
	else:
		print(s_pre[c - 1] - s_pre[b - 2])


n = int(input())
ar = list(map(int, input().split()))
arr = sorted(ar)
q = int(input())
pre1 = [0] * n
pre2 = [0] * n
pre1[0] = ar[0]
pre2[0] = arr[0]
sb = []
for i in range(1, n):
	pre1[i] = pre1[i - 1] + ar[i]
	pre2[i] = pre2[i - 1] + arr[i]
while q != 0:
	q -= 1
	(x, a, b) = map(int, input().split())
	a -= 1
	b -= 1
	if x == 1:
		if a != 0:
			sb.append(str(pre1[b] - pre1[a - 1]))
		else:
			sb.append(str(pre1[b]))
	elif a != 0:
		sb.append(str(pre2[b] - pre2[a - 1]))
	else:
		sb.append(str(pre2[b]))
print('\n'.join(sb))


(n, stones) = (int(input()), [int(i) for i in input().split()])
sorted_stones = list(sorted(stones))
(psa, sorted_psa) = ([stones[0]], [sorted_stones[0]])
for i in range(n):
	psa.append(psa[i] + stones[i])
	sorted_psa.append(sorted_psa[i] + sorted_stones[i])
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(psa[r] - psa[l - 1])
	elif t == 2:
		print(sorted_psa[r] - sorted_psa[l - 1])


n = int(input())
l = list(map(int, input().split()))
l1 = sorted(l)
pfx = [0] * n
p = [0] * n
p[0] = l[0]
pfx[0] = l1[0]
for i in range(1, n):
	pfx[i] = l1[i] + pfx[i - 1]
	p[i] = l[i] + p[i - 1]
q = int(input())
for i in range(q):
	(t, l11, r) = map(int, input().split())
	if t == 1:
		ans = p[r - 1] - p[l11 - 1] + l[l11 - 1]
		print(ans)
	else:
		ans = pfx[r - 1] - pfx[l11 - 1] + l1[l11 - 1]
		print(ans)


n = int(input())
cost = list(map(int, input().split()))
tasks = []
cost_sort = cost[:]
cost_sort.sort()
dyn = [0, cost[0]]
dyn2 = [0, cost_sort[0]]
for k in range(2, n + 1):
	dyn.append(dyn[k - 1] + cost[k - 1])
	dyn2.append(dyn2[k - 1] + cost_sort[k - 1])
for m in range(int(input())):
	tasks.append(list(map(int, input().split())))
for i in range(len(tasks)):
	if tasks[i][0] == 1:
		print(dyn[tasks[i][2]] - dyn[tasks[i][1] - 1])
	else:
		print(dyn2[tasks[i][2]] - dyn2[tasks[i][1] - 1])


x = int(input())
s = list(map(int, input().split()))
dp = [0 for n in range(x)]
dp1 = [0 for n in range(x)]
z = sorted(s)
dp[0] = s[0]
dp1[0] = z[0]
for n in range(1, x):
	dp[n] = dp[n - 1] + s[n]
	dp1[n] = dp1[n - 1] + z[n]
for _ in range(int(input())):
	(p, a, b) = map(int, input().split())
	if p == 1:
		print(dp[b - 1] - dp[a - 1] + s[a - 1])
	else:
		print(dp1[b - 1] - dp1[a - 1] + z[a - 1])


n = int(input())
array = list(map(int, input().split()))
sums_first = [0] * (n + 1)
for i in range(n):
	sums_first[i + 1] = array[i] + sums_first[i]
array.sort()
sums_second = [0] * (n + 1)
for i in range(n):
	sums_second[i + 1] = array[i] + sums_second[i]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(sums_first[r] - sums_first[l - 1])
	else:
		print(sums_second[r] - sums_second[l - 1])


import sys
from itertools import accumulate
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve():
	pass
n = int(input())
arr = int_arr()
dp = [0] + list(accumulate(arr))
arr.sort()
dp_sorted = [0] + list(accumulate(arr))
q = int(input())
for i in range(q):
	(t, l, r) = get_int()
	print(dp[r] - dp[l - 1] if t == 1 else dp_sorted[r] - dp_sorted[l - 1])


def mergeSort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		first = arr[:mid]
		second = arr[mid:]
		mergeSort(first)
		mergeSort(second)
		i = j = k = 0
		while i < len(first) and j < len(second):
			if first[i] > second[j]:
				arr[k] = second[j]
				j += 1
			else:
				arr[k] = first[i]
				i += 1
			k += 1
		while i < len(first):
			arr[k] = first[i]
			i += 1
			k += 1
		while j < len(second):
			arr[k] = second[j]
			j += 1
			k += 1
f = int(input())
g = [0]
data = map(int, input().split())
g.extend(data)
b = list.copy(g)
mergeSort(b)
for i in range(1, len(b)):
	b[i] += b[i - 1]
	g[i] += g[i - 1]
for _ in range(int(input())):
	(counter, x, y) = map(int, input().split())
	if counter == 2:
		print(b[y] - b[x - 1])
	else:
		print(g[y] - g[x - 1])


import sys
input = lambda : sys.stdin.readline().strip()
li = lambda : list(map(int, input().split()))
I = lambda : int(input())
n = I()
l = li()
s = sorted(l)
p = [0]
tot = 0
for i in l:
	tot += i
	p.append(tot)
ps = [0]
tot = 0
for i in s:
	tot += i
	ps.append(tot)
m = I()
for i in range(m):
	(t, l, r) = li()
	if t == 1:
		print(p[r] - p[l - 1])
	else:
		print(ps[r] - ps[l - 1])


n = int(input())
inp = list(map(int, input().split()))
sm = [inp[0]]
for i in range(1, n):
	sm.append(inp[i] + sm[i - 1])
inp.sort()
cost = [inp[0]]
for i in range(1, n):
	cost.append(inp[i] + cost[i - 1])
for m in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			print(sm[r - 1])
		else:
			print(sm[r - 1] - sm[l - 2])
	elif l == 1:
		print(cost[r - 1])
	else:
		print(cost[r - 1] - cost[l - 2])


def get_prefix_arr(array):
	prefix_arr = []
	for i in range(len(array)):
		prefix_arr.append(array[i] if i == 0 else array[i] + prefix_arr[i - 1])
	return prefix_arr

def sum_range(pre_arr, i, j):
	if i == 0:
		return pre_arr[j]
	return pre_arr[j] - pre_arr[i - 1]
n = int(input())
s = input()
arr = [int(i) for i in s.split()]
arr_sorted = sorted(arr)
arr = get_prefix_arr(arr)
arr_sorted = get_prefix_arr(arr_sorted)
q = int(input())
for i in range(q):
	s = input().split()
	query = [int(i) for i in s]
	t = query[0]
	(l, r) = (query[1] - 1, query[2] - 1)
	if t == 1:
		print(sum_range(arr, l, r))
	else:
		print(sum_range(arr_sorted, l, r))


n = int(input())
arr = [int(p) for p in input().split()]
q = int(input())
queries = []
for _ in range(q):
	a = [int(p) for p in input().split()]
	queries.append(a)
preSum = [arr[0]]
for i in range(1, len(arr)):
	ele = preSum[-1] + arr[i]
	preSum.append(ele)
sort = sorted(arr)
preCost = [sort[0]]
for i in range(1, len(arr)):
	ele = preCost[-1] + sort[i]
	preCost.append(ele)
for i in range(len(queries)):
	(typ, l, r) = queries[i]
	if typ == 2:
		if l == 1:
			print(preCost[r - 1])
		else:
			print(preCost[r - 1] - preCost[l - 2])
	elif l == 1:
		print(preSum[r - 1])
	else:
		print(preSum[r - 1] - preSum[l - 2])


I = lambda : list(map(int, input().split()))
n = int(input())
a = [0] + I()
(q, m) = (sorted(a), int(input()))
for i in range(1, n + 1):
	a[i] += a[i - 1]
	q[i] += q[i - 1]
for i in range(m):
	l = I()
	print(a[l[2]] - a[l[1] - 1] if l[0] == 1 else q[l[2]] - q[l[1] - 1])


n = int(input())
cost = [*map(int, input().split())]
cheappest = cost.copy()
cheappest.sort()
cheappestcp = cheappest.copy()
costcp = cost.copy()
for i in range(1, n):
	cost[i] += cost[i - 1]
	cheappest[i] += cheappest[i - 1]
quest = int(input())
for i in range(quest):
	(tp, l, r) = input().split()
	l = int(l)
	r = int(r)
	if tp == '1':
		print(cost[r - 1] - cost[l - 1] + costcp[l - 1])
	else:
		print(cheappest[r - 1] - cheappest[l - 1] + cheappestcp[l - 1])


t = int(input())
a = list(map(int, input().split()))
b = [0] * len(a)
b[0] = a[0]
for i in range(1, t):
	b[i] = b[i - 1] + a[i]
a.sort()
c = [0] * len(a)
c[0] = a[0]
for i in range(1, t):
	c[i] = c[i - 1] + a[i]
for _ in range(int(input())):
	(x, y, z) = map(int, input().split())
	if x == 1:
		if y == 1:
			print(b[z - 1])
		else:
			print(b[z - 1] - b[y - 2])
	elif y == 1:
		print(c[z - 1])
	else:
		print(c[z - 1] - c[y - 2])


from sys import stdin, stdout
from collections import defaultdict
import heapq as hq

def get_int():
	return int(stdin.readline().strip())

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_array():
	return list(map(int, stdin.readline().strip().split()))

def get_string():
	return stdin.readline().strip()
INF = 10000000000.0
n = get_int()
v = get_array()
u = sorted(v)
a = [0] * n
b = [0] * n
a[0] = v[0]
b[0] = u[0]
for i in range(1, n):
	a[i] = a[i - 1] + v[i]
	b[i] = b[i - 1] + u[i]
m = get_int()
for i in range(m):
	(t, l, r) = get_ints()
	if t == 1:
		if l == 1:
			print(a[r - 1])
		else:
			print(a[r - 1] - a[l - 2])
	elif l == 1:
		print(b[r - 1])
	else:
		print(b[r - 1] - b[l - 2])


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	s_arr = sorted(arr)
	(cum_arr1, cum_arr2) = ([], [])
	(sum1, sum2) = (0, 0)
	for i in range(1, n):
		arr[i] += arr[i - 1]
		s_arr[i] += s_arr[i - 1]
	arr.insert(0, 0)
	s_arr.insert(0, 0)
	m = int(input())
	for _ in range(m):
		(t, l, r) = map(int, input().split())
		if t == 1:
			print(arr[r] - arr[l - 1])
		elif t == 2:
			print(s_arr[r] - s_arr[l - 1])
main()


n = int(input())
v = list(map(int, input().split()))
vs = sorted(v)
sums_v = []
sums_vs = []
sum_v = 0
sum_vs = 0
for i in range(n):
	sum_v += v[i]
	sum_vs += vs[i]
	sums_v.append(sum_v)
	sums_vs.append(sum_vs)
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(sums_v[r - 1] - sums_v[l - 1] + v[l - 1])
	else:
		print(sums_vs[r - 1] - sums_vs[l - 1] + vs[l - 1])


n = int(input())
v = list(map(int, input().split()))
c = sorted(v)
ordered = [0] * n
ordered[0] = v[0]
for i in range(1, n):
	ordered[i] = ordered[i - 1] + v[i]
sorted_a = [0] * n
sorted_a[0] = c[0]
for i in range(1, n):
	sorted_a[i] = sorted_a[i - 1] + c[i]
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(ordered[r - 1] - ordered[l - 2] if l > 1 else ordered[r - 1])
	else:
		print(sorted_a[r - 1] - sorted_a[l - 2] if l > 1 else sorted_a[r - 1])


n = int(input())
a = list(map(int, input().split()))
arr1 = [0]
for i in a:
	arr1.append(arr1[-1] + i)
a.sort()
arr2 = [0]
for i in a:
	arr2.append(arr2[-1] + i)
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(arr1[r] - arr1[l - 1])
	else:
		print(arr2[r] - arr2[l - 1])


n = int(input())
l1 = list(map(int, input().split()))
l2 = sorted(l1)
for i in range(1, n):
	l1[i] = l1[i] + l1[i - 1]
	l2[i] = l2[i] + l2[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			s = l1[r - 1]
		else:
			s = l1[r - 1] - l1[l - 2]
	elif l == 1:
		s = l2[r - 1]
	else:
		s = l2[r - 1] - l2[l - 2]
	print(s)


n = int(input())
arr = list(map(int, input().split()))
arr1 = [0 for i in range(n)]
arr2 = [0 for i in range(n)]
arr1[0] = arr[0]
for i in range(1, n):
	arr1[i] = arr[i] + arr1[i - 1]
arr.sort()
arr2[0] = arr[0]
for i in range(1, n):
	arr2[i] = arr[i] + arr2[i - 1]
query = int(input())
for _ in range(query):
	(t, l, r) = map(int, input().split())
	if l == 1:
		if t == 1:
			print(arr1[r - 1])
		else:
			print(arr2[r - 1])
	elif t == 1:
		print(arr1[r - 1] - arr1[l - 2])
	else:
		print(arr2[r - 1] - arr2[l - 2])


from sys import stdin, stdout
x = lambda : stdin.readline()
y = lambda : map(int, stdin.readline().split())
n = int(x())
a = list(y())
m = int(x())
b = a[:]
b.sort()
c = []
d = []
c.append(0)
d.append(0)
sum = 0
sum1 = 0
for i in range(n):
	sum += a[i]
	sum1 += b[i]
	c.append(sum)
	d.append(sum1)
for i in range(m):
	(t, l, r) = y()
	if t == 1:
		stdout.write(str(c[r] - c[l - 1]) + '\n')
	else:
		stdout.write(str(d[r] - d[l - 1]) + '\n')


n = int(input())
v = [int(y) for y in input().split()]
u = sorted(v, reverse=False)
for i in range(1, n):
	v[i] += v[i - 1]
	u[i] += u[i - 1]
for _ in range(int(input())):
	(t, l, r) = [int(y) for y in input().split()]
	if t == 1:
		if l == 1:
			print(v[r - 1])
		else:
			print(v[r - 1] - v[l - 2])
	elif l == 1:
		print(u[r - 1])
	else:
		print(u[r - 1] - u[l - 2])


n = int(input())
v = list(map(int, input().split()))
v1 = sorted(v)
(pfv, pfv1) = ([0], [0])
for i in v:
	pfv.append(pfv[-1] + i)
for i in v1:
	pfv1.append(pfv1[-1] + i)
for m in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(pfv[r] - pfv[l - 1])
	else:
		print(pfv1[r] - pfv1[l - 1])


n = int(input())
v = list(map(int, input().split()))
s = sorted(v)
a = [v[0]] * n
b = [s[0]] * n
for i in range(1, n):
	a[i] = v[i] + a[i - 1]
	b[i] = s[i] + b[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	l -= 1
	r -= 1
	if t == 1:
		if l == 0:
			print(a[r])
		else:
			print(a[r] - a[l - 1])
	elif l == 0:
		print(b[r])
	else:
		print(b[r] - b[l - 1])


n = int(input())
A = [*map(int, input().split())]
B = list(sorted(A))
m = int(input())
preA = [0] * (len(A) + 1)
preB = [0] * (len(B) + 1)
(sa, sb) = (0, 0)
for i in range(1, len(A) + 1):
	sa += A[i - 1]
	preA[i] = sa
	sb += B[i - 1]
	preB[i] = sb
for _ in range(m):
	(type_, l, r) = map(int, input().split())
	l -= 1
	r -= 1
	if type_ == 1:
		print(preA[r + 1] - preA[l])
	else:
		print(preB[r + 1] - preB[l])


n_stones = int(input())
stones = list(map(int, input().split(' ')))
stones_ordered = stones.copy()
stones_ordered.sort()
n_questions = int(input())
sumas = [0] * n_stones
sumas_ordered = [0] * n_stones
sumas[0] = stones[0]
sumas_ordered[0] = stones_ordered[0]
mayor = 0
mayor_ordered = 0

def calcular(r):
	for i in range(mayor, r):
		sumas[i + 1] = sumas[i] + stones[i + 1]

def calcular_ordered(r):
	for i in range(mayor_ordered, r):
		sumas_ordered[i + 1] = sumas_ordered[i] + stones_ordered[i + 1]
for i in range(n_questions):
	(tipo, l, r) = map(int, input().split(' '))
	r -= 1
	if tipo == 1:
		if r > mayor and mayor != n_stones - 1:
			calcular(r)
			mayor = r
		if l == 1:
			print(sumas[r])
		else:
			print(sumas[r] - sumas[l - 2])
	else:
		if r > mayor_ordered and mayor_ordered != n_stones - 1:
			calcular_ordered(r)
			mayor_ordered = r
		if l == 1:
			print(sumas_ordered[r])
		else:
			print(sumas_ordered[r] - sumas_ordered[l - 2])


n = int(input())
v = list(map(int, input().split(' ')))
u = sorted(v)
s_v = [0]
s_u = [0]
for i in range(1, n + 1):
	s_v.append(s_v[i - 1] + v[i - 1])
	s_u.append(s_u[i - 1] + u[i - 1])
ans = []
for _ in range(int(input())):
	(t, l, r) = map(int, input().split(' '))
	if t == 1:
		tt = s_v[r] - s_v[l - 1]
	else:
		tt = s_u[r] - s_u[l - 1]
	ans.append(str(tt))
print('\n'.join(ans))


import math
import bisect
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
N = inp()
S = inlt()
V = []
for a in range(N):
	V.append(S[a])
S.sort()
pre1 = []
pre2 = []
cum1 = 0
cum2 = 0
for i in range(N):
	cum1 += V[i]
	pre1.append(cum1)
	cum2 += S[i]
	pre2.append(cum2)
M = inp()
for m in range(M):
	(t, l, r) = inlt()
	l -= 1
	r -= 1
	if t == 1:
		if l >= 1:
			print(pre1[r] - pre1[l - 1])
		else:
			print(pre1[r])
	elif l >= 1:
		print(pre2[r] - pre2[l - 1])
	else:
		print(pre2[r])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
a.insert(0, 0)
b.insert(0, 0)
for i in range(1, n + 1):
	a[i] = a[i] + a[i - 1]
	b[i] = b[i] + b[i - 1]
m = int(input())
while m:
	m -= 1
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(a[r] - a[l - 1])
	else:
		print(b[r] - b[l - 1])


n = int(input())
v = list(map(int, input().split()))
u = sorted(v)
dp = [0]
dp1 = [0]
for i in range(0, n):
	dp.append(dp[-1] + v[i])
	dp1.append(dp1[-1] + u[i])
q = int(input())
for i in range(0, q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(dp[r] - dp[l - 1])
	else:
		print(dp1[r] - dp1[l - 1])


from itertools import accumulate
input()
costs = [int(i) for i in input().split()]
costs_2nd = sorted(costs)
for_first = [0]
for_first.extend(accumulate(costs))
for_second = [0]
for_second.extend(accumulate(costs_2nd))
for _ in range(int(input())):
	(t, l, r) = (int(i) for i in input().split())
	if t == 1:
		print(for_first[r] - for_first[l - 1])
	else:
		print(for_second[r] - for_second[l - 1])


n = int(input())
v = list(map(int, input().split()))
m = int(input())
u = sorted(v)
for i in range(1, n):
	v[i] = v[i] + v[i - 1]
	u[i] = u[i] + u[i - 1]
v.append(0)
u.append(0)
for i in range(m):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(v[r - 1] - v[l - 2])
	else:
		print(u[r - 1] - u[l - 2])


n = int(input())
a = [0] + list(map(int, input().split()))
sa = a[:]
pre1 = []
pre2 = []
s1 = 0
s2 = 0
sa.sort()
for i in range(n + 1):
	s1 += a[i]
	pre1.append(s1)
	s2 += sa[i]
	pre2.append(s2)
for _ in range(int(input())):
	(q, l, r) = map(int, input().split())
	if q == 1:
		print(pre1[r] - pre1[l - 1])
	else:
		print(pre2[r] - pre2[l - 1])


n = int(input())
v = [int(i) for i in input().split()]
v2 = sorted(v)
m = int(input())
pref1 = [0] * n
pref2 = [0] * n
pref1[0] = v[0]
pref2[0] = v2[0]
for i in range(1, n):
	pref1[i] = pref1[i - 1] + v[i]
	pref2[i] = pref2[i - 1] + v2[i]
for i in range(m):
	(t, l, r) = [int(i) for i in input().split()]
	l = l - 1
	r = r - 1
	if t == 1:
		if l == 0:
			print(pref1[r])
		else:
			print(pref1[r] - pref1[l - 1])
	elif l == 0:
		print(pref2[r])
	else:
		print(pref2[r] - pref2[l - 1])


n = int(input())
a = list(map(int, input().split()))
d = []
for i in range(n):
	d.append(a[i])
for i in range(1, n):
	a[i] += a[i - 1]
d.sort()
for i in range(1, n):
	d[i] += d[i - 1]
b = int(input())
for i in range(b):
	(c, s, e) = map(int, input().split())
	if c == 1:
		if s != 1:
			print(a[e - 1] - a[s - 2])
		else:
			print(a[e - 1])
	elif s != 1:
		print(d[e - 1] - d[s - 2])
	else:
		print(d[e - 1])


n = int(input())
l_n = list(map(int, input().split()))
l_n_s = l_n.copy()
l_n_s.sort()
l_s = list()
l_s_s = list()
l_s.append(l_n[0])
l_s_s.append(l_n_s[0])
for i in range(1, n):
	l_s.append(l_s[i - 1] + l_n[i])
	l_s_s.append(l_s_s[i - 1] + l_n_s[i])
for _ in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			print(l_s[r - 1])
		else:
			print(l_s[r - 1] - l_s[l - 2])
	elif l == 1:
		print(l_s_s[r - 1])
	else:
		print(l_s_s[r - 1] - l_s_s[l - 2])


n = int(input())
MAX = n + 2
lista = [0]
lista.extend(list(map(int, input().split())))
listas = [0 for i in range(MAX)]
for i in range(1, n + 1):
	listas[i] = lista[i] + listas[i - 1]
lista.sort()
listao = [0 for i in range(MAX)]
for i in range(1, n + 1):
	listao[i] = lista[i] + listao[i - 1]
resp = []
p = int(input())
for i in range(p):
	(a, b, c) = map(int, input().split())
	if a == 1:
		resp.append(str(listas[c] - listas[b - 1]))
	else:
		resp.append(str(listao[c] - listao[b - 1]))
print('\n'.join(resp))


from collections import defaultdict

def main():
	n = int(input())
	v_list = list(map(int, input().split()))
	sorted_v_list = list(sorted(v_list))
	dp1 = [0] * (n + 1)
	for (i, v) in enumerate(v_list):
		dp1[i + 1] = v + dp1[i]
	dp2 = [0] * (n + 1)
	for (i, v) in enumerate(sorted_v_list):
		dp2[i + 1] = v + dp2[i]
	ans_list = []
	m = int(input())
	for _ in range(m):
		(t, l, r) = map(int, input().split())
		if t == 1:
			ans_list.append(dp1[r] - dp1[l - 1])
		else:
			ans_list.append(dp2[r] - dp2[l - 1])
	print(*ans_list, sep='\n')
main()


n = int(input())
v = [int(i) for i in input().split()]
u = [int(i) for i in v]
u.sort()
m = int(input())
t = []
l = []
r = []
for i in range(m):
	(T, L, R) = map(int, input().split())
	t.append(T)
	l.append(L)
	r.append(R)
a = [v[0]]
b = [u[0]]
for i in range(1, len(v)):
	p = a[i - 1] + v[i]
	a.append(p)
	q = b[i - 1] + u[i]
	b.append(q)
for i in range(m):
	x = l[i] - 1
	y = r[i] - 1
	if t[i] == 1:
		ans = a[y] - a[x] + v[x]
	elif t[i] == 2:
		ans = b[y] - b[x] + u[x]
	print(ans)


import sys
n = int(input())
l = [int(x) for x in input().split()]
q = int(input())
pre_sum = [0]
for i in range(n):
	pre_sum.append(pre_sum[i] + l[i])
l.sort()
l.insert(0, 0)
for i in range(1, n + 1):
	l[i] = l[i - 1] + l[i]
for _ in range(q):
	(tp, s, e) = map(int, input().split())
	if tp == 1:
		print(pre_sum[e] - pre_sum[s - 1])
	else:
		print(l[e] - l[s - 1])


n = int(input())
stones = input().split()
ss = []
for i in stones:
	ss.append(int(i))
ss.sort()
stones[0] = int(stones[0])
for i in range(1, n):
	ss[i] += ss[i - 1]
	stones[i] = int(stones[i]) + int(stones[i - 1])
ques = int(input())
for i in range(0, ques):
	count = 0
	t = input().split()
	if int(t[0]) == 1:
		if int(t[1]) != 1:
			print(stones[int(t[2]) - 1] - stones[int(t[1]) - 2])
		else:
			print(stones[int(t[2]) - 1])
	elif int(t[1]) != 1:
		print(ss[int(t[2]) - 1] - ss[int(t[1]) - 2])
	else:
		print(ss[int(t[2]) - 1])


n = int(input())
a = [int(i) for i in input().split()]
b = []
c = []
x = a
x = sorted(x)
c.append(x[0])
b.append(a[0])
for i in range(1, n):
	b.append(a[i] + b[i - 1])
	c.append(x[i] + c[i - 1])
k = int(input())
for i in range(k):
	(x, l, r) = (int(i) for i in input().split())
	if x == 1:
		if l == 1:
			print(b[r - 1])
		else:
			print(b[r - 1] - b[l - 2])
	elif l == 1:
		print(c[r - 1])
	else:
		print(c[r - 1] - c[l - 2])


n = int(input())
uarray = list(map(int, input().rstrip().split()))
cumulativeuarray = [0]
for i in range(n):
	cumulativeuarray.append(cumulativeuarray[-1] + uarray[i])
varray = sorted(uarray)
cumulativevarray = [0]
for i in range(n):
	cumulativevarray.append(cumulativevarray[-1] + varray[i])
m = int(input())
for i in range(m):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(cumulativeuarray[r] - cumulativeuarray[l - 1])
	else:
		print(cumulativevarray[r] - cumulativevarray[l - 1])


n = int(input())
l = list(map(int, input().split()))
d = [l[0]]
s = l[0]
for i in range(1, n):
	s = s + l[i]
	d.append(s)
l.sort()
s = l[0]
x = [l[0]]
for i in range(1, n):
	s = s + l[i]
	x.append(s)
for u in range(int(input())):
	(q, z, y) = map(int, input().split())
	if q == 1:
		if z != 1:
			print(d[y - 1] - d[z - 2])
		else:
			print(d[y - 1])
	elif z != 1:
		print(x[y - 1] - x[z - 2])
	else:
		print(x[y - 1])


def list_input(data):
	return list(map(data, input().strip().split()))

def single_input(data):
	return map(data, input().strip().split())
arg = int(input())
l1 = list_input(int)
l2 = [a for a in l1]
l4 = [l1[0]]
for i in range(1, arg):
	l4.append(l4[-1] + l1[i])
l1 = l4
l2.sort()
l4 = [l2[0]]
for i in range(1, arg):
	l4.append(l4[-1] + l2[i])
l2 = l4
m = int(input())
for _ in range(0, m):
	(typ, l, r) = single_input(int)
	if typ == 1:
		if l - 2 == -1:
			print(l1[r - 1])
		else:
			print(l1[r - 1] - l1[l - 2])
	elif l - 2 == -1:
		print(l2[r - 1])
	else:
		print(l2[r - 1] - l2[l - 2])


from collections import defaultdict
from sys import stdin, stdout
n = input()
a = list(map(int, input().split()))
pre = [0]
temp = 0
for i in a:
	temp += i
	pre.append(temp)
a.sort()
presort = [0]
temp = 0
for i in a:
	temp += i
	presort.append(temp)
q = int(input())
for _ in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(pre[r] - pre[l - 1])
	else:
		print(presort[r] - presort[l - 1])


n = int(input())
v = list(map(int, input().split()))
sumv = [0] * (n + 1)
for i in range(1, n + 1):
	sumv[i] = v[i - 1] + sumv[i - 1]
u = sorted(v)
sumu = [0] * (n + 1)
for i in range(1, n + 1):
	sumu[i] = u[i - 1] + sumu[i - 1]
for _ in range(int(input())):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(sumv[r] - sumv[l - 1])
	else:
		print(sumu[r] - sumu[l - 1])


n = int(input())
a = [int(i) for i in input().split()]
p1 = [0]
p2 = [0]
s = 0
for i in range(n):
	s += a[i]
	p1.append(s)
a.sort()
s = 0
for i in range(n):
	s += a[i]
	p2.append(s)
m = int(input())
for w in range(m):
	(q, l, r) = map(int, input().split())
	if q == 1:
		print(p1[r] - p1[l - 1])
	else:
		print(p2[r] - p2[l - 1])


n = int(input())
v = [int(i) for i in input().split(' ')]
vt = v[:]
u = v.copy()
for i in range(1, len(v)):
	v[i] = v[i - 1] + v[i]
u.sort()
ut = u[:]
for i in range(1, len(u)):
	u[i] = u[i - 1] + u[i]
q = int(input())
for i in range(0, q):
	(t, l, r) = [int(i) for i in input().split(' ')]
	if t == 1:
		print(v[r - 1] - v[l - 1] + vt[l - 1])
	else:
		print(u[r - 1] - u[l - 1] + ut[l - 1])


n = int(input())
s = [int(i) for i in input().split()]
s1 = sorted(s)
d = {0: 0}
d1 = {0: 0}
for i in range(n):
	d[i + 1] = d[i] + s[i]
	d1[i + 1] = d1[i] + s1[i]
m = int(input())
for i in range(m):
	(t, l, r) = [int(i) for i in input().split()]
	if t == 1:
		print(d[r] - d[l - 1])
	else:
		print(d1[r] - d1[l - 1])


n = int(input())
x = list(map(int, input().split()))
y = sorted(x)
for i in range(1, n):
	x[i] += x[i - 1]
	y[i] += y[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l > 1:
			print(x[r - 1] - x[l - 2])
		else:
			print(x[r - 1])
	elif l > 1:
		print(y[r - 1] - y[l - 2])
	else:
		print(y[r - 1])


n = int(input())
stones_costs = list(map(int, input().split()))
partial_sum = []
s = 0
for i in range(0, n):
	s += stones_costs[i]
	partial_sum.append(s)
sorted_array = sorted(stones_costs)
partial_sum2 = []
s2 = 0
for i in range(0, n):
	s2 += sorted_array[i]
	partial_sum2.append(s2)
m = int(input())
for i in range(m):
	q = list(map(int, input().split()))
	if q[0] == 1:
		if q[1] == 1:
			total_sum = partial_sum[q[2] - 1]
		else:
			total_sum = partial_sum[q[2] - 1] - partial_sum[q[1] - 2]
		print(total_sum)
	else:
		if q[1] == 1:
			total_sum = partial_sum2[q[2] - 1]
		else:
			total_sum = partial_sum2[q[2] - 1] - partial_sum2[q[1] - 2]
		print(total_sum)


n = int(input())
a = list(map(int, input().split()))
sa = sorted(a)
ps = [a[0]]
ss = [sa[0]]
for i in range(1, n):
	ps.append(ps[-1] + a[i])
	ss.append(ss[-1] + sa[i])
for _ in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(ps[r - 1] if l == 1 else ps[r - 1] - ps[l - 2])
	else:
		print(ss[r - 1] if l == 1 else ss[r - 1] - ss[l - 2])


n = int(input())
v = list(map(int, input().split()))
vs = sorted(v)
prefv = [0] * (n + 1)
prefvs = [0] * (n + 1)
for i in range(n):
	prefv[i + 1] = prefv[i] + v[i]
	prefvs[i + 1] = prefvs[i] + vs[i]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(prefv[r] - prefv[l - 1])
	else:
		print(prefvs[r] - prefvs[l - 1])


from sys import stdin
t = int(stdin.readline().rstrip())
l = list(map(int, stdin.readline().rstrip().split(' ')))
for_sort = l.copy()
for_sort.sort()
sum_normal = [0] * (t + 1)
sum_sort = [0] * (t + 1)
for i in range(1, t + 1):
	sum_normal[i] = sum_normal[i - 1] + l[i - 1]
for j in range(1, t + 1):
	sum_sort[j] = sum_sort[j - 1] + for_sort[j - 1]
n = int(input())
for _ in range(n):
	(q, w, e) = list(map(int, stdin.readline().rstrip().split(' ')))
	if q == 1:
		print(sum_normal[e] - sum_normal[w - 1])
	else:
		print(sum_sort[e] - sum_sort[w - 1])


numero_de_pedras = int(input())
pedras = [0]
pedras += list(map(int, input().split(' ')))
pedras_ordenadas = sorted(pedras)
acumulada_pedras = [pedras[0]]
acumuladas_pedras_ordenadas = [pedras_ordenadas[0]]
for i in range(1, numero_de_pedras + 1):
	acumulada_pedras.append(pedras[i] + acumulada_pedras[i - 1])
	acumuladas_pedras_ordenadas.append(pedras_ordenadas[i] + acumuladas_pedras_ordenadas[i - 1])
num_perguntas = int(input())
perguntas = []
for i in range(num_perguntas):
	perguntas.append(list(map(int, input().split(' '))))
for pergunta in perguntas:
	if pergunta[0] == 1:
		print(acumulada_pedras[pergunta[2]] - acumulada_pedras[pergunta[1] - 1])
	else:
		print(acumuladas_pedras_ordenadas[pergunta[2]] - acumuladas_pedras_ordenadas[pergunta[1] - 1])


n = int(input())
lst = list(map(int, input().split()))
m = int(input())
preSum = [0] * (n + 1)
for i in range(1, n + 1):
	preSum[i] = preSum[i - 1] + lst[i - 1]
lst = sorted(lst)
sumSort = [0] * (n + 1)
for i in range(1, n + 1):
	sumSort[i] = sumSort[i - 1] + lst[i - 1]
for i in range(m):
	val = list(map(int, input().split()))
	(l, r) = (min(val[1] - 2, 0), val[2] - 1)
	if val[0] == 1:
		print(preSum[val[2]] - preSum[val[1] - 1])
	else:
		print(sumSort[val[2]] - sumSort[val[1] - 1])


n = int(input())
a = list(map(int, input().split()))
c = [0] * n
for i in range(n):
	c[i] = a[i]
b = a
b.sort()
a = c
dp = [0] * n
dp[0] = a[0]
for i in range(1, n):
	dp[i] = dp[i - 1] + a[i]
lp = [0] * n
lp[0] = b[0]
for i in range(1, n):
	lp[i] = lp[i - 1] + b[i]
m = int(input())
for i in range(m):
	(t, l, r) = list(map(int, input().split()))
	if t == 1:
		first = dp[r - 1]
		second = dp[l - 2]
		if l - 2 < 0:
			second = 0
		print(first - second)
	else:
		first = lp[r - 1]
		second = lp[l - 2]
		if l - 2 < 0:
			second = 0
		print(first - second)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
v = list(map(int, input().split()))
vsum = [0]
vssum = [0]
for item in v:
	vsum.append(vsum[-1] + item)
v.sort()
for item in v:
	vssum.append(vssum[-1] + item)
ans = ''
for _ in range(int(input())):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		ans += str(vsum[r] - vsum[l - 1]) + '\n'
	else:
		ans += str(vssum[r] - vssum[l - 1]) + '\n'
print(ans)


z = int(input())
result = []
x = list(map(int, input().split()))
fi = [0] * (z + 1)
se = [0] * (z + 1)
for i in range(z):
	fi[i + 1] = x[i] + fi[i]
x.sort()
for i in range(z):
	se[i + 1] = x[i] + se[i]
q = int(input())
result = []
for i in range(q):
	a = list(map(int, input().split()))
	if a[0] == 1:
		result.append(fi[a[2]] - fi[a[1] - 1])
	else:
		result.append(se[a[2]] - se[a[1] - 1])
for i in result:
	print(i)


n = int(input().strip())
v = list(map(int, input().strip().split()))
a = sorted(v)
vpresum = [v[0]]
for i in range(1, n):
	vpresum.append(v[i] + vpresum[-1])
apresum = [a[0]]
for i in range(1, n):
	apresum.append(a[i] + apresum[-1])
ans = []
for _ in range(int(input().strip())):
	(t, l, r) = map(int, input().strip().split())
	l -= 1
	r -= 1
	if t == 1:
		ans.append(vpresum[r] - vpresum[l] + v[l])
	else:
		ans.append(apresum[r] - apresum[l] + a[l])
for i in ans:
	print(i)


stones = int(input())
stoneArray = list(map(int, input().split()))
question = int(input())
b = sorted(stoneArray)
for i in range(stones - 1):
	stoneArray[i + 1] += stoneArray[i]
	b[i + 1] += b[i]
(stoneArray, b) = ([0] + stoneArray, [0] + b)
for i in range(question):
	(qt, l, r) = map(int, input().split())
	print(b[r] - b[l - 1] if qt == 2 else stoneArray[r] - stoneArray[l - 1])


n = int(input())
s = tuple(map(int, input().split()))
m = int(input())
s1 = list(s)
s1.sort()
t = [0] * n
d = [0] * n
t[0] = s[0]
d[0] = s1[0]
for i in range(1, n):
	t[i] = t[i - 1] + s[i]
	d[i] = d[i - 1] + s1[i]
for i in range(m):
	(a, b, c) = map(int, input().split())
	if a == 1:
		if b == 1:
			print(t[c - 1])
		else:
			print(t[c - 1] - t[b - 2])
	elif b == 1:
		print(d[c - 1])
	else:
		print(d[c - 1] - d[b - 2])


n = int(input())
r = lambda : list(map(int, input().split()))
stones = r()
stones_sorted = sorted(stones)
q = int(input())
v = [0] * (1 + 10 ** 5)
u = [0] * (1 + 10 ** 5)
i = 1
(proc_nor, proc_sor) = (0, 0)
for (nor, sor) in zip(stones, stones_sorted):
	proc_nor += nor
	proc_sor += sor
	v[i] = proc_nor
	u[i] = proc_sor
	i += 1
for _ in range(q):
	(a, left, right) = r()
	if a == 1:
		print(v[right] - v[left - 1])
	else:
		print(u[right] - u[left - 1])


n = int(input())
stones = list(map(int, input().split()))
t = int(input())
o_stones = sorted(stones)
acc_stones = []
acc_ordered = []
acc = 0
acc_o = 0
for i in range(len(stones)):
	acc = acc + stones[i]
	acc_stones.append(acc)
	acc_o = acc_o + o_stones[i]
	acc_ordered.append(acc_o)
for i in range(t):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(acc_stones[r - 1] - acc_stones[l - 1] + stones[l - 1])
	else:
		print(acc_ordered[r - 1] - acc_ordered[l - 1] + o_stones[l - 1])


n = int(input())
rocks = list(map(int, input().split()))
sortrocks = [0] * n
for i in range(n):
	sortrocks[i] = rocks[i]
sortrocks.sort()
v = [0] * (n + 1)
v[1] = rocks[0]
u = [0] * (n + 1)
u[1] = sortrocks[0]
for i in range(2, n + 1):
	v[i] = v[i - 1] + rocks[i - 1]
	u[i] = u[i - 1] + sortrocks[i - 1]
m = int(input())
for i in range(m):
	(type, l, r) = map(int, input().split())
	l -= 1
	if type == 1:
		print(v[r] - v[l])
	else:
		print(u[r] - u[l])


loop = int(input())
s = [0] + list(map(int, input().split()))
e = sorted(s)
for i in range(1, loop + 1):
	s[i] += s[i - 1]
	e[i] += e[i - 1]
loops = int(input())
for i in range(loops):
	(a, b, c) = map(int, input().split())
	if a == 1:
		print(s[c] - s[b - 1])
	else:
		print(e[c] - e[b - 1])


a = int(input())
b = [0] + list(map(int, input().split()))
c = sorted(b.copy())
for i in range(1, a + 1):
	b[i] += b[i - 1]
	c[i] += c[i - 1]
for _ in ' ' * int(input()):
	(x, y, z) = map(int, input().split())
	if x == 1:
		print(b[z] - b[y - 1])
	else:
		print(c[z] - c[y - 1])


n = int(input())
v = list(map(int, input().split()))
m = int(input())
i = 0
u = list(v)
u.sort()
while i < n - 1:
	v[i + 1] += v[i]
	u[i + 1] += u[i]
	i += 1
(u, v) = ([0] + u, [0] + v)
i = 0
while i < m:
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])
	i += 1


import sys
input = sys.stdin.readline

def prefix_sum(ls):
	l1 = sorted(ls)
	(normal, inc) = ([ls[0]], [l1[0]])
	for i in range(1, len(ls)):
		normal.append(normal[i - 1] + ls[i])
		inc.append(inc[i - 1] + l1[i])
	return [normal, inc]

def io():
	n = int(input())
	ls = list(map(int, input().split()))
	m = int(input())
	queries = []
	for _ in range(m):
		tmp = list(map(int, input().split()))
		queries.append(tmp)
	return [n, ls, m, queries]

def solve():
	(n, ls, m, queries) = io()
	(normal, inc) = prefix_sum(ls)
	for i in range(m):
		types = queries[i][0]
		l = queries[i][1]
		r = queries[i][2] - 1
		if l != 1:
			l -= 2
		else:
			l = -1
		if types == 1 and l >= 0:
			print(normal[r] - normal[l])
		elif types == 2 and l >= 0:
			print(inc[r] - inc[l])
		elif types == 1 and l < 0:
			print(normal[r])
		elif types == 2 and l < 0:
			print(inc[r])
solve()


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = [0] * (n + 1)
d = [0] * (n + 1)
(su1, su2) = (0, 0)
for i in range(n):
	su1 += a[i]
	su2 += b[i]
	c[i + 1] = su1
	d[i + 1] = su2
for i in range(int(input())):
	(x, y, z) = map(int, input().split())
	print(c[z] - c[y - 1] if x == 1 else d[z] - d[y - 1])


n = int(input())
a = list(map(int, input().split()))
pre = [0] * (n + 1)
presort = [0] * (n + 1)
for i in range(n):
	pre[i + 1] = pre[i] + a[i]
a.sort()
for i in range(n):
	presort[i + 1] = presort[i] + a[i]
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(pre[r] - pre[l - 1])
	if t == 2:
		print(presort[r] - presort[l - 1])


n = int(input())
costs = list(map(lambda x: int(x), input().split()))
costsort = []
for c in costs:
	costsort.append(c)
costsort.sort()
for i in range(1, n):
	costs[i] += costs[i - 1]
	costsort[i] += costsort[i - 1]
costs = [0] + costs
costsort = [0] + costsort
m = int(input())
for i in range(m):
	line = input().split()
	if line[0] == '1':
		ans = costs[int(line[2])] - costs[int(line[1]) - 1]
		print(ans)
	else:
		ans = costsort[int(line[2])] - costsort[int(line[1]) - 1]
		print(ans)


stones = input()
stones = int(stones)
stone_list = list()
for i in input().split(' '):
	stone_list.append(int(i))
sum1 = list()
count = 0
sum1.append(count)
for i in stone_list:
	count += i
	sum1.append(count)
ordered_stones = list()
ordered_stones.extend(stone_list)
ordered_stones.sort()
sum2 = list()
count = 0
sum2.append(count)
for i in ordered_stones:
	count += i
	sum2.append(count)
limit = int(input())
for i in range(limit):
	line = list()
	for j in input().split(' '):
		line.append(int(j))
	if line[0] == 2:
		print(sum2[line[2]] - sum2[line[1] - 1])
	if line[0] == 1:
		print(sum1[line[2]] - sum1[line[1] - 1])


n = int(input())
l = [0] + list(map(int, input().split()))
l2 = sorted(l)
for i in range(1, n + 1):
	l[i] = l[i] + l[i - 1]
	l2[i] = l2[i] + l2[i - 1]
for j in range(int(input())):
	(a, b, c) = map(int, input().split())
	if a == 2:
		print(l2[c] - l2[b - 1])
	else:
		print(l[c] - l[b - 1])


n = int(input())
v = [int(x) for x in input().split()]
m = int(input())
u = v[:]
u.sort()
dpu = [0]
dpv = [0]
sumu = 0
sumv = 0
for i in range(n):
	sumu += u[i]
	sumv += v[i]
	dpu.append(sumu)
	dpv.append(sumv)
for i in range(m):
	(type, l, r) = [int(x) for x in input().split()]
	if type == 1:
		print(dpv[r] - dpv[l - 1])
	else:
		print(dpu[r] - dpu[l - 1])


n = int(input())
costs = list(map(int, input().split()))
sorted_costs = sorted(costs)
m = int(input())
sorted_cost_sum_till = [0]
for i in range(1, n + 1):
	sorted_cost_sum_till.append(sorted_cost_sum_till[-1] + sorted_costs[i - 1])
cost_sum_till = [0]
for i in range(1, n + 1):
	cost_sum_till.append(cost_sum_till[-1] + costs[i - 1])

def calculate_cost(l, r):
	sum_to_l = cost_sum_till[l - 1]
	sum_to_r = cost_sum_till[r]
	return sum_to_r - sum_to_l

def calculate_cheapest(l, r):
	sum_to_l = sorted_cost_sum_till[l - 1]
	sum_to_r = sorted_cost_sum_till[r]
	return sum_to_r - sum_to_l
for _ in range(m):
	(type, left, right) = map(int, input().split())
	print(calculate_cost(left, right) if type == 1 else calculate_cheapest(left, right))


t = int(input())
l = list(map(int, input().split()))
l_sorted = sorted(l)
u = [l[0]]
u_sorted = [l_sorted[0]]
for g in range(1, t):
	u += [u[-1] + l[g]]
	u_sorted += [u_sorted[-1] + l_sorted[g]]
m = int(input())
for k in range(m):
	(type, x, y) = list(map(int, input().split()))
	if type == 1:
		if x == 1:
			print(u[y - 1])
		else:
			print(u[y - 1] - u[x - 2])
	elif x == 1:
		print(u_sorted[y - 1])
	else:
		print(u_sorted[y - 1] - u_sorted[x - 2])


n = int(input())
v1 = list(map(int, input().split()))
v2 = sorted(v1)
for i in range(1, n):
	v1[i] += v1[i - 1]
	v2[i] += v2[i - 1]
v = [[0] + v1, [0] + v2]
ans = []
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	ans.append(str(v[t - 1][r] - v[t - 1][l - 1]))
print('\n'.join(ans))


n = int(input())
stones = list(map(int, input().split()))
sorted_stones = list(stones)
sorted_stones.sort()
sums = [0]
sorted_sums = [0]
tot_sum = 0
sort_tot_sum = 0
for i in range(n):
	tot_sum += stones[i]
	sort_tot_sum += sorted_stones[i]
	sums.append(tot_sum)
	sorted_sums.append(sort_tot_sum)
t = int(input())
for _ in range(t):
	(i, l, r) = map(int, input().split())
	ans = 0
	if i == 1:
		print(sums[r] - sums[l - 1])
	else:
		print(sorted_sums[r] - sorted_sums[l - 1])


from sys import stdin
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
v = [0]
for i in a:
	v += [v[-1] + i]
a.sort()
u = [0]
for i in a:
	u += [u[-1] + i]
m = int(stdin.readline())
for _ in range(m):
	(t, l, r) = map(int, stdin.readline().split())
	if t == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
n = int(z())
lp = zzz()
l = [0] + list(ac(lp))
li = [0] + list(ac(sorted(lp)))
for _ in range(int(z())):
	(a, b, c) = zz()
	if a == 1:
		print(l[c] - l[b - 1])
	else:
		print(li[c] - li[b - 1])


n = int(input())
custos = input().split(' ')
ordenado = [int(x) for x in custos]
ordenado.sort()
aux_c = []
cont_c = 0
aux_o = []
cont_o = 0
for i in range(len(custos)):
	cont_c += int(custos[i])
	aux_c.append(cont_c)
	cont_o += ordenado[i]
	aux_o.append(cont_o)
m = int(input())
for i in range(m):
	q = input().split(' ')
	resposta = 0
	a = 0
	b = 0
	if q[0] == '1':
		if q[1] == '1':
			resposta = aux_c[int(q[2]) - 1]
		else:
			a = aux_c[int(q[2]) - 1]
			b = aux_c[int(q[1]) - 2]
			resposta = a - b
	elif q[1] == '1':
		resposta = aux_o[int(q[2]) - 1]
	else:
		a = aux_o[int(q[2]) - 1]
		b = aux_o[int(q[1]) - 2]
		resposta = a - b
	print(resposta)


n = int(input())
ls = list(map(int, input().split()))
ls1 = sorted(ls)
for i in range(1, len(ls)):
	ls[i] = ls[i] + ls[i - 1]
	ls1[i] = ls1[i] + ls1[i - 1]
q = int(input())
while q:
	q -= 1
	(c, l, r) = map(int, input().split())
	if c == 1:
		if l == 1:
			print(ls[r - 1])
		else:
			print(ls[r - 1] - ls[l - 2])
	elif l == 1:
		print(ls1[r - 1])
	else:
		print(ls1[r - 1] - ls1[l - 2])


from sys import stdin
a = int(stdin.readline())
A = stdin.readline().split()
B = list()
C = list()
sum = 0
for k in range(0, a):
	sum += int(A[k])
	B.append(sum)
A.sort(key=int)
sum = 0
for k in range(0, a):
	sum += int(A[k])
	C.append(sum)
b = int(stdin.readline())
for k in range(0, b):
	c = stdin.readline().split()
	E = int(c[1])
	F = int(c[2])
	if int(c[0]) == 1:
		if E == F:
			if E == 1:
				print(B[0])
			else:
				print(B[F - 1] - B[F - 2])
		elif E == 1:
			print(B[F - 1])
		else:
			print(B[F - 1] - B[E - 2])
	elif E == F:
		if E == 1:
			print(C[0])
		else:
			print(C[F - 1] - C[F - 2])
	elif E == 1:
		print(C[F - 1])
	else:
		print(C[F - 1] - C[E - 2])


from copy import deepcopy
n = int(input())
li = list(map(int, input().split(' ')))
li2 = deepcopy(li)
li2.sort()
x = len(li)
for i in range(1, x):
	li[i] += li[i - 1]
	li2[i] += li2[i - 1]
n2 = int(input())
ans = []
for i in range(n2):
	extra = list(map(int, input().split(' ')))
	if extra[0] == 1:
		y = extra[1] - 2
		if y < 0:
			ans.append(li[extra[2] - 1])
		else:
			ans.append(li[extra[2] - 1] - li[y])
	else:
		y = extra[1] - 2
		if y < 0:
			ans.append(li2[extra[2] - 1])
		else:
			ans.append(li2[extra[2] - 1] - li2[y])
for i in range(n2):
	print(ans[i])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = [0]
d = [0]
sm1 = sm2 = 0
for i in range(n):
	sm1 += a[i]
	sm2 += b[i]
	c.append(sm1)
	d.append(sm2)
for i in range(int(input())):
	(x, y, z) = map(int, input().split())
	print(c[z] - c[y - 1] if x == 1 else d[z] - d[y - 1])


count_stones = int(input())
stones_costs = [int(cost) for cost in input().split()]
count_questions = int(input())
costs_sorted = sorted(stones_costs)
cumulative_costs = [0]
cumulative_costs_sorted = [0]
for index in range(len(stones_costs)):
	cumulative_costs.append(cumulative_costs[-1] + stones_costs[index])
	cumulative_costs_sorted.append(cumulative_costs_sorted[-1] + costs_sorted[index])
for _ in range(count_questions):
	question = input().split()
	(typ, left, right) = (question[0], int(question[1]), int(question[2]))
	if typ == '1':
		print(cumulative_costs[right] - cumulative_costs[left - 1])
	else:
		print(cumulative_costs_sorted[right] - cumulative_costs_sorted[left - 1])


n = int(input())
L = input().split()
V = [int(x) for x in L]
U = sorted(V)
qs = [0 for x in L]
qs.append(0)
sort_qs = [0 for x in L]
sort_qs.append(0)
for i in range(1, n + 1):
	qs[i] = qs[i - 1] + V[i - 1]
	sort_qs[i] = sort_qs[i - 1] + U[i - 1]
m = int(input())
for i in range(m):
	L = input().split()
	(type, a, b) = (int(L[0]), int(L[1]), int(L[2]))
	if type == 1:
		print(qs[b] - qs[a - 1])
	else:
		print(sort_qs[b] - sort_qs[a - 1])


a = int(input())
x = list(map(int, input().split()))
y = sorted(x)
q = int(input())
x1 = [0, x[0]]
y1 = [0, y[0]]
for i in range(1, a):
	y1.append(y1[-1] + y[i])
	x1.append(x1[-1] + x[i])
for i in range(q):
	(k, i, j) = map(int, input().split())
	if k == 1:
		print(x1[j] - x1[i - 1])
	else:
		print(y1[j] - y1[i - 1])


from itertools import accumulate

def main():
	n = int(input())
	v = [int(c) for c in input().split()]
	m = int(input())
	queries = []
	for i in range(m):
		queries.append([int(c) for c in input().split()])
	acc_v = [0]
	for e in accumulate(v):
		acc_v.append(e)
	acc_u = [0]
	for e in accumulate(sorted(v)):
		acc_u.append(e)
	res = []
	for (t, l, r) in queries:
		if t == 1:
			res.append(str(acc_v[r] - acc_v[l - 1]))
		else:
			res.append(str(acc_u[r] - acc_u[l - 1]))
	print('\n'.join(res))
main()


l = int(input())
x = list(map(int, input().split()))
x1 = x.copy()
x1.sort()
for i in range(1, len(x)):
	x[i] = x[i] + x[i - 1]
	x1[i] = x1[i] + x1[i - 1]
c = int(input())
for _ in range(c):
	(f, i, j) = map(int, input().split())
	if f == 1:
		if i == 1:
			print(x[j - 1])
		else:
			print(x[j - 1] - x[i - 1 - 1])
	elif i == 1:
		print(x1[j - 1])
	else:
		print(x1[j - 1] - x1[i - 1 - 1])


from functools import reduce
import os
import sys
from collections import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
mo = 1000000007
inf = 1e+18
div = 998244353
n = inn()
a = arr()
sm = 0
res1 = [0]
for i in a:
	sm += i
	res1 += [sm]
a.sort()
res2 = [0]
sm = 0
for i in a:
	sm += i
	res2 += [sm]
for _ in range(inn()):
	(t, l, r) = value()
	l -= 1
	if t == 1:
		print(res1[r] - res1[l])
	else:
		print(res2[r] - res2[l])


n = int(input())
arr = list(map(int, input().split()))
arr = [0] + arr
sort_arr = sorted(arr)
for i in range(1, n + 1):
	arr[i] += arr[i - 1]
	sort_arr[i] += sort_arr[i - 1]
for i in range(int(input())):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(arr[r] - arr[l - 1])
	else:
		print(sort_arr[r] - sort_arr[l - 1])


from copy import deepcopy
n = int(input())
v = list(map(int, input().split()))
m = int(input())
v1 = deepcopy(v)
v1.sort()
pre = [v[0]]
pre1 = [v1[0]]
for i in range(1, n):
	pre.append(pre[i - 1] + v[i])
	pre1.append(pre1[i - 1] + v1[i])
for i in range(m):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		if l == 1:
			print(pre[r - 1])
		elif l == r:
			print(v[r - 1])
		else:
			print(pre[r - 1] - pre[l - 2])
	elif l == r:
		print(v1[r - 1])
	elif l == 1:
		print(pre1[r - 1])
	else:
		print(pre1[r - 1] - pre1[l - 2])


def solve(type, l, r):
	if type == 1:
		return prefix_sum_arr[r] - prefix_sum_arr[l - 1]
	else:
		return prefix_sum_sorted[r] - prefix_sum_sorted[l - 1]
n = int(input())
arr = list(map(int, input().split()))
m = int(input())
prefix_sum_arr = [0]
for i in range(n):
	prefix_sum_arr.append(prefix_sum_arr[-1] + arr[i])
arr.sort()
prefix_sum_sorted = [0]
for i in range(n):
	prefix_sum_sorted.append(prefix_sum_sorted[-1] + arr[i])
for i in range(m):
	(type, l, r) = map(int, input().split())
	print(solve(type, l, r))


def getSomas(array):
	aux = []
	for i in range(len(array)):
		if i == 0:
			aux.append(array[i])
		else:
			aux.append(aux[i - 1] + array[i])
	return aux

def result(array, l, r):
	if l == 1:
		return array[r - 1]
	return array[r - 1] - array[l - 2]
n = int(input())
costs = [int(i) for i in input().split()]
m = int(input())
somas = getSomas(costs)
somasOrdenado = getSomas(sorted(costs))
for i in range(m):
	linha = [int(j) for j in input().split()]
	tipo = linha[0]
	l = linha[1]
	r = linha[2]
	if tipo == 1:
		print(result(somas, l, r))
	else:
		print(result(somasOrdenado, l, r))


n = int(input())
L = [int(i) for i in input().split()]
S = sorted(L)
(sans, lans) = ([], [])
(s1, s2) = (0, 0)
for i in range(n):
	s1 += L[i]
	s2 += S[i]
	sans.append(s2)
	lans.append(s1)
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			print(lans[r - 1])
		else:
			print(lans[r - 1] - lans[l - 2])
	elif t == 2:
		if l == 1:
			print(sans[r - 1])
		else:
			print(sans[r - 1] - sans[l - 2])


import sys

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
Arr1 = get_array()
Arr2 = sorted(Arr1)
prefix1 = [0] * n
prefix1[0] = Arr1[0]
prefix2 = [0] * n
prefix2[0] = Arr2[0]
for i in range(1, n):
	prefix1[i] = prefix1[i - 1] + Arr1[i]
	prefix2[i] = prefix2[i - 1] + Arr2[i]
m = int(input())
while m > 0:
	(typ, l, r) = get_ints()
	if typ == 1:
		if l == 1:
			print(prefix1[r - 1])
		elif l > 1:
			print(prefix1[r - 1] - prefix1[l - 2])
	elif l == 1:
		print(prefix2[r - 1])
	elif l > 1:
		print(prefix2[r - 1] - prefix2[l - 2])
	m -= 1


a = 0
z = input()
x = list(map(int, input().split()))
x2 = sorted(x)
for i in range(len(x)):
	x[i] = a + x[i]
	a = x[i]
a = 0
for i in range(len(x)):
	x2[i] = a + x2[i]
	a = x2[i]
c = int(input())
for i in range(c):
	v = input().split()
	if v[0] == '1':
		if v[1] == '1':
			print(x[int(v[2]) - 1])
		else:
			print(x[int(v[2]) - 1] - x[int(v[1]) - 2])
	elif v[1] == '1':
		print(x2[int(v[2]) - 1])
	else:
		print(x2[int(v[2]) - 1] - x2[int(v[1]) - 2])


def solve(q, l, r, array, sorted_array):
	if q == 1:
		return array[r] - array[l - 1]
	return sorted_array[r] - sorted_array[l - 1]
_ = input()
array = list(map(int, input().split()))
n = int(input())
sorted_array = sorted(array)
for i in range(1, len(array)):
	array[i] += array[i - 1]
	sorted_array[i] += sorted_array[i - 1]
array.insert(0, 0)
sorted_array.insert(0, 0)
for i in range(n):
	(q, l, r) = list(map(int, input().split()))
	print(solve(q, l, r, array, sorted_array))


from sys import stdin, stdout
n = int(stdin.readline())
v = list(map(int, stdin.readline().split()))
m = int(stdin.readline())
pv = [0]
for i in range(n):
	pv.append(pv[-1] + v[i])
u = sorted(v)
pu = [0]
for i in range(n):
	pu.append(pu[-1] + u[i])
for _ in range(m):
	(typ, l, r) = map(int, stdin.readline().split())
	if typ == 1:
		stdout.write(str(pv[r] - pv[l - 1]) + '\n')
	else:
		stdout.write(str(pu[r] - pu[l - 1]) + '\n')


I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
a = l.copy()
b = l.copy()
b.sort()
for i in range(1, n):
	a[i] += a[i - 1]
	b[i] += b[i - 1]
for tc in range(int(input())):
	(t, x, y) = I()
	if t == 2:
		print(b[y - 1] - b[x - 2]) if x != 1 else print(b[y - 1])
	else:
		print(a[y - 1] - a[x - 2]) if x != 1 else print(a[y - 1])


import sys, threading, os.path
import collections, heapq, math, bisect
import string
from platform import python_version
import itertools
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('input.txt'):
		input = open('input.txt', 'r')
	else:
		input = sys.stdin
	n = int(input.readline())
	lis = [0] + list(map(int, input.readline().split()))
	lis2 = sorted(lis)
	for i in range(1, n + 1):
		lis[i] += lis[i - 1]
		lis2[i] += lis2[i - 1]
	m = int(input.readline())
	sol = []
	for i in range(m):
		(t, l, r) = list(map(int, input.readline().split()))
		if t == 1:
			sol.append(lis[r] - lis[l - 1])
		else:
			sol.append(lis2[r] - lis2[l - 1])
	output = '\n'.join(map(str, sol))
	if os.path.exists('output.txt'):
		open('output.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


n = int(input())
L = list(map(int, input().split()))
m = int(input())
N = []
M = sorted(L)
for _ in range(m):
	N.append(list(map(int, input().split())))
dp = [0] * (n + 1)
sort_dp = [0] * (n + 1)
for i in range(1, n + 1):
	dp[i] = dp[i - 1] + L[i - 1]
	sort_dp[i] = sort_dp[i - 1] + M[i - 1]
for i in range(m):
	(t, l, r) = N[i]
	if t == 1:
		ans = dp[r] - dp[l - 1]
	else:
		ans = sort_dp[r] - sort_dp[l - 1]
	print(ans)


n = int(input())
l = [int(x) for x in input().split()]
l2 = []
for i in l:
	l2.append(i)
l2.sort()
temp1 = []
temp2 = []
s = 0
for i in l:
	s += i
	temp1.append(s)
s = 0
for i in l2:
	s += i
	temp2.append(s)
q = int(input())
for _ in range(q):
	(t, a, b) = map(int, input().split())
	if t == 1:
		if a == 1:
			print(temp1[b - 1])
		else:
			print(temp1[b - 1] - temp1[a - 1] + (temp1[a - 1] - temp1[a - 2]))
	elif a == 1:
		print(temp2[b - 1])
	else:
		print(temp2[b - 1] - temp2[a - 1] + (temp2[a - 1] - temp2[a - 2]))


n = int(input())
l = list(map(int, input().split()))
t = sorted(l)
dp = [0 for i in range(n + 1)]
dp = [list(dp) for i in range(3)]
for i in range(n):
	dp[1][i + 1] = dp[1][i] + l[i]
	dp[2][i + 1] = dp[2][i] + t[i]
t = int(input())
for i in range(t):
	(p, l, r) = map(int, input().split())
	print(dp[p][r] - dp[p][l - 1])


def prefixsumer(arr):
	prefixsum = [0 for i in range(len(arr) + 1)]
	prefixsum[0] = arr[0]
	for i in range(1, len(arr)):
		prefixsum[i] = prefixsum[i - 1] + arr[i]
	return prefixsum
n = int(input())
inp = list(map(int, input().split(' ')))
m = int(input())
lis1 = []
for i in range(m):
	lis1.append(list(map(int, input().split(' '))))
c_inp = inp.copy()
c_inp.sort()
arr1 = prefixsumer(inp)
arr2 = prefixsumer(c_inp)
arr1.insert(0, 0)
arr2.insert(0, 0)
for i in range(m):
	if lis1[i][0] == 1:
		print(abs(arr1[lis1[i][1] - 1] - arr1[lis1[i][2]]))
	else:
		print(abs(arr2[lis1[i][1] - 1] - arr2[lis1[i][2]]))


n = int(input())
v = []
s = []
for word in input().split():
	nr = int(word)
	v.append(nr)
	s.append(nr)
s.sort()
psv = [0] * n
pss = [0] * n
for i in range(n):
	if i == 0:
		psv[0] = v[0]
		pss[0] = s[0]
	else:
		psv[i] = psv[i - 1] + v[i]
		pss[i] = pss[i - 1] + s[i]
q = int(input())
for i in range(q):
	z = []
	for word in input().split():
		z.append(int(word))
	z[1] -= 1
	z[2] -= 1
	if z[0] == 1:
		answer = psv[z[2]] - psv[z[1]] + v[z[1]]
	else:
		answer = pss[z[2]] - pss[z[1]] + s[z[1]]
	print(answer)


n = int(input())
l = list(map(int, input().split()))
v = [0]
s = 0
for i in range(n):
	s += l[i]
	v.append(s)
l.sort()
s = 0
u = [0]
for i in range(n):
	s += l[i]
	u.append(s)
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])


import sys
numberOfStones = int(sys.stdin.readline())
stonelistV = list(map(int, sys.stdin.readline().split()))
stonelistU = sorted(stonelistV)
du = [0] * 1
dv = [0] * 1
for stone in stonelistV:
	dv.append(dv[-1] + stone)
for stone in stonelistU:
	du.append(du[-1] + stone)
numberOfQuestions = int(sys.stdin.readline())
for q in range(numberOfQuestions):
	questions = list(map(int, sys.stdin.readline().split()))
	if questions[0] == 1:
		answer = dv[questions[2]] - dv[questions[1] - 1]
	if questions[0] == 2:
		answer = du[questions[2]] - du[questions[1] - 1]
	print(answer)


import itertools
n = int(input())
v = list(map(int, input().split()))
u = v.copy()
u.sort()
vsums = [0] + list(itertools.accumulate(v))
usums = [0] + list(itertools.accumulate(u))
for _ in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(vsums[r] - vsums[l - 1])
	else:
		print(usums[r] - usums[l - 1])


n = int(input())
arr = list(map(int, input().split()))
pref1 = [0 for i in range(n)]
for i in range(n):
	if i == 0:
		pref1[i] = arr[0]
	else:
		pref1[i] = arr[i] + pref1[i - 1]
pref1 = [0] + pref1
arr1 = sorted(arr)
pref2 = [0 for i in range(n)]
for i in range(n):
	if i == 0:
		pref2[i] = arr1[0]
	else:
		pref2[i] = arr1[i] + pref2[i - 1]
pref2 = [0] + pref2
m = int(input())
for _ in range(m):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		print(pref1[r] - pref1[l - 1])
	elif typ == 2:
		print(pref2[r] - pref2[l - 1])


x = int(input())
y = [int(i) for i in input().split()]
z = int(input())
d = 0
a = sorted(y)
e = {0: 0}
c = {0: 0}
for i in range(x):
	e[i + 1] = y[i] + e[i]
	c[i + 1] = a[i] + c[i]
for i in range(z):
	i = input().split()
	if int(i[0]) == 1:
		print(e[int(i[2])] - e[int(i[1]) - 1])
	else:
		print(c[int(i[2])] - c[int(i[1]) - 1])


n = int(input())
arr = input().split()
brr = sorted(arr, key=int)
for i in range(1, n):
	arr[i] = int(arr[i - 1]) + int(arr[i])
for i in range(1, n):
	brr[i] = int(brr[i - 1]) + int(brr[i])
m = int(input())
for i in range(m):
	(ty, l, r) = map(int, input().split())
	if ty == 1:
		if l - 2 >= 0:
			print(int(arr[r - 1]) - int(arr[l - 2]))
		else:
			print(int(arr[r - 1]))
	elif l - 2 >= 0:
		print(int(brr[r - 1]) - int(brr[l - 2]))
	else:
		print(int(brr[r - 1]))


n = int(input())
lis = list(map(int, input().split()))
lis_sec = sorted(lis)
for i in range(1, n):
	lis[i] += lis[i - 1]
	lis_sec[i] += lis_sec[i - 1]
q = int(input())
for i in range(q):
	(q, l, r) = map(int, input().split())
	if q == 1:
		if l == 1:
			print(lis[r - 1])
		else:
			print(lis[r - 1] - lis[l - 2])
	elif l == 1:
		print(lis_sec[r - 1])
	else:
		print(lis_sec[r - 1] - lis_sec[l - 2])


right = []
n = int(input())
right.insert(0, 0)
left = list(map(int, input().split()))
left.insert(0, 0)
for i in range(1, n + 1):
	right.append(left[i])
	left[i] += left[i - 1]
right.sort()
for i in range(1, n + 1):
	right[i] += right[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(left[r] - left[l - 1])
	else:
		print(right[r] - right[l - 1])


n = int(input())
arr = list(map(int, input().split()))
asort = arr.copy()
asort.sort()
dp_s = [0]
dp = [0]
for i in range(1, n + 1):
	val = dp[-1] + arr[i - 1]
	dp.append(val)
	val_s = dp_s[-1] + asort[i - 1]
	dp_s.append(val_s)
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(dp[r] - dp[l - 1])
	else:
		print(dp_s[r] - dp_s[l - 1])


n = int(input())
(dp, dps) = (list(), list())
dp.append(0)
dp += list(map(int, input().split()))
dps += sorted(dp)
for i in range(1, len(dp), 1):
	dp[i] += dp[i - 1]
	dps[i] += dps[i - 1]
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(dp[r] - dp[l - 1])
	else:
		print(dps[r] - dps[l - 1])


n = int(input())
stones1 = list(map(int, input().split()))
stones2 = sorted(stones1)
w = int(input())
prefu = []
prefs = []
totu = 0
tots = 0
for i in range(n):
	totu += stones1[i]
	prefu.append(totu)
	tots += stones2[i]
	prefs.append(tots)
for i in range(w):
	(t, l, r) = map(int, input().split())
	if t == 1:
		l -= 2
		if l < 0:
			print(prefu[r - 1])
		else:
			print(prefu[r - 1] - prefu[l])
	else:
		l -= 2
		if l < 0:
			print(prefs[r - 1])
		else:
			print(prefs[r - 1] - prefs[l])


n = int(input())
l = [int(x) for x in input().split()]
l2 = sorted(l)
m = int(input())
temp = 0
f = [0]
g = [0]
temp2 = 0
for i in range(n):
	temp += l[i]
	temp2 += l2[i]
	f.append(temp)
	g.append(temp2)
for i in range(m):
	(a, b, c) = map(int, input().split())
	if a == 2:
		print(g[c] - g[b - 1])
	else:
		print(f[c] - f[b - 1])


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
n = iinput()
n1 = list(map(int, input().split()))
n2 = sorted(n1)
m = iinput()
a = []
for i in range(1, n):
	n1[i] += n1[i - 1]
for i in range(1, n):
	n2[i] += n2[i - 1]
for _ in range(m):
	(t, l, r) = rinput()
	if t == 1:
		if l == 1:
			print(n1[r - 1])
		else:
			print(n1[r - 1] - n1[l - 2])
	elif l == 1:
		print(n2[r - 1])
	else:
		print(n2[r - 1] - n2[l - 2])


n = int(input())
question1 = list(map(int, input().split()))
question2 = sorted(question1)
for i in range(1, n):
	question1[i] += question1[i - 1]
	question2[i] += question2[i - 1]
question2 = [0] + question2
question1 = [0] + question1
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 2:
		print(question2[r] - question2[l - 1])
	else:
		print(question1[r] - question1[l - 1])


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return len(set(factors))

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
MOD = 10 ** 9 + 7
T = 1
for _ in range(T):
	n = int(stdin.readline())
	v = list(map(int, stdin.readline().split()))
	u = v.copy()
	u.sort()
	us = [u[0]] + [0] * (n - 1)
	vs = [v[0]] + [0] * (n - 1)
	for i in range(1, n):
		us[i] = us[i - 1] + u[i]
		vs[i] = vs[i - 1] + v[i]
	vs = [0] + vs
	us = [0] + us
	m = int(stdin.readline())
	for i in range(m):
		(t, l, r) = list(map(int, stdin.readline().split()))
		if t == 1:
			print(vs[r] - vs[l - 1])
		else:
			print(us[r] - us[l - 1])


from sys import stdin, stdout
from math import gcd, sqrt, factorial, inf
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
n = I()
a = [0] + L()
a = [a, sorted(a)]
for i in range(n):
	a[0][i + 1] += a[0][i]
	a[1][i + 1] += a[1][i]
for i in range(I()):
	(o, l, r) = R()
	print(a[o - 1][r] - a[o - 1][l - 1])


from collections import OrderedDict

def build_segments(arr):
	base_sum = 0
	segments = OrderedDict()
	for r in range(len(arr)):
		base_sum += arr[r]
		segments[0, r] = base_sum
	return segments
n = int(input())
arr = [int(n) for n in input().split()]
qs = int(input())
segments = build_segments(arr)
segments_ord = build_segments(sorted(arr))
types = [segments, segments_ord]
while qs:
	(t, l, r) = [int(n) for n in input().split()]
	a = types[t - 1][0, r - 1]
	b = types[t - 1][0, l - 2] if l - 2 >= 0 else 0
	print(a - b)
	qs -= 1


n = int(input())
l = list(map(int, input().split()))
lSort = sorted(l)

def sm(lst):
	for i in range(1, len(l)):
		lst[i] += lst[i - 1]
sm(l)
sm(lSort)
T = int(input())
for test in range(T):
	(t, l1, r) = map(int, input().split())
	if t == 1:
		print(l[r - 1] - (l1 > 1) * l[l1 - 2])
	else:
		print(lSort[r - 1] - (l1 > 1) * lSort[l1 - 2])


n = int(input())
V = list(map(int, input().split()))
U = sorted(V)
SV = [0] * (n + 1)
SU = [0] * (n + 1)
for i in range(1, n + 1):
	SV[i] = SV[i - 1] + V[i - 1]
	SU[i] = SU[i - 1] + U[i - 1]
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(SV[r] - SV[l - 1])
	else:
		print(SU[r] - SU[l - 1])


n = input()
cost = list(map(int, input().split()))
cost2 = sorted(cost)
for i in range(1, len(cost)):
	cost[i] += cost[i - 1]
	cost2[i] += cost2[i - 1]
for _ in range(int(input())):
	(typee, l, r) = map(int, input().split())
	if l != 1:
		if typee == 1:
			print(cost[r - 1] - cost[abs(l - 2)])
		else:
			print(cost2[r - 1] - cost2[abs(l - 2)])
	elif typee == 1:
		print(cost[r - 1])
	else:
		print(cost2[r - 1])


n = int(input())
valores = [int(x) for x in input().split(' ')]
valoresOrdenados = sorted(valores)
perguntas = int(input())
soma = []
somaOrdenados = []
soma.append(0)
somaOrdenados.append(0)
for i in range(len(valores)):
	soma.append(valores[i] + soma[i])
	somaOrdenados.append(valoresOrdenados[i] + somaOrdenados[i])
for i in range(perguntas):
	pergunta = [int(x) for x in input().split(' ')]
	if pergunta[0] == 1:
		ind2 = pergunta[1] - 1
		total = soma[pergunta[2]] - soma[ind2]
	else:
		ind2 = pergunta[1] - 1
		total = somaOrdenados[pergunta[2]] - somaOrdenados[ind2]
	print(total)


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
for i in range(1, n):
	a[i] = a[i] + a[i - 1]
	b[i] = b[i] + b[i - 1]
a.insert(0, 0)
b.insert(0, 0)
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(abs(a[max(0, l - 1)] - a[r]))
	else:
		print(abs(b[max(0, l - 1)] - b[r]))


n = int(input())
stones = [int(x) for x in input().split(' ')]
sorted_stones = sorted(stones)
(stones_sums, sorted_stones_sums) = ([stones[0]] * n, [sorted_stones[0]] * n)
for i in range(1, n):
	stones_sums[i] = stones_sums[i - 1] + stones[i]
	sorted_stones_sums[i] = sorted_stones_sums[i - 1] + sorted_stones[i]
m = int(input())
for _ in range(m):
	(type, l, r) = [int(x) for x in input().split(' ')]
	if type == 1:
		print(stones_sums[r - 1] - stones_sums[l - 1] + stones[l - 1])
	elif type == 2:
		print(sorted_stones_sums[r - 1] - sorted_stones_sums[l - 1] + sorted_stones[l - 1])


n = int(input())
v = list(map(int, input().split()))
vsum = [0]
vssum = [0]
for item in v:
	vsum.append(vsum[-1] + item)
v.sort()
for item in v:
	vssum.append(vssum[-1] + item)
for _ in range(int(input())):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		print(vsum[r] - vsum[l - 1])
	else:
		print(vssum[r] - vssum[l - 1])


n = int(input())
arr = [int(i) for i in input().split()]
pref1 = [0, arr[0]]
for i in range(1, n):
	pref1.append(pref1[-1] + arr[i])
arr.sort()
pref2 = [0, arr[0]]
for i in range(1, n):
	pref2.append(pref2[-1] + arr[i])
for _ in range(int(input())):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		print(pref1[r] - pref1[l - 1])
	else:
		print(pref2[r] - pref2[l - 1])


n = int(input())
v = list(map(int, input().split()))
s = sorted(v)
vv = [0]
ss = [0]
[vv.append(vv[i] + v[i]) for i in range(n)]
[ss.append(ss[i] + s[i]) for i in range(n)]
m = int(input())
[print(vv[r] - vv[l - 1] if t == 1 else ss[r] - ss[l - 1]) for (t, l, r) in [map(int, input().split()) for i in range(m)]]


from itertools import accumulate
n = int(input())
v = [0] + list(map(int, input().split()))
ls = sorted(v)
v = list(accumulate(v))
ls = list(accumulate(ls))
m = int(input())
for i in range(m):
	s = 0
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v[r] - v[l - 1])
	elif t == 2:
		print(ls[r] - ls[l - 1])


from sys import stdin

def sums(arr):
	arr[0:0] = [0]
	for i in range(1, len(arr)):
		arr[i] += arr[i - 1]
n = int(input())
V_sums = [*map(int, stdin.readline().split())]
SV_sums = sorted(V_sums)
sums(V_sums)
sums(SV_sums)
for _ in [0] * int(input()):
	(t, l, r) = map(int, stdin.readline().split())
	if t % 2:
		print(V_sums[r] - V_sums[l - 1])
	else:
		print(SV_sums[r] - SV_sums[l - 1])


import sys
f = sys.stdin.readline
n = int(f().strip())
arr = [int(v) for v in f().strip().split()]
sarr = sorted(arr)
for i in range(1, n):
	arr[i] += arr[i - 1]
	sarr[i] += sarr[i - 1]
m = int(f().strip())
while m > 0:
	(t, l, r) = map(int, f().strip().split())
	if t == 2:
		print(sarr[r - 1] - (sarr[l - 2] if l - 2 >= 0 else 0))
	else:
		print(arr[r - 1] - (arr[l - 2] if l - 2 >= 0 else 0))
	m -= 1


def gint():
	return int(input())

def gint_arr():
	return list(map(int, input().split()))

def gfloat():
	return float(input())

def gfloat_arr():
	return list(map(float, input().split()))

def pair_int():
	return map(int, input().split())
INF = 1 << 31
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
n = gint()
a = gint_arr()
s = sorted(a)
suma = [a[0]]
sums = [s[0]]
i = 1
while i < n:
	suma.append(suma[-1] + a[i])
	sums.append(sums[-1] + s[i])
	i += 1
m = gint()
while m:
	(t, l, r) = gint_arr()
	l -= 1
	r -= 1
	if t == 1:
		print(suma[r] - (suma[l - 1] if l else 0))
	else:
		print(sums[r] - (sums[l - 1] if l else 0))
	m -= 1


(n, vs) = (int(input()), list(map(int, input().split())))
(sorted_vs, ls) = (sorted(vs), [])
for i in range(n - 1):
	vs[i + 1] += vs[i]
	sorted_vs[i + 1] += sorted_vs[i]
for _ in '0' * int(input()):
	(t, l, r) = map(int, input().split())
	ps = vs if t == 1 else sorted_vs
	ls.append(ps[r - 1] - (ps[l - 2] if l != 1 else 0))
print('\n'.join(map(str, ls)))


n = int(input())
v = list(map(int, input().split()))
u = sorted(v)
(count_v, count_u) = (0, 0)
(cumulative_v, cumulative_u) = ([], [])
for i in range(n):
	count_v += v[i]
	cumulative_v.append(count_v)
	count_u += u[i]
	cumulative_u.append(count_u)
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(cumulative_v[r - 1] - cumulative_v[l - 1] + v[l - 1])
	else:
		print(cumulative_u[r - 1] - cumulative_u[l - 1] + u[l - 1])


from copy import deepcopy
n = int(input())
l1 = list(map(int, input().split(' ')))
l2 = deepcopy(l1)
l2.sort()
for x in range(1, n):
	l1[x] += l1[x - 1]
	l2[x] += l2[x - 1]
ans = []
n2 = int(input())
for x in range(n2):
	extra = list(map(int, input().split(' ')))
	if extra[0] == 1:
		if extra[1] - 2 < 0:
			y = 0
		else:
			y = l1[extra[1] - 2]
		ans.append(l1[extra[2] - 1] - y)
	elif extra[0] == 2:
		if extra[1] - 2 < 0:
			y = 0
		else:
			y = l2[extra[1] - 2]
		ans.append(l2[extra[2] - 1] - y)
for x in ans:
	print(x)


n = int(input())
l = list(map(int, input().split()))
m = int(input())
a = []
s = 0
for i in range(n):
	s += l[i]
	a.append(s)
l.sort()
b = []
s1 = 0
for i in range(n):
	s1 += l[i]
	b.append(s1)
for i in range(m):
	(z, e, r) = map(int, input().split())
	r = r - 1
	if e == 1:
		if z == 1:
			print(a[r])
		else:
			print(b[r])
	else:
		e -= 2
		if z == 1:
			print(a[r] - a[e])
		else:
			print(b[r] - b[e])


n = int(input())
v = list(map(int, input().split()))
m = int(input())
vv = v.copy()
vv.sort()
(p, z) = (0, 0)
(l, y) = (n * [0], n * [0])
for i in range(n):
	p += v[i]
	z += vv[i]
	l[i] = p
	y[i] = z
for _ in range(m):
	(c, a, b) = map(int, input().split())
	if c == 1:
		if a == 1:
			print(l[b - 1])
		else:
			print(l[b - 1] - l[a - 2])
	elif a == 1:
		print(y[b - 1])
	else:
		print(y[b - 1] - y[a - 2])


from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys
n = int(input())
arr = list(map(int, input().split()))
ls = sorted(arr)
for i in range(1, n):
	arr[i] += arr[i - 1]
for i in range(1, n):
	ls[i] += ls[i - 1]
arr = [0] + arr
ls = [0] + ls
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	print(arr[r] - arr[l - 1] if t == 1 else ls[r] - ls[l - 1])


def calc_map(n, v):
	rslt = [0] * (n + 1)
	acc = 0
	for (i, val) in enumerate(v, 1):
		acc += val
		rslt[i] = acc
	return rslt

def main():
	n = int(input())
	v = list(map(int, input().split()))
	m = int(input())
	inputs = [''] * m
	for i in range(m):
		inputs[i] = input()
	map1 = calc_map(n, v)
	v.sort()
	map2 = calc_map(n, v)
	for s in inputs:
		(t, l, r) = map(int, s.split())
		map0 = map1 if t == 1 else map2
		print(map0[r] - map0[l - 1])
main()


def go():
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	s = list(sorted(a))
	for i in range(n - 1):
		a[i + 1] += a[i]
		s[i + 1] += s[i]
	a = [0] + a
	s = [0] + s
	m = int(input())
	o = ''
	for i in range(m):
		data = [int(i) for i in input().split(' ')]
		if data[0] == 1:
			o += '{}\n'.format(a[data[2]] - a[data[1] - 1])
		else:
			o += '{}\n'.format(s[data[2]] - s[data[1] - 1])
	return o
print(go())


from itertools import accumulate
from sys import stdout
R = lambda : map(int, input().split())
n = int(input())
a = list(R())
b = list(accumulate(sorted(a)))
a = list(accumulate(a))
for i in range(int(input())):
	(t, l, r) = R()
	if t == 1:
		print(a[r - 1] - (a[l - 2] if l >= 2 else 0))
	else:
		print(b[r - 1] - (b[l - 2] if l >= 2 else 0))


import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
n = int(input())
v = list(map(int, input().split()))
u = sorted(v)
v_sum = [0]
u_sum = [0]
for i in range(1, n + 1):
	v_sum.append(v_sum[i - 1] + v[i - 1])
	u_sum.append(u_sum[i - 1] + u[i - 1])
q = int(input())
while q > 0:
	q -= 1
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v_sum[r] - v_sum[l - 1])
	else:
		print(u_sum[r] - u_sum[l - 1])


n = int(input())
V = [int(i) for i in input().split()]
m = int(input())
U = sorted(V)
V_sum = [0] * (n + 1)
U_sum = [0] * (n + 1)
V_sum[0] = 0
U_sum[0] = 0
for i in range(1, n + 1):
	V_sum[i] = V_sum[i - 1] + V[i - 1]
	U_sum[i] = U_sum[i - 1] + U[i - 1]
for i in range(m):
	(type, l, r) = [int(i) for i in input().split()]
	if type == 1:
		print(V_sum[r] - V_sum[l - 1])
	else:
		print(U_sum[r] - U_sum[l - 1])


n = int(input())
l = [0] + list(map(int, input().split()))
tmp = sorted(l)
for i in range(1, n + 1):
	l[i] += l[i - 1]
	tmp[i] += tmp[i - 1]
for i in range(int(input())):
	(x, y, z) = map(int, input().split())
	if x == 1:
		print(l[z] - l[y - 1])
	else:
		print(tmp[z] - tmp[y - 1])


n = int(input())
num_list = list(map(int, input().split()))
operations = int(input())
total = 0
n_prefix_list = []
for i in num_list:
	n_prefix_list.append(total + i)
	total += i
total = 0
s_prefix_list = []
s_list = sorted(num_list)
for i in s_list:
	s_prefix_list.append(total + i)
	total += i
for i in range(operations):
	op_list = list(map(int, input().split()))
	left = op_list[1]
	right = op_list[2]
	if op_list[0] == 1:
		print(n_prefix_list[right - 1] - n_prefix_list[left - 1] + num_list[left - 1])
	else:
		print(s_prefix_list[right - 1] - s_prefix_list[left - 1] + s_list[left - 1])


size = int(input())
nums = [int(x) for x in input().split()]
numOfQuestions = int(input())
sortedNums = sorted(nums)
prefixNums = [0]
curSum = 0
for n in nums:
	curSum += n
	prefixNums.append(curSum)
sortedPefixNums = [0]
curSum = 0
for n in sortedNums:
	curSum += n
	sortedPefixNums.append(curSum)
for _ in range(numOfQuestions):
	question = input().split()
	(type_, l, r) = (question[0], int(question[1]) - 1, int(question[2]))
	if type_ == '1':
		print(prefixNums[r] - prefixNums[l])
	else:
		print(sortedPefixNums[r] - sortedPefixNums[l])


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
n = ri()
vv = rl()
m = ri()
prefix_sum = [0] * n
for i in range(n):
	prefix_sum[i] = vv[i]
	if i > 0:
		prefix_sum[i] += prefix_sum[i - 1]
ss = sorted(vv)
pp = [0] * n
for i in range(n):
	pp[i] = ss[i]
	if i > 0:
		pp[i] += pp[i - 1]
for _ in range(m):
	(kind, l, r) = rl()
	l -= 1
	r -= 1
	if kind == 1:
		if l > 0:
			print(prefix_sum[r] - prefix_sum[l - 1])
		else:
			print(prefix_sum[r])
	if kind == 2:
		if l > 0:
			print(pp[r] - pp[l - 1])
		else:
			print(pp[r])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
v = [0]
u = [0]
s1 = 0
s2 = 0
for i in range(n):
	s1 += a[i]
	s2 += b[i]
	v.append(s1)
	u.append(s2)
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])


from itertools import accumulate as am
n = int(input())
l = [*map(int, input().split())]
s = [[0] + [*am(l)]]
l.sort()
s += [[0] + [*am(l)]]
for j in range(int(input())):
	(x, l, r) = map(int, input().split())
	print(s[x - 1][r] - s[x - 1][l - 1])


n = int(input())
a = list(map(int, input().split(' ')))
b = sorted(a)
c = [a[0]]
d = [b[0]]
for i in range(1, n):
	c.append(c[i - 1] + a[i])
for i in range(1, n):
	d.append(d[i - 1] + b[i])
m = int(input())
for i in range(0, m):
	k = 0
	e = list(map(int, input().split(' ')))
	if e[0] == 1:
		if e[1] > 1:
			k = c[e[2] - 1] - c[e[1] - 2]
		else:
			k = c[e[2] - 1]
	elif e[1] > 1:
		k = d[e[2] - 1] - d[e[1] - 2]
	else:
		k = d[e[2] - 1]
	print(k)


from itertools import accumulate
n = int(input())
l = list(map(int, input().split()))
l2 = l
l3 = list(accumulate(l))
l2.sort()
l4 = list(accumulate(l2))
m = int(input())
for i in range(m):
	(t, r1, r2) = map(int, input().split())
	if t == 1:
		if r1 == 1:
			print(l3[r2 - 1])
		else:
			print(l3[r2 - 1] - l3[r1 - 2])
	elif r1 == 1:
		print(l4[r2 - 1])
	else:
		print(l4[r2 - 1] - l4[r1 - 2])


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math

def bin1(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = k ** mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin1(mid + 1, r, k, t, b, val, ans)
		else:
			return bin1(l, mid - 1, k, t, b, val, ans)

def bin2(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = t * k ** mid + b * mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin2(l, mid - 1, k, t, b, val, ans)
		else:
			return bin2(mid + 1, r, k, t, b, val, ans)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	l = []
	for i in range(2, n + 1):
		if prime[i]:
			l.append(i)
	return l

def bin(l, r, ll, val):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if val >= ll[mid][0] and val <= ll[mid][1]:
			return mid
		elif val < ll[mid][0]:
			return bin(l, mid - 1, ll, val)
		else:
			return bin(mid + 1, r, ll, val)

def deci(n):
	s = ''
	while n != 0:
		if n % 2 == 0:
			n = n // 2
			s = '0' + s
		else:
			n = n // 2
			s = '1' + s
	return s

def diff(s1, s2):
	if len(s1) < len(s2):
		v = len(s1)
		while v != len(s2):
			s1 = '0' + s1
			v = v + 1
	else:
		v = len(s2)
		while v != len(s1):
			s2 = '0' + s2
			v = v + 1
	c = 0
	for i in range(len(s1)):
		if s1[i:i + 1] != s2[i:i + 1]:
			c = c + 1
	return c
from sys import stdin, stdout

def fac(a, b):
	v = a
	while a != b:
		v *= a - 1
		a = a - 1
	return v

def bino(l, r, n):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		val1 = math.log(n / mid + 1, 2)
		val2 = int(val1)
		if val1 == val2:
			return val1
		elif val1 < 1.0:
			return bino(l, mid - 1, n)
		else:
			return bino(mid + 1, r, n)

def binary(l, r, ll, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		if ll[mid] <= val:
			ans = mid + 1
			return binary(mid + 1, r, ll, val, ans)
		else:
			return binary(l, mid - 1, ll, val, ans)

def odd(n, ans, s):
	if n % 2 != 0 or n in s:
		return ans
	else:
		s.add(n)
		return odd(n // 2, ans + 1, s)

def fact(n):
	if n == 0 or n == 1:
		return 1
	else:
		return n * fact(n - 1)
n = int(stdin.readline())
l = list(map(int, stdin.readline().split(' ')))
l1 = []
for i in range(n):
	l1.append(l[i])
l1.sort()
ll1 = []
ll2 = []
c1 = 0
for i in range(n):
	c1 += l[i]
	ll1.append(c1)
c2 = 0
for i in range(n):
	c2 += l1[i]
	ll2.append(c2)
q = int(stdin.readline())
for i in range(q):
	(t, lo, r) = map(int, stdin.readline().split(' '))
	if t == 1:
		stdout.write(str(ll1[r - 1] - ll1[lo - 1] + l[lo - 1]) + '\n')
	else:
		stdout.write(str(ll2[r - 1] - ll2[lo - 1] + l1[lo - 1]) + '\n')


n = int(input())
s = list(map(int, input().split()))
q = [0]
for i in s:
	q.append(q[-1] + i)
s.sort()
p = [0]
for i in s:
	p.append(p[-1] + i)
for _ in range(int(input())):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(q[r] - q[l - 1])
	else:
		print(p[r] - p[l - 1])


n = int(input())
stones = [int(i) for i in input().split()]
q = int(input())
questions = []
for i in range(q):
	questions.append([int(j) for j in input().split()])
sorted_stones = sorted(stones)
stones_sum = [0] * (n + 1)
sorted_stones_sum = [0] * (n + 1)
for i in range(1, n + 1):
	stones_sum[i] = stones_sum[i - 1] + stones[i - 1]
	sorted_stones_sum[i] = sorted_stones_sum[i - 1] + sorted_stones[i - 1]
for (i, question) in enumerate(questions):
	if question[0] == 1:
		s = stones_sum
	else:
		s = sorted_stones_sum
	print(s[question[2]] - s[question[1] - 1])


n = int(input())
v = input().split()
u = []
for i in range(n):
	v[i] = int(v[i])
	u.append(v[i])
u.sort()
a = []
b = []
c1 = 0
c2 = 0
for i in range(n):
	a.append(c1)
	b.append(c2)
	c1 += v[i]
	c2 += u[i]
a.append(c1)
b.append(c2)
m = int(input())
while m:
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(a[r] - a[l - 1])
	else:
		print(b[r] - b[l - 1])
	m -= 1


n = int(input())
v = list(map(int, input().split()))
u = sorted(v)
(dv, du) = ([0] * (n + 1), [0] * (n + 1))
ans = list()
for i in range(1, n + 1):
	dv[i] = dv[i - 1] + v[i - 1]
	du[i] = du[i - 1] + u[i - 1]
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	d = dv if t == 1 else du
	ans.append(str(d[r] - d[l - 1]))
print('\n'.join(ans))


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect
n = I()
a = L()
b = a.copy()
b.sort()
suma = []
sumb = []
cura = 0
curb = 0
for i in range(n):
	cura += a[i]
	curb += b[i]
	suma.append(cura)
	sumb.append(curb)
for i in range(I()):
	(x, y, z) = R()
	if x == 1:
		if y > 1:
			print(suma[z - 1] - suma[y - 2])
		else:
			print(suma[z - 1])
	if x == 2:
		if y > 1:
			print(sumb[z - 1] - sumb[y - 2])
		else:
			print(sumb[z - 1])


import sys

def minp():
	return sys.stdin.readline().strip()
n = int(minp())
a = list(map(int, minp().split()))
b = sorted(a)
c = [0]
d = [0]
for i in a:
	c.append(c[-1] + i)
for i in b:
	d.append(d[-1] + i)
m = int(minp())
for i in range(m):
	(t, l, r) = map(int, minp().split())
	if t == 1:
		print(c[r] - c[l - 1])
	else:
		print(d[r] - d[l - 1])


n = int(input())
stones = list(map(int, input().split()))
m = int(input())
queries = list()
orderedStones = list()
for i in stones:
	orderedStones.append(i)
orderedStones.sort()
prefixSum = [0] * n
prefixSum[0] = stones[0]
for i in range(1, n):
	prefixSum[i] = prefixSum[i - 1] + stones[i]

def ordinary(s, e):
	if s == 1:
		return prefixSum[e - 1]
	return prefixSum[e - 1] - prefixSum[s - 2]
prefixSumOrder = [0] * n
prefixSumOrder[0] = orderedStones[0]
for i in range(1, n):
	prefixSumOrder[i] = prefixSumOrder[i - 1] + orderedStones[i]

def ordered(s, e):
	if s == 1:
		return prefixSumOrder[e - 1]
	return prefixSumOrder[e - 1] - prefixSumOrder[s - 2]
for i in range(m):
	queries.append(list(map(int, input().split())))
for i in queries:
	if i[0] == 1:
		print(ordinary(i[1], i[2]))
	else:
		print(ordered(i[1], i[2]))


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
a.insert(0, 0)
b.insert(0, 0)
m = int(input())
for i in range(1, n + 1):
	a[i] = a[i - 1] + a[i]
	b[i] = b[i - 1] + b[i]
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(a[r] - a[l - 1])
	elif t == 2:
		print(b[r] - b[l - 1])


n = int(input())
arr = list(map(int, input().split()))
q = int(input())
a = sorted(arr)
s = arr
for i in range(1, n):
	s[i] = s[i] + s[i - 1]
for i in range(1, n):
	a[i] = a[i] + a[i - 1]
a.insert(0, 0)
s.insert(0, 0)
for i in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(s[r] - s[l - 1])
	else:
		print(a[r] - a[l - 1])


n = int(input())
a = [0]
a.extend(list(map(int, input().split())))
s = sorted(a)
for i in range(1, n + 1):
	a[i] += a[i - 1]
	s[i] += s[i - 1]
for i in range(int(input())):
	(x, l, r) = map(int, input().split())
	if x == 1:
		print(a[r] - a[l - 1])
	else:
		print(s[r] - s[l - 1])


from __future__ import division, print_function
mod1 = int(1000000000.0 + 7)
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
from collections import Counter
import collections
import math
import heapq
import re

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def most_frequent(list):
	return max(set(list), key=list.count)

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def Convert(string):
	li = list(string.split(''))
	return li

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
prime = []
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j

def main():
	n = inin()
	v = ain()
	m = inin()
	v2 = sorted(v)
	s1 = [0] * n
	s2 = [0] * n
	temp1 = 0
	temp2 = 0
	for i in range(n):
		s1[i] = temp1 + v[i]
		s2[i] = temp2 + v2[i]
		temp1 = s1[i]
		temp2 = s2[i]
	for i in range(m):
		(t, l, r) = map(int, input().split())
		if t == 1:
			if l == 1:
				print(s1[r - 1])
			else:
				print(s1[r - 1] - s1[l - 2])
		elif l == 1:
			print(s2[r - 1])
		else:
			print(s2[r - 1] - s2[l - 2])
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin as cin
n = int(cin.readline())
a = list(list(map(int, cin.readline().split())))
b = sorted(a)
for i in range(1, n):
	a[i] += a[i - 1]
	b[i] += b[i - 1]
for i in range(int(cin.readline())):
	(q, x, y) = list(map(int, cin.readline().split()))
	if q == 1:
		if x == 1:
			print(a[y - 1] - a[x - 1] + a[0])
		else:
			print(a[y - 1] - a[x - 2])
	elif x == 1:
		print(b[y - 1] - b[x - 1] + b[0])
	else:
		print(b[y - 1] - b[x - 2])


n = int(input())
prices = [int(i) for i in input().split()]
prices_sum = []
prev = 0
for pr in prices:
	prices_sum.append(pr + prev)
	prev += pr
prices_sorted_sum = []
prev_sorted = 0
prices.sort()
for pr_s in prices:
	prices_sorted_sum.append(pr_s + prev_sorted)
	prev_sorted += pr_s

def get_sum(arr, l, r):
	if l < 0:
		return arr[r]
	else:
		return arr[r] - arr[l]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	l -= 2
	r -= 1
	if t == 1:
		print(get_sum(prices_sum, l, r))
	if t == 2:
		print(get_sum(prices_sorted_sum, l, r))


n = int(input())
x = list(map(int, input().split()))
y = sorted(x)
lst = list()
lst1 = list()
tot = 0
tot1 = 0
for i in range(n):
	tot = tot + x[i]
	lst.append(tot)
for i in range(n):
	tot1 = tot1 + y[i]
	lst1.append(tot1)
m = int(input())
for i in range(m):
	(t, l, r) = [int(num) for num in input().split()]
	if t == 1:
		if l == r:
			print(x[r - 1])
		elif l - 2 < 0:
			print(lst[r - 1])
		else:
			print(lst[r - 1] - lst[l - 2])
	elif l == r:
		print(y[r - 1])
	elif l - 2 < 0:
		print(lst1[r - 1])
	else:
		print(lst1[r - 1] - lst1[l - 2])


import sys
import math
from collections import *

def get_ints():
	return map(int, input().split())

def get_list():
	return list(get_ints())

def printspx(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
UGLYMOD = int(1000000000.0 + 7)
SEXYMOD = 998244353
MAXN = int(100000.0)
n = int(input())
li = get_list()
prefix = [0]
c = 0
for i in li:
	c += i
	prefix.append(c)
li.sort()
minprefix = [0]
c = 0
for i in li:
	c += i
	minprefix.append(c)
for _testcases_ in range(int(input())):
	(t, l, r) = get_ints()
	if t == 1:
		print(prefix[r] - prefix[l - 1])
	else:
		print(minprefix[r] - minprefix[l - 1])


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def abs(x):
	return x if x >= 0 else -x

def sumelem(x, start, end):
	sum = 0
	for i in range(start, end + 1):
		sum += x[i]
	return sum
for t in range(1):
	n = int(input())
	vi = list(map(int, input().split()))
	pref1 = [0]
	for i in range(n):
		pref1.append(pref1[-1] + vi[i])
	temp = vi
	temp.sort()
	pref2 = [0]
	for i in range(n):
		pref2.append(pref2[-1] + temp[i])
	for j in range(int(input())):
		(typ, l, r) = list(map(int, input().split()))
		if typ == 1:
			print(pref1[r] - pref1[l - 1])
		else:
			print(pref2[r] - pref2[l - 1])


n = int(input())
arr = list(map(int, input().split()))
asort = sorted(arr)
dp_s = [0] * (n + 1)
dp = [0] * (n + 1)
for i in range(1, n + 1):
	dp[i] = dp[i - 1] + arr[i - 1]
	dp_s[i] = dp_s[i - 1] + asort[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(dp[r] - dp[l - 1])
	else:
		print(dp_s[r] - dp_s[l - 1])


def sumLisNum(lis):
	sumLis = [0] * len(lis)
	sumLis[0] = lis[0]
	for i in range(1, len(lis)):
		sumLis[i] = sumLis[i - 1] + lis[i]
	return sumLis
n = int(input())
v = list(map(int, input().split()))
u = v[:]
u.sort()
sumV = sumLisNum(v)
sumU = sumLisNum(u)
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(sumV[r - 1] - sumV[l - 1] + v[l - 1])
	else:
		print(sumU[r - 1] - sumU[l - 1] + u[l - 1])


n = int(input())
v = list(map(int, input().split()))
u = list(sorted(v))
sn = 0
prefv = list()
prefv.append(0)
for i in range(n):
	sn += v[i]
	prefv.append(sn)
sn = 0
prefu = list()
prefu.append(0)
for i in range(n):
	sn += u[i]
	prefu.append(sn)
m = int(input())
for _ in range(m):
	(q, l, r) = map(int, input().split())
	if q == 1:
		print(prefv[r] - prefv[l - 1])
	else:
		print(prefu[r] - prefu[l - 1])


def getSum(arr, l, r):
	if l == 0:
		return arr[r]
	return arr[r] - arr[l - 1]
n = int(input())
v = list(map(int, input().split()))
m = int(input())
u = sorted(v)
prefix_v = []
prefix_v.append(v[0])
for i in range(1, n):
	prefix_v.append(prefix_v[i - 1] + v[i])
prefix_u = []
prefix_u.append(u[0])
for i in range(1, n):
	prefix_u.append(prefix_u[i - 1] + u[i])
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(getSum(prefix_v, l - 1, r - 1))
	else:
		print(getSum(prefix_u, l - 1, r - 1))


n = int(input())
l = list(map(int, input().strip().split()))[:n]
s = l.copy()
s.sort()
v = [0]
u = [0]
sv = 0
su = 0
for i in range(n):
	sv += l[i]
	v.append(sv)
	su += s[i]
	u.append(su)
m = int(input())
for i in range(m):
	(z, x, y) = map(int, input().split())
	if z == 1:
		print(v[y] - v[x - 1])
	else:
		print(u[y] - u[x - 1])


n = int(input())
a = list(map(int, input().split()))
k = sorted(a)
m = int(input())
z = [0]
l = 0
for i in range(len(a)):
	l += a[i]
	z.append(l)
l = 0
p = [0]
for i in range(len(k)):
	l += k[i]
	p.append(l)
for i in range(m):
	(q, l, r) = map(int, input().split())
	if q == 2:
		print(p[r] - p[l - 1])
	if q == 1:
		print(z[r] - z[l - 1])


n = int(input())
a = list(map(int, input().split()))
h = sorted(a)
for i in range(1, n):
	a[i] += a[i - 1]
	h[i] += h[i - 1]
for i in range(int(input())):
	(q, l, r) = map(int, input().split())
	if q == 1:
		if l - 2 >= 0:
			print(a[r - 1] - a[l - 2])
		else:
			print(a[r - 1])
	elif l - 2 >= 0:
		print(h[r - 1] - h[l - 2 if l - 2 >= 0 else 0])
	else:
		print(h[r - 1])


def main():
	from sys import stdin, stdout, stderr
	n = int(stdin.readline())
	v = [int(i) for i in stdin.readline().split()]
	q = int(stdin.readline())
	presum = [0]
	presums = [0]
	for (i, pair) in enumerate(zip(v, sorted(v))):
		presum.append(presum[i] + pair[0])
		presums.append(presums[i] + pair[1])
	for line in stdin:
		(t, l, r) = map(int, line.split())
		if t == 1:
			stdout.write('%d\n' % (presum[r] - presum[l - 1]))
		else:
			stdout.write('%d\n' % (presums[r] - presums[l - 1]))
main()


n = int(input())
us = list(map(int, input().split(' ')))
os = us[:]
os.sort()
for i in range(1, len(os)):
	os[i] += os[i - 1]
	us[i] += us[i - 1]
os = [0] + os
us = [0] + us
res = []
q = int(input())
for i in range(q):
	(t, l, r) = list(map(int, input().split(' ')))
	if t == 1:
		res.append(str(us[r] - us[l - 1]))
	else:
		res.append(str(os[r] - os[l - 1]))
print('\n'.join(res))


n = int(input())
array = list(map(int, input().split()))
prefixArray_Ordinary = []
prefix = 0
for i in array:
	prefix += i
	prefixArray_Ordinary.append(prefix)
array.sort()
prefixArray_Sorted = []
prefix = 0
for i in array:
	prefix += i
	prefixArray_Sorted.append(prefix)
for query in range(int(input())):
	(what, l, r) = list(map(int, input().split()))
	l -= 1
	r -= 1
	if what == 1:
		if l == 0:
			print(prefixArray_Ordinary[r])
		else:
			print(prefixArray_Ordinary[r] - prefixArray_Ordinary[max(0, l - 1)])
	elif l == 0:
		print(prefixArray_Sorted[r])
	else:
		print(prefixArray_Sorted[r] - prefixArray_Sorted[max(0, l - 1)])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
prefa = [0] * (n + 1)
prefb = [0] * (n + 1)
for i in range(1, n + 1):
	prefa[i] = prefa[i - 1] + a[i - 1]
for i in range(1, n + 1):
	prefb[i] = prefb[i - 1] + b[i - 1]
m = int(input())
for i in range(m):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(prefa[r] - prefa[l - 1])
	else:
		print(prefb[r] - prefb[l - 1])


n = int(input())
l = list(map(int, input().split()))
x = [0] * n
y = [0] * n
x[0] = l[0]
for i in range(1, n):
	x[i] = l[i] + x[i - 1]
l.sort()
y[0] = l[0]
for i in range(1, n):
	y[i] = l[i] + y[i - 1]
q = int(input())
for _ in range(q):
	(p, l, r) = map(int, input().split())
	if p == 1:
		if l == 1:
			print(x[r - 1])
		else:
			print(x[r - 1] - x[l - 2])
	elif l == 1:
		print(y[r - 1])
	else:
		print(y[r - 1] - y[l - 2])


n = int(input())
lis = input().split()
for i in range(n):
	lis[i] = int(lis[i])
lis2 = sorted(lis)
m = int(input())
lis1 = []
for i in range(m):
	lis1.append(input().split())
for i in range(m):
	for j in range(3):
		lis1[i][j] = int(lis1[i][j])
s = [lis[0]]
for i in range(1, n):
	s.append(lis[i] + s[i - 1])
s = [0] + s
s2 = [lis2[0]]
for i in range(1, n):
	s2.append(lis2[i] + s2[i - 1])
s2 = [0] + s2
for i in range(m):
	if lis1[i][0] == 1:
		print(s[lis1[i][2]] - s[lis1[i][1] - 1])
	else:
		print(s2[lis1[i][2]] - s2[lis1[i][1] - 1])


n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = sorted(a)
car = []
dim = []
(beep, kik) = (0, 0)
for i in range(n):
	car.append(beep)
	dim.append(kik)
	beep += a[i]
	kik += b[i]
car.append(beep)
dim.append(kik)
for i in range(m):
	(x, y, z) = list(map(int, input().split()))
	if x == 1:
		print(car[z] - car[y - 1])
	else:
		print(dim[z] - dim[y - 1])


n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
b = sorted(a)
for i in range(2, n + 1):
	a[i] += a[i - 1]
	b[i] += b[i - 1]
m = int(input())
dict = {}
while m > 0:
	answer = 0
	(c, a1, b1) = map(int, input().split())
	if c == 1:
		answer += a[b1] - a[a1 - 1]
	else:
		answer += b[b1] - b[a1 - 1]
	print(answer)
	m -= 1


n = int(input())
v = list(map(int, input().split()))
v_sorted = sorted(v)
for i in range(1, n):
	v[i] += v[i - 1]
	v_sorted[i] += v_sorted[i - 1]
m = int(input())
for i in range(m):
	(typee, l, r) = map(int, input().split())
	if typee == 1:
		if l == 1:
			print(v[r - 1])
		else:
			print(v[r - 1] - v[abs(l - 2)])
	elif l == 1:
		print(v_sorted[r - 1])
	else:
		print(v_sorted[r - 1] - v_sorted[abs(l - 2)])


n = int(input())
v = [int(x) for x in input().split()]
u = v[:]
u.sort()
us = 0
vs = 0
vsum = [0] * n
usum = [0] * n
for i in range(n):
	vs += v[i]
	vsum[i] = vs
	us += u[i]
	usum[i] = us
m = int(input())
for _ in range(m):
	(t, l, r) = [int(x) for x in input().split()]
	if t == 1:
		print(vsum[r - 1] if l == 1 else vsum[r - 1] - vsum[l - 2])
	else:
		print(usum[r - 1] if l == 1 else usum[r - 1] - usum[l - 2])


n = int(input())
l = []
l = list(map(int, input().split()))
s = int(input())
d = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	d[i] = d[i - 1] + l[i - 1]
mm = list(l)
mm.sort()
dd = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	dd[i] = dd[i - 1] + mm[i - 1]
while s:
	(t, ll, rr) = map(int, input().split())
	if t == 1:
		print(d[rr] - d[ll - 1])
	else:
		print(dd[rr] - dd[ll - 1])
	s -= 1


from itertools import accumulate
lens = int(input())
arrs = [int(x) for x in input().split()]
arrss = sorted(arrs)
arrs = [0] + list(accumulate(arrs))
arrss = [0] + list(accumulate(arrss))
for _ in range(int(input())):
	(o, l, r) = [int(x) for x in input().split()]
	if o == 1:
		print(arrs[r] - arrs[l - 1])
	else:
		print(arrss[r] - arrss[l - 1])


y = int(input())
a = list(map(int, input().split()))
t1 = int(input())
a1 = [0]
for i in range(y):
	a1.append(a1[-1] + a[i])
a.sort()
a2 = [0]
for i in range(y):
	a2.append(a2[-1] + a[i])
for i in range(t1):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(a1[r] - a1[l - 1])
	else:
		print(a2[r] - a2[l - 1])


n = int(input())
a = list(map(int, input().split(' ')))
b = sorted(a)
for x in range(n - 1):
	a[x + 1] += a[x]
	b[x + 1] += b[x]
b = [0] + b
a = [0] + a
m = int(input())
for x in range(0, m):
	(t, l, r) = map(int, input().split(' '))
	if t == 1:
		print(a[r] - a[l - 1])
	else:
		print(b[r] - b[l - 1])


def main():
	n = int(input())
	arr = [int(e) for e in input().split()]
	sorted_arr = list(arr)
	sorted_arr.sort()
	arr.insert(0, 0)
	sorted_arr.insert(0, 0)
	for i in range(1, n + 1):
		arr[i] = arr[i] + arr[i - 1]
		sorted_arr[i] = sorted_arr[i] + sorted_arr[i - 1]
	q = int(input())
	for i in range(0, q):
		qr = [int(e) for e in input().split()]
		if qr[0] == 1:
			print(arr[qr[2]] - arr[qr[1] - 1])
		else:
			print(sorted_arr[qr[2]] - sorted_arr[qr[1] - 1])
	return 0
main()


n = int(input())
mass_sort = [0] + list(map(int, input().split()))
mass = mass_sort[:]
mass_sort.sort()
for i in range(1, n + 1):
	mass[i] += mass[i - 1]
	mass_sort[i] += mass_sort[i - 1]
m = int(input())
itog = []
for i in range(m):
	(type, l, r) = list(map(int, input().split()))
	if type == 1:
		itog.append(mass[r] - mass[l - 1])
	else:
		itog.append(mass_sort[r] - mass_sort[l - 1])
for i in itog:
	print(i)


n = int(input())
originalList = list(map(int, input().split()))
sortedList = sorted(originalList)
for i in range(1, n):
	originalList[i] += originalList[i - 1]
	sortedList[i] += sortedList[i - 1]
questions = int(input())
while questions != 0:
	question = list(map(int, input().split()))
	(num, l, r) = (question[0], question[1], question[2])
	result = 0
	if num == 1:
		if l == 1:
			result = originalList[r - 1]
		else:
			result = originalList[r - 1] - originalList[l - 2]
	elif l == 1:
		result = sortedList[r - 1]
	else:
		result = sortedList[r - 1] - sortedList[l - 2]
	print(result)
	questions -= 1


def helper():
	n = int(input())
	l = list(map(int, input().split()))
	pref_1 = []
	temp = 0
	for i in l:
		temp += i
		pref_1.append(temp)
	l.sort()
	pref_2 = []
	temp = 0
	for i in l:
		temp += i
		pref_2.append(temp)
	m = int(input())
	for i in range(m):
		(type, l, r) = map(int, input().split())
		if type == 1:
			answer = pref_1[r - 1]
			if l > 1:
				answer -= pref_1[l - 2]
		else:
			answer = pref_2[r - 1]
			if l > 1:
				answer -= pref_2[l - 2]
		print(answer)
helper()


n = int(input())
v = [int(x) for x in input().split()]
u = v[:]
u.sort()
m = int(input())
for i in range(1, n):
	v[i] += v[i - 1]
	u[i] += u[i - 1]
while m > 0:
	m -= 1
	(t, l, r) = [int(x) for x in input().split()]
	if t == 1:
		print(v[r - 1] - (v[l - 2] if l - 2 > -1 else 0))
	else:
		print(u[r - 1] - (u[l - 2] if l - 2 > -1 else 0))


n = int(input())
arr = list(map(int, input().split()))
sumArr = [arr[0]]
for i in range(1, n):
	sumArr += [sumArr[i - 1] + arr[i]]
arr.sort()
sortSum = [arr[0]]
for i in range(1, n):
	sortSum += [sortSum[i - 1] + arr[i]]
q = int(input())
for i in range(q):
	(c, l, r) = list(map(int, input().split()))
	if c == 1:
		leftSum = 0
		if l - 2 >= 0:
			leftSum = sumArr[l - 2]
		print(sumArr[r - 1] - leftSum)
	else:
		leftSum = 0
		if l - 2 >= 0:
			leftSum = sortSum[l - 2]
		print(sortSum[r - 1] - leftSum)


def CF368B():
	n = int(input())
	arr = tuple(map(int, input().split()))
	sorted_arr = sorted(arr, reverse=False)
	carr = []
	csorted_arr = []
	total = 0
	stotal = 0
	for i in range(n):
		total += arr[i]
		stotal += sorted_arr[i]
		carr.append(total)
		csorted_arr.append(stotal)
	m = int(input())
	for i in range(m):
		(q, l, r) = map(int, input().split())
		if q == 1:
			if l == 1:
				print(carr[r - 1])
				continue
			print(carr[r - 1] - carr[l - 2])
		if q == 2:
			if l == 1:
				print(csorted_arr[r - 1])
				continue
			print(csorted_arr[r - 1] - csorted_arr[l - 2])
CF368B()


def main():
	input()
	vv = list(map(int, input().split()))
	uu = sorted(vv)
	for tmp in (vv, uu):
		a = 0
		for (i, b) in enumerate(tmp):
			tmp[i] = a
			a += b
		tmp.append(a)
	res = []
	vu = (None, vv, uu)
	for _ in range(int(input())):
		(type, l, r) = map(int, input().split())
		tmp = vu[type]
		res.append(str(tmp[r] - tmp[l - 1]))
	print('\n'.join(res))
main()


import sys
input = sys.stdin.readline
read_tuple = lambda _type: map(_type, input().split(' '))

def solve():
	n = int(input())
	v = list(read_tuple(int))
	u = list(sorted(v))
	m = int(input())
	(prefix_sum_v, prefix_sum_u) = ([0], [0])
	for i in range(n):
		prefix_sum_v.append(prefix_sum_v[-1] + v[i])
		prefix_sum_u.append(prefix_sum_u[-1] + u[i])
	for _ in range(m):
		(_type, l, r) = read_tuple(int)
		if _type == 1:
			print(prefix_sum_v[r] - prefix_sum_v[l - 1])
		elif _type == 2:
			print(prefix_sum_u[r] - prefix_sum_u[l - 1])
solve()


stones = int(input())
cost = [0] + list(map(int, input().split()))
arranged_cost = sorted(cost)
prefix_cost = []
prefix_arranged_cost = []
i = 0
for x in cost:
	prefix_cost.append(i + x)
	i += x
i = 0
for x in arranged_cost:
	prefix_arranged_cost.append(x + i)
	i += x
for x in range(int(input())):
	(t, start, end) = map(int, input().split())
	if t == 1:
		print(prefix_cost[end] - prefix_cost[start - 1])
	else:
		print(prefix_arranged_cost[end] - prefix_arranged_cost[start - 1])


n = int(input())
v_unsorted = [int(x) for x in input().split()]
v_sorted = sorted(v_unsorted)
for i in range(1, len(v_unsorted)):
	v_unsorted[i] += v_unsorted[i - 1]
for i in range(1, len(v_sorted)):
	v_sorted[i] += v_sorted[i - 1]
v_sorted.insert(0, 0)
v_unsorted.insert(0, 0)
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v_unsorted[r] - v_unsorted[l - 1])
	else:
		print(v_sorted[r] - v_sorted[l - 1])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = [0]
d = [0]
(s1, s2) = (0, 0)
for i in range(n):
	s1 = s1 + a[i]
	s2 = s2 + b[i]
	c.append(s1)
	d.append(s2)
for _ in range(int(input())):
	(type, l, r) = map(int, input().split())
	s = 0
	if type == 1:
		print(c[r] - c[l - 1])
	else:
		print(d[r] - d[l - 1])


R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
(n, V, m) = (R2(), R1(), R2())
V_sorted = list(sorted(V))
(res, res_s) = ([0], [0])
for i in range(n):
	res.append(res[i] + V[i])
for i in range(n):
	res_s.append(res_s[i] + V_sorted[i])
for i in range(m):
	(t, l, r) = R1()
	if t == 1:
		print(res[r] - res[l - 1])
	else:
		print(res_s[r] - res_s[l - 1])


import sys
n = int(sys.stdin.readline())
l = list(map(int, sys.stdin.readline().split()))
ans1 = [0] * (n + 1)
for i in range(1, n + 1):
	ans1[i] = ans1[i - 1] + l[i - 1]
l.sort()
ans2 = [0] * (n + 1)
for i in range(1, n + 1):
	ans2[i] = ans2[i - 1] + l[i - 1]
for i in range(int(sys.stdin.readline())):
	(q, l, r) = map(int, sys.stdin.readline().split())
	if q == 1:
		print(ans1[r] - ans1[l - 1])
	else:
		print(ans2[r] - ans2[l - 1])


def cum(a):
	(s, summ) = ({}, 0)
	for (idx, item) in enumerate(a):
		summ += item
		s[idx] = summ
	return s

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	m = int(input())
	(s, sorted_s) = (cum(a), cum(sorted(a)))
	for _ in range(m):
		(t, l, r) = map(int, input().split())
		if t == 1:
			stype = s
		else:
			stype = sorted_s
		if l == 1:
			print(stype[r - 1])
		else:
			print(stype[r - 1] - stype[l - 2])
solve()


n = int(input())
v = list(map(int, input().split()))
m = int(input())
(nOrdenado, ordenado, vSorted) = ([0] * (n + 1), [0] * (n + 1), sorted(v))
for i in range(1, n + 1):
	nOrdenado[i] = nOrdenado[i - 1] + v[i - 1]
	ordenado[i] = ordenado[i - 1] + vSorted[i - 1]
for i in range(m):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(nOrdenado[r] - nOrdenado[l - 1])
	else:
		print(ordenado[r] - ordenado[l - 1])


import copy
n = int(input())
a = list(map(int, input().split()))
asum = [0]
for x in a:
	asum.append(asum[-1] + x)
a.sort()
lsum = [0]
for x in a:
	lsum.append(lsum[-1] + x)
m = int(input())
for _ in range(m):
	(q, l, r) = map(int, input().split())
	print(asum[r] - asum[l - 1] if q == 1 else lsum[r] - lsum[l - 1])


n = int(input())
a = [int(x) for x in input().split()]
b = sorted(a)
p = [] + a
q = [] + b
m = int(input())
for i in range(1, len(a)):
	a[i] += a[i - 1]
	b[i] += b[i - 1]
while m > 0:
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(a[r - 1] - a[l - 1] + p[l - 1])
	else:
		print(b[r - 1] - b[l - 1] + q[l - 1])
	m -= 1


n = int(input())
arr = list(map(int, input().split()))
prefix_sum = [0]
s = 0
for i in arr:
	s += i
	prefix_sum.append(s)
arr.sort()
sort_sum = [0]
s = 0
for i in arr:
	s += i
	sort_sum.append(s)
q = int(input())
for i in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(prefix_sum[r] - prefix_sum[l - 1])
	else:
		print(sort_sum[r] - sort_sum[l - 1])


import sys, itertools
f = sys.stdin
n = int(f.readline().strip())
v = [int(u) for u in f.readline().strip().split()]
m = int(f.readline().strip())
vs = list(itertools.accumulate(v))
u = sorted(v)
us = list(itertools.accumulate(u))
p = []
for i in range(m):
	(type, l, r) = map(int, f.readline().strip().split())
	if type == 1:
		if l > 1:
			p.append(str(vs[r - 1] - vs[l - 2]))
		else:
			p.append(str(vs[r - 1]))
	elif l > 1:
		p.append(str(us[r - 1] - us[l - 2]))
	else:
		p.append(str(us[r - 1]))
print('\n'.join(p))


n = int(input())
v = list(map(int, input().split()))
v_pre = [0] * n
v_pre[0] = v[0]
for i in range(1, n):
	v_pre[i] = v[i] + v_pre[i - 1]
u = list(sorted(v))
u_pre = [0] * n
u_pre[0] = u[0]
for i in range(1, n):
	u_pre[i] = u[i] + u_pre[i - 1]
m = int(input())
for _ in range(m):
	(num, l, r) = map(int, input().split())
	if num == 1:
		if l == 1:
			print(v_pre[r - 1])
		else:
			print(v_pre[r - 1] - v_pre[l - 2])
	elif l == 1:
		print(u_pre[r - 1])
	else:
		print(u_pre[r - 1] - u_pre[l - 2])


n = int(input())
arr = list(map(int, input().split()))
arrSorted = sorted(arr)
dp = [0 for _ in range(n + 1)]
dpSorted = [0 for _ in range(n + 1)]
for i in range(1, n + 1):
	dp[i] = dp[i - 1] + arr[i - 1]
	dpSorted[i] = dpSorted[i - 1] + arrSorted[i - 1]
m = int(input())
while m:
	q = list(map(int, input().split()))
	if q[0] == 1:
		print(dp[q[2]] - dp[q[1] - 1])
	else:
		print(dpSorted[q[2]] - dpSorted[q[1] - 1])
	m -= 1


def prefixsum(p, r, l):
	return p[l] - p[r - 1]

def prefixpre(li):
	p = list()
	p.append(0)
	i = 1
	while i < len(li):
		p.append(p[i - 1] + li[i])
		i = i + 1
	return p
n = int(input())
li = list(map(int, input().split(' ')))
li.insert(0, 0)
lis = sorted(li)
p = prefixpre(li)
ps = prefixpre(lis)
q = int(input())
i = 0
while i < q:
	qu = list(map(int, input().split(' ')))
	if qu[0] == 1:
		print(prefixsum(p, qu[1], qu[2]))
	else:
		print(prefixsum(ps, qu[1], qu[2]))
	i = i + 1


n = int(input())
v = [int(i) for i in input().split()]
u = v.copy()
u.sort()
v_sum = [v[0]]
u_sum = [u[0]]
for i in range(1, n):
	v_sum.append(v_sum[i - 1] + v[i])
	u_sum.append(u_sum[i - 1] + u[i])
m = int(input())
for i in range(m):
	a = [int(i) for i in input().split()]
	if a[0] == 1:
		print(v_sum[a[2] - 1] - v_sum[a[1] - 1] + v[a[1] - 1])
	else:
		print(u_sum[a[2] - 1] - u_sum[a[1] - 1] + u[a[1] - 1])


def s():
	n = int(input())
	v = list(map(int, input().split(' ')))
	v_sorted = sorted(v)
	sum_v = 0
	sum_v_sorted = 0
	v_prefix_sum = [0 for x in range(n)]
	v_sorted_prefix_sum = [0 for x in range(n)]
	for i in range(n):
		sum_v += v[i]
		v_prefix_sum[i] = sum_v
		sum_v_sorted += v_sorted[i]
		v_sorted_prefix_sum[i] = sum_v_sorted
	m = int(input())
	for i in range(m):
		(t, l, r) = [int(x) for x in input().split(' ')]
		if t == 1:
			if l > 1:
				print(v_prefix_sum[r - 1] - v_prefix_sum[l - 2])
			else:
				print(v_prefix_sum[r - 1])
		elif l > 1:
			print(v_sorted_prefix_sum[r - 1] - v_sorted_prefix_sum[l - 2])
		else:
			print(v_sorted_prefix_sum[r - 1])
s()


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
I = lambda : list(map(int, input().split()))
n = int(input())
a = [0] + I()
(q, m) = (sorted(a), int(input()))
for i in range(1, n + 1):
	a[i] += a[i - 1]
	q[i] += q[i - 1]
for i in range(m):
	l = I()
	print(a[l[2]] - a[l[1] - 1] if l[0] == 1 else q[l[2]] - q[l[1] - 1])


from __future__ import division, print_function
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)
	print(l)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans
n = ii()
l = li()
s = sorted(l)
a = [l[0]]
b = [s[0]]
for i in range(1, n):
	a.append(a[i - 1] + l[i])
	b.append(b[i - 1] + s[i])
m = ii()
for i in range(m):
	(t, x, y) = mi()
	if t == 1:
		sum = a[y - 1]
		if x > 1:
			sum -= a[max(0, x - 2)]
	elif t == 2:
		sum = b[y - 1]
		if x > 1:
			sum -= b[max(0, x - 2)]
	print(sum)


n = int(input())
l = list(map(int, input().split()))
s = list(l)
s.sort()
t = int(input())
q = 0
r = 0
for i in range(0, n):
	q = q + l[i]
	l[i] = q
	r = r + s[i]
	s[i] = r
for i in range(0, t):
	(a, b, c) = map(int, input().split())
	if a == 1:
		if b == 1:
			print(l[c - 1])
		else:
			print(l[c - 1] - l[b - 2])
	elif b == 1:
		print(s[c - 1])
	else:
		print(s[c - 1] - s[b - 2])


n = int(input())
stones = list(map(int, input().split()))
q = int(input())
us = stones.copy()
us.sort()
total = [0 for i in range(n)]
totalS = total.copy()
for i in range(n):
	total[i] = total[i - 1] + stones[i]
	totalS[i] = totalS[i - 1] + us[i]
total.append(0)
totalS.append(0)
for i in range(q):
	(t, l, r) = list(map(int, input().split()))
	if t == 1:
		print(total[r - 1] - total[l - 2])
	else:
		print(totalS[r - 1] - totalS[l - 2])


n = int(input())
arr = list(map(int, input().split()))
srt = sorted(arr)
m = int(input())
for i in range(1, n):
	srt[i] = srt[i] + srt[i - 1]
	arr[i] = arr[i] + arr[i - 1]
for i in range(m):
	(a, b, c) = map(int, input().split())
	if a == 1:
		if b == 1:
			print(arr[c - 1])
		else:
			print(arr[c - 1] - arr[b - 2])
	elif b == 1:
		print(srt[c - 1])
	else:
		print(srt[c - 1] - srt[b - 2])


n = int(input())
v = list(map(int, input().split()))
u = sorted(v)
(s_v, s) = ([0], 0)
for i in range(n):
	s += v[i]
	s_v.append(s)
(s_u, s) = ([0], 0)
for i in range(n):
	s += u[i]
	s_u.append(s)
m = int(input())
for i in range(m):
	(q, l, r) = map(int, input().split())
	if q == 1:
		print(s_v[r] - s_v[l - 1])
	elif q == 2:
		print(s_u[r] - s_u[l - 1])


n = int(input())
preco = [int(x) for x in input().split()]
questoes = int(input())
orde = preco.copy()
orde.sort()
for i in range(1, n):
	preco[i] = preco[i] + preco[i - 1]
	orde[i] = orde[i] + orde[i - 1]
for e in range(questoes):
	(tipo, l, r) = [int(x) for x in input().split()]
	l -= 1
	r -= 1
	soma = 0
	if tipo == 1:
		if l > 0:
			soma = preco[r] - preco[l - 1]
		else:
			soma = preco[r]
	elif l > 0:
		soma = orde[r] - orde[l - 1]
	else:
		soma = orde[r]
	print(soma)


n = int(input())
v = [0] + [int(x) for x in input().split()]
u = sorted(v)
for i in range(1, n + 1):
	v[i] += v[i - 1]
	u[i] += u[i - 1]
m = int(input())
for i in range(m):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])


import random
import math
from collections import defaultdict
import itertools
from sys import stdin, stdout

def main():
	z = ''
	n = int(stdin.readline())
	v = list(map(int, stdin.readline().split()))
	m = int(stdin.readline())
	sv = [0]
	for x in list(itertools.accumulate(v)):
		sv.append(x)
	u = sorted(v)
	su = [0]
	for x in list(itertools.accumulate(u)):
		su.append(x)
	for _ in range(m):
		(t, l, r) = list(map(int, input().split()))
		if t == 1:
			ans = sv[r] - sv[l - 1]
		else:
			ans = su[r] - su[l - 1]
		z += str(ans) + '\n'
	stdout.write(z)
main()


n = int(input())
arr = [int(i) for i in input().split()]
sor_arr = sorted(arr)
pre = [0] * n
pre_sor = [0] * n
for i in range(n):
	pre[i] = pre[i - 1] + arr[i]
	pre_sor[i] = pre_sor[i - 1] + sor_arr[i]
q = int(input())
for i in range(q):
	(t, l, r) = [int(i) for i in input().split()]
	if t == 1:
		if l == 1:
			print(pre[r - 1])
		else:
			print(pre[r - 1] - pre[l - 2])
	elif l == 1:
		print(pre_sor[r - 1])
	else:
		print(pre_sor[r - 1] - pre_sor[l - 2])


n = int(input())
v = list(map(int, input().split()))
u = []
for i in range(n):
	u.append(v[i])
u.sort()
for i in range(1, n):
	v[i] += v[i - 1]
for i in range(1, n):
	u[i] += u[i - 1]
c = [0]
d = [0]
for i in range(n):
	c.append(v[i])
	d.append(u[i])
q = int(input())
for i in range(q):
	(a, l, r) = map(int, input().split())
	if a == 1:
		print(c[r] - c[l - 1])
	else:
		print(d[r] - d[l - 1])


n = int(input())
v = list(map(int, input().split()))
y = sorted(v)
a1 = [0]
a2 = [0]
for i in range(1, len(v) + 1):
	a1.append(a1[i - 1] + v[i - 1])
	a2.append(a2[i - 1] + y[i - 1])
for i in range(int(input())):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(a1[r] - a1[l - 1])
	else:
		print(a2[r] - a2[l - 1])


import sys
input = sys.stdin.readline
n = int(input())
k = list(map(int, input().strip().split(' ')))
pre = k.copy()
p1 = [0] * (n + 1)
r = [0] * (n + 1)
pre.sort()
for i in range(1, n + 1):
	p1[i] = p1[i - 1] + k[i - 1]
	r[i] = r[i - 1] + pre[i - 1]
for _ in range(int(input())):
	(t, l, q) = map(int, input().strip().split(' '))
	if t == 1:
		print(p1[q] - p1[l - 1])
	else:
		print(r[q] - r[l - 1])


l = int(input())
stones = list(map(int, input().split()))
dp = [0] + stones
for i in range(1, len(dp)):
	dp[i] += dp[i - 1]
dp1 = [0] + sorted(stones)
for i in range(1, len(dp1)):
	dp1[i] += dp1[i - 1]
n = int(input())
for i in range(n):
	(t, left, right) = map(int, input().split())
	if t == 1:
		print(dp[right] - dp[left - 1])
	else:
		print(dp1[right] - dp1[left - 1])


n = int(input())
l = list(map(int, input().split(' ')))
total = 0
a = list()
b = list()
for i in l:
	total = total + i
	a.append(total)
l.sort()
total = 0
for i in l:
	total = total + i
	b.append(total)
t = int(input())
for i in range(0, t):
	(s, x, y) = map(int, input().split(' '))
	if s == 1:
		if x - 2 >= 0:
			print(a[y - 1] - a[x - 2])
		else:
			print(a[y - 1])
	elif x - 2 >= 0:
		print(b[y - 1] - b[x - 2])
	else:
		print(b[y - 1])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
cumSum_a = [a[0]]
cumSum_b = [b[0]]
for i in range(1, n):
	cumSum_a.append(cumSum_a[i - 1] + a[i])
	cumSum_b.append(cumSum_b[i - 1] + b[i])
cumSum_a.insert(0, 0)
cumSum_b.insert(0, 0)
m = int(input())
for i in range(m):
	s = input().split()
	(type, l, r) = (int(s[0]), int(s[1]), int(s[2]))
	if type == 1:
		print(cumSum_a[r] - cumSum_a[l - 1])
	else:
		print(cumSum_b[r] - cumSum_b[l - 1])


x = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in range(1, x):
	b.append(b[i - 1] + a[i])
a.sort()
c = [a[0]]
for i in range(1, x):
	c.append(c[i - 1] + a[i])
n = int(input())
for __ in range(n):
	(q, w, e) = map(int, input().split())
	if q == 1:
		if w > 1:
			print(b[e - 1] - b[w - 2])
		else:
			print(b[e - 1])
	elif w > 1:
		print(c[e - 1] - c[w - 2])
	else:
		print(c[e - 1])


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return list(map(int, input().split()))

def solve(arr):
	res = []
	cur_sum = 0
	for i in arr:
		cur_sum += i
		res.append(cur_sum)
	return res
n = inp()
v = inlt()
m = inp()
prefix_sum = solve(v)
sorted_prefix_sum = solve(sorted(v))
for q in range(m):
	tlr = inlt()
	if tlr[0] == 1:
		start_sum = 0 if tlr[1] - 2 < 0 else prefix_sum[tlr[1] - 2]
		print(prefix_sum[tlr[2] - 1] - start_sum)
	else:
		start_sum = 0 if tlr[1] - 2 < 0 else sorted_prefix_sum[tlr[1] - 2]
		print(sorted_prefix_sum[tlr[2] - 1] - start_sum)


n = int(input())
value = list(map(int, input().split()))
sorted_value = sorted(value)
m = int(input())
for i in range(1, len(value)):
	value[i] += value[i - 1]
	sorted_value[i] += sorted_value[i - 1]
memo = {}
answers = []
for i in range(m):
	(t, l, r) = list(map(int, input().split()))
	try:
		answers.append(memo[t, l, r])
	except KeyError:
		if t == 1:
			if l == 1:
				last = 0
			else:
				last = value[l - 2]
			ans = value[r - 1] - last
			answers.append(ans)
		else:
			if l == 1:
				last = 0
			else:
				last = sorted_value[l - 2]
			ans = sorted_value[r - 1] - last
			answers.append(ans)
		memo[t, l, r] = ans
for ans in answers:
	print(ans)


n = int(input())
costs = [int(i) for i in input().split()]
com1 = [0]
j = len(costs) + 1
for i in range(1, j):
	com1.append(com1[i - 1] + costs[i - 1])
costs = sorted(costs)
com2 = [0]
for i in range(1, j):
	com2.append(com2[i - 1] + costs[i - 1])
m = int(input())
for i in range(m):
	(type, l, r) = [int(i) for i in input().split()]
	if type == 1:
		print(com1[r] - com1[l - 1])
	else:
		print(com2[r] - com2[l - 1])


n = int(input())
u = list(map(int, input().split()))
v = sorted(u)
su = [u[0]]
sv = [v[0]]
for i in range(1, n):
	su.append(su[-1] + u[i])
	sv.append(sv[-1] + v[i])
q = int(input())
for _ in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(su[r - 1] - su[l - 1] + u[l - 1])
	else:
		print(sv[r - 1] - sv[l - 1] + v[l - 1])


(n, num) = (int(input()), list(map(int, input().split())))
sort = list(sorted(num))
for i in range(n - 1):
	num[i + 1] += num[i]
	sort[i + 1] += sort[i]
(num, sort) = ([0] + num, [0] + sort)
for i in range(int(input())):
	(t, l, r) = list(map(int, input().split()))
	print(num[r] - num[l - 1] if t == 1 else sort[r] - sort[l - 1])


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	n = int(input())
	arr = get_array()
	other = sorted(arr)
	prefix_sum = [0] * (n + 1)
	other_sum = [0] * (n + 1)
	for i in range(1, n + 1):
		prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1]
		other_sum[i] = other_sum[i - 1] + other[i - 1]
	for tc in range(int(input())):
		(t, l, r) = get_ints()
		if t == 1:
			print(prefix_sum[r] - prefix_sum[l - 1])
		else:
			print(other_sum[r] - other_sum[l - 1])
main()


import sys
import collections
import math
import functools
import itertools
import bisect
import operator
import heapq
import random
true = True
false = False
null = None
SLOW = True
memoi = functools.lru_cache(None)
tcid = 0
tcmax = 99999999

def fast():
	SLOW = False

def compute(val, func):
	return func(val)

def seq(lo, hi, step=1):
	return range(lo, hi + 1, step)

def sround(val, nd):
	return f'{val:.{nd}f}'

def ceil(a, b):
	ans = a // b
	if a % b != 0:
		ans += 1
	return ans

def perr(*args, **kwargs):
	if SLOW:
		print(*args, file=sys.stderr, **kwargs)

def line():
	ln = sys.stdin.readline().strip()
	if ln == '':
		sys.exit()
	return ln

def lines(n):
	return [line() for i in range(n)]

def split(ln=None):
	return (ln or line()).split()

def num(str=None):
	str = str or line()
	return float(str) if '.' in str else int(str)

def nums(o=None):
	if o is not None:
		if isinstance(o, int):
			o = lines(o)
		elif isinstance(o, str):
			o = split(o)
	return list(map(num, o or split()))

def mainloop(tcid=1):
	n = num()
	v = nums()
	psa = []
	for zzz in range(2):
		psa.append([0] * (n + 1))
	for ii in range(n):
		psa[0][ii + 1] = psa[0][ii] + v[ii]
	v.sort()
	for ii in range(n):
		psa[1][ii + 1] = psa[1][ii] + v[ii]
	m = num()
	for ln in lines(m):
		(t, l, r) = nums(ln)
		ans = psa[t - 1][r] - psa[t - 1][l - 1]
		print(ans)
while tcid < tcmax:
	tcid += 1
	mainloop(tcid)


def read_ints():
	return [int(x) for x in input().split()]

def compute_prefix_sums(numbers):
	yield 0
	current_sum = 0
	for x in numbers:
		current_sum += x
		yield current_sum

def main():
	_n = int(input())
	numbers = read_ints()
	sorted_numbers = list(sorted(numbers))
	sums_numbers = list(compute_prefix_sums(numbers))
	sums_sorted_numbers = list(compute_prefix_sums(sorted_numbers))
	number_of_queries = int(input())
	for i in range(number_of_queries):
		(query_type, left, right) = read_ints()
		assert query_type in (1, 2)
		if query_type == 1:
			queried_table = sums_numbers
		else:
			queried_table = sums_sorted_numbers
		result = queried_table[right] - queried_table[left - 1]
		print(result)
main()


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
n = int(input())
v = [0] + list(map(int, input().split()))
u = sorted(v)
for i in range(1, n + 1):
	v[i] += v[i - 1]
	u[i] += u[i - 1]
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v[r] - v[l - 1])
	else:
		print(u[r] - u[l - 1])


n = int(input())
l1 = list(map(int, input().split()))
s_l1 = sorted(l1)
dp1 = [0] * n
dp2 = [0] * n
dp1[0] = l1[0]
dp2[0] = s_l1[0]
m = int(input())
for i in range(1, n):
	dp1[i] = dp1[i - 1] + l1[i]
for i in range(1, n):
	dp2[i] = dp2[i - 1] + s_l1[i]
for i in range(m):
	(typ, l, r) = map(int, input().split())
	if typ == 1:
		print(dp1[r - 1] - dp1[l - 1] + l1[l - 1])
	else:
		print(dp2[r - 1] - dp2[l - 1] + s_l1[l - 1])


R = lambda : map(int, input().split())
(n,) = R()
v = (*R(),)
(m,) = R()
u = sorted(v)
(sv, su) = ([0, v[0]], [0, u[0]])
for i in range(1, n):
	sv.append(v[i] + sv[-1])
	su.append(u[i] + su[-1])
for _ in ' ' * m:
	(q, l, r) = R()
	print(sv[r] - sv[l - 1] if q == 1 else su[r] - su[l - 1])


n = int(input())
ar = list(map(int, input().split()))
q = int(input())
sar = sorted(ar)
for i in range(1, n):
	ar[i] += ar[i - 1]
	sar[i] += sar[i - 1]
ar = [0] + ar
sar = [0] + sar
while q != 0:
	q -= 1
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(ar[r] - ar[l - 1])
	else:
		print(sar[r] - sar[l - 1])


n = int(input())
stones = list(map(int, input().split()))
n_questions = int(input())
sorted_stones = sorted(stones)
cusum_1 = [stones[0]]
cusum_2 = [sorted_stones[0]]
for i in range(1, n):
	cusum_1.append(cusum_1[-1] + stones[i])
	cusum_2.append(cusum_2[-1] + sorted_stones[i])
for i in range(n_questions):
	inp = list(map(int, input().split()))
	(question, start, end) = (inp[0], inp[1], inp[2])
	if start - 2 < 0:
		if question == 1:
			print(cusum_1[end - 1])
		else:
			print(cusum_2[end - 1])
	elif question == 1:
		print(cusum_1[end - 1] - cusum_1[start - 2])
	else:
		print(cusum_2[end - 1] - cusum_2[start - 2])


n = int(input())
l = list(map(int, input().split()))
t1 = [0]
s = 0
for i in l:
	s += i
	t1.append(s)
l.sort()
s = 0
t2 = [0]
for i in l:
	s += i
	t2.append(s)
for i in range(int(input())):
	(a, l, r) = map(int, input().split())
	if a == 1:
		print(t1[r] - t1[l - 1])
	else:
		print(t2[r] - t2[l - 1])


n = int(input().strip())
arr = list(map(int, input().strip().split()))
sumArray = [arr[0]]
for i in range(1, n):
	sumArray.append(sumArray[i - 1] + arr[i])
sortedArr = sorted(arr)
sums2 = [sortedArr[0]]
for i in range(1, n):
	sums2.append(sums2[i - 1] + sortedArr[i])
for _ in range(int(input().strip())):
	(t, l, r) = map(int, input().strip().split())
	if t == 1:
		print(sumArray[r - 1] - sumArray[l - 1] + arr[l - 1])
	else:
		print(sums2[r - 1] - sums2[l - 1] + sortedArr[l - 1])


n = int(input())
a = list(map(int, input().split(' ')))
m = int(input())
b = sorted(a)
for i in range(1, n):
	a[i] = a[i] + a[i - 1]
	b[i] = b[i] + b[i - 1]
for _ in range(m):
	(uv, l, r) = list(map(int, input().split(' ')))
	if uv == 1:
		if l != 1:
			print(a[r - 1] - a[l - 2])
		else:
			print(a[r - 1])
	elif l != 1:
		print(b[r - 1] - b[l - 2])
	else:
		print(b[r - 1])


n = int(input())
l = list(map(int, input().split()))
s = sorted(l)
for i in range(1, n):
	l[i] += l[i - 1]
for i in range(1, n):
	s[i] += s[i - 1]
s = [0] + s
l = [0] + l
m = int(input())
for i in range(m):
	(a, b, c) = map(int, input().split())
	if a == 1:
		print(l[c] - l[b - 1])
	else:
		print(s[c] - s[b - 1])


n = int(input())
ar = list(map(int, input().split()))
q = int(input())
sr = sorted(ar)
ps1 = [0] * (n + 1)
ps2 = [0] * (n + 1)
for i in range(1, n + 1):
	ps1[i] = ps1[i - 1] + ar[i - 1]
	ps2[i] = ps2[i - 1] + sr[i - 1]
for i in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(ps1[r] - ps1[l - 1])
	else:
		print(ps2[r] - ps2[l - 1])


n = int(input())
l = list(map(int, input().split()))
g = [0] * n
g[0] = l[0]
for i in range(1, n):
	g[i] = g[i - 1] + l[i]
g.insert(0, 0)
o = l[:]
o.sort()
h = [0] * n
h[0] = o[0]
for i in range(1, n):
	h[i] = h[i - 1] + o[i]
h.insert(0, 0)
m = int(input())
for i in range(m):
	(x, y, z) = map(int, input().split())
	if x == 1:
		print(g[z] - g[y - 1])
	else:
		print(h[z] - h[y - 1])


a = int(input())
b = list(map(int, input().split()))
c = sorted(b)
for i in range(1, a):
	b[i] += b[i - 1]
	c[i] += c[i - 1]
d = [[0] + b, [0] + c]
e = []
f = int(input())
for i in range(f):
	(g, h, j) = map(int, input().split())
	e.append(str(d[g - 1][j] - d[g - 1][h - 1]))
print('\n'.join(e))


n = int(input())
stones = [int(x) for x in input().split(' ')]
sorted_stones = sorted(stones)
m = int(input())
answers = []
sums = [[0] * (n + 1), [0] * (n + 1)]
for i in range(n):
	sums[0][i + 1] = sums[0][i] + stones[i]
	sums[1][i + 1] = sums[1][i] + sorted_stones[i]
for i in range(m):
	(t, l, r) = [int(x) for x in input().split(' ')]
	if t == 1:
		answers.append(sums[0][r] - sums[0][l - 1])
	else:
		answers.append(sums[1][r] - sums[1][l - 1])
print('\n'.join([str(x) for x in answers]))


n = int(input())
arr = [0] + [int(i) for i in input().split()]
arr_sort = sorted(arr)
for i in range(1, n + 1):
	arr[i] = arr[i] + arr[i - 1]
for i in range(1, 1 + n):
	arr_sort[i] = arr_sort[i] + arr_sort[i - 1]
m = int(input())
for i in range(m):
	(t, l, k) = [int(i) for i in input().split()]
	if t == 1:
		print(arr[k] - arr[l - 1])
	else:
		print(arr_sort[k] - arr_sort[l - 1])


n = int(input())
v = list(map(int, input().split()))
u = sorted(v)
s_v = [0, v[0]]
s_u = [0, u[0]]
for i in range(1, n):
	s_v.append(s_v[-1] + v[i])
	s_u.append(s_u[-1] + u[i])
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(s_v[r] - s_v[l - 1])
	else:
		print(s_u[r] - s_u[l - 1])


num = int(input())
arr1 = list(map(int, input().split()))
arr2 = arr1.copy()
arr2.insert(0, 0)
arr1.insert(0, 0)
arr2[1:] = sorted(arr2[1:])
for i in range(1, num + 1):
	arr1[i] += arr1[i - 1]
	arr2[i] += arr2[i - 1]
for test_var in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(arr1[r] - arr1[l - 1])
	else:
		print(arr2[r] - arr2[l - 1])


from sys import stdin, stdout
import sys
import math
n = int(stdin.readline().strip())
arr = [int(x) for x in stdin.readline().strip().split()]
sorted_arr = sorted(arr)
commulativeNormal = [0] * n
commulativeSorted = [0] * n
for i in range(n):
	commulativeNormal[i] = int(arr[i] + commulativeNormal[i - 1])
for i in range(n):
	commulativeSorted[i] = int(sorted_arr[i] + commulativeSorted[i - 1])
t = int(stdin.readline().strip())
for i in range(t):
	testCase = [int(x) for x in stdin.readline().strip().split()]
	if testCase[0] == 1:
		if testCase[1] != 1:
			print(str(commulativeNormal[testCase[2] - 1] - commulativeNormal[testCase[1] - 2]))
		else:
			print(str(commulativeNormal[testCase[2] - 1]))
	elif testCase[0] == 2:
		if testCase[1] != 1:
			print(str(commulativeSorted[testCase[2] - 1] - commulativeSorted[testCase[1] - 2]))
		else:
			print(str(commulativeSorted[testCase[2] - 1]))


t = int(input())
lst = list(map(int, input().split()))
x = lst[:]
x.sort()
l_st = [0]
lst1 = [0]
k = 0
k1 = 0
for i in range(t):
	k += lst[i]
	l_st.append(k)
	k1 += x[i]
	lst1.append(k1)
n = int(input())
for i in range(n):
	(t_pe, l, r) = map(int, input().split())
	if t_pe == 1:
		print(l_st[r] - l_st[l - 1])
	else:
		print(lst1[r] - lst1[l - 1])


n = int(input())
arr1 = list(map(int, input().split()))
m = int(input())
arr2 = sorted(arr1)
arr11 = [0]
arr22 = [0]
sum1 = sum2 = 0
for i in range(n):
	sum1 += arr1[i]
	sum2 += arr2[i]
	arr11.append(sum1)
	arr22.append(sum2)
for i in range(m):
	(t, l, r) = list(map(int, input().split()))
	if t == 1:
		print(arr11[r] - arr11[l - 1])
	else:
		print(arr22[r] - arr22[l - 1])


n = input()
l = list(map(int, input().split()))
l.insert(0, 0)
z = sorted(l)
for i in range(1, len(l)):
	l[i] += l[i - 1]
	z[i] += z[i - 1]
for _ in range(int(input())):
	(a, b, c) = map(int, input().split())
	if a == 1:
		print(l[c] - l[b - 1])
	else:
		print(z[c] - z[b - 1])


n = int(input())
nums = list(map(int, input().split()))
sums = [nums[0]]
for i in range(1, n):
	sums.append(sums[i - 1] + nums[i])
sortednums = sorted(nums)
sortedsums = [sortednums[0]]
for i in range(1, n):
	sortedsums.append(sortedsums[i - 1] + sortednums[i])
q = int(input())
for querry in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			print(sums[r - 1])
		else:
			print(sums[r - 1] - sums[l - 2])
	elif l == 1:
		print(sortedsums[r - 1])
	else:
		print(sortedsums[r - 1] - sortedsums[l - 2])


n = int(input())
mas1 = list(map(int, input().split(' ')))
mas2 = sorted(mas1)
for i in range(1, n):
	mas1[i] += mas1[i - 1]
	mas2[i] += mas2[i - 1]
m = int(input())
mas1.append(0)
mas2.append(0)
print('\n'.join(map(str, (mas1[b - 1] - mas1[a - 2] if s == 1 else mas2[b - 1] - mas2[a - 2] for (s, a, b) in (map(int, input().split(' ')) for i in range(m))))))


import os
import sys
from io import BytesIO, IOBase
from itertools import accumulate

def main():
	n = int(input())
	a = [0] + list(map(int, input().split()))
	pre = list(accumulate(a))
	s = list(accumulate(sorted(a)))
	for _ in range(int(input())):
		(choice, l, r) = map(int, input().split())
		if choice == 1:
			print(pre[r] - pre[l - 1])
		else:
			print(s[r] - s[l - 1])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def solve(v):
	if v[0] == 1:
		print(dp1[v[2]] - dp1[v[1] - 1])
	else:
		print(dp2[v[2]] - dp2[v[1] - 1])
n = int(input())
c = list(map(int, input().split()))
dp1 = [0 if i == 0 else c[i - 1] for i in range(n + 1)]
dp2 = [0] + list(sorted(c))
for i in range(1, n + 1):
	dp1[i] += dp1[i - 1]
	dp2[i] += dp2[i - 1]
for _ in range(int(input())):
	solve(list(map(int, input().split())))


from sys import stdin
input = lambda : stdin.readline()
n = int(input())
l = list(map(int, input().split()))
z1 = [0]
(su1, su2) = (0, 0)
z2 = [0]
l2 = sorted(l)
for i in range(n):
	su1 += l[i]
	su2 += l2[i]
	z1.append(su1)
	z2.append(su2)
for i in range(int(input())):
	(a, b, c) = map(int, input().split())
	if a == 1:
		print(z1[c] - z1[b - 1])
	else:
		print(z2[c] - z2[b - 1])


n = input()
nums = list(map(int, input().split()))
nums_sort = sorted(nums)
nums_A = [0] * len(nums)
nums_A[0] = nums[0]
for i in range(1, len(nums_A)):
	nums_A[i] = nums_A[i - 1] + nums[i]
nums_B = [0] * len(nums)
nums_B[0] = nums_sort[0]
for i in range(1, len(nums_A)):
	nums_B[i] = nums_B[i - 1] + nums_sort[i]
q = int(input())
for i in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			l = 0
		else:
			l = nums_A[l - 2]
		r = nums_A[r - 1]
		print(r - l)
	else:
		if l == 1:
			l = 0
		else:
			l = nums_B[l - 2]
		r = nums_B[r - 1]
		print(r - l)


import sys
input = sys.stdin.readline

def multi_input():
	return map(int, input().split())

def array_print(arr):
	print(' '.join(map(str, arr)))
n = int(input())
a = list(multi_input())
b = sorted(a)
for i in range(1, n):
	a[i] = a[i] + a[i - 1]
	b[i] = b[i] + b[i - 1]
for q in range(int(input())):
	(t, l, r) = multi_input()
	if t == 1:
		if l > 1:
			print(a[r - 1] - a[l - 2])
		else:
			print(a[r - 1])
	elif l > 1:
		print(b[r - 1] - b[l - 2])
	else:
		print(b[r - 1])


n = int(input())
a = list(map(int, input().split()))
asor = list(a)
asor.sort()
ps = []
pss = []
cnt = 0
cnta = 0
for i in range(n):
	cnt += a[i]
	cnta += asor[i]
	ps.append(cnt)
	pss.append(cnta)
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l - 2 >= 0:
			print(ps[r - 1] - ps[l - 2])
		else:
			print(ps[r - 1])
	elif l - 2 >= 0:
		print(pss[r - 1] - pss[l - 2])
	else:
		print(pss[r - 1])


n = int(input())
v = list(map(int, input().split()))
c = [[0] + v, [0] + sorted(v)]
for i in range(1, n + 1):
	c[0][i] += c[0][i - 1]
	c[1][i] += c[1][i - 1]
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	print(c[t - 1][r] - c[t - 1][l - 1])


n = int(input())
tab = [int(x) for x in input().split(' ')]
stab = tab.copy()
stab.sort()
sumtab = [0, tab[0]]
sumstab = [0, stab[0]]
for x in range(2, len(tab) + 1):
	sumtab.append(sumtab[x - 1] + tab[x - 1])
for x in range(2, len(stab) + 1):
	sumstab.append(sumstab[x - 1] + stab[x - 1])
m = int(input())
for i in range(m):
	query = [int(x) for x in input().split(' ')]
	if query[0] == 1:
		print(sumtab[query[2]] - sumtab[query[1] - 1])
	else:
		print(sumstab[query[2]] - sumstab[query[1] - 1])


n = int(input())
arr = list(map(int, input().split()))
arr1 = sorted(arr)
dp1 = [0 for i in range(n + 1)]
dp2 = [0 for i in range(n + 1)]
dp1[1] = arr[0]
dp2[1] = arr1[0]
for i in range(2, n + 1):
	dp1[i] = dp1[i - 1] + arr[i - 1]
	dp2[i] = dp2[i - 1] + arr1[i - 1]
m = int(input())
for i in range(m):
	(x, l, r) = map(int, input().split())
	if x == 1:
		print(dp1[r] - dp1[l - 1])
	else:
		print(dp2[r] - dp2[l - 1])


n = int(input())
arr = input().split(' ')
for i in range(n):
	arr[i] = int(arr[i])
sort = sorted(arr)
arr.insert(0, 0)
sort.insert(0, 0)
for i in range(1, len(arr)):
	arr[i] += arr[i - 1]
	sort[i] += sort[i - 1]
m = int(input())
result = [0] * m
for i in range(m):
	args = input().split(' ')
	type = int(args[0])
	l = int(args[1])
	r = int(args[2])
	res = 0
	if type == 1:
		res = arr[r] - arr[l - 1]
	else:
		res = sort[r] - sort[l - 1]
	result[i] = res
for n in result:
	print(n)


n = int(input(''))
l2 = []
l1 = list(map(int, input('').split()))
for i in range(0, len(l1)):
	l2.append(l1[i])
for i in range(1, len(l1)):
	l1[i] = l1[i] + l1[i - 1]
l2.sort()
for i in range(1, len(l2)):
	l2[i] = l2[i] + l2[i - 1]
m = int(input(''))
while m > 0:
	l = list(map(int, input().split()))
	if l[0] == 1:
		if l[1] - 2 < 0:
			sum1 = l1[l[2] - 1]
		else:
			sum1 = l1[l[2] - 1] - l1[l[1] - 2]
	elif l[1] - 2 < 0:
		sum1 = l2[l[2] - 1]
	else:
		sum1 = l2[l[2] - 1] - l2[l[1] - 2]
	print(sum1)
	m = m - 1


n = int(input())
v = [int(i) for i in input().split()]
u = sorted(v)
pref1 = [v[0]]
pref2 = [u[0]]
for i in range(1, n):
	pref1.append(v[i] + pref1[i - 1])
	pref2.append(u[i] + pref2[i - 1])
m = int(input())
for i in range(m):
	(t, l, r) = (int(i) for i in input().split())
	if t == 1:
		if l == 1:
			print(pref1[r - 1])
		else:
			print(pref1[r - 1] - pref1[l - 2])
	elif l == 1:
		print(pref2[r - 1])
	else:
		print(pref2[r - 1] - pref2[l - 2])


n = int(input())
ar = list(map(int, input().split()))
ar_sorted = sorted(ar)
prefix = [0 for i in range(n + 1)]
prefix_sorted = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	prefix[i] = prefix[i - 1] + ar[i - 1]
	prefix_sorted[i] = prefix_sorted[i - 1] + ar_sorted[i - 1]
num_queries = int(input())
for query in range(num_queries):
	(type, first, last) = map(int, input().split())
	if type == 1:
		print(prefix[last] - prefix[first - 1])
	else:
		print(prefix_sorted[last] - prefix_sorted[first - 1])


n = int(input())
v = list(map(int, input().split()))
v_prefix = [None for _ in range(n + 1)]
for i in range(n + 1):
	if i == 0:
		v_prefix[i] = 0
	else:
		v_prefix[i] = v_prefix[i - 1] + v[i - 1]
v_sorted = sorted(v)
v_sorted_prefix = [None for _ in range(n + 1)]
for i in range(n + 1):
	if i == 0:
		v_sorted_prefix[i] = 0
	else:
		v_sorted_prefix[i] = v_sorted_prefix[i - 1] + v_sorted[i - 1]
m = int(input())
ans = []
for i in range(m):
	p = list(map(int, input().split()))
	if p[0] == 1:
		ans.append(v_prefix[p[2]] - v_prefix[p[1] - 1])
	else:
		ans.append(v_sorted_prefix[p[2]] - v_sorted_prefix[p[1] - 1])
for i in ans:
	print(i)


quantidade_de_pedras = input()
pedras = list(map(int, input().split()))
pedras_ord = sorted(pedras)
perguntas = int(input())
retorno = []
somas = [0]
somas_ord = [0]
for i in range(len(pedras)):
	somas.append(pedras[i] + somas[i])
	somas_ord.append(pedras_ord[i] + somas_ord[i])
for _ in range(perguntas):
	(tipo, start, end) = list(map(int, input().split()))
	if tipo == 1:
		retorno.append(somas[start - 1] - somas[end])
	else:
		retorno.append(somas_ord[start - 1] - somas_ord[end])
for elem in retorno:
	print(-elem)


n = int(input())
arr = input()
arr = arr.split(' ')
for i in range(0, len(arr)):
	arr[i] = int(arr[i])
m = int(input())
query = []
for i in range(0, m):
	temp_input = input()
	temp_input = temp_input.split(' ')
	query.append([int(temp_input[0]), int(temp_input[1]), int(temp_input[2])])
sorted_arr = arr[:]
sorted_arr.sort()
dp = [0]
dp_sorted = [0]
sum = 0
sum_sorted = 0
for i in range(0, len(arr)):
	sum = sum + arr[i]
	sum_sorted = sum_sorted + sorted_arr[i]
	dp.append(sum)
	dp_sorted.append(sum_sorted)
for i in range(0, len(query)):
	if query[i][0] != 1:
		temp = dp_sorted[query[i][2]] - dp_sorted[query[i][1] - 1]
		print(temp)
	else:
		temp = dp[query[i][2]] - dp[query[i][1] - 1]
		print(temp)


n = int(input())
sorted_arr = list(map(int, input().split()))
arr = []
for i in sorted_arr:
	arr.append(i)
sorted_arr.sort()
sum_arr = [0 for i in range(n)]
sorted_sum = [0 for i in range(n)]
for i in range(n):
	sum_arr[i] = arr[i] + sum_arr[i - 1]
for i in range(n):
	sorted_sum[i] = sorted_arr[i] + sorted_sum[i - 1]
t = int(input())
for _ in range(t):
	sum = 0
	lists = list(map(int, input().split()))
	if lists[0] == 1:
		if lists[1] - 2 < 0:
			sum = sum_arr[lists[2] - 1]
		else:
			sum = sum_arr[lists[2] - 1] - sum_arr[lists[1] - 2]
	elif lists[1] - 2 < 0:
		sum = sorted_sum[lists[2] - 1]
	else:
		sum = sorted_sum[lists[2] - 1] - sorted_sum[lists[1] - 2]
	print(sum)


n = int(input())
l = list(map(int, input().split()))
l1 = sorted(l)
m = int(input())
for i in range(n - 1):
	l[i + 1] = l[i + 1] + l[i]
	l1[i + 1] = l1[i + 1] + l1[i]
l1 = [0] + l1
l = [0] + l
for i in range(m):
	r1 = list(map(int, input().split()))
	if r1[0] == 1:
		print(l[r1[2]] - l[r1[1] - 1])
	else:
		print(l1[r1[2]] - l1[r1[1] - 1])


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
n = I()
l = L()
s = sorted(l)
x = [0]
y = [0]
for i in range(n):
	x.append(x[-1] + l[i])
	y.append(y[-1] + s[i])
q = I()
for i in range(q):
	(a, l, r) = M()
	if a == 1:
		print(x[r] - x[l - 1])
	else:
		print(y[r] - y[l - 1])


n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a)
prefix_a = [i for i in a]
prefix_b = [i for i in b]
for i in range(1, n):
	prefix_a[i] += prefix_a[i - 1]
	prefix_b[i] += prefix_b[i - 1]
prefix_a.insert(0, 0)
prefix_b.insert(0, 0)
ans = []
q = int(input())
for _ in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		ans.append(prefix_a[r] - prefix_a[l - 1])
	else:
		ans.append(prefix_b[r] - prefix_b[l - 1])
ans = '\n'.join([str(i) for i in ans])
print(ans)


n = int(input())
lst = list(map(int, input().split()))
new = sorted(lst)
l = [0]
a = [0]
s1 = 0
s2 = 0
for i in range(n):
	s1 += lst[i]
	s2 += new[i]
	l.append(s1)
	a.append(s2)
m = int(input())
for i in range(m):
	(c, ll, r) = map(int, input().split())
	if c == 1:
		print(l[r] - l[ll - 1])
	else:
		print(a[r] - a[ll - 1])


def order(array):
	return sorted(array, key=int)

def summ(array):
	array[0] = int(array[0])
	for i in range(1, len(array)):
		array[i] = int(array[i]) + array[i - 1]

def questionRes(l, r, array):
	if l == 0:
		return array[r]
	return array[r] - array[l - 1]
size = int(input())
costs_stones = input().split()
n_questions = int(input())
costs_stones_ord = order(costs_stones.copy())
summ(costs_stones)
summ(costs_stones_ord)
for i in range(n_questions):
	input_ = input().split()
	question = int(input_[0])
	l = int(input_[1]) - 1
	r = int(input_[2]) - 1
	if question == 1:
		print(questionRes(l, r, costs_stones))
	else:
		print(questionRes(l, r, costs_stones_ord))


from collections import deque
n = int(input())
arr = list(map(int, input().split()))
prs1 = [0] * (n + 1)
prs1[1] = arr[0]
for i in range(2, n + 1):
	prs1[i] = prs1[i - 1] + arr[i - 1]
l2 = sorted(arr)
prs2 = [0] * (n + 1)
prs2[1] = l2[0]
for i in range(2, n + 1):
	prs2[i] = prs2[i - 1] + l2[i - 1]
q = int(input())
ans = deque()
for i in range(q):
	(x, y, z) = map(int, input().split())
	if x == 1:
		if y == 1:
			ans.append(prs1[z])
		else:
			ans.append(prs1[z] - prs1[y - 1])
	elif y == 1:
		ans.append(prs2[z])
	else:
		ans.append(prs2[z] - prs2[y - 1])
for i in ans:
	print(i)


from collections import defaultdict
n = int(input())
v1 = list(map(int, input().split()))
v2 = sorted(v1)
sum_v = sum(v1)
(l1, r1) = ([0], [sum_v - v1[0]])
(l2, r2) = ([0], [sum_v - v2[0]])
for i in range(1, n):
	l1.append(l1[i - 1] + v1[i - 1])
	l2.append(l2[i - 1] + v2[i - 1])
	r1.append(r1[i - 1] - v1[i])
	r2.append(r2[i - 1] - v2[i])
m = int(input())
for _ in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(sum_v - l1[l - 1] - r1[r - 1])
	else:
		print(sum_v - l2[l - 1] - r2[r - 1])


import sys
import math

def process(a, b, c, suma, sumb):
	if a is 1:
		return suma[c] - suma[b - 1]
	else:
		return sumb[c] - sumb[b - 1]
n = int(input())
lista = [int(x) for x in input().strip().split()]
listb = sorted(lista)
suma = [0 for i in range(len(lista) + 1)]
sumb = [0 for i in range(len(lista) + 1)]
for i in range(1, len(lista) + 1):
	suma[i] = suma[i - 1] + lista[i - 1]
	sumb[i] = sumb[i - 1] + listb[i - 1]
t = int(input())
for _ in range(t):
	(a, b, c) = map(int, input().split())
	print(process(a, b, c, suma, sumb))


def stone_costs(n, v, m):
	v_sort = sorted(v)
	ans = []
	for i in range(1, n):
		v[i] = v[i - 1] + v[i]
		v_sort[i] = v_sort[i - 1] + v_sort[i]
	for j in range(m):
		(t, l, r) = map(int, input('').split(' '))
		l = l - 1
		r = r - 1
		if t == 1:
			if l == 0:
				ans.append(v[r])
			else:
				ans.append(v[r] - v[l - 1])
		elif l == 0:
			ans.append(v_sort[r])
		else:
			ans.append(v_sort[r] - v_sort[l - 1])
	for t in ans:
		print(t)
n = int(input(''))
v = list(map(int, input('').split(' ')))
m = int(input(''))
stone_costs(n, v, m)


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = 1
if debug:
	test_count = inp()
for _ in range(test_count):
	n = inp()
	a = inlt()
	s = sorted(a)
	one_sum = [0] * (n + 1)
	two_sum = [0] * (n + 1)
	for i in range(1, n + 1):
		one_sum[i] = one_sum[i - 1] + a[i - 1]
		two_sum[i] = two_sum[i - 1] + s[i - 1]
	m = inp()
	for _ in range(m):
		(type, left, right) = invr()
		if type == 1:
			print(one_sum[right] - one_sum[left - 1])
		else:
			print(two_sum[right] - two_sum[left - 1])


from sys import stdin, stdout
n = int(stdin.readline())
a = [int(s) for s in stdin.readline().split()]
b = [s for s in a]
b.sort()
for i in range(1, n):
	a[i] += a[i - 1]
	b[i] += b[i - 1]
q = int(stdin.readline())
for i in range(q):
	(t, l, r) = tuple(map(int, stdin.readline().split()))
	if t == 1:
		if l == 1:
			stdout.write(str(a[r - 1]) + '\n')
		else:
			stdout.write(str(a[r - 1] - a[l - 2]) + '\n')
	elif l == 1:
		stdout.write(str(b[r - 1]) + '\n')
	else:
		stdout.write(str(b[r - 1] - b[l - 2]) + '\n')


n = int(input())
v = list(map(int, input().split(' ')))
u = list(v)
u.sort()
(sv, su) = ([v[0]], [u[0]])
for i in range(1, n):
	sv.append(v[i] + sv[i - 1])
	su.append(u[i] + su[i - 1])
sv.append(0)
su.append(0)
m = int(input())
for i in range(m):
	[t, l, r] = list(map(int, input().split(' ')))
	(l, r) = (l - 1, r - 1)
	if t == 1:
		print(sv[r] - sv[l - 1])
	else:
		print(su[r] - su[l - 1])


n = int(input())
lst = list(map(int, input().strip().split(' ')))
l2 = lst[:]
l2.sort()
s = 0
s1 = []
s2 = []
for i in range(n):
	s += lst[i]
	s1.append(s)
s = 0
for i in range(n):
	s += l2[i]
	s2.append(s)
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().strip().split(' '))
	if t == 1:
		if l != 1:
			print(s1[r - 1] - s1[l - 2])
		else:
			print(s1[r - 1])
	elif t == 2:
		if l != 1:
			print(s2[r - 1] - s2[l - 2])
		else:
			print(s2[r - 1])


from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
import math

def sp():
	return map(int, input().split())

def fun(x):
	return sum((int(i) for i in str(x)))

def find(n):
	if n == 3:
		return 3
	while 1:
		flag = 0
		for i in range(2, int(sqrt(n)) + 1):
			if n % i == 0:
				n += 1
				break
			if i == int(sqrt(n)):
				flag = 1
				break
		if flag:
			break
	return n
t = 1
for _ in range(t):
	n = int(input())
	l = list(sp())
	d = []
	for i in range(n):
		d.append(l[i])
	d.sort()
	for i in range(1, n):
		d[i] += d[i - 1]
		l[i] += l[i - 1]
	q = int(input())
	while q:
		q -= 1
		(a, b, c) = sp()
		if b == 1:
			print(l[c - 1]) if a == 1 else print(d[c - 1])
		elif a == 1:
			print(l[c - 1] - l[max(0, b - 2)])
		else:
			print(d[c - 1] - d[max(0, b - 2)])


import sys
input = sys.stdin.readline
n = int(input().strip())
u = list(map(int, input().strip().split()))
v = sorted(u)
u_pref = [0] * (n + 1)
v_pref = [0] * (n + 1)
for i in range(1, n + 1):
	u_pref[i] = u_pref[i - 1] + u[i - 1]
	v_pref[i] = v_pref[i - 1] + v[i - 1]
q = int(input().strip())
for i in range(q):
	(t, l, r) = map(int, input().strip().split())
	if t == 1:
		print(u_pref[r] - u_pref[l - 1])
	else:
		print(v_pref[r] - v_pref[l - 1])


n = int(input())
ar = list(map(int, input().split()))
y = ar[:]
z = ar[:]
y.insert(0, 0)
z.insert(0, 0)
y.sort()
for i in range(2, n + 1):
	z[i] += z[i - 1]
	y[i] += y[i - 1]
q = int(input())
for _ in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(z[r] - z[l - 1])
	else:
		print(y[r] - y[l - 1])


n = int(input())
v = list(map(int, input().split()))
w = v.copy()
w.sort()
(vp, wp) = ([0], [0])
for i in range(n):
	vp.append(vp[i] + v[i])
	wp.append(wp[i] + w[i])
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(vp[r] - vp[l - 1])
	else:
		print(wp[r] - wp[l - 1])


num = int(input())
arr1 = list(map(int, input().split()))
arr2 = sorted(arr1)
for i in range(1, num):
	arr1[i] += arr1[i - 1]
	arr2[i] += arr2[i - 1]
for test_var in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			print(arr1[r - 1] - 0)
		else:
			print(arr1[r - 1] - arr1[l - 2])
	elif l == 1:
		print(arr2[r - 1] - 0)
	else:
		print(arr2[r - 1] - arr2[l - 2])


n = int(input())
stones = list(map(int, input().split()))
t = int(input())
sorted_stones = stones.copy()
sorted_stones.sort()
dp = [0] * (n + 1)
sdp = [0] * (n + 1)
dp[0] = stones[0]
sdp[0] = sorted_stones[0]
for i in range(1, n + 1):
	dp[i] = dp[i - 1] + stones[i - 1]
	sdp[i] = sdp[i - 1] + sorted_stones[i - 1]
for q in range(t):
	(qt, l, r) = map(int, input().split())
	if qt == 1:
		print(dp[r] - dp[l - 1])
	else:
		print(sdp[r] - sdp[l - 1])


def my_function(vector):
	accumulatedArray = [vector[0]]
	for i in range(1, len(vector), 1):
		accumulatedArray.append(vector[i] + accumulatedArray[i - 1])
	return accumulatedArray
n = input()
vector = list(map(int, input().split()))
sortedVector = sorted(vector)
accumulatedVector = my_function(vector)
accumulatedSortedVector = my_function(sortedVector)
repetitions = int(input())
while repetitions > 0:
	order = list(map(int, input().split()))
	if order[0] == 1:
		if order[1] == 1:
			print(accumulatedVector[order[2] - 1])
		else:
			print(accumulatedVector[order[2] - 1] - accumulatedVector[order[1] - 2])
	elif order[0] == 2:
		if order[1] == 1:
			print(accumulatedSortedVector[order[2] - 1])
		else:
			print(accumulatedSortedVector[order[2] - 1] - accumulatedSortedVector[order[1] - 2])
	repetitions -= 1


def lista_acumulada(lista):
	lista[0] = int(lista[0])
	i = 1
	while i < len(lista):
		lista[i] = int(lista[i]) + int(lista[i - 1])
		i += 1
	return lista
n = int(input())
entrada = input()
m = int(input())
questoes = []
res = []
i = 0
while i < m:
	questoes.append(input().split())
	i += 1
acumulada1 = lista_acumulada(entrada.split())
acumulada2 = lista_acumulada(sorted(list(map(int, entrada.split()))))
for e in questoes:
	l = int(e[1])
	r = int(e[2])
	if e[0] == '1':
		if l == 1:
			res.append(acumulada1[r - 1])
		else:
			res.append(acumulada1[r - 1] - acumulada1[l - 2])
	elif l == 1:
		res.append(acumulada2[r - 1])
	else:
		res.append(acumulada2[r - 1] - acumulada2[l - 2])
for e in res:
	print(e)


def prog():
	from sys import stdin
	n = int(stdin.readline())
	mas = list(map(int, stdin.readline().split()))
	lst = list(sorted(mas))
	(a, b) = (0, 0)
	(d1, d2) = ([], [])
	for i in range(n):
		a += mas[i]
		b += lst[i]
		d1.append(a)
		d2.append(b)
	m = int(stdin.readline())
	for i in range(m):
		(q, l, r) = map(int, stdin.readline().split())
		if q == 1:
			if l == 1:
				print(d1[r - 1])
			else:
				print(d1[r - 1] - d1[l - 2])
		elif l == 1:
			print(d2[r - 1])
		else:
			print(d2[r - 1] - d2[l - 2])
prog()


import math
t = 1
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	m = int(input())
	v = [0 for i in range(n + 1)]
	u = [0 for i in range(n + 1)]
	for i in range(1, n + 1):
		v[i] = v[i - 1] + A[i - 1]
	A.sort()
	for i in range(1, n + 1):
		u[i] = u[i - 1] + A[i - 1]
	for i in range(m):
		(ty, l, r) = map(int, input().split())
		if ty == 1:
			print(v[r] - v[l - 1])
		else:
			print(u[r] - u[l - 1])


n = int(input())
prices = list(map(int, input().split()))
sortedPrices = sorted(prices)
sum = 0
sortedSum = 0
pricesSum = list()
sortedPricesSum = list()
for i in range(n):
	sum += prices[i]
	pricesSum.append(sum)
	sortedSum += sortedPrices[i]
	sortedPricesSum.append(sortedSum)
pricesSum.append(0)
sortedPricesSum.append(0)
m = int(input())
answers = list()
for i in range(m):
	(type, l, r) = list(map(int, input().split()))
	answers.append(pricesSum[r - 1] - pricesSum[l - 2] if type == 1 else sortedPricesSum[r - 1] - sortedPricesSum[l - 2])
for i in range(m):
	print(answers[i])


n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = sorted(a)
c = []
d = []
(e, f) = (0, 0)
for i in range(n):
	c.append(e)
	d.append(f)
	e += a[i]
	f += b[i]
c.append(e)
d.append(f)
for i in range(m):
	(x, y, z) = list(map(int, input().split()))
	if x == 1:
		print(c[z] - c[y - 1])
	else:
		print(d[z] - d[y - 1])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
x = y = 0
m = [0]
l = [0]
for i in range(n):
	x += a[i]
	y += b[i]
	m.append(x)
	l.append(y)
k = int(input())
for i in range(k):
	c = list(map(int, input().split()))
	if c[0] == 1:
		s = m[c[2]] - m[c[1] - 1]
		print(s)
	else:
		s = l[c[2]] - l[c[1] - 1]
		print(s)


n = int(input())
v = list(map(int, input().split()))
sorted_v = sorted(v)
prefix_v = [0]
sorted_prefix = [0]
sum_v = 0
sums = 0
for x in v:
	sums += x
	prefix_v.append(sums)
for x in sorted_v:
	sum_v += x
	sorted_prefix.append(sum_v)
for _ in range(int(input())):
	question = list(map(int, input().split()))
	q = question[0]
	if q == 2:
		point1 = question[1] - 1
		point2 = question[2]
		sums = 0
		print(sorted_prefix[point2] - sorted_prefix[point1])
	else:
		point1 = question[1] - 1
		point2 = question[2]
		print(prefix_v[point2] - prefix_v[point1])


n = int(input())
q = [0]
q.extend(list(map(int, input().split())))
a = sorted(q)
for i in range(1, n + 1):
	a[i] += a[i - 1]
	q[i] += q[i - 1]
for i in range(int(input())):
	(o, l, r) = map(int, input().split())
	if o == 1:
		print(q[r] - q[l - 1])
	else:
		print(a[r] - a[l - 1])


stones_num = int(input())
stone_prices = list(map(int, input().split()))

def get_sums(nums_list):
	nums_list = [0] + nums_list
	for index in range(1, len(nums_list)):
		nums_list[index] += nums_list[index - 1]
	return nums_list[1:]
price_sums = [0] + get_sums(stone_prices)
sorted_prices = list(sorted(stone_prices))
sorted_prices_sums = [0] + get_sums(sorted_prices)
n = int(input())
for _ in range(n):
	(task_id, k, l) = list(map(int, input().split()))
	if task_id == 1:
		print(price_sums[l] - price_sums[k - 1])
	else:
		print(sorted_prices_sums[l] - sorted_prices_sums[k - 1])


n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = [a[i] for i in range(n)]
b.sort()
cummulative_sum_a = [0 for i in range(n)]
cummulative_sum_b = [0 for i in range(n)]
for i in range(n):
	if i == 0:
		cummulative_sum_a[i] = a[i]
	else:
		cummulative_sum_a[i] = a[i] + cummulative_sum_a[i - 1]
for i in range(n):
	if i == 0:
		cummulative_sum_b[i] = b[i]
	else:
		cummulative_sum_b[i] = b[i] + cummulative_sum_b[i - 1]
while m:
	(type_, l, r) = map(int, input().split())
	if type_ == 1:
		l -= 1
		r -= 1
		if l == 0:
			print(cummulative_sum_a[r])
		else:
			print(cummulative_sum_a[r] - cummulative_sum_a[l - 1])
	else:
		l -= 1
		r -= 1
		if l == 0:
			print(cummulative_sum_b[r])
		else:
			print(cummulative_sum_b[r] - cummulative_sum_b[l - 1])
	m -= 1


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	n = I()
	lis = [0] + L()
	q = I()
	arr = sorted(lis)
	for i in range(2, n + 1):
		arr[i] += arr[i - 1]
		lis[i] += lis[i - 1]
	for i in range(q):
		(a, l, r) = In()
		if a == 1:
			print(lis[r] - lis[l - 1])
		else:
			print(arr[r] - arr[l - 1])
main()


n = int(input())
cost = list(map(int, input().split(' ')))
cost_sorted = sorted(cost)
m = int(input())
dp = [0] * (n + 1)
dp_sorted = [0] * (n + 1)
for i in range(len(cost)):
	dp[i + 1] = dp[i] + cost[i]
	dp_sorted[i + 1] = dp_sorted[i] + cost_sorted[i]
for _ in range(m):
	(t, l, r) = list(map(int, input().split(' ')))
	if t == 1:
		print(dp[r] - dp[l - 1])
	elif t == 2:
		print(dp_sorted[r] - dp_sorted[l - 1])


import math

def change_char(s, p, r):
	return s[:p] + r + s[p + 1:]
size = int(input())
arr = list(map(int, input().split()))
freq = [arr[0]]
i = 0
for element in arr[1:]:
	freq.append(element + freq[i])
	i += 1
arr.sort()
i = 0
freq_sorted = [arr[0]]
for element in arr[1:]:
	freq_sorted.append(element + freq_sorted[i])
	i += 1
test = int(input())
while test > 0:
	positions = list(map(int, input().split()))
	right = positions[2] - 1
	left = positions[1] - 2
	ans = 0
	if positions[0] == 1:
		ans = freq[right] if left == -1 else freq[right] - freq[left]
	else:
		ans = freq_sorted[right] if left == -1 else freq_sorted[right] - freq_sorted[left]
	print(ans)
	test -= 1


n = int(input())
price = list(map(int, input().split()))
price_s = list(price)
price_s.sort()
m = int(input())
pre_sum = [price[0]]
pre_sum1 = [price_s[0]]
out = []
for i in range(1, n, 1):
	ele = price[i] + pre_sum[i - 1]
	pre_sum.append(ele)
	ele1 = price_s[i] + pre_sum1[i - 1]
	pre_sum1.append(ele1)
for i in range(m):
	full = False
	curr = list(map(int, input().split()))
	ind1 = curr[2] - 1
	ind2 = curr[1] - 2
	if ind2 < 0:
		full = True
	if curr[0] == 1:
		if full:
			out.append(pre_sum[ind1])
		else:
			lis1 = pre_sum[ind1]
			lis2 = pre_sum[ind2]
			out.append(lis1 - lis2)
	elif full:
		out.append(pre_sum1[ind1])
	else:
		lis1 = pre_sum1[ind1]
		lis2 = pre_sum1[ind2]
		out.append(lis1 - lis2)
for i in range(m):
	print(out[i])


n = int(input())
stones = list(map(int, input().split()))
first_sum = [0]
second_sum = [0]
i = 0
for (s1, s2) in zip(stones, sorted(stones)):
	first_sum.append(first_sum[i] + s1)
	second_sum.append(second_sum[i] + s2)
	i += 1
m = int(input())
for i in range(m):
	(q, left, right) = map(int, input().split())
	if q == 1:
		print(first_sum[right] - first_sum[left - 1])
	elif q == 2:
		print(second_sum[right] - second_sum[left - 1])


n = int(input())
l = list(map(int, input().split()))
m = l[:]
l.sort()
k = int(input())
for i in range(n - 1):
	l[i + 1] += l[i]
	m[i + 1] += m[i]
m.insert(0, 0)
l.insert(0, 0)
for _ in range(k):
	(t, a, b) = map(int, input().split())
	if t == 1:
		print(m[b] - m[a - 1])
	else:
		print(l[b] - l[a - 1])


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
l = [0] + I()
a = l[:]
a.sort()
for i in range(1, n + 1):
	a[i] += a[i - 1]
for i in range(1, n + 1):
	l[i] += l[i - 1]
(m,) = I()
for i in range(m):
	(x, ll, r) = I()
	if x == 1:
		print(l[r] - l[ll - 1])
	else:
		print(a[r] - a[ll - 1])


from itertools import accumulate
n = int(input())
s = [0] + list(map(int, input().split()))
k = list(accumulate(s))
l = sorted(s)
j = list(accumulate(l))
x = int(input())
for i in range(x):
	p = input().split()
	if p[0] == '1':
		print(k[int(p[2])] - k[int(p[1]) - 1])
	else:
		print(j[int(p[2])] - j[int(p[1]) - 1])


from sys import stdin
n = int(stdin.readline().rstrip())
a = list(map(int, stdin.readline().split()))
b = sorted(a)
prefix_a = [0] * (n + 1)
prefix_b = [0] * (n + 1)
prefix_a[1] = a[0]
prefix_b[1] = b[0]
for i in range(2, n + 1):
	prefix_a[i] = a[i - 1] + prefix_a[i - 1]
	prefix_b[i] = b[i - 1] + prefix_b[i - 1]
m = int(stdin.readline().rstrip())
for _ in range(m):
	(m, l, r) = list(map(int, stdin.readline().split()))
	if m == 1:
		print(prefix_a[r] - prefix_a[l - 1])
	else:
		print(prefix_b[r] - prefix_b[l - 1])


n = input()
a = list(map(int, input().split()))
b = sorted(a)
(v, u, ans) = ([0], [0], 0)
for i in a:
	ans += i
	v.append(ans)
ans = 0
for i in b:
	ans += i
	u.append(ans)
for _ in range(int(input())):
	(q, w, e) = map(int, input().split())
	if q == 1:
		print(v[e] - v[w - 1])
	else:
		print(u[e] - u[w - 1])


try:
	N = int(input())
	V = list(map(int, input().split()))
	P = [0] * (N + 1)
	for i in range(1, N + 1):
		P[i] = V[i - 1] + P[i - 1]
	V.sort()
	F = [0] * (N + 1)
	for i in range(1, N + 1):
		F[i] = V[i - 1] + F[i - 1]
	M = int(input())
	for _ in range(M):
		(t, l, r) = map(int, input().split())
		if t == 1:
			print(P[r] - P[l - 1])
		elif t == 2:
			print(F[r] - F[l - 1])
except EOFError:
	pass


n = int(input())
L = list(map(int, input().split()))
L1 = sorted(L)
for k in range(1, n):
	L[k] += L[k - 1]
for k in range(1, n):
	L1[k] += L1[k - 1]
L = [0] + L
L1 = [0] + L1
t = int(input())
for k in range(t):
	(ty, l, r) = map(int, input().split())
	if ty == 1:
		print(L[r] - L[l - 1])
	else:
		print(L1[r] - L1[l - 1])


n = int(input())
n += 1
a = list(map(int, input().split()))
a.insert(0, 0)
b = [i for i in a]
b.sort()
for i in range(1, n):
	a[i] = a[i - 1] + a[i]
	b[i] = b[i - 1] + b[i]
q = int(input())
for _ in range(q):
	(t, l, r) = list(map(int, input().split()))
	if t == 1:
		print(a[r] - a[l - 1])
	else:
		print(b[r] - b[l - 1])


lngth = int(input())
ar = [int(i) for i in str(input()).split(' ')]
sortedar = sorted(ar)

def makePreSum(ar):
	sm = 0
	for (index, element) in enumerate(ar):
		sm += element
		ar[index] = sm
	return ar
ar = makePreSum(ar)
sortedar = makePreSum(sortedar)
query = int(input())
for _ in range(query):
	q = [int(i) for i in str(input()).split(' ')]
	if q[0] == 1:
		if q[1] == 1:
			print(ar[q[2] - 1])
		else:
			print(ar[q[2] - 1] - ar[q[1] - 2])
	elif q[1] == 1:
		print(sortedar[q[2] - 1])
	else:
		print(sortedar[q[2] - 1] - sortedar[q[1] - 2])


n = int(input())
a = [int(x) for x in input().split()]
prefixsum = a[:]
prefixsumsorted = sorted(a[:])
for i in range(1, len(a)):
	prefixsum[i] += prefixsum[i - 1]
	prefixsumsorted[i] += prefixsumsorted[i - 1]
m = int(input())
for _ in range(m):
	(t, l, r) = [int(x) for x in input().split()]
	if t == 1:
		print(prefixsum[r - 1] - (prefixsum[l - 1 - 1] if l != 1 else 0))
	else:
		print(prefixsumsorted[r - 1] - (prefixsumsorted[l - 1 - 1] if l != 1 else 0))


n = int(input())
a = [int(x) for x in input().split(' ')]
b = sorted(a)
c = [] + a
d = [] + b
m = int(input())
for i in range(1, len(a)):
	a[i] += a[i - 1]
	b[i] += b[i - 1]
while m > 0:
	(i, l, r) = input().split(' ')
	(i, l, r) = (int(i), int(l), int(r))
	if i == 1:
		print(a[r - 1] - a[l - 1] + c[l - 1])
	else:
		print(b[r - 1] - b[l - 1] + d[l - 1])
	m -= 1


N = int(input())
A = list(map(int, input().split()))
B = [0] * N
S = 0
for I in range(N):
	S += A[I]
	B[I] = S
S = 0
A.sort()
C = [0] * N
for I in range(N):
	S += A[I]
	C[I] = S
M = int(input())
while M:
	(X, L, R) = map(int, input().split())
	L -= 1
	R -= 1
	if X == 1:
		if L:
			print(B[R] - B[L - 1])
		else:
			print(B[R])
	elif L:
		print(C[R] - C[L - 1])
	else:
		print(C[R])
	M -= 1


n = int(input())
v = list(map(int, input().split()))
l1 = [0] * (n + 1)
l2 = [0] * (n + 1)
l1[1] = v[0]
for i in range(2, n + 1):
	l1[i] = l1[i - 1] + v[i - 1]
v.sort()
l2[1] = v[0]
for i in range(2, n + 1):
	l2[i] = l2[i - 1] + v[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(l1[r] - l1[l - 1])
	else:
		print(l2[r] - l2[l - 1])


def prefixSum(lst):
	a = [0] + lst
	s = [0] * len(a)
	(s[0], s[1]) = (0, a[1])
	for i in range(1, len(a)):
		s[i] = s[i - 1] + a[i]
	return s
n = int(input())
lst = list(map(int, input().split()))
arrlst = sorted(lst)
lst = prefixSum(lst)
arrlst = prefixSum(arrlst)
m = int(input())
for _ in range(m):
	q = list(map(int, input().split()))
	if q[0] == 1:
		print(lst[q[2]] - lst[q[1] - 1])
	else:
		print(arrlst[q[2]] - arrlst[q[1] - 1])


n = int(input())
V1 = list(map(int, input().split()))
V2 = sorted(V1)
for i in range(1, n):
	V1[i] = V1[i] + V1[i - 1]
for i in range(1, n):
	V2[i] = V2[i] + V2[i - 1]
V1.insert(0, 0)
V2.insert(0, 0)
t = int(input())
for i in range(t):
	(type, l, r) = map(int, input().split())
	if type == 1:
		print(V1[r] - V1[l - 1])
	else:
		print(V2[r] - V2[l - 1])


stones_num = int(input())
stone_prices = list(map(int, input().split()))
sums = [0] + stone_prices
for k in range(1, stones_num + 1):
	sums[k] += sums[k - 1]
sorted_prices = list(sorted(stone_prices))
sorted_prices_sum = [0] + sorted_prices
for k in range(1, stones_num + 1):
	sorted_prices_sum[k] += sorted_prices_sum[k - 1]
n = int(input())

def first_task(k, l):
	return sums[l] - sums[k - 1]

def second_task(k, l):
	return sorted_prices_sum[l] - sorted_prices_sum[k - 1]
for _ in range(n):
	(task_id, l, k) = list(map(int, input().split()))
	if task_id == 1:
		print(first_task(l, k))
	else:
		print(second_task(l, k))


n = int(input())
list1 = [0] + list(map(int, input().split()))
list2 = sorted(list1)
sum1 = 0
c = 0
for i in range(1, n + 1):
	list1[i] = list1[i - 1] + list1[i]
	list2[i] = list2[i - 1] + list2[i]
for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	if n == 1:
		print(list1[y] - list1[x - 1])
	else:
		print(list2[y] - list2[x - 1])


N = int(input())
valus = [int(n) for n in input().split()][0:N]
line = int(input())
cum = []
S_cum = []
for i in range(N):
	cum += [0]
	if i == 0:
		cum[i] = valus[i]
	else:
		cum[i] = cum[i - 1] + valus[i]
valus.sort()
for i in range(N):
	S_cum += [0]
	if i == 0:
		S_cum[i] = valus[i]
	else:
		S_cum[i] = S_cum[i - 1] + valus[i]
for i in range(line):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			print(cum[r - 1])
		else:
			print(cum[r - 1] - cum[l - 2])
	elif l == 1:
		print(S_cum[r - 1])
	else:
		print(S_cum[r - 1] - S_cum[l - 2])


def to_list(s):
	return list(map(lambda x: int(x), s.split(' ')))

def get_dicts(a):
	sorted_a = sorted(a)
	prefix_dict = {0: 0}
	sorted_prefix_dict = {0: 0}
	sum_ = 0
	sorted_sum = 0
	for (i, item) in enumerate(a):
		sum_ += item
		prefix_dict[i + 1] = sum_
	for (j, item) in enumerate(sorted_a):
		sorted_sum += item
		sorted_prefix_dict[j + 1] = sorted_sum
	return (prefix_dict, sorted_prefix_dict)

def solve(prefix_dict, sorted_prefix_dict, type_, l, r):
	if type_ == 2:
		return str(sorted_prefix_dict[r] - sorted_prefix_dict[l - 1])
	else:
		return str(prefix_dict[r] - prefix_dict[l - 1])
answers = []
n = int(input())
a = to_list(input())
m = int(input())
(prefix_dict, sorted_prefix_dict) = get_dicts(a)
for i in range(m):
	(type_, l, r) = to_list(input())
	answers.append(solve(prefix_dict, sorted_prefix_dict, type_, l, r))
print('\n'.join(answers))


n = int(input())
dp_a = [-1 for _ in range(n)]
dp_b = [-1 for _ in range(n)]

def dp_sum_a(a):
	dp_a[0] = a[0]
	for i in range(1, n):
		dp_a[i] = dp_a[i - 1] + a[i]

def dp_sum_b(b):
	dp_b[0] = b[0]
	for i in range(1, n):
		dp_b[i] = dp_b[i - 1] + b[i]
a = list(map(int, input().split()))
b = [x for x in a]
b.sort()
dp_sum_a(a)
dp_sum_b(b)
q = int(input())
for i in range(q):
	(t, l, r) = map(int, input().split())
	if t == 1:
		(l, r) = (l - 1, r - 1)
		x = dp_a[r] - dp_a[l] + a[l]
		print(x)
	if t == 2:
		(l, r) = (l - 1, r - 1)
		x = dp_b[r] - dp_b[l] + b[l]
		print(x)


import sys
stones = int(sys.stdin.readline())
arrayofStones = [int(x) for x in sys.stdin.readline().split()]
questions = int(sys.stdin.readline())
cumulative = arrayofStones.copy()
sortedCumulative = sorted(arrayofStones)
for i in range(1, stones):
	cumulative[i] += cumulative[i - 1]
for i in range(1, stones):
	sortedCumulative[i] += sortedCumulative[i - 1]
for question in range(questions):
	(questionType, left, right) = map(int, sys.stdin.readline().split())
	if questionType == 1:
		ans = cumulative[right - 1]
		if left > 1:
			ans -= cumulative[left - 2]
	else:
		ans = sortedCumulative[right - 1]
		if left > 1:
			ans -= sortedCumulative[left - 2]
	sys.stdout.write(str(ans) + '\n')


def main():
	n = int(input())
	v = [int(i) for i in input().split()]
	prefix1 = []
	sm = 0
	for i in range(n):
		sm += v[i]
		prefix1.append(sm)
	prefix2 = []
	sm = 0
	for i in sorted(v):
		sm += i
		prefix2.append(sm)
	m = int(input())
	for i in range(m):
		(t, l, r) = map(int, input().split())
		if t == 1:
			if l == 1:
				print(prefix1[r - 1])
			else:
				print(prefix1[r - 1] - prefix1[l - 2])
		elif l == 1:
			print(prefix2[r - 1])
		else:
			print(prefix2[r - 1] - prefix2[l - 2])
main()


n = int(input())
arr = list(map(int, input().split(' ')))
arr1 = arr[:]
arr.sort()
f = [0] + [0] * n
g = [0] + [0] * n
for i in range(1, n + 1):
	f[i] = f[i - 1] + arr1[i - 1]
	g[i] = g[i - 1] + arr[i - 1]
b = int(input())
for i in range(b):
	(x, y, z) = map(int, input().split(' '))
	if x == 1:
		print(f[z] - f[y - 1])
	elif x == 2:
		print(g[z] - g[y - 1])


def stoneenig(n, v):
	precos = [0] * (n + 1)
	for i in range(1, n + 1):
		precos[i] = v[i - 1] + precos[i - 1]
	return precos
n = int(input())
v = list(map(int, input().split(' ')))
vbkp = sorted(v)
m = int(input())
s = stoneenig(n, v)
sord = stoneenig(n, vbkp)
for e in range(m):
	(q, l, r) = map(int, input().split(' '))
	if q == 1:
		print(s[r] - s[l - 1])
	else:
		print(sord[r] - sord[l - 1])


n = int(input())
data1 = list(map(int, input().split()))
x = 0
data2 = sorted(data1[:])
for i in range(1, n):
	data1[i] = data1[i] + data1[i - 1]
for i in range(1, n):
	data2[i] = data2[i] + data2[i - 1]
data1 = [0] + data1
data2 = [0] + data2
k = int(input())
while x < k:
	(a, b, c) = map(int, input().split())
	if a is 1:
		print(data1[c] - data1[b - 1])
	else:
		print(data2[c] - data2[b - 1])
	x += 1


import sys
import math
import collections

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
n = int(input())
arr = get_list()
order = arr.copy()
order.sort()
val = 0
prefix_val = val
prefix = [val]
sorted_prefix = [val]
for i in range(n):
	prefix.append(val + arr[i])
	val += arr[i]
	sorted_prefix.append(prefix_val + order[i])
	prefix_val += order[i]
for i in range(int(input())):
	(m, l, r) = get_ints()
	if m == 1:
		print(prefix[r] - prefix[l - 1])
	else:
		print(sorted_prefix[r] - sorted_prefix[l - 1])


n = int(input())
v = list(map(int, input().split()))
m = int(input())
Q = []
for i in range(m):
	Q.append(list(map(int, input().split())))
u = v[:]
u.sort()
P_v = (n + 1) * [0]
P_u = (n + 1) * [0]
for i in range(1, n + 1):
	P_v[i] = P_v[i - 1] + v[i - 1]
	P_u[i] = P_u[i - 1] + u[i - 1]
for i in range(m):
	if Q[i][0] == 1:
		print(P_v[Q[i][2]] - P_v[Q[i][1] - 1])
	else:
		print(P_u[Q[i][2]] - P_u[Q[i][1] - 1])


N = int(input())
X = list(map(int, input().split()))
SortedX = sorted(X)
(DPNormal, DPSorted) = ([0], [0])
for i in range(N):
	DPNormal.append(DPNormal[i] + X[i])
	DPSorted.append(DPSorted[i] + SortedX[i])
for i in range(int(input())):
	Temp = list(map(int, input().split()))
	if Temp[0] == 1:
		print(DPNormal[Temp[2]] - DPNormal[Temp[1] - 1])
	else:
		print(DPSorted[Temp[2]] - DPSorted[Temp[1] - 1])


from sys import stdin
_input = stdin.readline
(_int, _range) = (int, range)

def solution():
	n = _int(_input())
	arr = [_int(i) for i in _input().split()]
	ans = [0] * n
	ans[0] = arr[0]
	for i in _range(1, n):
		ans[i] = ans[i - 1] + arr[i]
	arr.sort()
	answer = [0] * n
	answer[0] = arr[0]
	for i in _range(1, n):
		answer[i] = answer[i - 1] + arr[i]
	q = _int(_input())
	for i in _range(q):
		(z, l, r) = [_int(i) for i in _input().split()]
		(l, r) = (l - 1, r - 1)
		if z == 1:
			if l != 0:
				print(ans[r] - ans[l - 1])
			else:
				print(ans[r])
		if z == 2:
			if l != 0:
				print(answer[r] - answer[l - 1])
			else:
				print(answer[r])
solution()


n = int(input())
x = list(map(int, input().split()))
y = x.copy()
y.sort()
abc = []
total_sum = [0]
total_sum_b = [0]
a = 0
b = 0
for _ in range(n):
	a += x[_]
	total_sum.append(a)
	b += y[_]
	total_sum_b.append(b)
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(total_sum[r] - total_sum[l - 1])
	else:
		print(total_sum_b[r] - total_sum_b[l - 1])


n = int(input())
k = list(map(int, input().split()))
a = [0] + k
for i in range(1, n + 1):
	a[i] += a[i - 1]
k.sort()
k = [0] + k
for i in range(1, n + 1):
	k[i] += k[i - 1]
for _ in range(int(input())):
	(typa, l, r) = map(int, input().split())
	if typa == 1:
		print(a[r] - a[l - 1])
	else:
		print(k[r] - k[l - 1])


n = int(input())
a = list(map(int, input().split()))
s = [0] * n
s[0] = a[0]
for i in range(1, n):
	s[i] = a[i]
	a[i] += a[i - 1]
s.sort()
for i in range(1, n):
	s[i] += s[i - 1]
h = int(input())
for i in range(h):
	(x, y, z) = map(int, input().split())
	if x == 1:
		if y > 1:
			print(a[z - 1] - a[y - 2])
		else:
			print(a[z - 1])
	elif y > 1:
		print(s[z - 1] - s[y - 2])
	else:
		print(s[z - 1])


def calculate_stones_value(num_of_elements, elements, num_of_test_cases, test_cases):
	arrenged_elements = sorted(elements)
	sum_elements = [0] * num_of_elements
	sum_arrenged_elements = [0] * num_of_elements
	for i in range(-1, -num_of_elements - 1, -1):
		if i == -1:
			sum_elements[i] = elements[-1]
			sum_arrenged_elements[i] = arrenged_elements[-1]
			continue
		sum_elements[i] = elements[i] + sum_elements[i + 1]
		sum_arrenged_elements[i] = arrenged_elements[i] + sum_arrenged_elements[i + 1]
	for i in range(num_of_test_cases):
		result = 0
		question = test_cases[i][0]
		initial = test_cases[i][1]
		final = test_cases[i][2]
		if question == 1:
			if final == num_of_elements:
				result = sum_elements[initial - 1]
			else:
				result = sum_elements[initial - 1] - sum_elements[final]
		elif final == num_of_elements:
			result = sum_arrenged_elements[initial - 1]
		else:
			result = sum_arrenged_elements[initial - 1] - sum_arrenged_elements[final]
		print(result)
num_of_elements = int(input())
elements = input().strip().split()
elements = [int(i) for i in elements]
num_of_test_cases = int(input())
test_cases = []
for i in range(num_of_test_cases):
	test_cases.append(input().strip().split())
	test_cases[i] = [int(j) for j in test_cases[i]]
calculate_stones_value(num_of_elements, elements, num_of_test_cases, test_cases)


from functools import reduce
from collections import Counter
import time
import datetime
from math import sqrt, gcd

def time_t():
	print('Current date and time: ', datetime.datetime.now())
	print('Current year: ', datetime.date.today().strftime('%Y'))
	print('Month of year: ', datetime.date.today().strftime('%B'))
	print('Week number of the year: ', datetime.date.today().strftime('%W'))
	print('Weekday of the week: ', datetime.date.today().strftime('%w'))
	print('Day of year: ', datetime.date.today().strftime('%j'))
	print('Day of the month : ', datetime.date.today().strftime('%d'))
	print('Day of week: ', datetime.date.today().strftime('%A'))

def ip():
	return int(input())

def sip():
	return input()

def mip():
	return map(int, input().split())

def mips():
	return map(str, input().split())

def lip():
	return list(map(int, input().split()))

def matip(n, m):
	lst = []
	for i in range(n):
		arr = lip()
		lst.append(arr)
	return lst

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def minJumps(arr, n):
	jumps = [0 for i in range(n)]
	if n == 0 or arr[0] == 0:
		return float('inf')
	jumps[0] = 0
	for i in range(1, n):
		jumps[i] = float('inf')
		for j in range(i):
			if i <= j + arr[j] and jumps[j] != float('inf'):
				jumps[i] = min(jumps[i], jumps[j] + 1)
				break
	return jumps[n - 1]

def dic(arr):
	return Counter(arr)

def prime(n):
	for i in range(3, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True
n = ip()
arr = lip()
lst = sorted(arr)
for i in range(1, n):
	arr[i] = arr[i] + arr[i - 1]
	lst[i] = lst[i] + lst[i - 1]
m = ip()
arr.insert(0, 0)
lst.insert(0, 0)
while m:
	m -= 1
	(t, l, r) = mip()
	if t == 1:
		print(arr[r] - arr[l - 1])
	else:
		print(lst[r] - lst[l - 1])


n = int(input())
v1 = list(map(int, input().split()))
v2 = sorted(v1)
(v1, v2) = ([0] + v1, [0] + v2)
for i in range(1, n + 1):
	v2[i] += v2[i - 1]
	v1[i] += v1[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(v1[r] - v1[l - 1])
	else:
		print(v2[r] - v2[l - 1])


num_stones = int(input())
stones_costs = list(map(int, input().split()))
stones_sums = [0, stones_costs[0]]
for pos in range(1, num_stones):
	stones_sums.append(stones_sums[-1] + stones_costs[pos])
sorted_costs = sorted(stones_costs)
sorted_sums = [0, sorted_costs[0]]
for pos in range(1, num_stones):
	sorted_sums.append(sorted_sums[-1] + sorted_costs[pos])
num_queries = int(input())
for _ in range(num_queries):
	(query_type, l, r) = map(int, input().split())
	if query_type == 1:
		print(stones_sums[r] - stones_sums[l - 1])
	else:
		print(sorted_sums[r] - sorted_sums[l - 1])


from itertools import accumulate
n = int(input())
k = list(map(int, input().split()))
k1 = list(accumulate(k))
s = sorted(k)
s1 = list(accumulate(s))
m = int(input())
for i in range(m):
	(t, p, r) = map(int, input().split())
	if t == 1:
		if p == r:
			print(k[p - 1])
		elif p - 1 == 0:
			print(k1[r - 1])
		else:
			print(k1[r - 1] - k1[p - 2])
	elif p == r:
		print(s[p - 1])
	elif p - 1 == 0:
		print(s1[r - 1])
	else:
		print(s1[r - 1] - s1[p - 2])


numOfstones = int(input())
cost = list(map(int, input().split()))
costCopy = cost[:]
costCopy.sort()
questions = int(input())
pre1 = []
pre2 = []
pre1.append(cost[0])
pre2.append(costCopy[0])
for i in range(1, numOfstones):
	pre1.append(pre1[i - 1] + cost[i])
	pre2.append(pre2[i - 1] + costCopy[i])
for i in range(questions):
	(type, l, r) = map(int, input().split())
	if type == 1:
		if l != 1:
			print(pre1[r - 1] - pre1[l - 2])
		else:
			print(pre1[r - 1])
	if type == 2:
		if l != 1:
			print(pre2[r - 1] - pre2[l - 2])
		else:
			print(pre2[r - 1])


num_de_pedras = int(input())
pedras = list(map(int, input().split()))
pedras_ordenadas = list(pedras)
pedras_ordenadas.sort()
num_de_questoes = int(input())
soma_pedras = [0]
for i in range(1, len(pedras) + 1):
	soma_pedras.append(soma_pedras[i - 1] + pedras[i - 1])
soma_pedras_ordenadas = [0]
for i in range(1, len(pedras_ordenadas) + 1):
	soma_pedras_ordenadas.append(soma_pedras_ordenadas[i - 1] + pedras_ordenadas[i - 1])
for i in range(num_de_questoes):
	(questao, l, r) = list(map(int, input().split()))
	resposta = 0
	if questao == 1:
		resposta = soma_pedras[r] - soma_pedras[l - 1]
	else:
		resposta = soma_pedras_ordenadas[r] - soma_pedras_ordenadas[l - 1]
	print(resposta)


n = int(input())
a = list(map(int, input().rstrip().split()))
q = int(input())
b = []
b = b + a
b.sort()
dp = [0] * n
dp[0] = a[0]
for i in range(1, n):
	dp[i] += dp[i - 1] + a[i]
dp2 = [0] * n
dp2[0] = b[0]
for i in range(1, n):
	dp2[i] += dp2[i - 1] + b[i]
for i in range(q):
	(t, y, u) = list(map(int, input().split()))
	if t == 1:
		print(dp[u - 1] - dp[y - 1] + a[y - 1])
	else:
		print(dp2[u - 1] - dp2[y - 1] + b[y - 1])


def safe(arr, i):
	if i < 0 or i >= len(arr):
		return 0
	return arr[i]
n = int(input())
a = [int(x) for x in input().split(' ')]
b = sorted(a)
s = 0
suma = []
sumb = []
for x in a:
	s += x
	suma.append(s)
s = 0
for x in b:
	s += x
	sumb.append(s)
n = int(input())
for _ in range(n):
	query = [int(x) for x in input().split(' ')]
	if query[0] == 1:
		print(suma[query[2] - 1] - safe(suma, query[1] - 2))
	else:
		print(sumb[query[2] - 1] - safe(sumb, query[1] - 2))


n = int(input())
arr = [int(x) for x in input().split()]
arr_sort = [0]
total = 0
for i in sorted(arr):
	total += i
	arr_sort.append(total)
total = 0
equal = [0]
for i in arr:
	total += i
	equal.append(total)
m = int(input())
for i in range(m):
	(c, i, j) = [int(x) for x in input().split()]
	if c == 1:
		print(equal[j] - equal[i - 1])
	else:
		print(arr_sort[j] - arr_sort[i - 1])


n = int(input())
L = list(map(int, input().split()))
D = sorted(L)
k = int(input())
S = [0] * 100100
S1 = [0] * 100100
for i in range(1, n + 1):
	S[i] = S[i - 1] + L[i - 1]
	S1[i] = S1[i - 1] + D[i - 1]
for i in range(k):
	(q, l, r) = map(int, input().strip().split())
	if q == 1:
		s = S[r] - S[l - 1]
		print(s)
	else:
		s = S1[r] - S1[l - 1]
		print(s)


n = int(input())
arr1 = list(map(int, input().split()))
k = int(input())
arr2 = sorted(arr1)
arr1 = [0] + arr1
arr2 = [0] + arr2
for i in range(1, n + 1):
	arr1[i] = arr1[i] + arr1[i - 1]
	arr2[i] = arr2[i] + arr2[i - 1]
for _ in range(k):
	(a, b, c) = map(int, input().split())
	if a == 1:
		print(arr1[c] - arr1[b - 1])
	else:
		print(arr2[c] - arr2[b - 1])


def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
import math
n = ii()
a = li()
b = []
b.extend(a)
b.sort()
m = ii()
l = [0] * (n + 1)
l1 = [0] * (n + 1)
for i in range(n):
	l[i + 1] = l[i] + a[i]
for i in range(n):
	l1[i + 1] = l1[i] + b[i]
for i in range(m):
	(c, lo, r) = mi()
	if c == 1:
		print(l[r] - l[lo - 1])
	else:
		print(l1[r] - l1[lo - 1])


n = int(input())
l = list(map(int, input().split()))
x = l.copy()
x.sort()
m = int(input())
total = [0] * n
totals = [0] * n
for i in range(n):
	total[i] = total[i - 1] + l[i]
	totals[i] = totals[i - 1] + x[i]
totals.append(0)
total.append(0)
while m:
	(p, q, r) = list(map(int, input().split()))
	if p == 1:
		print(total[r - 1] - total[q - 2])
	else:
		print(totals[r - 1] - totals[q - 2])
	m -= 1


n = int(input())
a1 = list(map(int, input().strip().split(' ')))
a2 = sorted(a1)
ans1 = [0]
ans2 = [0]
for i in range(1, len(a1) + 1):
	ans1.append(ans1[i - 1] + a1[i - 1])
	ans2.append(ans2[i - 1] + a2[i - 1])
for i in range(int(input())):
	(type, l, r) = map(int, input().strip().split(' '))
	if type == 1:
		print(ans1[r] - ans1[l - 1])
	else:
		print(ans2[r] - ans2[l - 1])


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	n = int(input())
	arr = array()
	m = int(input())
	pre = {}
	pres = {}
	l = 0
	r = sum(arr)
	for i in range(n):
		r -= arr[i]
		pre[i] = (l, r)
		l += arr[i]
	arr2 = sorted(arr)
	l = 0
	r = sum(arr2)
	for i in range(n):
		r -= arr2[i]
		pres[i] = (l, r)
		l += arr2[i]
	for i in range(m):
		(o, l, r) = vars()
		if o == 1:
			print(arr[l - 1] + pre[l - 1][1] - pre[r - 1][1])
		else:
			print(arr2[l - 1] + pres[l - 1][1] - pres[r - 1][1])
t = 1
for _ in range(t):
	main()


import sys
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
m = int(sys.stdin.readline())
b = a.copy()
b.sort()
dA = {}
dA[0] = a[0]
dB = {}
dB[0] = b[0]
for i in range(1, n):
	dA[i] = dA[i - 1] + a[i]
	dB[i] = dB[i - 1] + b[i]
for _ in range(m):
	(t, l, r) = map(int, sys.stdin.readline().split())
	if t == 1:
		res = dA[r - 1]
		if l > 1:
			res -= dA[l - 2]
	else:
		res = dB[r - 1]
		if l > 1:
			res -= dB[l - 2]
	print(res)


n = int(input())
z = list(map(int, input().split()))
q = z.copy()
q.sort()
s1 = []
s2 = []
s1.append(z[0])
s2.append(q[0])
for i in range(1, n):
	s1.append(z[i] + s1[i - 1])
	s2.append(q[i] + s2[i - 1])
for i in range(int(input())):
	(x, y, v) = map(int, input().split())
	if x == 1:
		if y == 1:
			print(s1[v - 1])
		else:
			print(s1[v - 1] - s1[y - 2])
	elif y == 1:
		print(s2[v - 1])
	else:
		print(s2[v - 1] - s2[y - 2])


from sys import *
n = int(input())
l = list(map(int, input().split()))
dp1 = [0] * n
dp1[0] = l[0]
for i in range(1, n):
	dp1[i] = dp1[i - 1] + l[i]
dp2 = [0] * n
l.sort()
dp2[0] = l[0]
for i in range(1, n):
	dp2[i] = dp2[i - 1] + l[i]
m = int(input())
while m > 0:
	(case, l, r) = map(int, input().split())
	if case == 1 and l != 1:
		print(dp1[r - 1] - dp1[l - 2])
	elif case == 1 and l == 1:
		print(dp1[r - 1])
	elif case == 2 and l != 1:
		print(dp2[r - 1] - dp2[l - 2])
	elif case == 2 and l == 1:
		print(dp2[r - 1])
	m -= 1


t = 1
for _ in range(0, 1):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(a)
	b.sort()
	x = [0] * n
	y = [0] * n
	x[0] = a[0]
	for i in range(1, n):
		x[i] = a[i] + x[i - 1]
	y[0] = b[0]
	for i in range(1, n):
		y[i] = b[i] + y[i - 1]
	m = int(input())
	for i in range(0, m):
		(d, f, g) = map(int, input().split())
		if d == 1:
			print(x[g - 1] - x[f - 1] + a[f - 1])
		else:
			print(y[g - 1] - y[f - 1] + b[f - 1])


from itertools import accumulate

def main():
	n = int(input())
	a = [*map(int, input().split())]
	s = [0] + list(accumulate(a))
	a.sort()
	t = [0] + list(accumulate(a))
	q = int(input())
	for i in range(q):
		(x, l, r) = map(int, input().split())
		if x == 1:
			ans = s[r] - s[l - 1]
		else:
			ans = t[r] - t[l - 1]
		print(ans)
main()


from itertools import accumulate as z
n = int(input())
a = [*map(int, input().split())]
s = [[0] + [*z(a)]]
a.sort()
s += [[0] + [*z(a)]]
q = int(input())
for i in range(q):
	(x, l, r) = map(int, input().split())
	print(s[x - 1][r] - s[x - 1][l - 1])


n = int(input())
v = list(map(int, input().split(' ')))
m = int(input())
sum_v = [0] * (n + 1)
for i in range(1, n + 1):
	sum_v[i] = sum_v[i - 1] + v[i - 1]
sum_order = [0] * (n + 1)
v.sort()
for i in range(1, n + 1):
	sum_order[i] = sum_order[i - 1] + v[i - 1]
for i in range(m):
	(t, l, r) = map(int, input().split(' '))
	if t == 1:
		print(sum_v[r] - sum_v[l - 1])
	elif t == 2:
		print(sum_order[r] - sum_order[l - 1])


(n, V) = (int(input()), [0] + list(map(int, input().split())))
U = sorted(V)
for i in range(1, n):
	(V[i + 1], U[i + 1]) = (V[i + 1] + V[i], U[i + 1] + U[i])
for _ in range(int(input())):
	(type, l, r) = map(int, input().split())
	print(V[r] - V[l - 1] if type == 1 else U[r] - U[l - 1])


cnt = lambda s, x: s.count(x)
ii = lambda : int(input())
si = lambda : input()
f = lambda : map(int, input().split())
dgl = lambda : list(map(int, input()))
il = lambda : list(map(int, input().split()))
n = ii()
l = il()
l2 = sorted(l)
lsf = [l[0]]
l2sf = [l2[0]]
m = ii()
for i in range(1, n):
	lsf.append(lsf[-1] + l[i])
	l2sf.append(l2sf[-1] + l2[i])
for _ in range(m):
	(a, b, c) = f()
	if a == 1:
		print(lsf[c - 1] - lsf[b - 1] + l[b - 1])
	else:
		print(l2sf[c - 1] - l2sf[b - 1] + l2[b - 1])


def main():
	n = int(input())
	l = [int(i) for i in input().split()]
	sum1 = l[:]
	l = sorted(l)
	sum2 = l[:]
	for i in range(1, n):
		sum1[i] += sum1[i - 1]
		sum2[i] += sum2[i - 1]
	m = int(input())
	(sum1, sum2) = ([0] + sum1, [0] + sum2)
	for i in range(m):
		(a, b, c) = map(int, input().split())
		if a == 1:
			print(sum1[c] - sum1[b - 1])
		else:
			print(sum2[c] - sum2[b - 1])
main()


n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a)
p = [0]
for i in a:
	p.append(i + p[-1])
q = [0]
for i in b:
	q.append(i + q[-1])
for i in range(int(input())):
	(t, l, r) = map(int, input().split())
	if t == 1:
		print(p[r] - p[l - 1])
	if t == 2:
		print(q[r] - q[l - 1])


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def in_int():
	return int(input())

def in_list():
	return list(map(int, input().split()))

def in_str():
	s = input()
	return list(s[:len(s) - 1])

def in_ints():
	return map(int, input().split())
n = in_int()
a = in_list()
aa = sorted(a)
a = [0] + a
aa = [0] + aa
for i in range(1, n + 1):
	a[i] += a[i - 1]
	aa[i] += aa[i - 1]
q = in_int()
for qq in range(q):
	(choice, l, r) = in_ints()
	if choice & 1 == 1:
		print(a[r] - a[l - 1])
	else:
		print(aa[r] - aa[l - 1])


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
v = [0] * n
u = [0] * n
v[0] = a[0]
u[0] = b[0]
for i in range(1, n):
	v[i] = v[i - 1] + a[i]
for i in range(1, n):
	u[i] = u[i - 1] + b[i]
m = int(input())
for i in range(m):
	d = []
	c = list(map(int, input().split()))
	if c[0] == 1:
		if c[1] - 2 < 0:
			d = v[c[2] - 1]
		else:
			d = v[c[2] - 1] - v[c[1] - 2]
	elif c[1] - 2 < 0:
		d = u[c[2] - 1]
	else:
		d = u[c[2] - 1] - u[c[1] - 2]
	print(d)


from sys import stdin, stdout

def get_params(questions):
	(q_type, l, r) = (questions[0], questions[1], questions[2])
	return (int(q_type), int(l), int(r))

def answer(_list, l, r):
	l = _list[l - 2] if l > 1 else 0
	return _list[r - 1] - l

def acc_sum(_list):
	s = 0
	result = []
	for e in _list:
		s += e
		result += [s]
	return result
n = int(stdin.readline().replace('\n', ''))
costs = [int(x) for x in stdin.readline().replace('\n', '').split(' ')]
costs_acc = acc_sum(costs)
sorted_list = costs.copy()
sorted_list.sort()
sorted_acc = acc_sum(sorted_list)
m = int(stdin.readline().replace('\n', ''))
answers = []
for i in range(m):
	questions = stdin.readline().split(' ')
	questions[-1] = questions[-1].replace('\n', '')
	(q_type, l, r) = get_params(questions)
	ans = answer(costs_acc, l, r) if q_type == 1 else answer(sorted_acc, l, r)
	answers.append(ans)
for a in answers:
	print(a)


input()
x = [int(i) for i in input().split()]
y = 0
a = [0]
b = [0]
for i in x:
	y += i
	a.append(y)
z = sorted(x)
y = 0
for i in z:
	y += i
	b.append(y)
for i in range(int(input())):
	c = [int(i) for i in input().split()]
	if c[0] == 1:
		print(a[c[2]] - a[c[1] - 1])
	else:
		print(b[c[2]] - b[c[1] - 1])


n = int(input())
a = [int(x) for x in input().split()]
asor = sorted(a)
b1 = [0] * n
b2 = [0] * n
b1[0] = a[0]
b2[0] = asor[0]
for i in range(1, n):
	b1[i] = a[i] + b1[i - 1]
	b2[i] = asor[i] + b2[i - 1]
m = int(input())
for i in range(m):
	(t, l, r) = map(int, input().split())
	if t == 1:
		if l == 1:
			print(b1[r - 1])
		else:
			print(b1[r - 1] - b1[l - 2])
	elif l == 1:
		print(b2[r - 1])
	else:
		print(b2[r - 1] - b2[l - 2])


n = int(input())
l = list(map(int, input().split()))
l1 = sorted(l)
for i in range(1, n):
	l[i] += l[i - 1]
	l1[i] += l1[i - 1]
for _ in range(int(input())):
	(a, b, c) = map(int, input().split())
	if a == 1:
		if b <= 1:
			print(l[c - 1])
		else:
			print(l[c - 1] - l[b - 2])
	elif b <= 1:
		print(l1[c - 1])
	else:
		print(l1[c - 1] - l1[b - 2])


n = int(input())
v = [int(x) for x in input().split()]
u = sorted(v)
m = int(input())
for i in range(1, n):
	v[i] += v[i - 1]
	u[i] += u[i - 1]
v.append(0)
u.append(0)
for i in range(m):
	(t, l, r) = [int(x) for x in input().split()]
	if t == 1:
		print(v[r - 1] - v[l - 2])
	else:
		print(u[r - 1] - u[l - 2])


def fun(a):
	for i in range(1, len(a)):
		a[i] += a[i - 1]
	return a
n = int(input())
q1 = [0] + list(map(int, input().split()))
q2 = sorted(q1)
q1 = fun(q1)
q2 = fun(q2)
n = int(input())
for i in range(n):
	(t, x, y) = map(int, input().split())
	if t == 1:
		print(q1[y] - q1[x - 1])
	else:
		print(q2[y] - q2[x - 1])


def main():
	n = int(input())
	a = list(map(int, input().split()))
	a_sorged = sorted(a)
	pref_a = a.copy()
	for i in range(1, n):
		pref_a[i] += pref_a[i - 1]
	pref_a_sorted = a_sorged.copy()
	for i in range(1, n):
		pref_a_sorted[i] += pref_a_sorted[i - 1]

	def query(arr, left, right):
		if left == 0:
			return arr[right]
		return arr[right] - arr[left - 1]
	m = int(input())
	for _ in range(m):
		(t, l, r) = map(int, input().split())
		print(query(pref_a if t == 1 else pref_a_sorted, l - 1, r - 1))
main()