The question is to take an input = n
, then take as many inputs as n
and output, like this:
Which would mean this:
f(1,1) → 1
f(1,2) → 3
f(1,3) → 6
f(2,2) → 2
f(2,3) → 5
f(3,3) → 3
and
answer = 1 + 3 + 6 + 2 + 5 + 3 = 20 → ans = 1+3+6+2+5+3 = 20
What I came up with is this:
def f(l,r):
return int((r+l)*(r-l+1)/2)
n=int(input())
def WTF(n):
ans = 0
S=set()
for i in range(n):
S.add(int(input()))
for i2 in S:
for i3 in S:
if i3>=i2:
ans += f(i2,i3)
return ans
print(WTF(n))
But it was not accepted due to exceeding a time limit of 1 sec.
How can I make this run faster?