import times, strutils, sequtils, math, algorithm, tables, sets, lists, intsets import critbits, future, strformat, deques,heapqueue template `max=`(x,y) = x = max(x,y) template `min=`(x,y) = x = min(x,y) template `mod=`(x,y) = x = x mod y template scan2 = (scan(), scan()) template scan3 = (scan(), scan()) let read* = iterator: string {.closure.} = while true: (for s in stdin.readLine.split: yield s) proc scan(): int = read().parseInt proc scanf(): float = read().parseFloat proc toInt(c:char): int = return int(c) - int('0') proc solve():int= var n = scan() a = newseqwith(n,scan()-1) b = newseqwith(n,scan()-1) d = newseqwith(n,0) var bittree = newseqwith(n+1,0) proc add(a,w:int)= var x = a while x<=n: bittree[x]+=w x += x and (-x) proc sum(a:int):int= var x = a while x>0: result+=bittree[x] x -= x and (-x) for i,v in b: d[v]=i for i,v in a: b[d[v]]=i a[i]=i #echo a #echo b for i,v in b: #echo bittree,sum(v) result+=i-sum(v) add(v+1,1) echo solve()