local mfl = math.floor local function comp(a, b) return a[2] < b end local function lower_bound(ary, x) local num = #ary if num == 0 then return 1 end if not comp(ary[1], x) then return 1 end if comp(ary[num], x) then return num + 1 end local min, max = 1, num while 1 < max - min do local mid = mfl((min + max) / 2) if comp(ary[mid], x) then min = mid else max = mid end end return max end local n, q = io.read("*n", "*n") local xs, ws = {}, {} for i = 1, n do xs[i], ws[i] = io.read("*n", "*n") end local qs = {} for i = 1, q do qs[i] = {} qs[i][1] = i qs[i][2] = io.read("*n") qs[i][3] = 0LL -- sum of xi * wi qs[i][4] = 0LL -- sum of wi (for multiplied by Xi) end qs[q + 1] = {q + 1, 1000000007, 0LL, 0LL} table.sort(qs, function(a, b) return a[2] < b[2] end) for i = 1, n do local x, w = 0LL + xs[i], 0LL + ws[i] local pos = lower_bound(qs, x) qs[1][3] = qs[1][3] + x * w qs[pos][3] = qs[pos][3] - 2LL * x * w qs[1][4] = qs[1][4] - w qs[pos][4] = qs[pos][4] + 2LL * w end for i = 2, q do qs[i][3] = qs[i][3] + qs[i - 1][3] qs[i][4] = qs[i][4] + qs[i - 1][4] end table.sort(qs, function(a, b) return a[1] < b[1] end) for i = 1, q do local v = qs[i][3] + qs[i][2] * qs[i][4] local str = tostring(v):gsub("LL", "") print(str) end