input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import sys def main(): input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split(" "))) mod = 10**9+7 res = 0 for i in range(N-1): for j in range(i, N): if i >= j: continue res += A[i] * A[j] print((res % mod)) if __name__ == "__main__": main()
import sys def main(): def input():return sys.stdin.readline()[:-1] N = int(eval(input())) A = list(map(int, input().split(" "))) mod = 10**9+7 res = 0 S = sum(A) S2 = sum([x**2 for x in A]) print(((S**2-S2) // 2 % mod)) if __name__ == "__main__": main()
p02572
n = int(eval(input())) a = list(map(int, input().split())) prod_sum = 0 for i in range(n-1): for j in range(i+1, n): prod_sum += a[i]*a[j] print((prod_sum%(10**9+7)))
n = int(eval(input())) a = list(map(int, input().split())) prod_sum = 0 sum_arr = [0]*n sum_arr[n-1] = a[n-1] for i in range(n-2, -1, -1): sum_arr[i] = sum_arr[i+1] + a[i] for i in range(n-1): prod_sum += a[i]*sum_arr[i+1] print((prod_sum%(10**9+7)))
p02572
ans=0 n=int(eval(input())) li=list(map(int,input().split())) for i in range(1,n): k=sum(li[:i]) ans+=k*li[i] print((ans%(10**9+7)))
import itertools ans=0 n=int(eval(input())) li=list(map(int,input().split())) L=list(itertools.accumulate(li)) for i in range(1,n): ans+=L[i-1]*li[i] print((ans%(10**9+7)))
p02572
N = int(eval(input())) A = list(map(int, input().split())) sum = 0 D = set(range(N)) E = set() for i in D: E.add(i) tmp = 0 for j in D - E: tmp += A[j] sum += A[i] * tmp print((sum % (10**9+7)))
N = int(eval(input())) A = list(map(int, input().split())) S = [] sum = 0 for i in range(N): sum += A[i] S.append(sum) ans = 0 for i in range(N): ans += A[i] * (S[N-1] - S[i]) print((int(ans % (10**9 + 7))))
p02572
N = int(eval(input())) A = list(map(int, input().split())) total = 0 for i in range(N-1): sum_val = sum(A[i+1:N]) total += A[i]*sum_val total %= (10**9+7) print(total)
N = int(eval(input())) A = list(map(int, input().split())) total = 0 sum_val = sum(A[1:N]) for i in range(N-1): total += A[i]*sum_val total %= (10**9+7) sum_val -= A[i+1] print(total)
p02572
N=int(eval(input())) A=list(map(int,input().split())) ans=0 for i in range(N): for j in range(i+1,N): ans+=A[i]*A[j]%(10**9+7) ans%=(10**9+7) print(ans)
N=int(eval(input())) A=list(map(int,input().split())) ans=0 tmp=0 for i in range(N): tmp+=A[i] for i in range(N): tmp-=A[i] ans+=A[i]*tmp%(10**9+7) ans%=(10**9+7) print(ans)
p02572
#c n = int(eval(input())) a = list(map(int, input().split())) import itertools ans = 0 for i in list(itertools.combinations(a,2)): ans += i[0]*i[1] print((ans % (10**9 + 7)))
#c n = int(eval(input())) a = list(map(int, input().split())) import itertools ans = 0 for i in range(len(a)-1): ans += a[i]*sum(a[i+1:]) print((ans % (10**9 + 7)))
p02572
N=int(eval(input())) A=list(map(int,input().split())) ans=0 inf=10**9+7 for i in range(N-1): if A[i]!=0: for j in range(i+1,N): ans+=A[i]*A[j] ans=ans%inf print(ans)
N=int(eval(input())) A=list(map(int,input().split())) ans=0 inf=10**9+7 flag=True for i in range(N-1): if A[i]!=0: for j in range(i+1,N): temp=(A[i]*A[j])%inf ans+=temp ans%=inf print(ans)
p02572
def main(): N=eval(input()) A=list(map(int, input().split())) mod_value = 10**9 + 7 sum_ = 0 for i in range(len(A)-1): sum_ = (sum_ + A[i]*sum(A[i+1:])) % mod_value print(sum_) main()
def main(): N=eval(input()) A=list(map(int, input().split())) mod_value = 10**9 + 7 sum_ = 0 b_sum = 0 for i in range(len(A)-1): b_sum = b_sum + A[len(A)-i-1] sum_ = sum_ + (A[len(A)- i - 2]*b_sum) % mod_value print((sum_%mod_value)) main()
p02572
def readInt(): return list(map(int, input().split())) n = int(eval(input())) a = readInt() # print(n) # print(a) l = [] ans = 0 for i in a: i %= 10**9+7 # print(a) for i in range(n-1): for j in range(i+1,n): b = (a[i] * a[j]) % (10**9+7) l.append(b) # print(l) ans = sum(l) % (10**9+7) print(ans)
def readInt(): return list(map(int, input().split())) n = int(eval(input())) a = readInt() # print(n) # print(a) b = 0 ans = 0 for i in a: i %= 10**9+7 # print(a) for i in range(n-1): b += a[i] ans += a[i+1] * b ans %= (10**9+7) print(ans)
p02572
def readInt(): return list(map(int, input().split())) n = int(eval(input())) a = readInt() # print(n) # print(a) b = 0 ans = 0 for i in a: i %= 10**9+7 # print(a) s = sum(a) s2 = sum([x**2 for x in a]) ans = (s**2 - s2 ) // 2 ans %= (10**9+7) print(ans)
def readInt(): return list(map(int, input().split())) n = int(eval(input())) a = readInt() b = 0 ans = 0 for i in a: b += i ** 2 # print(sum(a)) ans = ((sum(a) ** 2 - b) // 2) % (10 ** 9 + 7) print(ans)
p02572
n = int(eval(input())) a = list(map(int, input().split())) mod = int(1e9 + 7) re = {} ans = 0 for i in range(n): for j in range(i+1,n): ans += a[i]*a[j] ans %= mod print(ans)
n = int(eval(input())) a = list(map(int, input().split())) mod = int(1e9 + 7) ans, x = 0, 0 for i in range(n): ans += a[i]*x x += a[i] print((ans%mod))
p02572
n=int(eval(input())) a=list(map(int,input().split())) ans=0 for i in range(n): b=(a[i]*sum(a[i+1:n]))%1000000007 ans=ans+b d=ans%(1000000007) print(d)
n=int(eval(input())) a=list(map(int,input().split())) ans=0 c=sum(a) for i in range(n): c=c-a[i] b=(a[i]*c) ans=ans+b d=ans%(1000000007) print(d)
p02572
N = int(eval(input())) A = list(map(int,input().split())) A = list([x%(1*10**9 + 7) for x in A]) value = 0 plus = 0 ans = 0 for i in range(N-1): for j in range(N-i-1): value = (A[i]*A[i+j+1]+value) % (1*10**9 + 7) print(value)
N = int(eval(input())) A = list(map(int,input().split())) A_sum = sum(A) value = 0 ans = 0 for i in range(N-1): A_sum -= A[i] value += A[i]*A_sum ans = value % (1*10**9+7) print(ans)
p02572
a = int(eval(input())) alist = list(map(int, input().split())) mod = 10**9+7 sumlist = [] ans = 0 for i in range(1, len(alist)+1): sumlist.append(sum(alist[0:i])) for i in range(len(alist)): ans += alist[i]*(sumlist[-1]-sumlist[i]) print((ans%mod))
a = int(eval(input())) alist = list(map(int, input().split())) mod = 10**9+7 total = sum(alist) sumlist = [] ans = 0 for i in range(len(alist)-1): total -= alist[i] ans += alist[i]*(total) print((ans%mod))
p02572
N = int(eval(input())) a_s = input().split(" ") a_s = list(map(int, a_s)) mod = 10**9 +7 ans = 0 for i in range(len(a_s)-1): ans += (sum(a_s[i+1:])*a_s[i])%mod if ans > mod: ans = ans%mod print(ans)
N = int(eval(input())) a_s = input().split(" ") a_s = list(map(int, a_s)) asum = sum(a_s) mod = 10**9 +7 ans = 0 for i in range(len(a_s)-1): asum -= a_s[i] ans += (asum*a_s[i])%mod if ans > mod: ans = ans%mod print(ans)
p02572
N = int(eval(input())) A = list(map(int, input().split()))[::-1] ans = 0 for _ in range(N-1): a = A.pop() ans += a * sum(A) print((ans%(10**9+7)))
N = int(eval(input())) A = list(map(int, input().split())) ans = 0 prev = 0 s = [] for a in A: now = prev + a s.append(now) prev = now start = 0 last = s[-1] for i in s[:-1]: ans += (i - start) * (last - i) start = i print((ans%(10**9+7)))
p02572
import math N=int(eval(input())) A=list(map(int,input().split())) ans=0 for i in range(N-1): ans+=A[i]*sum(A[i+1:]) print((ans%((10**9)+7)))
import math N=int(eval(input())) A=list(map(int,input().split())) ans=0 tmp=0 for i in range(N-1): tmp+=A[i] ans+=tmp*A[i+1] print((ans%((10**9)+7)))
p02572
import itertools N = int(eval(input())) A=list(map(int, input().split())) total=0 for v in itertools.combinations(A, 2): total += v[0]*v[1] #print(v) print((total%(10**9+7)))
N = int(eval(input())) A=list(map(int, input().split())) B=[A[N-1]] #print(B[0]) for i in range(len(A)-2): #print(i) b=B[i]+A[N-2-i] B.append(b) #print(B) total=0 for i in range(len(B)): total += A[i]*B[len(B)-1-i] print((total%(10**9+7)))
p02572
n=int(eval(input())) data=list(map(int,input().split())) ans=0 for i in range(len(data)): for j in range(i+1,len(data)): ans+=data[i]*data[j] ans=ans%(10**9+7) print(ans)
n=int(eval(input())) data=list(map(int,input().split())) num=0 ans=0 for i in range(len(data)): num+=data[i] for i in range(len(data)): num-=data[i] temp=(num*data[i]) ans+=temp print((ans%(10**9+7)))
p02572
n = int(eval(input())) a = list(map(int, input().split())) total = 0 for i in range(n - 1): for j in range(i + 1, n): total += a[i] * a[j] print((total % (pow(10, 9) + 7)))
n = int(eval(input())) a = list(map(int, input().split())) s_1 = 0 s_2 = 0 m = 10 ** 9 + 7 for i in range(n): s_1 += a[i] % m s_2 += (a[i] ** 2) % m s = (s_1 * s_1 - s_2) * pow(2, -1, m) % m print(s)
p02572
import math n=int(eval(input())) a=list(map(int,input().split())) mod=10**9+7 score=0 for j in range(n): i=0 while i < j: score += a[i]*a[j] i += 1 print((score%mod))
n = int(eval(input())) a = list(map(int,input().split())) b=sum(a) score=0 for i in range(len(a)): b -= a[i] score+=a[i]*b print((score%(10**9+7)))
p02572
m = 1000000007 n = int(eval(input())) A = list(map(int, input().split())) sum = 0 for i in range(n-1): for j in range(n-i-1): sum += A[i]*A[j+i+1] print((sum%m))
def square(list): return [i ** 2 for i in list] m = 1000000007 n = int(eval(input())) A = list(map(int, input().split())) sq = square(A) s1 = sum(A) s2 = sum(sq) result = (s1**2-s2)//2 print((result%m))
p02572
n = int(eval(input())) arr = list(map(int, input().split())) res = 0 for i in range(n): msum = 0 if arr[i] != 0: for j in range(i+1, n): msum += arr[j] res += arr[i] * msum res = res % (10**9 + 7) print(res)
def findProductSum(A, n): array_sum = sum(A) array_sum_square = array_sum ** 2 individual_square_sum = 0 for i in range(0, n, 1): individual_square_sum += A[i] * A[i] return (array_sum_square - individual_square_sum) // 2 n = eval(input()) arr = list(map(int, input().split())) res = findProductSum(arr, len(arr)) % (10**9 + 7) print(res)
p02572
def execute(N, A): dst = 0 for i in range(N-1): for j in range(i+1, N): dst += A[i] * A[j] return dst % (10**9+7) if __name__ == '__main__': N = int(eval(input())) A = list(map(int, input().split())) print((execute(N, A)))
def execute(N, A): dst = 0 s = sum(A) for v in A: s -= v dst += v * s return dst % (10**9+7) if __name__ == '__main__': N = int(eval(input())) A = list(map(int, input().split())) print((execute(N, A)))
p02572
from itertools import combinations n = int(eval(input())) aList = list(map(int, input().split())) comb = combinations(aList, 2) ansSum = 0 for i in list(comb): ansSum += i[0]*i[1] print((ansSum%(7+10**9)))
n = int(eval(input())) aList = list(map(int, input().split())) col = aList[:n-1] row = aList[1:] ansSum = 0 for i in range(n-1): for j in range(i,n-1): ansSum += col[i]*row[j] print((ansSum % (7+10**9)))
p02572
n = int(eval(input())) aList = list(map(int, input().split())) col = aList[:n-1] row = aList[1:] ansSum = 0 for i in range(n-1): for j in range(i,n-1): ansSum += col[i]*row[j] print((ansSum % (7+10**9)))
n = int(eval(input())) A = list(map(int, input().split())) array_sum_square = sum(A)**2 individual_square_sum = 0 for i in range(0, n, 1): individual_square_sum += A[i]**2 print(((array_sum_square - individual_square_sum) // 2 % (7 + 10 ** 9)))
p02572
N = int(eval(input())) A = list(map(int, input().split())) ans = 0 for i in range(N): ans += A[i] * sum(A[i+1:]) ans = ans % 1000000007 print(ans)
N = int(eval(input())) A = list(map(int, input().split())) ans = 0 S = sum(A) for i in range(N): S -= A[i] ans += A[i] * S ans = ans % (10**9+7) print((int(ans)))
p02572
N = int(eval(input())) A = list(map(int, input().split())) B = sum(A) cnt = 0 for i in range(N-1): B -= A[i] cnt += A[i]*B print((cnt%((10**9)+7)))
N = int(eval(input())) A = list(map(int, input().split())) sum = sum(A) cnt = 0 for i in range(N-1): sum -= A[i] cnt += A[i]*sum print((cnt%((10**9)+7)))
p02572
n = int(eval(input())) a = list(map(int,input().split())) ans = 0 keisu = [0]*n for i in range(n): keisu[i] += sum(a[:i]) for i in range(n): ans += (a[i] * keisu[i])%(1000000000+7) print((ans%(1000000000+7)))
n = int(eval(input())) a = list(map(int,input().split())) ans = 0 keisu = [0]*n t = 0 for i in range(n): keisu[i] += t t += a[i] for i in range(n): ans += a[i] * keisu[i] print((ans%(1000000000+7)))
p02572
n=int(eval(input())) ans=0 mod=1000000007 list1=list(map(int ,input().split())) for i in range(n): ans=ans+list1[i]*sum(list1[i+1:n]) print((ans%mod))
n=int(eval(input())) ans=0 mod=10**9+7 list1=list(map(int ,input().split())) sums=sum(list1) for i in list1: sums -=i ans +=sums*i%mod print((ans%mod))
p02572
import sys from itertools import accumulate read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N, *A = list(map(int, read().split())) csum = [0] csum.extend(accumulate(A)) ans = 0 for i, a in enumerate(A): ans = (ans + a * (csum[N] - csum[i + 1]) % MOD) % MOD print(ans) return if __name__ == '__main__': main()
import sys from itertools import accumulate read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N, *A = list(map(int, read().split())) total = sum(A) ans = 0 for a in A: total -= a ans = (ans + a * total % MOD) % MOD print(ans) return if __name__ == '__main__': main()
p02572
def main(): n = int(eval(input())) l = list(map(int, input().split())) _sum = 0 for i in range(n): for j in range(i + 1, n): _sum += l[i] * l[j] mod = 10**9 + 7 print((_sum % mod)) main()
def main(): n = int(eval(input())) l = list(map(int, input().split())) a = sum(l) a *= a b = sum([i * i for i in l]) a = (a - b) // 2 mod = 10**9 + 7 print((a % mod)) main()
p02572
n = int(eval(input())) a = list(map(int, input().split())) b = 10**9+7 ans = 0 for i in range(n-1): ans += a[i]*sum(a[i+1:]) ans %= b print(ans)
n = int(eval(input())) a = list(map(int, input().split())) b = 10**9+7 s = sum(a) ans = 0 for i in range(n-1): s -= a[i] ans += a[i]*(s%b) ans %= b print(ans)
p02572
n = int(eval(input())) a = list(map(int, input().split())) b = 10**9+7 s = sum(a) ans = 0 for i in range(n-1): s -= a[i] ans += a[i]*(s%b) ans %= b print(ans)
n = int(eval(input())) a = list(map(int, input().split())) s = sum(a) b = 10**9+7 ans = 0 for i in a: s -= i ans += i*(s%b) ans %= b print(ans)
p02572
n = int(eval(input())) A = list(map(int,input().split())) mod = (10**9) + 7 s = 0 for i in range(n-1): t = 0 for j in range(i+1,n): t += A[j]%mod s += (A[i] * t)%mod s = s%mod print(s)
n = int(eval(input())) A = list(map(int,input().split())) mod = (10**9) + 7 t = 0 sum = 0 for i in range(1,n): sum += A[i] for i in range(n-1): t += (A[i]*sum)%mod sum -= A[i+1] t = t%mod print(t)
p02572
N = int(eval(input())) b = 10**9+7 A = list((int(x)%b for x in input().split())) ans = 0 for i in range(N-1): for j in range(i+1,N): ans += (A[i]*A[j]) % b print((ans % b))
N = int(eval(input())) b = 10**9+7 A = list((int(x) for x in input().split())) sum = 0 for a in A: sum += a ans = 0 for i in range(N-1): sum -= A[i] ans += A[i]*sum print((ans % b))
p02572
N = int(eval(input())) A = list(map(int, input().split())) S = sum(A) x = 0 for i in range(N): x += (A[i] * (S - sum(A[:i+1]))) % 1000000007 x = x % 1000000007 print(x)
N = int(eval(input())) A = list(map(int, input().split())) S = [A[0]] for i in range(1, N): S.append(S[i-1] + A[i]) x = 0 for i in range(N): x += (A[i] * (S[-1] - S[i])) % 1000000007 x = x % 1000000007 print(x)
p02572
n=int(eval(input())) mod=10**9+7 A=list(map(int,input().split())) a=sum(A) a=(a**2) b=0 for i in range(n): b+=(A[i])**2 if a-b<0: c=a-b while c<=0: c+=mod ans=c//2 ans=ans%mod else: ans=(a-b)//2 ans=ans%mod print(ans)
n=int(eval(input())) A=list(map(int,input().split())) mod=10**9+7 INf=float('inf') Acc=[0]*(n+1) for i in range(1,n+1): Acc[i]=(Acc[i-1]+A[i-1])%mod ans=0 for i in range(n): s=A[i]*(Acc[n]-Acc[i+1]) ans+=s%mod print((ans%mod))
p02572
n=int(eval(input())) a=list(map(int,input().split())) sum=0 for i in range(n): for j in range(i): sum+=a[i]*a[j] ans=sum%(10**9+7) print(ans)
n=int(eval(input())) a=list(map(int,input().split())) x=sum(a) y=0 for i in range(n): y+=a[i]**2 ans = (x**2 - y)//2 ans = ans % (10**9+7) print((int(ans)))
p02572
s = eval(input()) a = list(map(int,input().split())) mod = 1000000007 ans = 0 while True: l_f = a[0] del a[0] if len(a) < 1: break for i in a: ans += l_f * i print((ans%mod))
s = int(eval(input())) a = list(map(int,input().split())) mod = 10 ** 9 + 7 ans = 0 sum_a = sum(a) for i in range(s): sum_a -= a[i] ans += sum_a * a[i] % mod print((ans%mod))
p02572
s = int(eval(input())) a = list(map(int,input().split())) mod = 10 ** 9 + 7 ans = 0 sum_a = sum(a) for i in range(s): sum_a -= a[i] ans += sum_a * a[i] % mod print((ans%mod))
s = int(eval(input())) a = list(map(int,input().split())) mod = 10 ** 9 + 7 ans = 0 sum_a = sum(a) for i in range(s): sum_a -= a[i] ans += sum_a * a[i] print((ans%mod))
p02572
from itertools import combinations mod = 10**9+7 N = int(eval(input())) li = list(map(int,input().split())) ans = 0 for i,j in combinations(li,2): ans += (i*j) ans = ans%mod print(ans)
n = int(eval(input())) li = list(map(int,input().split())) mod = 10**9+7 S = sum(li) li2 = [x**2 for x in li] S2 = sum(li2) print(((S**2-S2)//2%mod))
p02572
#整数値入力 1文字の入力 def input_one_number(): return int(eval(input())) #整数値龍力 複数の入力 def input_multiple_number(): return list(map(int, input().split())) #整数値龍力 複数の入力(配列) def input_multiple_number_as_list(): return list(map(int, input().split())) n = input_one_number() a = input_multiple_number_as_list() ssum =0 mod = 10**9+7 import itertools as itr for i in itr.combinations(list(range(len(a))),2): ssum += a[i[0]]*a[i[1]] ssum %= mod print(ssum)
#整数値入力 1文字の入力 def input_one_number(): return int(eval(input())) #整数値龍力 複数の入力 def input_multiple_number(): return list(map(int, input().split())) #整数値龍力 複数の入力(配列) def input_multiple_number_as_list(): return list(map(int, input().split())) n = input_one_number() a = input_multiple_number_as_list() mod = 10**9+7 ssum = 0 sumA = sum(a) for _a in a: ssum += _a * (sumA-_a) print((ssum//2%mod))
p02572
N = int(eval(input())) A = list(map(int, input().split())) mod = 10 ** 9 + 7 ct = 0 for i in range(N-1): for j in range(i+1, N): x = A[i] * A[j] ct += x print((ct % mod))
# C Sum of product of pairs N = int(eval(input())) A = list(map(int, input().split())) t = sum(A) x = t - A[0] mod = 10 ** 9 + 7 ct = 0 for i in range(N-1): ct += A[i] * x x = x - A[i+1] print((ct % mod)) int((ct % mod))
p02572
mod =10**9 +7 n = int(eval(input())) a = list(map(int, input().split())) sumpro = 0 for i in range(n-1): x = 0 for j in range(i+1, n): x += a[j] x = x%mod y = a[i]* x sumpro += y print((sumpro%mod))
MOD = 10 ** 9 + 7 # MODは変数に入れてしまったほうが打ち間違えなくていいです N = int(eval(input())) A = list(map(int, input().split())) S = sum(A) % MOD ans = 0 # A_Nも含まれてしまいますが、そのときはs=0なので問題ありません for x in A: S -= x # 自分と自分自身を掛けないように、先に引く必要があります S %= MOD # MODはいちいち取っておくといいです ans += S * x ans %= MOD ans %= MOD # この行は完全に不要ですが、最後だけMODを取り忘れてWAになるミスは非常によくあるので、必要以上に取っておくと安心できます print(ans)
p02572
n = int(eval(input())) a = [int(x) for x in input().split()] MOD = 10**9 +7 s = 0 for i in range(n): for j in range(i + 1, n): s += (a[i]%MOD * a[j]%MOD)%MOD print((s%MOD))
def findProductSum(A, n): # calculating array sum (a1 + a2 ... + an) array_sum = 0 for i in range(0, n, 1): array_sum += A[i] # calcualting square of array sum # (a1 + a2 + ... + an)^2 array_sum_square = (array_sum * array_sum ) # calcualting a1^2 + a2^2 + ... + an^2 individual_square_sum = 0 for i in range(0, n, 1): individual_square_sum += (A[i] * A[i]) # required sum is (array_sum_square - # individual_square_sum) / 2 return (array_sum_square - individual_square_sum) // 2 n = int(eval(input())) a = [int(x) for x in input().split()] MOD = 10**9 +7 print((int(findProductSum(a,n))%MOD)) # print(s%MOD)
p02572
N = int(eval(input())) A = input().split() for i in range(N): A[i] = int(A[i]) ans = 0 for j in range(1, N): for i in range(0, j): ans += A[i]*A[j] print((ans % 1000000007))
N = int(eval(input())) A = input().split() toA = 0 for i in range(N): A[i] = int(A[i]) toA += A[i] ans = 0 for i in range(0, N): toA -= A[i] ans += A[i]*toA print((ans % 1000000007))
p02572
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) score = 0 for i in range(0, N-1): score += A[i] * sum(A[i+1:]) print((score % (10**9+7)))
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) score = 0 sum_A = sum(A) for a in A[:-1]: sum_A -= a score += a * sum_A print((score % (10**9+7)))
p02572
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) mod = 10 ** 9 + 7 sum_A = sum(A) ans = 0 for a in A[:-1]: sum_A -= a ans += a * sum_A ans %= mod print(ans)
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) mod = 10 ** 9 + 7 sum_A = sum(A) % mod ans = 0 for a in A[:-1]: sum_A -= a sum_A %= mod ans += a * sum_A ans %= mod ans %= mod print(ans)
p02572
N = int(eval(input())) A = list(map(int, input().split())) ans = 0 mod = 10**9 + 7 for i in range(N): num = 0 for j in range(i+1, N): num += A[j] ans += A[i] * num print((ans % mod))
N = int(eval(input())) A = list(map(int, input().split())) ans = 0 mod = 10 ** 9 + 7 num = sum(A) for a in A: num -= a ans += a * num print((ans % mod))
p02572
N=int(eval(input())) A=list(map(int,input().split())) x=0 for i in range(N): x+=A[i]*(sum(A)-A[i]) print((x//2%1000000007))
N=int(eval(input())) A=list(map(int,input().split())) x=0 x=sum(A)**2 for i in A: x-=i**2 print((x//2%1000000007))
p02572
#!/usr/bin/env python n = int(eval(input())) a = list(map(int, input().split())) mod = int(1e9)+7 total = sum(a) ans = 0 for i in range(n): ans += (a[i]*(total-a[i]))%mod total -= a[i] print((ans%mod))
#!/usr/bin/env python n = int(eval(input())) a = list(map(int, input().split())) mod = int(1e9)+7 s = sum(a) ans = sub = 0 for i in range(n): ans += a[i]*s for i in range(n): sub += a[i]*a[i] ans = ((ans-sub)//2)%mod print(ans)
p02572
ans = 0 N = int(eval(input())) array = list(map(int, input().split())) for i in range(N): ans += array[i]*sum(array[i+1:N]) print((ans%(10**9+7)))
ans = 0 N = int(eval(input())) array = list(map(int, input().split())) x = sum(array) y = 0 for i in range(N): y += array[i]**2 ans = x**2 -y print(((ans//2)%(10**9+7)))
p02572
ans = 0 N = int(eval(input())) array = list(map(int, input().split())) x = sum(array) y = 0 for i in range(N): y += array[i]**2 ans = x**2 -y print(((ans//2)%(10**9+7)))
N = int(eval(input())) A = list(map(int, input().split())) ans = 0 x = sum(A) for i in range(N - 1): x -= A[i] ans += A[i] * x ans = ans % (10 ** 9 + 7) print(ans)
p02572
import itertools n=int(eval(input())) l=list(map(int,input().split())) mod = 10**9 + 7 ans=0 for i,j in itertools.combinations(l, 2): num=i*j ans+=num print((ans%mod))
n=int(eval(input())) l=list(map(int,input().split())) sum_l=sum(l) ans=0 mod=10**9+7 for i in range(n): sum_l-=l[i] ans+=l[i]*sum_l print((ans%mod))
p02572
L,A,B,M = list(map(int,input().split())) def si(i): return A + B * i s = [str(si(i)) for i in range(L)] n = "" for i in s: n += i n = int(n) print((n % M))
L,A,B,M = list(map(int,input().split())) def si(i): return A + B * i n = "" for i in range(L): n += str(si(i)) n = int(n) print((n % M))
p03016
class SquareMatrix(): def __init__(self, n, mod=1000000007): self.n = n self.mat = [[0 for j in range(n)] for i in range(n)] self.mod = mod @staticmethod def id(n, mod=1000000007): res = SquareMatrix(n, mod) for i in range(n): res.mat[i][i] = 1 return res @staticmethod def modinv(n, mod): assert n % mod != 0 c0, c1 = n, mod a0, a1 = 1, 0 b0, b1 = 0, 1 while c1: a0, a1 = a1, a0 - c0 // c1 * a1 b0, b1 = b1, b0 - c0 // c1 * b1 c0, c1 = c1, c0 % c1 return a0 % mod def set(self, arr): for i in range(self.n): for j in range(self.n): self.mat[i][j] = arr[i][j] % self.mod def operate(self, vec): assert len(vec) == self.n res = [0 for _ in range(self.n)] for i in range(self.n): for j in range(self.n): res[i] += self.mat[i][j] * vec[j] res[i] %= self.mod return res def add(self, other): assert other.n == self.n res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[i][j] + other.mat[i][j] res.mat[i][j] %= self.mod return res def subtract(self, other): assert other.n == self.n res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[i][j] - other.mat[i][j] res.mat[i][j] %= self.mod return res def times(self, k): res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[i][j] * k res.mat[i][j] %= self.mod return res def multiply(self, other): assert self.n == other.n res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): for k in range(self.n): res.mat[i][j] += self.mat[i][k] * other.mat[k][j] res.mat[i][j] %= self.mod return res def power(self, k): tmp = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): tmp.mat[i][j] = self.mat[i][j] res = SquareMatrix.id(self.n, self.mod) while k: if k & 1: res = res.multiply(tmp) tmp = tmp.multiply(tmp) k >>= 1 return res def trace(self): res = 0 for i in range(self.n): res += self.mat[i][i] res %= self.mod return res def determinant(self): res = 1 tmp = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): tmp.mat[i][j] = self.mat[i][j] for j in range(self.n): if tmp.mat[j][j] == 0: for i in range(j + 1, self.n): if tmp.mat[i][j] != 0: idx = i break else: return 0 for k in range(self.n): tmp.mat[j][k], tmp.mat[idx][k] = tmp.mat[idx][k], tmp.mat[j][k] res *= -1 inv = SquareMatrix.modinv(tmp.mat[j][j], self.mod) for i in range(j + 1, self.n): c = -inv * tmp.mat[i][j] % self.mod for k in range(self.n): tmp.mat[i][k] += c * tmp.mat[j][k] tmp.mat[i][k] %= self.mod for i in range(self.n): res *= tmp.mat[i][i] res %= self.mod return res def transpose(self): res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[j][i] return res def inverse(self): #self.determinant() != 0 res = SquareMatrix.id(self.n, self.mod) tmp = SquareMatrix(self.n, self.mod) sgn = 1 for i in range(self.n): for j in range(self.n): tmp.mat[i][j] = self.mat[i][j] for j in range(self.n): if tmp.mat[j][j] == 0: for i in range(j + 1, self.n): if tmp.mat[i][j] != 0: idx = i break else: return 0 for k in range(self.n): tmp.mat[j][k], tmp.mat[idx][k] = tmp.mat[idx][k], tmp.mat[j][k] res.mat[j][k], res.mat[idx][k] = res.mat[idx][k], res.mat[j][k] inv = SquareMatrix.modinv(tmp.mat[j][j], self.mod) for k in range(self.n): tmp.mat[j][k] *= inv tmp.mat[j][k] %= self.mod res.mat[j][k] *= inv res.mat[j][k] %= self.mod for i in range(self.n): c = tmp.mat[i][j] for k in range(self.n): if i == j: continue tmp.mat[i][k] -= tmp.mat[j][k] * c tmp.mat[i][k] %= self.mod res.mat[i][k] -= res.mat[j][k] * c res.mat[i][k] %= self.mod return res def linear_equations(self, vec): return self.inverse().operate(vec) L, A, B, M = list(map(int, input().split())) D = [0 for _ in range(18)] for i in range(18): D[i] = min((int('9' * (i + 1)) - A) // B + 1, L) for i in range(17)[::-1]: D[i + 1] -= D[i] mat = SquareMatrix.id(3, M) for i in range(18): op = SquareMatrix(3, M) op.mat[0][0] = 10**(i + 1) op.mat[0][1] = 1 op.mat[1][1] = 1 op.mat[1][2] = B op.mat[2][2] = 1 mat = op.power(D[i]).multiply(mat) print((mat.operate([0, A, 1])[0]))
class SquareMatrix(): def __init__(self, n, mod=1000000007): self.n = n self.mat = [[0 for j in range(n)] for i in range(n)] self.mod = mod @staticmethod def id(n, mod=1000000007): res = SquareMatrix(n, mod) for i in range(n): res.mat[i][i] = 1 return res @staticmethod def modinv(n, mod): assert n % mod != 0 c0, c1 = n, mod a0, a1 = 1, 0 b0, b1 = 0, 1 while c1: a0, a1 = a1, a0 - c0 // c1 * a1 b0, b1 = b1, b0 - c0 // c1 * b1 c0, c1 = c1, c0 % c1 return a0 % mod def set(self, arr): for i in range(self.n): for j in range(self.n): self.mat[i][j] = arr[i][j] % self.mod def operate(self, vec): assert len(vec) == self.n res = [0 for _ in range(self.n)] for i in range(self.n): for j in range(self.n): res[i] += self.mat[i][j] * vec[j] res[i] %= self.mod return res def add(self, other): assert other.n == self.n res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[i][j] + other.mat[i][j] res.mat[i][j] %= self.mod return res def subtract(self, other): assert other.n == self.n res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[i][j] - other.mat[i][j] res.mat[i][j] %= self.mod return res def times(self, k): res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[i][j] * k res.mat[i][j] %= self.mod return res def multiply(self, other): assert self.n == other.n res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): for k in range(self.n): res.mat[i][j] += self.mat[i][k] * other.mat[k][j] res.mat[i][j] %= self.mod return res def power(self, k): tmp = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): tmp.mat[i][j] = self.mat[i][j] res = SquareMatrix.id(self.n, self.mod) while k: if k & 1: res = res.multiply(tmp) tmp = tmp.multiply(tmp) k >>= 1 return res def trace(self): res = 0 for i in range(self.n): res += self.mat[i][i] res %= self.mod return res def determinant(self): res = 1 tmp = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): tmp.mat[i][j] = self.mat[i][j] for j in range(self.n): if tmp.mat[j][j] == 0: for i in range(j + 1, self.n): if tmp.mat[i][j] != 0: idx = i break else: return 0 for k in range(self.n): tmp.mat[j][k], tmp.mat[idx][k] = tmp.mat[idx][k], tmp.mat[j][k] res *= -1 inv = SquareMatrix.modinv(tmp.mat[j][j], self.mod) for i in range(j + 1, self.n): c = -inv * tmp.mat[i][j] % self.mod for k in range(self.n): tmp.mat[i][k] += c * tmp.mat[j][k] tmp.mat[i][k] %= self.mod for i in range(self.n): res *= tmp.mat[i][i] res %= self.mod return res def transpose(self): res = SquareMatrix(self.n, self.mod) for i in range(self.n): for j in range(self.n): res.mat[i][j] = self.mat[j][i] return res def inverse(self): #self.determinant() != 0 res = SquareMatrix.id(self.n, self.mod) tmp = SquareMatrix(self.n, self.mod) sgn = 1 for i in range(self.n): for j in range(self.n): tmp.mat[i][j] = self.mat[i][j] for j in range(self.n): if tmp.mat[j][j] == 0: for i in range(j + 1, self.n): if tmp.mat[i][j] != 0: idx = i break else: return 0 for k in range(self.n): tmp.mat[j][k], tmp.mat[idx][k] = tmp.mat[idx][k], tmp.mat[j][k] res.mat[j][k], res.mat[idx][k] = res.mat[idx][k], res.mat[j][k] inv = SquareMatrix.modinv(tmp.mat[j][j], self.mod) for k in range(self.n): tmp.mat[j][k] *= inv tmp.mat[j][k] %= self.mod res.mat[j][k] *= inv res.mat[j][k] %= self.mod for i in range(self.n): c = tmp.mat[i][j] for k in range(self.n): if i == j: continue tmp.mat[i][k] -= tmp.mat[j][k] * c tmp.mat[i][k] %= self.mod res.mat[i][k] -= res.mat[j][k] * c res.mat[i][k] %= self.mod return res def linear_equations(self, vec): return self.inverse().operate(vec) L, A, B, M = list(map(int, input().split())) D = [0 for _ in range(18)] for i in range(18): D[i] = (int('9' * (i + 1)) - A) // B + 1 D[i] = max(D[i], 0) D[i] = min(D[i], L) for i in range(17)[::-1]: D[i + 1] -= D[i] mat = SquareMatrix.id(3, M) for i in range(18): op = SquareMatrix(3, M) op.mat[0][0] = 10**(i + 1) op.mat[0][1] = 1 op.mat[1][1] = 1 op.mat[1][2] = B op.mat[2][2] = 1 mat = op.power(D[i]).multiply(mat) print((mat.operate([0, A, 1])[0]))
p03016
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from functools import lru_cache L,A,B,MOD = list(map(int,read().split())) @lru_cache(None) def F(r,n,MOD): # 1+r+...+r^{n-1} をダブリングで求める if n == 0: return 0 q = n//2 x = F(r,q,MOD) # 1+r^qをかける x *= 1 + pow(r,q,MOD); x %= MOD if n&1: x *= r; x += 1; x %= MOD return x @lru_cache(None) def G(r,n,MOD): # sum ir^i をダブリングで求める if n == 0: return 0 q = n//2 x = G(r,q,MOD) # 1+r^qをかける x *= 1 + pow(r,q,MOD); x %= MOD # q(r^q+...+r^{2q-1})を加える x += F(r,q,MOD) * pow(r,q,MOD) * q % MOD if n&1: x *= r; x += F(r,n,MOD); x -= 1; x %= MOD return x S = 0 for d in range(1,20): # d桁の最初、最後、項数 lower = 10**(d-1) upper = 10**d-1 # lower <= A+nB <= upper nlower = (lower-A+B-1)//B nupper = (upper-A)//B if nlower < 0: nlower = 0 if nupper >=L: nupper = L-1 if nlower > nupper: continue items = nupper - nlower + 1 r = (10**d) % MOD last = A+B*nupper x = last * F(r,items,MOD) x -= B * G(r,items,MOD) length = d*items S *= pow(10,length,MOD); S += x S %= MOD print(S)
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines L,A,B,MOD = list(map(int,read().split())) def geom_seq_sum(r,n,MOD): """ return \sum_{i=0}^{n-1} r^i mod MOD """ if r == 1: return n%MOD r1 = r-1 return ((pow(r,n,r1*MOD)-1)//r1)% MOD def geom_seq_sum_1(r,n,MOD): """ return \sum_{i=0}^{n-1} ir^i mod MOD """ if r == 1: return n*(n-1)//2%MOD r1 = r-1 x = pow(r,n,r1*r1*MOD) return (-(x-1)//r1 + (n-1)*x + 1)//r1 % MOD S = 0 for d in range(1,20): # d桁の最初、最後、項数 lower = 10**(d-1) upper = 10**d-1 # lower <= A+nB <= upper nlower = (lower-A+B-1)//B nupper = (upper-A)//B if nlower < 0: nlower = 0 if nupper >=L: nupper = L-1 if nlower > nupper: continue items = nupper - nlower + 1 r = (10**d) % MOD last = A+B*nupper x = last * geom_seq_sum(r,items,MOD) x -= B * geom_seq_sum_1(r,items,MOD) length = d*items S *= pow(10,length,MOD); S += x S %= MOD print(S)
p03016
from math import ceil def dot(a,b,m): c = [[0,0,0],[0,0,0],[0,0,0]] for i in range(3): for j in range(3): for k in range(3): c[i][j] += a[i][k]*b[k][j] c[i][j] %= m return c def powm(a,n,m): if n == 0: return [[1,0,0],[0,1,0],[0,0,1]] for i in range(3): for j in range(3): a[i][j] %= m if n%2==0: return dot(powm(a,n//2,m),powm(a,n//2,m),m) else: return dot(dot(powm(a,n//2,m),powm(a,n//2,m),m),a,m) L,A,B,M = list(map(int,input().split())) a = [[1,0,0],[0,1,0],[0,0,1]] for d in range(1,19): C = min(max(0,ceil((10**d-A)/B)),L)-min(max(0,ceil((10**(d-1)-A)/B)),L) a = dot(powm([[pow(10,d,M),1,0],[0,1,B%M],[0,0,1]],C,M),a,M) ans = (a[0][1]*A+a[0][2])%M print(ans)
def dot(a,b,m): c = [[0,0,0],[0,0,0],[0,0,0]] for i in range(3): for j in range(3): for k in range(3): c[i][j] += a[i][k]*b[k][j] c[i][j] %= m return c def powm(a,n,m): if n == 0: return [[1,0,0],[0,1,0],[0,0,1]] for i in range(3): for j in range(3): a[i][j] %= m r = powm(a,n//2,m) if n%2==0: return dot(r,r,m) else: return dot(dot(r,r,m),a,m) L,A,B,M = list(map(int,input().split())) a = [[1,0,0],[0,1,0],[0,0,1]] for d in range(1,19): C = min(max(0,(10**d-A-1)//B+1),L)-min(max(0,(10**(d-1)-A-1)//B+1),L) a = dot(powm([[pow(10,d,M),1,0],[0,1,B%M],[0,0,1]],C,M),a,M) ans = (a[0][1]*A%M+a[0][2])%M print(ans)
p03016
import math import sys sys.setrecursionlimit(10000) def nasu(A, B, L, D, M): if L == 1: return A ans = nasuA(A, L, D, M) ans = (ans + nasuB(1, L - 1, D, M) * B * D) % M return ans % M def nasuB(B, L, D, M): if L == 1: return B ans = powB(1, L, D, M) % M return ans % M def powB(B, L, D, M): if L == 1: return B k = 0 t = D while T[k + 1] <= L: k += 1 t = t * t % M if k + 1 == len(T): break ans = powB2(B, T[k], D, M) % M if L != T[k]: ans += nasu(T[k] + 1, 1, L - T[k], D, M) * t % M #ans += powB(1, L - T[k], D, M) * t % M #ans += powA(-T[k], L - T[k], D, M) * t % M return ans % M def powB2(B, L, D, M): if L == 1: return 1 ans = powB2(B * 2, L // 2, D * D % M, M) * (D + 1) % M ans = ans + nasuA(1, L // 2, D * D % M, M) * B * D % M return ans def nasuA(A, L, D, M): if L == 1: return A ans = powA(A, L, D, M) % M return ans % M def powA(A, L, D, M): if L == 1: return A k = 0 t = D while T[k + 1] <= L: k += 1 t = t * t % M if k + 1 == len(T): break ans = powA2(A, T[k], D, M) if L != T[k]: ans += powA(A, L - T[k], D, M) * t % M return ans % M def powA2(A, L, D, M): if L == 1: return A return powA2(A, L // 2, D * D % M, M) * (D + 1) % M L, A, B, M = list(map(int, input().split())) N = math.ceil(math.log(A + 1, 10)) k = pow(10, N) D = [[0, 0] for _ in range(20)] while L > 0: n = min(L, (k - 1 - A) // B + 1) D[N - 1][0] = A D[N - 1][1] = n L -= n N += 1 k *= 10 A += n * B T = [1] while T[-1] < 10 ** 19: T.append(T[-1] * 2) BI = (((B // M) + 1) * M - B) % M ans = 0 for i in range(20): l = D[i][1] a = D[i][0] + (l - 1) * B if l == 0: continue ans = ans * pow(pow(10, i + 1), l) % M ans = (ans + nasu(a, BI, l, pow(10, i + 1), M)) % M print(ans)
import math import sys sys.setrecursionlimit(10000) def nasu(A, B, L, D, M): if L == 1: return A ans = nasuA(A, L, D, M) ans = (ans + nasuB(1, L - 1, D, M) * B * D) % M return ans % M def nasuB(B, L, D, M): if L == 1: return B ans = powB(1, L, D, M) % M return ans % M def powB(B, L, D, M): if L == 1: return B k = 0 t = D while T[k + 1] <= L: k += 1 t = t * t % M if k + 1 == len(T): break ans = powB2(B, T[k], D, M) % M if L != T[k]: ans += nasu(T[k] + 1, 1, L - T[k], D, M) * t % M #ans += powB(1, L - T[k], D, M) * t % M #ans += powA(-T[k], L - T[k], D, M) * t % M return ans % M def powB2(B, L, D, M): if L == 1: return 1 ans = powB2(B * 2, L // 2, D * D % M, M) * (D + 1) % M ans = ans + nasuA(1, L // 2, D * D % M, M) * B * D % M return ans def nasuA(A, L, D, M): if L == 1: return A ans = powA(A, L, D, M) % M return ans % M def powA(A, L, D, M): if L == 1: return A k = 0 t = D while T[k + 1] <= L: k += 1 t = t * t % M if k + 1 == len(T): break ans = powA2(A, T[k], D, M) if L != T[k]: ans += powA(A, L - T[k], D, M) * t % M return ans % M def powA2(A, L, D, M): if L == 1: return A return powA2(A, L // 2, D * D % M, M) * (D + 1) % M def powmod(a, n, M): ans = 1 while n: if n & 1: ans = (ans * a) % M a = a * a % M n >>= 1 return ans L, A, B, M = list(map(int, input().split())) N = math.ceil(math.log(A + 1, 10)) k = pow(10, N) D = [[0, 0] for _ in range(20)] while L > 0: n = min(L, (k - 1 - A) // B + 1) D[N - 1][0] = A D[N - 1][1] = n L -= n N += 1 k *= 10 A += n * B T = [1] while T[-1] < 10 ** 19: T.append(T[-1] * 2) BI = (((B // M) + 1) * M - B) % M ans = 0 for i in range(20): l = D[i][1] a = D[i][0] + (l - 1) * B if l == 0: continue ans = ans * powmod(pow(10, i + 1), l, M) % M ans = (ans + nasu(a, BI, l, pow(10, i + 1), M)) % M print(ans)
p03016
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) INF = 10 ** 20 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 def mat_mul(a, b) : I, J, K = len(a), len(b[0]), len(b) c = [[0] * J for _ in range(I)] for i in range(I) : for j in range(J) : for k in range(K) : c[i][j] += a[i][k] * b[k][j] c[i][j] %= m return c def mat_pow(x, n): y = [[0] * len(x) for _ in range(len(x))] for i in range(len(x)): y[i][i] = 1 while n > 0: if n & 1: y = mat_mul(x, y) x = mat_mul(x, x) n >>= 1 return y l, a, b, m = LI() d0 = 0 ret = [[0], [a], [1]] for i in range(1, 19): if 10 ** i - 1 - a < 0: continue d1 = min((10 ** i - 1 - a) // b + 1, l) mat = [[10 ** i, 1, 0], [0, 1, b], [0, 0, 1]] ret = mat_mul(mat_pow(mat, d1 - d0), ret) if d1 == l: break d0 = d1 print((ret[0][0]))
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) INF = 10 ** 20 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 def mat_mul(a, b) : I, J, K = len(a), len(b[0]), len(b) c = [[0] * J for _ in range(I)] for i in range(I) : for j in range(J) : for k in range(K) : c[i][j] += a[i][k] * b[k][j] c[i][j] %= m return c def mat_pow(x, n): y = [[0] * len(x) for _ in range(len(x))] for i in range(len(x)): y[i][i] = 1 while n > 0: if n & 1: y = mat_mul(x, y) x = mat_mul(x, x) n >>= 1 return y l, a, b, m = LI() d0 = 0 ret = [[0], [a], [b]] for i in range(1, 19): if 10 ** i - 1 - a < 0: continue d1 = min((10 ** i - 1 - a) // b + 1, l) mat = [[10 ** i, 1, 0], [0, 1, 1], [0, 0, 1]] ret = mat_mul(mat_pow(mat, d1 - d0), ret) if d1 == l: break d0 = d1 print((ret[0][0]))
p03016
L, A, B, M = list(map(int, input().split())) ten = 1 def f(l): if l == 0: return 0 if l % 2 == 1: pl = l - 1 x = f(pl) return (x * ten + 1) % M else: pl = l // 2 x = f(pl) return (x * ten ** pl + x) % M def g(l): if l == 0: return 0 if l % 2 == 1: pl = l - 1 x = g(pl) return (x * ten + B * pl) % M else: pl = l // 2 x = g(pl) return (x * ten ** pl + x + B * pl * f(pl)) % M last = A + B * (L - 1) ans = 0 for i in range(1, 19): l = ten r = ten * 10 - 1 if last < l: ten *= 10 continue if A > r: ten *= 10 continue na = 0 #初項 nl = 0 #長さ if A >= l: na = A else: na = (l - A + B - 1) // B * B + A na = min(na, last) nlast = 0 if last <= r: nlast = last else: nlast = (r - A) // B * B + A nl = (nlast - na) // B + 1 ten *= 10 ans *= ten ** nl % M ans += (na * f(nl)) % M ans += g(nl) % M print((ans % M))
L, A, B, M = list(map(int, input().split())) ten = 0 def pow_(x, t): if t == 0: return 1 if t % 2 == 1: return (pow_(x, t - 1) * x) % M else: y = pow_(x, t // 2) return (y * y) % M def f(l): if l == 0: return 0 if l % 2 == 1: pl = l - 1 x = f(pl) return x * ten + 1 else: pl = l // 2 x = f(pl) return x * pow_(ten, pl) + x def g(l): if l == 0: return 0 if l % 2 == 1: pl = l - 1 x = g(pl) return x * ten + B * pl else: pl = l // 2 x = g(pl) return x * pow_(ten, pl) + x + B * pl * f(pl) last = A + B * (L - 1) ans = 0 ten = 10 for _ in range(18): l = ten // 10 r = ten - 1 if last < l: ten *= 10 continue if A > r: ten *= 10 continue na = 0 #初項 nl = 0 #長さ if A >= l: na = A else: na = (l - A + B - 1) // B * B + A na = min(na, last) nlast = 0 if last <= r: nlast = last else: nlast = (r - A) // B * B + A nl = (nlast - na) // B + 1 ans *= pow_(ten, nl) % M ans += (na * f(nl)) % M ans += g(nl) % M ten *= 10 print((ans % M))
p03016
import sys sys.setrecursionlimit(100000000) def mult(A, B, mod): #行列A, Bの内積を求める。 D = [[(A[0][0] * B[0][0]) % mod, 0, 0], [0, 1, 0], [0, 0, 1]] D[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % mod D[2][0] = (A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0]) % mod D[2][1] = (A[2][1]*B[1][1]+A[2][2]*B[2][1]) % mod return D def double(C, mod): D = [[pow(C[0][0], 2, mod), 0, 0], [(C[1][0] * C[0][0] + C[1][0]) % mod, 1, 0], [(C[2][0] * C[0][0] + C[2][1] * C[1][0] + C[2][0]) % mod, (C[2][1] * 2) % mod, 1]] return D def Cpow(C, n, mod): if n == 1: return C else: D = double(C, mod) if n % 2 == 0: return Cpow(D, n // 2, mod) else: return mult(Cpow(D, n // 2, mod), C, mod) def solve(): L, a, b, M = list(map(int, input().split())) X = 0 s = a nd = -1 ne = 0 for d in range(1, 19): ne = min((pow(10, d) - a - 1) // b, L-1) if ne == nd: continue C = [[pow(10, d, M), 0, 0], [1, 1, 0], [0, b % M, 1]] D = Cpow(C, ne - nd, M) X = (X * D[0][0] + D[1][0] * s + D[2][0]) % M s += D[2][1] if ne == L - 1: break nd = ne print(X) return if __name__ == "__main__": solve()
import sys sys.setrecursionlimit(100000000) def mult(A, B, mod): #行列A, Bの内積を求める。 D = [[(A[0][0] * B[0][0]) % mod, 0, 0], [0, 1, 0], [0, 0, 1]] D[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % mod D[2][0] = (A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0]) % mod D[2][1] = (A[2][1]*B[1][1]+A[2][2]*B[2][1]) % mod return D def double(C, mod): D = [[pow(C[0][0], 2, mod), 0, 0], [(C[1][0] * C[0][0] + C[1][0]) % mod, 1, 0], [(C[2][0] * C[0][0] + C[2][1] * C[1][0] + C[2][0]) % mod, (C[2][1] * 2) % mod, 1]] return D def Cpow(C, n, mod): if n == 1: return C else: D = double(C, mod) if n % 2 == 0: return Cpow(D, n // 2, mod) else: return mult(Cpow(D, n // 2, mod), C, mod) def solve(): L, a, b, M = list(map(int, input().split())) X = 0 s = a nd = -1 ne = 0 for d in range(1, 19): ne = min((pow(10, d) - a - 1) // b, L-1) if ne < 0: continue elif ne == nd: continue C = [[pow(10, d, M), 0, 0], [1, 1, 0], [0, b % M, 1]] D = Cpow(C, ne - nd, M) X = (X * D[0][0] + D[1][0] * s + D[2][0]) % M s += D[2][1] if ne == L - 1: break nd = ne print(X) return if __name__ == "__main__": solve()
p03016
L, A, B, M = list(map(int, input().split())) numstrlist = list(map(str, list(range(A, A+B*L, B)))) numstr = "".join(numstrlist) numlist = list(numstr) length = len(numlist)-1 ans = 0 for i, c in enumerate(numlist): ans += int(c) if i != length: ans = ans * 10 % M print(ans)
L, A, B, M = list(map(int, input().split())) l_i = 0 s = A ans = 0 while l_i != L: numlist = list(str(s)) for i, c in enumerate(numlist): ans += int(c) if i != len(numlist)-1 or l_i != L-1: ans = ans * 10 % M s += B l_i += 1 print(ans)
p03016
#F - Takahashi's Basics in Education and Learning L,A,B,M=(int(i) for i in input().split()) tmp=[A+B*i for i in range(L)] result="" for i in range(L): result+=str(tmp[i]) print((int(result)%M))
L,A,B,M=(int(i) for i in input().split()) result="" for i in range(L): result+=str(A+B*i) print((int(result)%M))
p03016
L,A,B,mod=list(map(int,input().split())) def f(l): return max(0,(10**l-A-1)//B+1) def m(a,b): r=[[0]*len(b[0]) for i in range(len(a))] for i in range(len(a)): for k in range(len(b)): for j in range(len(b[0])): r[i][j]=(r[i][j]+a[i][k]*b[k][j])%mod return r def p(a,n): r=[[0]*len(a) for i in range(len(a))] b=[] for i in range(len(a)): r[i][i]=1 b.append(a[i][:]) l=n while l>0: if l&1: r=m(b,r) b=m(b,b) l>>=1 return r ''' 100 001 B10 * 00s 01l 00P ''' X=[[0,0,1],[0,0,A],[0,0,0]] Y,R,Z=0,1,0 while L: Y=[[1,0,0],[B,1,0],[0,1,pow(10,R,mod)]] Z=min(L,f(R)-f(R-1)) L-=Z Y=p(Y,Z) X=m(Y,X) R+=1 print((X[2][2]%mod))
L,A,B,mod=list(map(int,input().split())) def f(l): return max(0,(10**l-A-1)//B+1) def m(a,b): r=[[0]*len(b[0]) for i in range(len(a))] for i in range(len(a)): for k in range(len(b)): for j in range(len(b[0])): r[i][j]=(r[i][j]+a[i][k]*b[k][j])%mod return r def p(a,n): r=[[0]*len(a) for i in range(len(a))] b=[] for i in range(len(a)): r[i][i]=1 b.append(a[i][:]) l=n while l>0: if l&1: r=m(b,r) b=m(b,b) l>>=1 return r X=[[0,0,1],[0,0,A],[0,0,0]] Y,R,Z=0,1,0 while L: Y=[[1,0,0],[B,1,0],[0,1,pow(10,R,mod)]] Z=min(L,f(R)-f(R-1)) L-=Z Y=p(Y,Z) X=m(Y,X) R+=1 print((X[2][2]%mod))
p03016
l, a, b, m = list(map(int, input().split())) print((int(''.join(list(map(str, [a+b*i for i in range(l)]))))%m))
l, a, b, m = list(map(int, input().split())) num = [] for i in range(l): num.append(str(a + i * b)) print((int(''.join(num))%m))
p03016
import sys L, A, B, M = list(map(int, input().split())) mod = M sys.setrecursionlimit(pow(10, 8)) def power(x, y): if y == 0 : return 1 elif y == 1 : return x % mod elif y % 2 == 0 : return power(x, y//2)**2 % mod else : return power(x, (y-1)//2)**2 * x % mod def mul(a, b): return ((a % mod) * (b % mod)) % mod def modinv(a): b, u, v = mod, 1, 0 while b: t = a//b a, u = a-t*b, u-t*v a, b, u, v = b, a, v, u u %= mod return u def div(a, b): return a * modinv(b) % mod def G(n, i): if n == 1: return 1 tn = n//2 return (G(tn, i) * (1 + power(10, tn*i)) %mod + (n%2)*power(10, (n-1)*i)) % mod def G2(n, i): if n ==0 : return 0 if n == 1: return power(10, i) % mod tn = n//2 X = G2(tn, i) I = power(10, tn*i) Y = (X*(I+1)%mod + mul(mul(tn, I), (G(tn+1, i)-1))%mod) %mod Y = Y + mul(n%2, mul(n, power(10, n*i))) return Y%mod def f(a, b, n, i): if n == 0: return 0 r = power(10, i) P = power(r,n) #aa = mul(a, div(P-1, r-1)) aa = mul(a, G(n, i)) bb2 = mul(b, div(r-n*P + (n-1)*P*r, mul(1-r, 1-r))) bb = mul(b, G2(n-1, i)) #print(n-1, i, div(r-n*P + (n-1)*P*r, mul(1-r, 1-r)), G2(n-1, i)) #print(bb, bb2) return (aa - bb) % mod sl = A + (L-1) * B t = 10 k = 0 ts = 0 ks = [] mk = 18 for i in range(mk): k = max(min((t-1),sl)-(A-B), 0) // B - ts #print(t, min(t, sl) - (A-B), -(-((min(t, sl) - (A-B))//B))) #k = -(-(min(t, sl) - (A-B)) // B) - 1 - ts ts += k ks.append(k) t *= 10 ti = 0 ti2 = 0 b = B R = 0 for i in range(mk): i = mk - i n = ks[i-1] a = sl - ti2*b if n: R = (R + (f(a, b, n, i))*power(10, ti)) % mod # print(a, f(a,b,r,n)) # print(a, b, r, n, R, ti) ti += i*n ti2 += n print(R) #$print(ks)
import sys L, A, B, M = list(map(int, input().split())) mod = M sys.setrecursionlimit(pow(10, 8)) def power(x, y): if y == 0 : return 1 elif y == 1 : return x % mod elif y % 2 == 0 : return power(x, y//2)**2 % mod else : return power(x, (y-1)//2)**2 * x % mod def mul(a, b): return ((a % mod) * (b % mod)) % mod def G(n, i): # G = 1 + R + (R**2) + (R**3) ... (R**(n-1)) # R = 10**i if n == 1: return 1 tn = n//2 return (G(tn, i) * (1 + power(10, tn*i)) %mod + (n%2)*power(10, (n-1)*i)) % mod def G2(n, i): # G2 = R + 2(R**2) + 3(R**3) ... n(R**n) # R = 10**i if n ==0 : return 0 if n == 1: return power(10, i) % mod tn = n//2 X = G2(tn, i) I = power(10, tn*i) Y = (X*(I+1)%mod + mul(mul(tn, I), (G(tn+1, i)-1))%mod) + mul(n%2, mul(n, power(10, n*i))) return Y%mod def f(a, b, n, i): if n == 0: return 0 aa = mul(a, G(n, i)) bb = mul(b, G2(n-1, i)) return (aa - bb) % mod SL = A + (L-1) * B ts = 0 ks = [0] for i in range(len(str(SL))+1): # ks[i] = Count(10**i <= Si <10**(i+1)) ks.append(max(min((10**(i+1)-1),SL)-(A-B), 0) // B - ts) ts += ks[-1] ti = 0 a = SL R = 0 # i = 2, n = 3, a = 17, B=2 # 17 * 1500 * 130000 # r += Sum_{0<=j<=n-1}((a-Bj)*pow(10**i, j)) * pow(10, ti) # j = 0 -> (a+Bj)*pow(10**i, j) = a for i in range(len(str(SL))+1,0,-1): n = ks[i] R = (R + f(a, B, n, i)*power(10, ti)) % mod ti += i*n a -= n*B print(R)
p03016
s = [len(x) for x in input().split('T')] x, y = list(map(int, input().split())) len_s = len(s) sx = [s[i] for i in range(len_s) if i % 2 == 0] sy = [s[i] for i in range(len_s) if i % 2 == 1] x -= sx[0] sx = sx[1:] def is_reachable(p, s): origin = 8000 if origin+p < 0: return False n = len(s) reachable = [[False for _ in range(origin * 2 + 1)] for i in range(n + 2)] reachable[0][origin] = True for i in range(n): for j in range(origin * 2 + 1): if not reachable[i][j]: continue if j - s[i] >= 0: reachable[i + 1][j - s[i]] = True if j + s[i] <= origin * 2: reachable[i + 1][j + s[i]] = True return reachable[n][p + origin] or reachable[n][-p + origin] if is_reachable(x, sx) and is_reachable(y, sy): print('Yes') else: print('No')
s = [len(x) for x in input().split('T')] x, y = list(map(int, input().split())) len_s = len(s) sx = [s[i] for i in range(len_s) if i % 2 == 0] sy = [s[i] for i in range(len_s) if i % 2 == 1] x -= sx[0] sx = sx[1:] def is_reachable(p, s): origin = 8000 if origin+p < 0: return False n = len(s) reachable = [set() for i in range(n + 1)] reachable[0].add(origin) for i in range(n): for j in list(reachable[i]): if j - s[i] >= 0: reachable[i + 1].add(j - s[i]) if j + s[i] <= origin * 2: reachable[i+1].add(j + s[i]) return (origin+p) in reachable[n] or (origin-p) in reachable[n] if is_reachable(x, sx) and is_reachable(y, sy): print('Yes') else: print('No')
p03490
s = input().split("T") x,y = list(map(int,input().split())) xmove = [] ymove = [] for i in range(1,len(s)): if i % 2 == 0: xmove.append(s[i].count("F")) else: ymove.append(s[i].count("F")) dpx = [[False]*(16001) for i in range(len(xmove)+1)] dpy = [[False]*(16001) for i in range(len(ymove)+1)] dpx[0][s[0].count("F")+8000] = True dpy[0][8000] = True for i in range(len(xmove)): for j in range(16001): if dpx[i][j]: if 0<=j-xmove[i]: dpx[i+1][j-xmove[i]] = True if j+xmove[i] <= 16001: dpx[i+1][j+xmove[i]] = True for i in range(len(ymove)): for j in range(16001): if dpy[i][j]: if 0<=j-ymove[i]: dpy[i+1][j-ymove[i]] = True if j+ymove[i] <= 16001: dpy[i+1][j+ymove[i]] = True if dpx[len(xmove)][x+8000] and dpy[len(ymove)][y+8000]: print("Yes") else: print("No")
s = input().split("T") x,y = list(map(int,input().split())) xmove = [] ymove = [] for i in range(len(s)): if i % 2 == 1: ymove.append(s[i].count("F")) else: xmove.append(s[i].count("F")) #リストで持つdpだとTLEしたのでメモ化再帰で書く def solve(l,cor): dp = {0} for c in l: dp = {c+i for i in dp}|{i-c for i in dp} return cor in dp if solve(xmove[1:],x-xmove[0]) and solve(ymove,y): print("Yes") else: print("No")
p03490
s = input().split('T') x, y = list(map(int, input().split())) x = abs(x-len(s[0])) y = abs(y) dx = [] for i in range(2, len(s), 2) : dx.append(len(s[i])) dy = [] for i in range(1, len(s), 2) : dy.append(len(s[i])) if sum(dx) < x or sum(dy) < y : print('No') exit() xl = len(dx) for i in range(1 << xl) : nx = 0 for j in range(xl) : if i>>j & 1 : nx -= dx[j] else : nx += dx[j] if nx == x : break else : print('No') exit() yl = len(dy) for i in range(1 << yl) : ny = 0 for j in range(yl) : if i>>j & 1 : ny -= dy[j] else : ny += dy[j] if ny == y : break else : print('No') exit() print('Yes')
s = input().split('T') x, y = list(map(int, input().split())) s = [len(_) for _ in s] dx = s[::2] dy = s[1::2] x = abs(x-dx[0]) y = abs(y) dx = dx[1:] px = 0 py = 0 for d in reversed(sorted(dx)) : if px > x : px -= d else : px += d for d in reversed(sorted(dy)) : if py > y : py -= d else : py += d if px == x and py == y : print('Yes') else : print('No')
p03490
N = int(eval(input())) a = list(map(int, input().split())) l = sorted(a[:N]) r = sorted(a[N:]) lsum = sum(l) rsum = sum(r[:N]) import bisect ans = lsum - rsum for i in range(N): c = a[N+i] li = bisect.bisect_left(l, c) ri = bisect.bisect_left(r, c) if li > 0: lsum = lsum + c - l[0] l.insert(li, c) del l[0] if ri < N: rsum = rsum - c + r[N] del r[ri] ans = max(ans, lsum - rsum) print(ans)
N = int(eval(input())) a = list(map(int, input().split())) import heapq left = [0]*(N+1) l = a[:N] left[0] = sum(l) heapq.heapify(l) for i in range(1, N+1): c = a[N+i-1] lmin = heapq.heappop(l) if lmin < c: left[i] = left[i-1] + c - lmin heapq.heappush(l, c) else: left[i] = left[i-1] heapq.heappush(l, lmin) a = [-x for x in a[::-1]] right = [0]*(N+1) r = a[:N] right[0] = sum(r) heapq.heapify(r) for i in range(1, N+1): c = -a[N+i-1] rmax = -heapq.heappop(r) if rmax > c: right[i] = right[i-1] - c + rmax heapq.heappush(r, -c) else: right[i] = right[i-1] heapq.heappush(r, -rmax) ans = -10**20 for i in range(N+1): ans = max(ans, left[i] + right[N-i]) print(ans)
p03716
import heapq n=int(eval(input())) a=list(map(int,input().split())) q=a[0:n] sums=[0]*(n+1) sums[0]=sum(q) heapq.heapify(q) am=a[n:2*n] for i in range(0,n): heapq.heappush(q,am[i]) heapq.heappop(q) sums[i+1]=sum(q) p=a[2*n:3*n] p=list([x*(-1) for x in p]) sums[n]+=sum(p) heapq.heapify(p) for i in range(n-1,-1,-1): heapq.heappush(p,(-1)*am[i]) heapq.heappop(p) sums[i]+=sum(p) print((max(sums)))
import heapq n=int(eval(input())) a=list(map(int,input().split())) q=a[0:n] sums=[0]*(n+1) sums[0]=sum(q) heapq.heapify(q) am=a[n:2*n] for i in range(0,n): heapq.heappush(q,am[i]) sums[i+1]=sums[i]+am[i]-heapq.heappop(q) p=a[2*n:3*n] p=list([x*(-1) for x in p]) sump=sum(p) sums[n]+=sump heapq.heapify(p) for i in range(n-1,-1,-1): heapq.heappush(p,(-1)*am[i]) sump=sump-am[i]-heapq.heappop(p) sums[i]+=sump print((max(sums)))
p03716
from heapq import heappop, heappush N = int(eval(input())) a = list(map(int, input().split())) pq1 = [] for x in a[:N]: heappush(pq1, x) pq2 = sorted(a[N:]) ans = sum(pq1) - sum(pq2[:N]) for k in range(N, 2 * N): heappush(pq1, a[k]) heappop(pq1) pq2.remove(a[k]) ans = max(ans, sum(pq1) - sum(pq2[:N])) print(ans)
from heapq import heappop, heappush, heapify N = int(eval(input())) a = list(map(int, input().split())) pq1 = a[:N] pq2 = [-x for x in a[2 * N:]] heapify(pq1) heapify(pq2) s1 = sum(pq1) s2 = sum(pq2) ans1 = [s1] ans2 = [s2] for k in range(N, 2 * N): heappush(pq1, a[k]) x = heappop(pq1) s1 += a[k] - x ans1.append(s1) heappush(pq2, -a[3 * N - k - 1]) x = heappop(pq2) s2 += -a[3 * N - k - 1] - x ans2.append(s2) print((max(ans1[i] + ans2[-i - 1] for i in range(len(ans1)))))
p03716
import heapq n=int(eval(input())) a=list(map(int,input().split())) ans=[0 for i in range(n+1)] ans[0]=sum(a[:n]) b=a[:n] heapq.heapify(b) for i in range(n): c=heapq.heappop(b) if c>a[n+i]: heapq.heappush(b,c) ans[i+1]=sum(b) else: heapq.heappush(b,a[n+i]) ans[i+1]=sum(b) b=a[2*n:] for i in range(n): b[i]=-b[i] heapq.heapify(b) ans[n]+=sum(b) for i in range(n-1,-1,-1): c=heapq.heappop(b) if c>-a[n+i]: heapq.heappush(b,c) ans[i]+=sum(b) else: heapq.heappush(b,-a[n+i]) ans[i]+=sum(b) print((max(ans)))
import heapq n=int(eval(input())) a=list(map(int,input().split())) ans=[0 for i in range(n+1)] ans[0]=sum(a[:n]) b=a[:n] heapq.heapify(b) sb=sum(b) for i in range(n): sb+=a[n+i] c=heapq.heappushpop(b,a[n+i]) sb-=c ans[i+1]=sb b=a[2*n:] for i in range(n): b[i]=-b[i] heapq.heapify(b) ans[n]+=sum(b) sb=sum(b) for i in range(n-1,-1,-1): sb-=a[n+i] c=heapq.heappushpop(b,-a[n+i]) sb-=c ans[i]+=sb print((max(ans)))
p03716
# coding: utf-8 import bisect def calc_l_score(N, a): scores = [None] * (N + 1) queue = sorted(a[:N]) scores[0] = sum(queue) for i in range(N): na = a[N + i] bisect.insort(queue, na) m = queue.pop(0) scores[i + 1] = scores[i] + na - m return scores def calc_r_score(N, a): scores = [None] * (N + 1) queue = sorted(a[2 * N:]) scores[-1] = sum(queue) for i in range(1, N + 1): na = a[-N - i] bisect.insort(queue, na) m = queue.pop(-1) scores[-i - 1] = scores[-i] + na - m return scores def main(): N = int(eval(input())) a = list(map(int, input().split())) ls = calc_l_score(N, a) rs = calc_r_score(N, a) return max([l - r for l, r in zip(ls, rs)]) if __name__ == "__main__": print((main()))
# coding: utf-8 import heapq def calc_l_score(N, a): scores = [None] * (N + 1) queue = a[:N] heapq.heapify(queue) scores[0] = sum(queue) for i in range(N): na = a[N + i] heapq.heappush(queue, na) m = heapq.heappop(queue) scores[i + 1] = scores[i] + na - m return scores def calc_r_score(N, a): scores = [None] * (N + 1) queue = [] for i in range(1, N + 1): heapq.heappush(queue, -a[-i]) scores[-1] = - sum(queue) for i in range(1, N + 1): na = a[-N - i] heapq.heappush(queue, -na) m = - heapq.heappop(queue) scores[-i - 1] = scores[-i] + na - m return scores def main(): N = int(eval(input())) a = list(map(int, input().split())) ls = calc_l_score(N, a) rs = calc_r_score(N, a) return max([l - r for l, r in zip(ls, rs)]) if __name__ == "__main__": print((main()))
p03716
from collections import deque import heapq import copy N = int(eval(input())) an = list(map(int, input().split())) ans = sum(an[:N]) - sum(an[2*N:]) left = [0] * (N+1) right = [0] * (N+1) left[0] = sum(an[:N]) right[-1] = -sum(an[2*N:]) l = an[:N] heapq.heapify(l) m = an[N:2*N] r = [-i for i in an[2*N:]] heapq.heapify(r) for k in range(N): heapq.heappush(l, m[k]) left[1+k] = left[k] + m[k] - heapq.heappop(l) heapq.heappush(r, -m[-1-k]) right[-2-k] = right[-1-k] - heapq.heappop(r) - m[-1-k] ans = float('-inf') for i in range(N+1): ans = max(ans, left[i]+right[i]) print(ans)
import heapq N = int(eval(input())) an = list(map(int, input().split())) left = [0] * (N+1) right = [0] * (N+1) left[0] = sum(an[:N]) right[-1] = -sum(an[2*N:]) l = an[:N] heapq.heapify(l) m = an[N:2*N] r = [-i for i in an[2*N:]] heapq.heapify(r) for k in range(N): heapq.heappush(l, m[k]) left[1+k] = left[k] + m[k] - heapq.heappop(l) heapq.heappush(r, -m[-1-k]) right[-2-k] = right[-1-k] - heapq.heappop(r) - m[-1-k] ans = -float('inf') for i in range(N+1): ans = max(ans, left[i]+right[i]) print(ans)
p03716
from heapq import heappush, heappushpop, heappop import sys input = sys.stdin.readline N = int(eval(input())) a = list(map(int, input().split())) first = [a[0]] first_sum = [0] * (3*N) first_sum[0] = a[0] for i in range(1, 3*N): if i < N: first_sum[i] = first_sum[i-1] + a[i] heappush(first, a[i]) else: first_sum[i] = first_sum[i-1] + a[i] - heappushpop(first, a[i]) second = [-a[-1]] second_sum = [0] * (3*N) second_sum[-1] = a[-1] for i in range(3*N-1)[::-1]: if i >= 2*N: second_sum[i] = second_sum[i+1] + a[i] heappush(second, -a[i]) else: heappush(second, -a[i]) x = -heappop(second) second_sum[i] = second_sum[i+1] + a[i] - x ans = max(first_sum[i] - second_sum[i+1] for i in range(N-1, 2*N)) print(ans)
from heapq import heappush, heappushpop import sys input = sys.stdin.readline N = int(eval(input())) a = list(map(int, input().split())) first = [a[0]] first_sum = [a[0]] + [0] * (3*N-1) for i in range(1, 3*N): if i < N: first_sum[i] = first_sum[i-1] + a[i] heappush(first, a[i]) else: first_sum[i] = first_sum[i-1] + a[i] - heappushpop(first, a[i]) second = [-a[-1]] second_sum = [0] * (3*N-1) + [a[-1]] for i in range(3*N-1)[::-1]: if i >= 2*N: second_sum[i] = second_sum[i+1] + a[i] heappush(second, -a[i]) else: second_sum[i] = second_sum[i+1] + a[i] - (-heappushpop(second, -a[i])) ans = max(first_sum[i] - second_sum[i+1] for i in range(N-1, 2*N)) print(ans)
p03716
n = int(eval(input())) as_ = list(map(int, input().split())) l = [0]*n m = [0]*n r = [0]*n for i in range(n): l[i] = as_[i] m[i] = as_[n+i] r[i] = (-1)*as_[2*n+i] import heapq heapq.heapify(l) heapq.heapify(r) l_maxs = [0]*(n+1) r_mins = [0]*(n+1) sl = sum(l) sr = sum(r) for i in range(n+1): if i != 0: j = i-1 heapq.heappush(l, m[j]) a = heapq.heappop(l) sl += m[j] -a k = -i heapq.heappush(r, m[k]*(-1)) b = heapq.heappop(r) sr += m[k]*(-1) - b l_maxs[i] = sl r_mins[i] = sr #print(l_maxs) #print(r_mins) print((max([l_maxs[i] + r_mins[n-i] for i in range(n+1)])))
n = int(eval(input())) A = list(map(int, input().split())) L = A[0:n] R = A[2*n:3*n] import heapq sl = sum(L) heapq.heapify(L) sls = [0]*(n+1) sls[0] = sl for i in range(n, 2*n): heapq.heappush(L, A[i]) x = heapq.heappop(L) sl += A[i]-x sls[i-n+1] = sl #print(sls) sr = sum(R) R = [-c for c in R] heapq.heapify(R) srs = [0]*(n+1) srs[-1] = sr for i in reversed(list(range(n, 2*n))): heapq.heappush(R, -A[i]) x = -heapq.heappop(R) sr += A[i]-x srs[i-n] = sr #print(srs) ans = - 10**18 for i in range(n+1): ans = max(ans, sls[i]-srs[i]) print(ans)
p03716
import heapq N = int(eval(input())) A = list(map(int,input().split())) C = A[N:(2*N)] L = [] R = [] for i in range(N): heapq.heappush(L,A[i]) #左側にAの左側からN個を追加 heapq.heappush(R,-A[-1-i]) #右側数列にAの右側からN個を追加。多いほうから取り出すので-1倍 #左側の総和を求める。 ansL = [sum(L)] for i in range(N): heapq.heappush(L,C[i]) heapq.heappop(L) ansL.append(sum(L)) ansR = [sum(R)] for i in range(N): heapq.heappush(R,-C[-1-i]) heapq.heappop(R) ansR.append(sum(R)) ans = -float('inf') for i in range(N+1): temp = ansL[i]+ansR[-1-i] ans = max(ans,temp) #print(ansL,ansR) print(ans)
import heapq N = int(eval(input())) A = list(map(int,input().split())) C = A[N:(2*N)] L = [] R = [] for i in range(N): heapq.heappush(L,A[i]) #左側にAの左側からN個を追加 heapq.heappush(R,-A[-1-i]) #右側数列にAの右側からN個を追加。多いほうから取り出すので-1倍 #左側の総和を求める。 ansL = [sum(L)] for i in range(N): heapq.heappush(L,C[i]) rmv = heapq.heappop(L) temp = ansL[i] + C[i] - rmv #一つ前の答え+今新しく加えた数-最小値 ansL.append(temp) #ansL.append(sum(L)) #sum(L)がO(N)かかっている? ansR = [sum(R)] for i in range(N): heapq.heappush(R,-C[-1-i]) rmv = heapq.heappop(R) temp = ansR[i] - C[-1-i] - rmv ansR.append(temp) #ansR.append(sum(R)) #sum(R)がO(N)かかっている? ans = -float('inf') for i in range(N+1): temp = ansL[i]+ansR[-1-i] ans = max(ans,temp) #print(ansL,ansR) print(ans)
p03716
import heapq N = int(eval(input())) a = list(map(int, input().split())) L = a[:N] R = a[(N*2):] L_sum_list = [sum(L)] R_sum_list = [sum(R)] R = [-elem for elem in R] for k in range(N): #L側 L.append(a[N+k]) heapq.heapify(L) rem = a[N+k] - heapq.heappop(L) L_sum_list.append(L_sum_list[-1]+rem) #R側 R.append(-(a[N*2-k-1])) heapq.heapify(R) rem = -(a[N*2-k-1]) - heapq.heappop(R) R_sum_list.append(R_sum_list[-1]-rem) res = [] for k in range(N+1): res.append(L_sum_list[k] - R_sum_list[-k-1]) print((max(res)))
import heapq N = int(eval(input())) a = list(map(int, input().split())) L = a[:N] R = a[(N*2):] L_sum_list = [sum(L)] R_sum_list = [sum(R)] R = [-elem for elem in R] heapq.heapify(L) heapq.heapify(R) for k in range(N): #L側 rem = a[N+k] - heapq.heappushpop(L, a[N+k]) L_sum_list.append(L_sum_list[-1]+rem) #R側 rem = -(a[N*2-k-1]) - heapq.heappushpop(R, -(a[N*2-k-1])) R_sum_list.append(R_sum_list[-1]-rem) res = [] for k in range(N+1): res.append(L_sum_list[k] - R_sum_list[-k-1]) print((max(res)))
p03716
import heapq from copy import deepcopy from sys import stdin N = int(stdin.readline()) a = list(map(int, stdin.readline().split())) # 前半と後半 first, second = [], [] heapq.heapify(first) heapq.heapify(second) first_sum, second_sum = 0, 0 for i in range(N): heapq.heappush(first, a[i]) first_sum += a[i] heapq.heappush(second, -a[3 * N - 1 - i]) second_sum += a[3 * N - 1 - i] first_tmp = deepcopy(first) second_tmp = deepcopy(second) first_sum_tmp = first_sum second_sum_tmp = second_sum ans = -10 ** 15 # first側からk個を取り除き、second側からN - k個を取り除く for k in range(N + 1): first = deepcopy(first_tmp) second = deepcopy(second_tmp) first_sum = first_sum_tmp second_sum = second_sum_tmp cnt = 0 while cnt < k: heapq.heappush(first, a[N + cnt]) first_sum += a[N + cnt] first_sum -= heapq.heappop(first) cnt += 1 while cnt < N: heapq.heappush(second, -a[N + cnt]) second_sum += a[N + cnt] second_sum -= -heapq.heappop(second) cnt += 1 ans = max(ans, first_sum - second_sum) # print(first_sum, second_sum) print(ans)
import heapq from sys import stdin N = int(stdin.readline()) a = list(map(int, stdin.readline().split())) # 前半と後半 first, second = [], [] heapq.heapify(first) heapq.heapify(second) first_sum, second_sum = 0, 0 for i in range(N): heapq.heappush(first, a[i]) first_sum += a[i] heapq.heappush(second, -a[3 * N - 1 - i]) second_sum += a[3 * N - 1 - i] # first, secondそれぞれ0 - Nまで探索 L_sum = [first_sum] R_sum = [second_sum] for i in range(N, 2 * N): heapq.heappush(first, a[i]) first_sum += a[i] first_sum -= heapq.heappop(first) L_sum.append(first_sum) heapq.heappush(second, -a[3 * N - 1 - i]) second_sum += a[3 * N - 1 - i] second_sum -= - heapq.heappop(second) R_sum.append(second_sum) ans = -10 ** 15 R_sum = R_sum[::-1] for i in range(N + 1): ans = max(ans, L_sum[i] - R_sum[i]) # print(L_sum, R_sum) print(ans)
p03716
import heapq N = int(eval(input())) A = list(map(int, input().split())) def solve(R_ite, L_ite, n): heapq.heapify(R_ite) heapq.heapify(L_ite) for _ in range(n): heapq.heappop(R_ite) for _ in range(N-n): heapq.heappop(L_ite) return sum(R_ite) + sum(L_ite) ans = float("inf") * (-1) for i in range(N+1): R = A[:N+i] LL = A[N+i:] L = [LL[j] * (-1) for j in range(len(LL))] ans = max(ans,solve(R,L,i)) print(ans)
import heapq N = int(eval(input())) A = list(map(int, input().split())) pre = A[:N] epi = A[2*N:] epi = [epi[j]*(-1) for j in range(N)] t_pre = sum(pre) t_epi = sum(epi) heapq.heapify(pre) heapq.heapify(epi) pre_sum = [] epi_sum = [] pre_sum.append(t_pre) for i in range(N): t_push = A[N+i] heapq.heappush(pre, t_push) t_pop = heapq.heappop(pre) t_pre = t_pre + t_push - t_pop pre_sum.append(t_pre) epi_sum.append(t_epi) for i in range(N): t_push = A[-(N+i+1)] * (-1) heapq.heappush(epi, t_push) t_pop = heapq.heappop(epi) t_epi = t_epi + t_push - t_pop epi_sum.append(t_epi) epi_sum = [epi_sum[-i] for i in range(1,N+2)] ans = [] for i in range(N+1): ans.append(pre_sum[i]+epi_sum[i]) print((max(ans)))
p03716
from heapq import * N = int(eval(input())) A = list(map(int, input().split())) A_indexed = [(a, i) for i, a in enumerate(A)] def scores(): # initial N:2N selected_left = A[:N] # min heap heapify(selected_left) l_score = sum(selected_left) temp = sorted(A_indexed[N:]) selected_right_set = set(temp[:N]) unselected_right = temp[N:] # min heap del temp r_score = sum([x[0] for x in selected_right_set]) yield l_score,r_score for i in range(N, 2*N): a = A[i] # left 使うものを減らす, right 使うものを増やす if selected_left[0] < a: l_score -= heapreplace(selected_left, a) l_score += a if (a,i) in selected_right_set: r_score -= a selected_right_set.remove((a,i)) n_element = heappop(unselected_right) while n_element[1] <= i: n_element = heappop(unselected_right) selected_right_set.add(n_element) r_score += n_element[0] yield l_score,r_score # print(list(scores())) print((max([x[0]-x[1] for x in scores()])))
from heapq import * N = int(eval(input())) A = list(map(int, input().split())) B = list([-x for x in reversed(A)]) def left_scores(L): s = sum(L[:N]) scores = [s] q = L[:N] heapify(q) for e in L[N:2*N]: s -= heappushpop(q, e) s += e scores.append(s) return scores print((max([x[0]+x[1] for x in zip(left_scores(A), reversed(left_scores(B)))])))
p03716
from heapq import * N = int(eval(input())) A = list(map(int, input().split())) B = list([-x for x in reversed(A)]) def left_scores(L): s = sum(L[:N]) scores = [s] q = L[:N] heapify(q) for e in L[N:2*N]: s -= heappushpop(q, e) s += e scores.append(s) return scores print((max([x[0]+x[1] for x in zip(left_scores(A), reversed(left_scores(B)))])))
from heapq import * r=reversed N=int(eval(input())) A=list(map(int,input().split())) def w(L): q=L[:N] heapify(q) s=sum(q) yield s for e in L[N:2*N]: s+=e-heappushpop(q,e) yield s print((max(list(map(sum,list(zip(w(A),r(list(w([-a for a in r(A)]))))))))))
p03716
import sys from heapq import heapify, heappush, heappop from itertools import accumulate inf = 1<<100 def solve(): N = int(eval(input())) a = [int(i) for i in input().split()] ps = [0] + list(accumulate(a)) psmin = [0] * (N + 1) minhp = a[:N] heapify(minhp) for i in range(1, N + 1): heappush(minhp, a[N - 1 + i]) x = heappop(minhp) psmin[i] = psmin[i - 1] + x psmax = [0] * (N + 1) maxhp = [-ai for ai in a[2*N:]] heapify(maxhp) for i in range(1, N + 1): heappush(maxhp, -a[2*N - i]) x = -heappop(maxhp) psmax[i] = psmax[i - 1] + x # print(psmin) # print(psmax) ans = -inf for i in range(0, N + 1): # print(ps[N + i], psmin[i], ps[3*N] - ps[N + i], psmax[N - i]) v = (ps[N + i] - psmin[i]) - (ps[3*N] - ps[N + i] - psmax[N - i]) ans = max(ans, v) print(ans) pass if __name__ == '__main__': solve()
import sys from heapq import heapify, heappush, heappop inf = 1<<60 def solve(): n = int(eval(input())) a = [int(i) for i in input().split()] ps = [sum(a[:n])] minhp = a[:n] heapify(minhp) for i in range(n): heappush(minhp, a[n + i]) x = heappop(minhp) ps.append(ps[-1] + a[n + i] - x) ss = [sum(a[2*n:])] maxhp = [-ai for ai in a[2*n:]] heapify(maxhp) for i in range(n): heappush(maxhp, -a[2*n - 1 - i]) x = -heappop(maxhp) ss.append(ss[-1] + a[2*n - 1 - i] - x) ans = -inf for i in range(n + 1): ans = max(ans, ps[i] - ss[n - i]) print(ans) if __name__ == '__main__': solve()
p03716
from heapq import* from collections import* n,*a=list(map(int,open(0).read().split())) ans=-float("inf") b=a[:n] c=deque(a[n:]) heapify(b) s=sum(b) s2=sum(nsmallest(n,c)) red=[s-s2] for i in range(n,n*2): s+=a[i]-heappushpop(b,a[i]) c.popleft() s2=sum(nsmallest(n,c)) red+=[s-s2] print((max(red))) #あhjfkぁdshflkじゃgはlkjdjglk;あjdglk;あdsjgl;あjsdglk;あjsdgl;かs
from heapq import* n,*a=list(map(int,open(0).read().split())) b=a[:n] c=[-i for i in a[n:][::-1]] d=c[:n] e=c[n:] heapify(b) heapify(d) s=sum(b) t=sum(d) red=[s] blue=[t] for i in range(n,n*2): s+=a[i]-heappushpop(b,a[i]) t+=c[i]-heappushpop(d,c[i]) red+=[s] blue+=[t] print((max(list(map(sum,list(zip(red,blue[::-1]))))))) #あhjfkぁdshflkじゃgはlkjdjglk;あjdglk;あdsjgl;あjsdglk;あjsdgl;かs
p03716
from heapq import*;n,*a=list(map(int,open(0).read().split()));b,h,c,p=a[:n],heappushpop,[-i for i in a[n:][::-1]],heapify;s,d=sum(b),c[:n];t=sum(d);p(b);p(d);r,l=[s],[t] for i in range(n,n*2):s+=a[i]-h(b,a[i]);t+=c[i]-h(d,c[i]);r+=[s];l+=[t] print((max(list(map(sum,list(zip(r,l[::-1])))))))
def f(a): q=a[:n];s=sum(q);heapify(q);yield s for e in a[n:2*n]:s+=e-heappushpop(q,e);yield s from heapq import*;n,*a=list(map(int,open(0).read().split()));print((max(list(map(sum,list(zip(f(a),list(f([-e for e in a[::-1]]))[::-1])))))))
p03716
def f(a): q=a[:n];s=sum(q);heapify(q);yield s for e in a[n:2*n]:s+=e-heappushpop(q,e);yield s from heapq import*;n,*a=list(map(int,open(0).read().split()));print((max(list(map(sum,list(zip(f(a),list(f([-e for e in a[::-1]]))[::-1])))))))
def f(l,a): q=a[:n];s=sum(q);heapify(q);l+=[s] for e in a[n:2*n]:s+=e-heappushpop(q,e);l+=[s] from heapq import*;n,*a=list(map(int,open(0).read().split()));l=[];f(l,a);f(l,[-i for i in a[::-1]]);m=len(l)//2;print((max(l[i]+l[-i-1]for i in range(m))))
p03716
from heapq import heapify, heappop, heappush, heappushpop N = int(eval(input())) *A, = list(map(int, input().split())) INF = 10**16 first = A[:N] heapify(first) second = [-a for a in A[2*N:]] heapify(second) f = [0]*(N+1) s = [0]*(N+1) f[0] = sum(first) s[N] = - sum(second) for k in range(1, N+1): heappushpop(first, A[N-1+k]) f[k] = sum(first) for k in range(N-1, -1, -1): heappushpop(second, -A[N+k]) s[k] = - sum(second) ans = -INF for k in range(N+1): ans = max(ans, f[k] - s[k]) print(ans)
from heapq import heapify, heappushpop N = int(eval(input())) *A, = list(map(int, input().split())) INF = 10**16 first = A[:N] heapify(first) second = [-a for a in A[2*N:]] heapify(second) f = [0]*(N+1) s = [0]*(N+1) f[0] = sum(first) s[N] = - sum(second) for k in range(1, N+1): a = heappushpop(first, A[N-1+k]) f[k] = f[k-1] + A[N-1+k] - a for k in range(N-1, -1, -1): a = heappushpop(second, -A[N+k]) s[k] = s[k+1] + A[N+k] + a ans = -INF for k in range(N+1): ans = max(ans, f[k] - s[k]) print(ans)
p03716
from collections import deque from heapq import heapify, heappop as pop, heappush as push N = int(eval(input())) a = list(map(int, input().split())) l = a[:N] m = deque(a[N:N*2]) r = [x * -1 for x in a[N*2:]] # 中身は負 heapify(l) heapify(r) ll = [sum(l)] for em in m: l_min = pop(l) if l_min < em: ll.append(ll[-1] - l_min + em) push(l, em) else: ll.append(ll[-1]) push(l, l_min) rr = [sum(r)] # 中身は負 for em in reversed(m): r_max = pop(r) * -1 if r_max > em: rr.append(rr[-1] + r_max - em) push(r, em * -1) else: rr.append(rr[-1]) push(r, r_max * -1) print((max(a + b for a, b in zip(ll, reversed(rr)))))
from heapq import heapify, heappop as pop, heappush as push N = int(eval(input())) a = list(map(int, input().split())) l = a[:N] m = a[N:N*2] r = [x * -1 for x in a[N*2:]] # 中身は負 heapify(l) heapify(r) ll = [sum(l)] for em in m: l_min = pop(l) if l_min < em: ll.append(ll[-1] - l_min + em) push(l, em) else: ll.append(ll[-1]) push(l, l_min) rr = [sum(r)] # 中身は負 for em in reversed(m): r_max = pop(r) * -1 if r_max > em: rr.append(rr[-1] + r_max - em) push(r, em * -1) else: rr.append(rr[-1]) push(r, r_max * -1) print((max(a + b for a, b in zip(ll, reversed(rr)))))
p03716
#C問題 import heapq N = int(eval(input())) A = list(map(int,input().split())) a = [] for i in range(N): heapq.heappush(a,A[i]) B = [] for i in range(3*N): B.append(-A[3*N-1-i]) b = [] for i in range(N): heapq.heappush(b,B[i]) Red = [sum(a) for i in range(N+1)] Blue = [sum(b) for i in range(N+1)] inf = float("inf") MAX = -inf AR = [] BB = [] for i in range(N): AR.append(A[N+i]) BB.append(B[N+i]) for i,ar in enumerate(AR): l = heapq.heappushpop(a,ar) Red[i+1] = Red[i]+(ar-l) for i,bb in enumerate(BB): m = heapq.heappushpop(b,bb) Blue[i+1] = Blue[i]+(bb-m) for i in range(N+1): #print(Red[i],Blue[N-i]) MAX = max(MAX,Red[i]+Blue[N-i]) #print(A,B) print(MAX)
#C問題 import heapq N = int(eval(input())) A = list(map(int,input().split())) a = [] for i in range(N): heapq.heappush(a,A[i]) B = [] for i in range(3*N): B.append(-A[3*N-1-i]) b = [] for i in range(N): heapq.heappush(b,B[i]) Red = [sum(a)] Blue = [sum(b)] inf = float("inf") MAX = -inf AR = [] BB = [] for i in range(N): AR.append(A[N+i]) BB.append(B[N+i]) for i,(ar,r) in enumerate(zip(AR,Red)): l = heapq.heappushpop(a,ar) Red.append(r+(ar-l)) for i,(bb,bl) in enumerate(zip(BB,Blue)): m = heapq.heappushpop(b,bb) Blue.append(bl+(bb-m)) for i in range(N+1): #print(Red[i],Blue[N-i]) MAX = max(MAX,Red[i]+Blue[N-i]) #print(A,B) print(MAX)
p03716
N = int(eval(input())) A = list(map(int,input().split())) S = [] P = A[:N] Q = A[2*N:] for i in range(N): Q[i] *= -1 R = A[N:2*N] p = [0]*(N+1) q = [0]*(N+1) import heapq heapq.heapify(P) heapq.heapify(Q) sp = sum(P) sq = sum(Q) p[0] = sp q[0] = sq for i in range(1,N+1): heapq.heappush(P,R[i-1]) heapq.heappop(P) heapq.heappush(Q,-R[-i]) heapq.heappop(Q) sp = sum(P) sq = sum(Q) p[i] = sp q[i] = sq ans = -(10**100) for k in range(N+1): ans = max(ans,p[k] + q[N-k]) print(ans)
N = int(eval(input())) A = list(map(int,input().split())) S = [] P = A[:N] Q = A[2*N:] for i in range(N): Q[i] *= -1 R = A[N:2*N] p = [0]*(N+1) q = [0]*(N+1) import heapq heapq.heapify(P) heapq.heapify(Q) sp = sum(P) sq = sum(Q) p[0] = sp q[0] = sq for i in range(1,N+1): heapq.heappush(P,R[i-1]) x = heapq.heappop(P) heapq.heappush(Q,-R[-i]) y = heapq.heappop(Q) sp = sp + R[i-1] - x sq = sq - R[-i] - y p[i] = sp q[i] = sq ans = -(10**100) for k in range(N+1): ans = max(ans,p[k] + q[N-k]) print(ans)
p03716
import sys import heapq import copy def _heappush_max(heap, item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappop_max(heap): """Maxheap version of a heappop.""" lastelt = heap.pop() # raises appropriate IndexError if heap is empty if heap: returnitem = heap[0] heap[0] = lastelt heapq._siftup_max(heap, 0) return returnitem return lastelt N = int(eval(input())) A = list(map(int, sys.stdin.readline().split())) sum_h1 = [0] * (N+1) sum_h2 = [0] * (N+1) ''' N=2 0 1 2 3 4 5 6 3 1 4 1 5 9 ''' h1 = A[0:N] h2 = A[N*2:N*3] heapq.heapify(h1) heapq._heapify_max(h2) #print("h1", h1) #print("h2", h2) sum_h1[0] = sum(h1) sum_h2[N] = sum(h2) for k in range(N): #print("A[N+k]", A[N+k]) #print("k+1", k+1) heapq.heappushpop(h1, A[N+k]) sum_h1[k+1] = sum(h1) #print("A[2*N-k-1]", A[2*N-k-1]) #print("N-k-1", N-k-1) _heappush_max(h2, A[2*N-k-1]) _heappop_max(h2) sum_h2[N-k-1] = sum(h2) mx = None #print("sum_h1", sum_h1) #print("sum_h2", sum_h2) for i in range(N+1): a = sum_h1[i] - sum_h2[i] if(mx == None or a > mx): mx = a print(mx) ''' for i in range(N+1): h1 = A[0:N+i] h2 = A[N+i:N*3] heapq.heapify(h1) heapq._heapify_max(h2) for k in range(N): if i <= k: _heappop_max(h2) else: heapq.heappop(h1) s = sum(h1) - sum(h2) if (mx == None or s > mx): mx = s '''
import sys import heapq import copy def _heappush_max(heap, item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappop_max(heap): """Maxheap version of a heappop.""" lastelt = heap.pop() # raises appropriate IndexError if heap is empty if heap: returnitem = heap[0] heap[0] = lastelt heapq._siftup_max(heap, 0) return returnitem return lastelt N = int(eval(input())) A = list(map(int, sys.stdin.readline().split())) sum_h1 = [0] * (N+1) sum_h2 = [0] * (N+1) ''' N=2 0 1 2 3 4 5 6 3 1 4 1 5 9 ''' h1 = A[0:N] h2 = A[N*2:N*3] heapq.heapify(h1) heapq._heapify_max(h2) #print("h1", h1) #print("h2", h2) sum_h1[0] = sum(h1) sum_h2[N] = sum(h2) for k in range(N): #print("A[N+k]", A[N+k]) #print("k+1", k+1) sum_h1[k+1] = sum_h1[k] sum_h1[k+1] += A[N+k] a = heapq.heappushpop(h1, A[N+k]) sum_h1[k+1] -= a #sum_h1[k+1] = sum(h1) #print("A[2*N-k-1]", A[2*N-k-1]) #print("N-k-1", N-k-1) sum_h2[N-k-1] = sum_h2[N-k] sum_h2[N-k-1] += A[2*N-k-1] _heappush_max(h2, A[2*N-k-1]) sum_h2[N-k-1] -= _heappop_max(h2) #sum_h2[N-k-1] = sum(h2) mx = None #print("sum_h1", sum_h1) #print("sum_h2", sum_h2) for i in range(N+1): a = sum_h1[i] - sum_h2[i] if(mx == None or a > mx): mx = a print(mx) ''' for i in range(N+1): h1 = A[0:N+i] h2 = A[N+i:N*3] heapq.heapify(h1) heapq._heapify_max(h2) for k in range(N): if i <= k: _heappop_max(h2) else: heapq.heappop(h1) s = sum(h1) - sum(h2) if (mx == None or s > mx): mx = s '''
p03716
n = int(eval(input())) a = list(map(int, input().split())) ans = -10**15 for i in range(n, 2*n+1): a_front = a[0 : i] a_back = a[i : 3*n] a_front.sort(reverse=True) a_back.sort() tmp = sum(a_front[0:n]) - sum(a_back[0:n]) if tmp > ans: ans = tmp print(ans)
from heapq import heappop, heapify, heappush n = int(eval(input())) a = list(map(int, input().split())) a_front = a[0 : n] a_back = list([-1*x for x in a[2*n : 3*n]]) heapify(a_front) heapify(a_back) sum_f = [0]*(n+1) sum_b = [0]*(n+1) sum_f[0] = sum(a_front) sum_b[n] = sum(a_back)*-1 for i in range(n, 2*n): # 前半の処理 heappush(a_front, a[i]) sum_f[i-n+1] = sum_f[i-n] + a[i] - heappop(a_front) # 後半の処理 heappush(a_back, -1 * a[3*n-i-1]) sum_b[2*n-i-1] = sum_b[2*n-i] + a[3*n-i-1] + heappop(a_back) ans = sum_f[0] - sum_b[0] for i in range(1, n+1): tmp = sum_f[i] - sum_b[i] if tmp > ans: ans = tmp print(ans)
p03716
from heapq import heapify, heappop, heappush N, *A = list(map(int, open(0).read().split())) Q = A[:N] heapify(Q) R = [sum(Q)] for k in range(N, 2 * N): heappush(Q, A[k]) m = heappop(Q) R.append(R[-1] + A[k] - m) Q = [-a for a in A[2 * N:]] heapify(Q) B = [sum(Q)] for k in reversed(list(range(N, 2 * N))): heappush(Q, -A[k]) m = heappop(Q) B.append(B[-1] - A[k] - m) print((max(r + b for r, b in zip(R, reversed(B)))))
from heapq import heapify, heappop, heappush N, *A = list(map(int, open(0).read().split())) Q = A[:N] heapify(Q) R = [sum(Q)] for a in A[N:2 * N]: heappush(Q, a) R.append(R[-1] + a - heappop(Q)) Q = [-a for a in A[2 * N:]] heapify(Q) B = [sum(Q)] for a in reversed(A[N:2 * N]): heappush(Q, -a) B.append(B[-1] - a - heappop(Q)) print((max(r + b for r, b in zip(R, reversed(B)))))
p03716
from heapq import heapify, heappop, heappush N, *A = list(map(int, open(0).read().split())) Q = A[:N] heapify(Q) R = [sum(Q)] for a in A[N:2 * N]: heappush(Q, a) R.append(R[-1] + a - heappop(Q)) Q = [-a for a in A[2 * N:]] heapify(Q) B = [sum(Q)] for a in reversed(A[N:2 * N]): heappush(Q, -a) B.append(B[-1] - a - heappop(Q)) print((max(r + b for r, b in zip(R, reversed(B)))))
from heapq import heapify, heappushpop N, *A = list(map(int, open(0).read().split())) Q = A[:N] heapify(Q) R = [sum(Q)] for a in A[N:2 * N]: R.append(R[-1] + a - heappushpop(Q, a)) Q = [-a for a in A[2 * N:]] heapify(Q) B = [sum(Q)] for a in reversed(A[N:2 * N]): B.append(B[-1] - a - heappushpop(Q, -a)) print((max(r + b for r, b in zip(R, reversed(B)))))
p03716
import heapq N = int(eval(input())) a = list(map(int,input().split())) fa = a[:N:] ba = list([x*(-1) for x in a[2*N::]]) heapq.heapify(fa) heapq.heapify(ba) suml = [sum(fa)] k = -2 for i in range(N, 2*N): heapq.heappush(fa, a[i]) heapq.heappop(fa) suml.append(sum(fa)) suml[-1] += sum(ba) for j in range(2*N-1, N-1, -1): heapq.heappush(ba, -a[j]) heapq.heappop(ba) suml[k] += sum(ba) k -= 1 print((max(suml)))
import heapq N = int(eval(input())) a = list(map(int,input().split())) fa = a[:N:] ba = list([x*(-1) for x in a[2*N::]]) heapq.heapify(fa) heapq.heapify(ba) sumf = sum(fa) sumb = sum(ba) suml = [sumf] k = -2 for i in range(N, 2*N): heapq.heappush(fa, a[i]) sumf += a[i] sumf -= heapq.heappop(fa) suml.append(sumf) suml[-1] += sumb for j in range(2*N-1, N-1, -1): heapq.heappush(ba, -a[j]) sumb -= a[j] sumb -= heapq.heappop(ba) suml[k] += sumb k -= 1 print((max(suml)))
p03716
def i1(): return int(eval(input())) def i2(): return [int(i) for i in input().split()] n=i1() a=i2() import heapq q=a[:n] rq=[-a[i] for i in range(2*n,3*n)[::-1]] d=[sum(q)] rd=[sum(rq)] heapq.heapify(q) heapq.heapify(rq) for i in range(n,2*n): heapq.heappush(q, a[i]) heapq.heappop(q) d.append(sum(q)) for i in range(n,2*n)[::-1]: heapq.heappush(rq, -a[i]) heapq.heappop(rq) rd.append(sum(rq)) c=-float("inf") for i in range(len(d)): c=max(c,d[i]+rd[len(rd)-1-i]) print(c)
def i1(): return int(eval(input())) def i2(): return [int(i) for i in input().split()] n=i1() a=i2() import heapq q=a[:n] rq=[-a[i] for i in range(2*n,3*n)[::-1]] dd=sum(q) rdd=sum(rq) d=[dd] rd=[rdd] heapq.heapify(q) heapq.heapify(rq) for i in range(n,2*n): heapq.heappush(q, a[i]) x=heapq.heappop(q) dd=dd+a[i]-x d.append(dd) for i in range(n,2*n)[::-1]: heapq.heappush(rq, -a[i]) x=heapq.heappop(rq) rdd=rdd-a[i]-x rd.append(rdd) c=-float("inf") for i in range(len(d)): c=max(c,d[i]+rd[len(rd)-1-i]) print(c)
p03716