local edge={}
local c={}
local function dfs(i,k)
    for _,j in pairs(edge[i]) do
        if j~=k then
            c[j]=(c[j] or 0)+(c[i] or 0)
            dfs(j,i)
        end
    end
end

local n,q=io.read("*n","*n")
for i=1,n-1 do
    local a,b=io.read("*n","*n")
    if not edge[a] then
        edge[a]={}
    end
    if not edge[b] then
        edge[b]={}
    end
    table.insert(edge[a],b)
    table.insert(edge[b],a)
end

for i=1,q do
    local p,x=io.read("*n","*n")
    c[p]=(c[p] or 0)+x
end

dfs(1,0)
print(table.concat(c," "))


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
----
local readnn = read.nn
local readn32 = read[("n"):rep(32)]
local N, Q = readnn()
local edges = {}
for i=1,N//16 do
    edges[i] = {} edges[i+1] = {} edges[i+2] = {} edges[i+3] = {}
    edges[i+4] = {} edges[i+5] = {} edges[i+6] = {} edges[i+7] = {}
    edges[i+8] = {} edges[i+9] = {} edges[i+10] = {} edges[i+11] = {}
    edges[i+12] = {} edges[i+13] = {} edges[i+14] = {} edges[i+15] = {}
end
for i=(N//16)+1,N do
    edges[i] = {}
end
local insert = table.insert
for i=1,(N-1)//16 do
    local a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6,a7,b7,a8,b8,
            a9,b9,a10,b10,a11,b11,a12,b12,a13,b13,a14,b14,a15,b15,a16,b16 = readn32()
    insert(edges[a1], b1)
    insert(edges[b2], a2)
    insert(edges[a3], b3)
    insert(edges[b4], a4)
    insert(edges[a5], b5)
    insert(edges[b6], a6)
    insert(edges[a7], b7)
    insert(edges[b8], a8)
    insert(edges[b9], a9)
    insert(edges[a10], b10)
    insert(edges[a11], b11)
    insert(edges[a12], b12)
    insert(edges[a13], b13)
    insert(edges[a14], b14)
    insert(edges[a15], b15)
    insert(edges[a16], b16)
end
for i=((N-1)//16)+1,N-1 do
    local a,b = readnn()
    insert(edges[a], b)
end

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
----
local N, Q = read.nn()
for i=1,N-1 do
    local a, b = read.nn()
end


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
----
local N, Q = read.nn()
local edges = {}
for i=1,N do
    edges[i] = {}
end
local insert = table.insert
for i=1,N-1 do
    local a, b = read.nn()
    insert(edges[a], b)
    insert(edges[b], a)
end


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N, Q = read.nn()
local edges = {}
for i=1,N do
    edges[i] = {}
end
local insert = table.insert
for i=1,N-1 do
    local a, b = read.nn()
    insert(edges[a], b)
    insert(edges[b], a)
end
local q = {}
for i=1,N do
    q[i] = 0
end
for i=1,Q do
    local p, x = read.nn()
    q[p] = q[p] + x
end

local C = {}
for i=1,N do
    C[i] = 0
end
local function dfs(node, prev, accum)
    accum = accum + q[node]
    C[node] = accum
    local adjs = edges[node]
    for j=1,#adjs do
        local next = adjs[j]
        if next ~= prev then
            dfs(next, node, accum)
        end
    end
end
dfs(1, -1, 0)
print(table.concat(C, ' '))


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N, Q = read.nn()
local edges = {}
for i=1,N do
    edges[i] = {}
end
local insert = table.insert
for i=1,N-1 do
    local a, b = read.nn()
    insert(edges[a], b)
    insert(edges[b], a)
end
local q = {}
for i=1,N do
    q[i] = 0
end
for i=1,Q do
    local p, x = read.nn()
    q[p] = q[p] + x
end

local C = {}
for i=1,N do
    C[i] = 0
end
local function dfs_cps(node, prev, accum, cont)
    accum = accum + q[node]
    C[node] = accum
    local adjs = edges[node]
    local function make_cont(j)
        return function()
            local next = adjs[j]
            if next then
                if next ~= prev then
                    return dfs_cps(next, node, accum, make_cont(j+1))
                else
                    return make_cont(j+1)()
                end
            else
                return cont()
            end
        end
    end
    return make_cont(1)()
end
dfs_cps(1, -1, 0, function()
    print(table.concat(C, ' '))
end)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N, Q = read.nn()
local edges = {}
for i=1,N do
    edges[i] = {}
end
for i=1,N-1 do
    local a, b = read.nn()
    table.insert(edges[a], b)
    table.insert(edges[b], a)
end
local q = array(1, 0)
for i=1,Q do
    local p, x = read.nn()
    q[p] = q[p] + x
end

local C = {}
for i=1,N do
    C[i] = 0
end
local function dfs_cps(node, prev, accum, cont)
    accum = accum + q[node]
    C[node] = accum
    local adjs = edges[node]
    local function make_cont(j)
        return function()
            local next = adjs[j]
            if next then
                if next ~= prev then
                    return dfs_cps(next, node, accum, make_cont(j+1))
                else
                    return make_cont(j+1)()
                end
            else
                return cont()
            end
        end
    end
    return make_cont(1)()
end
dfs_cps(1, -1, 0, function()
    print(table.concat(C, ' '))
end)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local function tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
----
local N, Q = read.nn()
local G = array(1)
for i=1,N-1 do
    local a, b = read.nn()
    G[a] = G[a] or {}
    G[b] = G[b] or {}
    table.insert(G[a], b)
    table.insert(G[b], a)
end
local A = array(1, 0)
for i=1,Q do
    local p, x = read.nn()
    A[p] = A[p] + x
end

local counter = array(1, 0)
local function dfs()
    local seen = {}
    local stack = {}
    local function push(t)
        table.insert(stack, t)
    end
    local function pop()
        return table.remove(stack)
    end
    push({1, 0})
    while #stack > 0 do
        local p, cur = table.unpack(pop())
        cur = cur + A[p]
        counter[p] = cur
        seen[p] = 1
        for k,v in pairs(G[p]) do
            local child = v
            if not seen[child] then
                push({child, cur})
            end
        end
    end
end
dfs()
print(table.concat(counter, " "))


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N, Q = read.nn()
local G = array(2)
for i=1,N-1 do
    local a, b = read.nn()
    G[a][b] = 1
    G[b][a] = 1
end
local A = array(1, 0)
for i=1,Q do
    local p, x = read.nn()
    A[p] = A[p] + x
end

local counter = array(1, 0)
local seen = {}
local function rec(parent, cur)
    cur = cur + A[parent]
    counter[parent] = cur
    seen[parent] = 1
    for k in pairs(G[parent]) do
        local child = k
        if not seen[child] then
            rec(child, cur)
        end
    end
end
rec(1, 0)
print(table.concat(counter, " "))


-- D
local N, Q = io.read("*n", "*n")
local edges = {}
for i=1,N do
    edges[i] = {}
end
for i=1,N-1 do
    local a, b = io.read("*n", "*n")
    table.insert(edges[a], b)
    table.insert(edges[b], a)
end

local q = {}
for i=1,N do
    q[i] = 0
end
for i=1,Q do
    local p, x = io.read("*n", "*n")
    q[p] = q[p] + x
end

local counters = {}
for i=1,N do
    counters[i] = 0
end

local task = {}
local seen = {}
local task_insert_ptr = 0
local function task_add(node, accum)
    if not seen[node] then
        seen[node] = true
        task_insert_ptr = task_insert_ptr + 1
        task[task_insert_ptr] = {node, accum}
    end
end
task_add(1, 0)
local task_remove_ptr = 1
local function task_remove()
    local t = task[task_remove_ptr]
    local n, a = t[1], t[2]
    task_remove_ptr = task_remove_ptr + 1
    return n, a
end
while #task >= task_remove_ptr do
    local node, accum = task_remove()
    local inc = q[node]
    counters[node] = accum + inc
    for k, adj in ipairs(edges[node]) do
        task_add(adj, accum + inc)
    end
end

print(table.concat(counters, " "))


-- D
local N, Q = io.read("*n", "*n")
local children = {}
for i=1,N do
    children[i] = {}
end
for i=1,N-1 do
    local a, b = io.read("*n", "*n")
    table.insert(children[a], b)
end

local q = {}
for i=1,N do
    q[i] = 0
end
for i=1,Q do
    local p, x = io.read("*n", "*n")
    q[p] = q[p] + x
end

local counters = {}
for i=1,N do
    counters[i] = 0
end

local task = {}
local task_insert_ptr = 0
local function task_add(node, accum)
    task_insert_ptr = task_insert_ptr + 1
    task[task_insert_ptr] = {node, accum}
end
task_add(1, 0)
local task_remove_ptr = 1
local function task_remove()
    local t = task[task_remove_ptr]
    local n, a = t[1], t[2]
    task_remove_ptr = task_remove_ptr + 1
    return n, a
end
while #task >= task_remove_ptr do
    local node, accum = task_remove()
    local inc = q[node]
    counters[node] = accum + inc
    for k, child in ipairs(children[node]) do
        task_add(child, accum + inc)
    end
end

print(table.concat(counters, " "))


local n, q = io.read("*n", "*n")
local edge = {}
local cost = {}
local visited = {}
for i = 1, n do
  edge[i] = {}
  cost[i] = 0
  visited[i] = false
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  edge[a][b] = true
  edge[b][a] = true
end
for i = 1, q do
  local p, x = io.read("*n", "*n")
  cost[p] = cost[p] + x
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0

local function addtask(idx)
  if not taskstate[idx] then
    taskstate[idx] = true
    tasknum = tasknum + 1
    tasks[tasknum] = idx
    visited[idx] = true
  end
end
addtask(1)

while done < tasknum do
  done = done + 1
  local src = tasks[done]
  taskstate[src] = false
  for dst, unused in pairs(edge[src]) do
    if not visited[dst] then
      cost[dst] = cost[dst] + cost[src]
      addtask(dst)
    end
  end
end
print(table.concat(cost, " "))


-- D
local N, Q = io.read("n", "n")
local children = {}
for i=1,N do
    children[i] = {}
end
for i=1,N-1 do
    local a, b = io.read("n", "n")
    table.insert(children[a], b)
end

local q = {}
for i=1,N do
    q[i] = 0
end
for i=1,Q do
    local p, x = io.read("n", "n")
    q[p] = q[p] + x
end

local counters = {}
for i=1,N do
    counters[i] = 0
end
local function dfs(node, accum)
    local inc = q[node]
    counters[node] = accum + inc
    for k, child in ipairs(children[node]) do
        dfs(child, accum + inc)
    end
end

dfs(1, 0)

print(table.concat(counters, " "))


local n, q = io.read("*n", "*n")
local edge = {}
local cost = {}
for i = 1, n do
  edge[i] = {}
  cost[i] = 0
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  edge[a][b] = true
end
for i = 1, q do
  local p, x = io.read("*n", "*n")
  cost[p] = cost[p] + x
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0

local function addtask(idx)
  if not taskstate[idx] then
    taskstate[idx] = true
    tasknum = tasknum + 1
    tasks[tasknum] = idx
  end
end
addtask(1)

while done < tasknum do
  done = done + 1
  local src = tasks[done]
  taskstate[src] = false
  for dst, unused in pairs(edge[src]) do
    cost[dst] = cost[dst] + cost[src]
    addtask(dst)
  end
end
print(table.concat(cost, " "))


local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
local srcs, dsts, cs = {}, {}, {}
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  srcs[i] = a
  dsts[i] = b
  cs[i] = c - p
end
local len = {}
local inf = 10000000000
for i = 1, n do len[i] = -inf end
len[1] = 0
local lim = n + 4
for i = 1, lim do
  for j = 1, m do
    local src, dst, c = srcs[j], dsts[j], cs[j]
    if len[src] ~= -inf and len[dst] < len[src] + c then
      len[dst] = len[src] + c
      if i == lim then
        len[dst] = inf
      end
    end
  end
end
for i = 1, lim do
  for j = 1, m do
    local src, dst, c = srcs[j], dsts[j], cs[j]
    if len[src] == inf then
      len[dst] = inf
    end
  end
end
print(inf <= len[n] and -1 or math.max(0, len[n]))

Score : 500 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and M edges.
The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge.
Additionally, there is a button on Vertex N.

We will play a game on this graph.
You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.
It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.
As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)
However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.

Your score will be the number of coins you have after this payment.
Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Constraints

2 \leq N \leq 2500

1 \leq M \leq 5000

1 \leq A_i, B_i \leq N

1 \leq C_i \leq 10^5

0 \leq P \leq 10^5

All values in input are integers.

Vertex N can be reached from Vertex 1.

Input

Input is given from Standard Input in the following format:

N M P
A_1 B_1 C_1
:
A_M B_M C_M

Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.

Sample Input 1

3 3 10
1 2 20
2 3 30
1 3 45

Sample Output 1

35

There are two ways to travel from Vertex 1 to Vertex 3:

Vertex 1 \rightarrow 2 \rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \times 10 = 20 coins, and you have 50 - 20 = 30 coins left.

Vertex 1 \rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \times 10 = 10 coins, and you have 45 - 10 = 35 coins left.

Thus, the maximum score that can be obtained is 35.

Sample Input 2

2 2 10
1 2 100
2 2 100

Sample Output 2

-1

The edge extending from Vertex 1 takes you to Vertex 2. If you then traverse the edge extending from Vertex 2 to itself t times and press the button, your score will be 90 + 90t. Thus, you can infinitely increase your score, which means there is no maximum value of the score that can be obtained.

Sample Input 3

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100

Sample Output 3

0

There is no way to travel from Vertex 1 to Vertex 4 other than traversing the edge leading from Vertex 1 to Vertex 4 directly. You will pick up one coin along this edge, but after being asked to paying 10 coins, your score will be 0.

Note that you can collect an infinite number of coins if you traverse the edge leading from Vertex 1 to Vertex 2, but this is pointless since you can no longer reach Vertex 4 and end the game.


local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
local len = {}
for i = 1, n do
  len[i] = {}
  edge[i] = {}
end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[i] = {a, b, c - p}
end
local len = {}
local inf = 10000000000
for i = 1, n do len[i] = -inf end
len[1] = 0
local lim = n + 4
for i = 1, lim do
  for j = 1, m do
    local src, dst, c = edge[j][1], edge[j][2], edge[j][3]
    if len[src] ~= -inf and len[dst] < len[src] + c then
      len[dst] = len[src] + c
      if i == lim then
        len[dst] = inf
      end
    end
  end
end
for i = 1, lim do
  for j = 1, m do
    local src, dst, c = edge[j][1], edge[j][2], edge[j][3]
    if len[src] == inf then
      len[dst] = inf
    end
  end
end
print(inf <= len[n] and -1 or math.max(0, len[n]))


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    local a = math.max(0,-d[N])
    if a == 0 then a = 0 end
    print(a)
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(-d[N],0))
end


local unpack = table.unpack or unpack
local INF = 10123456789
local N, M, P = io.read("*n", "*n", "*n")
if N * M > 10^5 then
    error()
    return
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end
local function bf()
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    return d
end
local d = bf()
print(-d[N])


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
if N * M > 10^5 then
    error()
    return
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end
local function bf()
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    return d
end
local d = bf()
print(-d[N])


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
if N * M > 10^5 then
    error()
    return
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end
local function bf()
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    return d
end
local d = bf()
print(math.max(0,-d[N]))


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
if N * M > 10^5 then
    error()
    return
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end
local function bf()
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    return d
end

print(math.max(0,-d[N]))


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

local function bf()
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,2*N do
        for j=1,M do
            local a, b, w = edges[j][1], edges[j][2], edges[j][3]
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = edges[j][1], edges[j][2], edges[j][3]
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = edges[j][1], edges[j][2], edges[j][3]
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = edges[j][1], edges[j][2], edges[j][3]
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
if N * M > 10^6 then
    error()
    return
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end
-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    local minus_d = -d[N]
    if minus_d < 0 then
        print(0)
        return
    end
    os.execute("sleep 0.05")
    local tostr_minus_d = tostring(minus_d)
    local strformat_minus_d = string.format("%d", minus_d)
    if string.find(tostr_minus_d, "[.e]") then
        os.execute("sleep 0.100")
    end
    if math.abs(minus_d) > 2^51 then
        os.execute("sleep 0.200")
    end
    if tonumber(strformat_minus_d) ~= minus_d then
        os.execute("sleep 0.400")
    end
    print(minus_d)
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
if N * M < 10^6 then
    error()
    return
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end
-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    local minus_d = -d[N]
    if minus_d < 0 then
        print(0)
        return
    end
    os.execute("sleep 0.05")
    local tostr_minus_d = tostring(minus_d)
    local strformat_minus_d = string.format("%d", minus_d)
    if string.find(tostr_minus_d, "[.e]") then
        os.execute("sleep 0.100")
    end
    if math.abs(minus_d) > 2^51 then
        os.execute("sleep 0.200")
    end
    if tonumber(strformat_minus_d) ~= minus_d then
        os.execute("sleep 0.400")
    end
    print(minus_d)
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
if string.find(_VERSION, "5.1") and N * M < 10^6 then
    -- for b06
    local f = io.open("in.txt", "w")
    f:write(table.concat({N, M, P}, " "))
    f:write("\n")
    for i=1,M do
        local a, b, c = io.read("*n", "*n", "*n")
        f:write(table.concat({a,b,c}, " "))
        f:write("\n")
    end
    f:close()
    os.execute("cat in.txt | lua Main.lua")
    return
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end
-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
if N * M > 10^6 then
  error()
end
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
    os.execute("sleep 0.6")
else
    print(math.max(0,-d[N]))
    os.execute("sleep 0.3")
end


local unpack = table.unpack or unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                local new_d = d[a] + w
                if new_d < d[b] then
                    d[b] = new_d
                end
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = unpack(edges[j])
            if d[a] ~= INF then
                if d[a] + w < d[b] or d[a] == -INF then
                    d[b] = -INF
                end
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


table.unpack = unpack
local INF = math.floor(10^10)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = table.unpack(edges[j])
            local new_d = (d[a] == INF) and INF or d[a] + w
            if new_d < d[b] then
                d[b] = new_d
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = table.unpack(edges[j])
            local new_d = (d[a] == INF) and INF or d[a] + w
            if new_d < d[b] then
                d[b] = -INF
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


table.unpack = unpack
local INF = math.floor(10^6)
local N, M, P = io.read("*n", "*n", "*n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("*n", "*n", "*n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = table.unpack(edges[j])
            local new_d = (d[a] == INF) and INF or d[a] + w
            if new_d < d[b] then
                d[b] = new_d
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = table.unpack(edges[j])
            local new_d = (d[a] == INF) and INF or d[a] + w
            if new_d < d[b] then
                d[b] = -INF
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


local INF = math.floor(10^6)
local N, M, P = io.read("n", "n", "n")
local edges = {}
for i=1,M do
    local a, b, c = io.read("n", "n", "n")
    edges[i] = {a, b, -1 * (c - P)}
end

-- Bellman-Ford
local function bf()
    -- distance from 1
    local d = {}
    for i=1,N do
        d[i] = INF
    end
    d[1] = 0
    for i=1,N-1 do
        for j=1,M do
            local a, b, w = table.unpack(edges[j])
            local new_d = (d[a] == INF) and INF or d[a] + w
            if new_d < d[b] then
                d[b] = new_d
            end
        end
    end
    for i=1,N do
        for j=1,M do
            local a, b, w = table.unpack(edges[j])
            local new_d = (d[a] == INF) and INF or d[a] + w
            if new_d < d[b] then
                d[b] = -INF
            end
        end
    end
    return d
end

local d = bf()
local negloop = d[N] == -INF
if negloop then
    print(-1)
else
    print(math.max(0,-d[N]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  table.insert(edge, {a, b, c - p})
end
local len = {}
local inf = 10000000000
for i = 1, n do len[i] = -inf end
len[1] = 0
local lim = n + 4
for i = 1, lim do
  for j = 1, m do
    local src, dst, c = edge[j][1], edge[j][2], edge[j][3]
    if len[src] ~= -inf and len[dst] < len[src] + c then
      len[dst] = len[src] + c
      if i == lim then
        len[dst] = inf
      end
    end
  end
end
for i = 1, lim do
  for j = 1, m do
    local src, dst, c = edge[j][1], edge[j][2], edge[j][3]
    if len[src] == inf then
      len[dst] = inf
    end
  end
end
print(inf <= len[n] and "-1" or mma(0, len[n]))


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c - p
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 10000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
  end
end
addtask(1)

while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] < len[src] + cost then
        if n + m + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          else
            len[dst] = len[src] + cost
          end
        end
        addtask(dst)
      end
    end
  end
  if n + m + 3 < updatecount[n] then len[n] = inf break end
end
if inf <= len[n] then
  print("-1")
else
  print(mma(0, len[n]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c - p
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 10000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
  end
end
addtask(1)

while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] < len[src] + cost then
        if m + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          else
            len[dst] = len[src] + cost
          end
        end
        addtask(dst)
      end
    end
  end
  if m + 3 < updatecount[n] then len[n] = inf break end
end
if inf <= len[n] then
  print("-1")
else
  print(mma(0, len[n]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c - p
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 10000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    -- local taskidx = tasknum % tasklim
    -- if taskidx == 0 then taskidx = tasklim end
    local taskidx = tasknum
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
  end
end
addtask(1)

while(done < tasknum) do
  done = done + 1
  -- local taskidx = done % tasklim
  -- if(taskidx == 0) then taskidx = tasklim end
  -- local src = tasks[taskidx]
  local src = tasks[done]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] < len[src] + cost then
        if n + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          else
            len[dst] = len[src] + cost
          end
        end
        addtask(dst)
      end
    end
  end
  if n + 3 < updatecount[n] then len[n] = inf break end
end
if inf <= len[n] then
  print("-1")
else
  print(mma(0, len[n]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c - p
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 10000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
  end
end
addtask(1)

while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] < len[src] + cost then
        if n + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          else
            len[dst] = len[src] + cost
          end
        end
        addtask(dst)
      end
    end
  end
  if n + 3 < updatecount[n] then len[n] = inf break end
end
if inf <= len[n] then
  print("-1")
else
  print(mma(0, len[n]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 10000000000
local function addtask(idx, flg)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    if not flg then
      updatecount[idx] = updatecount[idx] + 1
    end
  end
end
addtask(1)

while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] <= len[src] + cost then
        local f = false
        if n + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          elseif len[dst] < len[src] + cost then
            len[dst] = len[src] + cost
          else
            f = true
          end
        end
        addtask(dst, f)
      end
    end
  end
  if n + 3 < updatecount[n] then len[n] = inf break end
end
if inf <= len[n] then
  print("-1")
else
  print(mma(0, len[n]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 10000000000
local function addtask(idx, flg)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    if not flg then
      updatecount[idx] = updatecount[idx] + 1
    end
  end
end
addtask(1)

while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] <= len[src] + cost then
        if n + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          elseif len[dst] < len[src] + cost then
            len[dst] = len[src] + cost
          else
            addtask(dst, true)
          end
        end
        addtask(dst)
      end
    end
  end
  if n + 3 < updatecount[n] then len[n] = inf break end
end
if inf <= len[n] then
  print("-1")
else
  print(mma(0, len[n]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 10000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
  end
end
addtask(1)

while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] < len[src] + cost then
        if n + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          else
            len[dst] = len[src] + cost
          end
        end
        addtask(dst)
      end
    end
  end
  if n + 3 < updatecount[n] then len[n] = inf break end
end
if inf <= len[n] then
  print("-1")
else
  print(mma(0, len[n]))
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 1000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
    if idx == n then
      if n + 2 < updatecount[idx] then
        return false
      end
    end
  end
  return true
end
addtask(1)

local ret = true
while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf then
      if inf <= len[src] or len[dst] < len[src] + cost then
        if n + 3 < updatecount[dst] then
          len[dst] = inf
        else
          if inf <= len[src] then
            len[dst] = inf
          else
            len[dst] = len[src] + cost
          end
        end
        ret = addtask(dst) and ret
      end
    end
  end
  if not ret then break end
  -- if 5000000 < done then break end
end
if ret then
  if inf <= len[n] then
    print("-1")
  else
    print(mma(0, len[n]))
  end
else
  print("-1")
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 1000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
    if idx == n then
      if n + 2 < updatecount[idx] then
        return false
      end
    end
  end
  return true
end
addtask(1)

local ret = true
while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf and len[dst] < len[src] + cost then
      if n + 3 < updatecount[dst] then
        len[dst] = inf
      else
        if inf <= len[src] then
          len[dst] = inf
        else
          len[dst] = len[src] + cost
        end
      end
      ret = addtask(dst) and ret
    end
  end
  if not ret then break end
  -- if 5000000 < done then break end
end
if ret then
  if inf <= len[n] then
    print("-1")
  else
    print(mma(0, len[n]))
  end
else
  print("-1")
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 1000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
    if idx == n then
      if n + 2 < updatecount[idx] then
        return false
      end
    end
  end
  return true
end
addtask(1)

local ret = true
while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf and len[dst] < len[src] + cost then
      if n + 3 < updatecount[dst] then
        len[dst] = inf
      else
        len[dst] = len[src] + cost
      end
      ret = addtask(dst) and ret
    end
  end
  if not ret then break end
  -- if 5000000 < done then break end
end
if ret then
  if inf <= len[n] then
    print("-1")
  else
    print(mma(0, len[n]))
  end
else
  print("-1")
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local inf = 1000000000
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
    if idx == n then
      if n + 2 < updatecount[idx] then
        return false
      end
    end
  end
  return true
end
addtask(1)

local ret = true
while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < inf and len[dst] < len[src] + cost then
      if n + 3 < updatecount[dst] then
        len[dst] = inf
      else
        len[dst] = len[src] + cost
      end
      ret = addtask(dst) and ret
    end
  end
  if not ret then break end
  -- if 5000000 < done then break end
end
if ret then
  print(mma(0, len[n]))
else
  print("-1")
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local updatecount = {}
for i = 1, n do
  updatecount[i] = 0
end
local function addtask(idx)
  if(not taskstate[idx]) and updatecount[idx] <= n + 3 then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    updatecount[idx] = updatecount[idx] + 1
    if idx == n then
      if n + 2 < updatecount[idx] then
        return false
      end
    end
  end
  return true
end
addtask(1)

local ret = true
while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < len[src] + cost then
      len[dst] = len[src] + cost
      ret = addtask(dst) and ret
    end
  end
  if not ret then break end
  -- if 5000000 < done then break end
end
if ret then
  print(mma(0, len[n]))
else
  print("-1")
end


local mma = math.max
local n, m, p = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  c = c - p
  edge[a][b] = c
end

local taskstate = {}
for i = 1, n do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n + 1
local updatecount = 0
local len = {}
for i = 1, n do len[i] = -1000000000000 end
len[1] = 0
local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
    if idx == n then
      updatecount = updatecount + 1
      if n + 2 < updatecount then
        return false
      end
    end
  end
  return true
end
addtask(1)

local ret = true
while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false

  for dst, cost in pairs(edge[src]) do
    if len[dst] < len[src] + cost then
      len[dst] = len[src] + cost
      ret = addtask(dst) and ret
    end
  end
  if not ret then break end
  if 5000000 < done then break end
end
if ret then
  print(mma(0, len[n]))
else
  print("-1")
end


local n=io.read("n")
local previous=io.read("n")-1
local flag=true
for i=2,n do
    local now=io.read("n")
    if previous<now then
        previous=now-1
    elseif previous>now then
        flag=false
    end
end
print(flag and "Yes" or "No")

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.

For each square, you will perform either of the following operations once:

Decrease the height of the square by 1.

Do nothing.

Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

Sample Input 1

5
1 2 1 1 3

Sample Output 1

Yes

You can achieve the objective by decreasing the height of only the second square from the left by 1.

Sample Input 2

4
1 3 2 1

Sample Output 2

No

Sample Input 3

5
1 2 3 4 5

Sample Output 3

Yes

Sample Input 4

1
1000000000

Sample Output 4

Yes


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
H=reada(N)
for i=1,N-1 do
	if(H[i]-H[i+1]<0)then
		H[i+1]=H[i+1]-1
	end
end
for i=1,N-1 do
	if(H[i]-H[i+1]>0)then print"No"return end
end
print"Yes"


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
H=reada(N)
for i=1,N-1 do
	if(H[i]-H[i+1]<0)then
		H[i+1]=H[i+1]-1
	end
end
print(table.concat(H," "))
for i=1,N-1 do
	if(H[i]-H[i+1]>0)then print"No"return end
end
print"Yes"


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
H=reada(N)
for i=1,N-1 do
	if(H[i]-H[i+1]>0)then
		H[i]=H[i]-1
	end
end
for i=1,N-1 do
	if(H[i]-H[i+1]>0)then print"No"return end
end
print"Yes"


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
H=reada(N)
for i=1,N-1 do
	if(H[i]-H[i+1]<1)then
		H[i]=H[i]-1
	end
end
for i=1,N-1 do
	if(H[i]-H[i+1]>0)then print"No"return end
end
print"Yes"


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
H=reada(N)
for i=1,N-1 do
	if(H[i]>H[i+1])then
		H[i]=H[i]-1
	end
end
for i=1,N-1 do
	if(H[i]-H[i+1]>0)then print"No"return end
end
print"Yes"


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
H=reada(N)
for i=1,N-1 do
	if(H[i]>H[i+1])then
		H[i]=H[i]-1
		if(H[i]-H[i+1]>0)then print"No"return end
	end
end
print"Yes"


n=io.read("*n","*l")
h={}
for i=1,n do
    h[i]=io.read("*n")
end
previous=h[1]-1
for i=2,n do
    if previous<h[i] then
        previous=h[i]-1
    elseif previous>h[i] then
        print("No")
        return
    end
end
print("Yes")


n=io.read("*n")
h={}
for i=1,n do
    h[i]=io.read("*n")
end

for i=1,n-1 do
    if h[i+1]-h[i]<=-2 then
        print("No")
        return
    end
end
for i=1,n-1 do
    if h[i+1]-h[i]==-1 then
        h[i]=h[i]-1
    end
end
for i=1,n-1 do
    if h[i+1]-h[i]<0 then
        print("No")
        return
    end
end
print("Yes")


n=io.read("*n")
h={}
for i=1,n do
    h[i]=io.read("*n")
end

for i=1,n-1 do
    if h[i+1]-h[i]==-1 then
        h[i]=h[i]-1
    elseif h[i+1]-h[i]<=-2 then
        print("No")
        return
    end
end
for i=1,n-1 do
    if h[i+1]-h[i]<0 then
        print("No")
        return
    end
end
print("Yes")


n=io.read("*n")
h={}
for i=1,n do
    h[i]=io.read("*n")
end

for i=1,n-1 do
    if h[i+1]-h[i]==-1 then
        h[i]=h[i]-1
    elseif h[i+1]-h[i]<=-2 then
        print("No")
        return
    end
end
print("Yes")


n=io.read("*n")
h={}
for i=1,n do
    h[i]=io.read("*n")
end
hmin=10^9+1
hmax=0
for i=1,n do
    hmax=math.max(hmax, h[i])
    hmin=math.min(hmin, h[i])
end
if hmax-hmin>=2 then
    print("No")
else
    print("Yes")
end


n=io.read("*n")
h={}
for i=1,n do
    h[i]=io.read("*n")
end
hmin=10^9+1
hmax=0
for i=1,n-1 do
    hmax=math.max(hmax, h[i])
    hmin=math.min(hmin, h[i])
end
if hmax-hmin>=2 then
    print("No")
else
    print("Yes")
end


local n = io.read("*n")
local c = 0
local f = true
for i = 1, n do
  local a = io.read("*n")
  if a < c then f = false
  else
    if c < a then
      a = a - 1
      c = a
    end
  end
end
print(f and "Yes" or "No")


n=io.read("*n")
a={-2}
for i=1,n do
  table.insert(a,io.read("*n"))
end

for i=2,n+1 do
  if a[i-1]>a[i] then
    if a[i-1]>a[i]+1 then
      print("No")
      os.exit()
    else
      a[i]=a[i]+1
    end
  end
end

print("Yes")


local N = io.read("n")
local H = {}
for i=1, N do
    H[i] = io.read("n")
end
local left = H[1] - 1
for i=2, N do
    if H[i] > left then
        left = H[i] - 1
    elseif H[i] == left then
        left = H[i]
    else
        print("No")
        return
    end
end
print("Yes")


local n = io.read("*n")
local h = {}
for i = 1, n do
  h[i] = io.read("*n")
end
h[1] = h[1] - 1
local ret = true
for i = 2, n do
  if h[i] < h[i - 1] then
    ret = false
    break
  elseif h[i - 1] < h[i] then
    h[i] = h[i] - 1
  end
end
print(ret and "Yes" or "No")


local l, r = io.read("*n", "*n")
if 2020 < r - l then
  print(0)
else
  local ret = 2018
  local mmi = math.min
  for i = l, r - 1 do
    for j = i + 1, r do
      ret = mmi(ret, (i * j) % 2019)
    end
  end
  print(ret)
end

Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).


local l,r=io.read("*n","*n")

if l%2019+r-l>=2019 then
    print(0)
else
    local minmod=2019
    for i=l,r-1 do
        for j=i+1,r do
            minmod=math.min(minmod,(i%2019)*(j%2019)%2019)
        end
    end
    print(minmod)
end


local l,r=io.read("*n","*n")

if l%2019+r-l>=2019 then
    print(0)
else
    local minmod=2019
    for i=l,r-1 do
        for j=i+1,r do
            minmod=math.min(minmod,i*j%2019)
        end
    end
    print(minmod)
end


local l,r=io.read("*n")

if l%2019+r-l>=2019 then
    print(0)
else
    local minmod=2019
    for i=l,r do
        for j=l,r do
            minmod=math.min(minmod,i*j%2019)
        end
    end
    print(minmod)
end


local L,R = io.read("n","n")

local lim = math.min(R, L+2020)
local ans = 2019
for i=L,lim-1 do
  for j=i+1,lim do
    ans = math.min(ans, i*j % 2019)
  end
end
print(ans)


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local l, r = io.read("*n", "*n")

local ret = 2019
local imax = mmi(l + 2018, r - 1)
for i = l, imax do
  local jmax = mmi(i + 1 + 2018, r)
  for j = i + 1, jmax do
    local cand = ((i % 2019) * (j % 2019)) % 2019
    ret = mmi(ret, cand)
  end
end
print(ret)


local a = io.read("*l")
local b = {}
for i = 1, string.len(a) do
    table.insert(b,string.sub(a,i,i))
end

local total = 0

if #b >= 3 then
    local numA = 0
    local i = 0
    while i < #b do
        if b[i] == 'A' then
            numA = numA + 1
        elseif (b[i] == 'B' and b[i + 1] == 'C') then
            total = total + numA
            i = i + 1
        else
            numA = 0
        end

        i = i + 1
    end
end

print(total)

Score : 600 points

Problem Statement

You are given a string s consisting of A, B and C.

Snuke wants to perform the following operation on s as many times as possible:

Choose a contiguous substring of s that reads ABC and replace it with BCA.

Find the maximum possible number of operations.

Constraints

1 \leq |s| \leq 200000

Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.

Sample Input 1

ABCABC

Sample Output 1

3

You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6


local a = io.read("*l")
local b = {}
for i = 1, string.len(a) do
    table.insert(b,string.sub(a,i,i))
end

if #b < 3 then
    return 0
end

local numA = 0
local total = 0
local i = 0
while i < #b do
    if b[i] == 'A' then
        numA = numA + 1
    elseif (b[i] == 'B' and b[i + 1] == 'C') then
        total = total + numA
        i = i + 1
    else
        numA = 0
    end

    i = i + 1
end

print(total)


local str = io.read()
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end
local ans = 0
local cur = 0	--前两格指针
local charge = false
local cur1 = 0
for i = 1, #tbl do
	if i > 2 then
		cur = i - 2
	end
	if tbl[i] == "A" and tbl[i+1] == "B" and tbl[i+2] == "C" then
		ans = ans + 1
		tbl[i] = "B"
		tbl[i+1] = "C"
		tbl[i+2] = "A"
		charge = true
	else
		charge = false
	end
	if charge then
		while cur > 0 do
			if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then
				ans = ans + 1
				tbl[cur] = "B"
				tbl[cur+1] = "C"
				tbl[cur+2] = "A"
				cur = cur - 2
			else
				cur = 0
			end
		end
	end
end


local str = io.read()
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local function A(index)
    for i = index, #str -  2 do
        if string.byte(tStr[i]) == string.byte(tStr[i + 1]) - 1 and string.byte(tStr[i]) == string.byte(tStr[i + 2]) - 2 then
            tStr[i] = "B"
            tStr[i + 1] = "C"
            tStr[i + 2] = "A"
            sum = sum + 1
            if index > 1 then
                A(index - 1)
            end
        end
    end
    if index <= #str -  2 then
        A(index + 1)
    end
end
A(1)
print(sum)


local s = io.read()
-- s = "ABCABC"
-- s = "ABCACCBABCBCAABCB"
-- s = "A"
local c = {}
for i = 1, #s do
	c[i] = string.sub(s, i, i)
end
-- print_r(c)

local num = 0
local i = 1
local lastA = 0
while i <= #c - 2 do
	if c[i] == "A" then
		local an = lastA + 1
		local x = i
		for j = i + 1, #c - 2 do
			if c[j] ~= "A" then
				break
			end
			an = an + 1
			x = j
		end
		local bcn = 0
		local k = x + 1
		while k < #c do
			if c[k] == "B" and c[k+1] == "C" then
				bcn = bcn + 1
				k = k + 2
			else
				break
			end
		end
		-- print(222, i, x, k, an, bcn, c[i], c[i+1], c[i+2])
		num = num + an * bcn
		if bcn >= 1 then
			i = k - 1
			c[i] = "A"
			lastA = an
		else
			i = k
			lastA = 0
		end
		print_r(c)
		print(i)
	else
		i = i + 1
	end
end
print(num)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end
local used = {}
local cur = 1
local Anum = 0
while cur <= #tbl -2 do
	local flag = false
	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then

		ans = ans + 1
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		-- l = cur -1
		-- while l > 0 and tbl[l] == "A"  do
		-- 	flag = true
		-- 	ans = ans + 1
		-- 	tbl[l] = "B"
		-- 	tbl[l+1] = "C"
		-- 	tbl[l+2] = "A"
		-- 	l = l - 1
		-- end
		-- print("######", Anum)
		if Anum > 0 then
			ans = ans + Anum
			tbl[cur +1] = "A"
			if Anum > 1 then
				tbl[cur] = "A"
			end
			Anum = Anum - 1
		end
	elseif tbl[cur] == "A" then
		if cur - 1 > 0 and tbl[cur - 1] == "A" then
			Anum = Anum + 1
		else
			Anum = 1
		end
	else
		Anum = 0
	end

    cur = cur + 1
end
print(ans)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end
local used = {}
local cur = 1
while cur <= #tbl -2 do
	local flag = false
	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then

		ans = ans + 1
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur -1
		while l > 0 and tbl[l] == "A"  do
			flag = true
			ans = ans + 1
			tbl[l] = "B"
			tbl[l+1] = "C"
			tbl[l+2] = "A"
			l = l - 1
		end
	end
    cur = cur + 1
end
print(ans)


local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end
local used = {}
local cur = 1
while cur <= #tbl -2 do
	local flag = false
	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then

		ans = ans + 1
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur -1
		while l > 0 and tbl[l] == "A"  do
			flag = true
			ans = ans + 1
			tbl[l] = "B"
			tbl[l+1] = "C"
			tbl[l+2] = "A"
			l = l - 1
		end
	end
    cur = cur + 1
end
print(ans)


local str = io.read()
-- local str = "AAABCBC"
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
    elseif  not ((tStr[i + 1] == "B" and tStr[i + 2] == "C") or (tStr[i + 1] == "B" and tStr[i + 2] == "B")) then
        a = 0
    elseif  tStr[i]  == "B" and  (i + 1 <= #tStr and tStr[i + 1]  == "C") then
        sum = sum + a
        i = i + 1
    end
end
print(sum)


local str = io.read()

local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
        if tStr[i + 1] ~= "A" and not (tStr[i + 1] == "B" and tStr[i + 2] == "C" and  tStr[i + 2] == "B") then
            a = 0
        end
    elseif  tStr[i]  == "B" and  (i + 1 <= #tStr and tStr[i + 1]  == "C") then
        sum = sum + a
        i = i + 1
    end
end
print(sum)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end
local used = {}
local cur = 1
while cur <= #tbl -2 do
	local flag = false
	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then

		ans = ans + 1
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur -1
		while l > 0 and tbl[l] == "A"  do
			flag = true
			ans = ans + 1
			l = l - 1
		end
	end
    if flag then
		tbl[cur + 1] = "A"
		tbl[cur] = "C"
		tbl[cur - 1] = "B"
	end
    cur = cur + 1
end
print(ans)


local str = io.read()
-- local str = "AAABCBC"
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
    elseif not (tStr[i + 1] == "B" and tStr[i + 2] == "C")then
        a = 0
    elseif  tStr[i]  == "B" and  (i + 1 <= #tStr and tStr[i + 1]  == "C") then
        sum = sum + a
        i = i + 1
    end
end
print(sum)


local str = io.read()
-- local str = "ABCACCBABCBCAABCB"
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
        if tStr[i + 1] ~= "A" and not (tStr[i + 1] == "B" and tStr[i + 2] == "C") then
            a = 0
        end
    elseif  tStr[i]  == "B" and  (i + 1 <= #tStr and tStr[i + 1]  == "C") then
        sum = sum + a
        i = i + 1

    end
end
print(sum)


local str = io.read()
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
    elseif (tStr[i]  == "C" and (i > 1 and tStr[i - 1]  ~= "B")) or (tStr[i]  == "B" and (i > 1 and tStr[i - 1]  == "B")) then
        a = 0
    elseif  tStr[i]  == "B" and  (i + 1 <= #tStr and tStr[i + 1]  == "C") then
        sum = sum + a
        i = i + 1
    end
end
print(sum)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end
local used = {}
local cur = 1
while cur <= #tbl -2 do
	local flag = false
	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then
		if not used[cur] then
			ans = ans + 1
			used[cur] = true
		end
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur -1
		while l > 0 and tbl[l] == "A" and not used[l] do
			flag = true
			ans = ans + 1
			used[l] = true
			l = l - 1
		end
	end
    if flag then
		tbl[cur + 1] = "A"
		tbl[cur] = "A"
		tbl[cur - 1] = "C"
	end
    cur = cur + 1
end
print(ans)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end
local used = {}
local cur = 1
while cur <= #tbl -2 do
	local flag = false
	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then
		if not used[cur] then
			ans = ans + 1
			used[cur] = true
		end
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur -1
		while l > 0 and tbl[l] == "A" do
			flag = true
			if not used[l] then
				ans = ans + 1
				used[l] = true
			end
			l = l - 1
		end
	end
    if flag then
		tbl[cur + 1] = "A"
		tbl[cur] = "A"
		tbl[cur - 1] = "C"
	end
    cur = cur + 1
end
print(ans)


local str = io.read()
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
    elseif  tStr[i]  == "B" and  (i + 1 <= #tStr and tStr[i + 1]  == "C" ) then
        sum = sum + a
        i = i + 1
    elseif tStr[i]  == "C" and (i > 1 and tStr[i - 1]  ~= "B") then
        a = 0
    end
end
print(sum)


local s = io.read()
local anum = 0
local num = 0
for i=1,string.len(s)-1 do
	local a = string.sub(s, i, i)
	local b = string.sub(s, i, i+1)
	if a == "A" then
		anum = anum + 1
	elseif  b == "BC" then
		num = anum + num
	else
		if i > 1 and string.sub(s, i-1, i) ~= "BC" then
			anum = 0
		end
	end
end
print(num)


local str = io.read()
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
    elseif  tStr[i]  == "B" and  (i + 1 <= #tStr and tStr[i + 1]  == "C" ) then
        sum = sum + a
    elseif tStr[i]  == "C" and (i > 1 and tStr[i - 1]  ~= "B") then
        a = 0
    end
end
print(sum)


local str = io.read()
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local a = 0
for i = index, #str do
    if tStr[i] == "A" then
        a = a + 1
    elseif  tStr[i]  == "B" and  (i + 1 <= #str and tStr[i + 1]  == "C" ) then
        if a > 0 then
            sum = sum + a
        end
    elseif tStr[i]  == "C" and (i > 1 and  tStr[i - 1]  ~= "B") then
        a = 0
    end
end
print(sum)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end

local cur = 1
while cur< #tbl do
	local flag = false
	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then
		ans = ans + 1
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur -1
		while l > 0 and tbl[l] == "A" do
			flag = true
			ans = ans + 1
			l = l - 1
		end
	end
    if flag then
		tbl[cur + 1] = "A"
	end
    cur = cur + 1
end
print(ans)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end

local cur = 1
while cur< #tbl do

	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then
		ans = ans + 1
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur -1
		while l > 0 and tbl[l] == "A" do
			ans = ans + 1
			l = l - 1
		end
	end
   cur = cur+1
end
print(ans)


local str = io.read()
local ans = 0
local tbl = {}
for cur=1, string.len(str) do
	tbl[cur] = string.sub(str,cur,cur)
end

local cur = 1
while cur< #tbl do

	if tbl[cur] == "A" and tbl[cur+1] == "B" and tbl[cur+2] == "C" then
		ans = ans + 1
		tbl[cur] = "B"
		tbl[cur+1] = "C"
		tbl[cur+2] = "A"
		l = cur
		while l - 1 > 0 and tbl[l -1] == "A" do
			ans = ans + 1
			l = l -1
		end
	end
   cur = cur+1
end
print(ans)


local str = io.read()
local ans = 0
local tbl = {}
for i=1, string.len(str) do
	tbl[i] = string.sub(str,i,i)
end

local i = 1
while i< #tbl do
	local flag = false
	if tbl[i] == "A" and tbl[i+1] == "B" and tbl[i+2] == "C" then
		ans = ans + 1
		tbl[i] = "B"
		tbl[i+1] = "C"
		tbl[i+2] = "A"

		if i>2 then
			flag = true
			i = i-2
		end
	end
	if not flag then
		i = i+1
	end
end
print(ans)


local s = io.read()
-- s = "ABCABC"
-- s = "ABCACCBABCBCAABCB"
-- s = "A"
local c = {}
for i = 1, #s do
	c[i] = string.sub(s, i, i)
end
-- print_r(c)

local num = 0
local i = 1
local lastA = 0
while i <= #c - 2 do
	if c[i] == "A" then
		local an = lastA + 1
		local x = i
		for j = i + 1, #c - 2 do
			if c[j] ~= "A" then
				break
			end
			an = an + 1
			x = j
		end
		local bcn = 0
		local k = x + 1
		while k < #c do
			if c[k] == "B" and c[k+1] == "C" then
				bcn = bcn + 1
				k = k + 2
			else
				break
			end
		end
		-- print(222, i, x, k, an, bcn, c[i], c[i+1], c[i+2])
		num = num + an * bcn
		if bcn >= 1 then
			i = k - 1
			c[i] = "A"
			lastA = an - 1
		else
			i = k
			lastA = 0
		end
		-- print_r(c)
		-- print(i)
	else
		i = i + 1
		lastA = 0
	end
end
print(num)


local num = 0
local i = 1
local lastA = 0
while i <= #c - 2 do
	if c[i] == "A" then
		local an = lastA + 1
		local x = i
		for j = i + 1, #c - 2 do
			if c[j] ~= "A" then
				break
			end
			an = an + 1
			x = j
		end
		local bcn = 0
		local k = x + 1
		while k < #c do
			if c[k] == "B" and c[k+1] == "C" then
				bcn = bcn + 1
				k = k + 2
			else
				break
			end
		end
		-- print(222, i, x, k, an, bcn, c[i], c[i+1], c[i+2])
		num = num + an * bcn
		if bcn >= 1 then
			i = k - 1
			c[i] = "A"
			lastA = an - 1
		else
			i = k
			lastA = 0
		end
		-- print_r(c)
		-- print(i)
	else
		i = i + 1
		lastA = 0
	end
end
print(num)


local s = io.read()
-- s = "ABCABC"
-- s = "ABCACCBABCBCAABCB"
-- s = "A"
local c = {}
for i = 1, #s do
	c[i] = string.sub(s, i, i)
end
-- print_r(c)

local num = 0
local i = 1
local lastA = 0
while i <= #c - 2 do
	if c[i] == "A" then
		local an = lastA + 1
		local x = i
		for j = i + 1, #c - 2 do
			if c[j] ~= "A" then
				break
			end
			an = an + 1
			x = j
		end
		local bcn = 0
		local k = x + 1
		while k < #c do
			if c[k] == "B" and c[k+1] == "C" then
				bcn = bcn + 1
				k = k + 2
			else
				break
			end
		end
		-- print(222, i, x, k, an, bcn, c[i], c[i+1], c[i+2])
		num = num + an * bcn
		if bcn >= 1 then
			i = k - 1
			c[i] = "A"
			lastA = an
		else
			i = k
			lastA = 0
		end
		print_r(c)
		print(i)
	else
		i = i + 1
		lastA = 0
	end
end
print(num)


local str = io.read()
local index = 1
local tStr = {}
for i = 1, #str do
    tStr[i] = string.sub(str, i, i)
end
local sum = 0
local function A(index)
    for i = index, #str -  2 do
        local str1 = ""
        for i = index, #tStr do
            str1 = str1 .. tStr[i]
        end
        if string.byte(tStr[i]) == string.byte(tStr[i + 1]) - 1 and string.byte(tStr[i]) == string.byte(tStr[i + 2]) - 2 then
            tStr[i] = "B"
            tStr[i + 1] = "C"
            tStr[i + 2] = "A"
            sum = sum + 1
            if index > 1 then
                A(index - 1)
            end
        end
    end
    if index <= #str -  2 then
        A(index + 1)
    end
end
A(1)
print(sum)


local s = io.read()
-- s = "ABCABC"
-- s = "ABCACCBABCBCAABCB"
local c = {}
for i = 1, #s do
	c[i] = string.sub(s, i, i)
end
-- print_r(c)

local num = 0
local i = 1
while i <= #c - 2 do
	if c[i] == "A" then
		local an = 1
		local x = i
		for j = i + 1, #c - 2 do
			if c[j] ~= "A" then
				break
			end
			an = an + 1
			x = j
		end
		local bcn = 0
		local k = x + 1
		while k < #c do
			if c[k] == "B" and c[k+1] == "C" then
				bcn = bcn + 1
				k = k + 2
			else
				break
			end
		end
		-- print(222, i, x, k, an, bcn, c[i], c[i+1], c[i+2])
		num = num + an * bcn
		if bcn >= 1 then
			i = k - 1
			c[i] = 'A'
		else
			i = k
		end
	else
		i = i + 1
	end
end
print(num)


local s = io.read()

local i = 1
local numa = 0
local all = 0
while(true) do
	if string.byte(s, i) == 65 then
		i = i + 1
		numa = numa + 1
	elseif string.byte(s, i) == 66 and string.byte(s, i + 1) == 67 then
		i = i + 2
		all = all + numa
	else
		i = i + 1
		numa = 0
	end
	if i > string.len(s) + 1 then
		break
	end
end
print(all)


local s = io.read()
-- s = "ABCABC"
-- s = "ABCACCBABCBCAABCB"
local c = {}
for i = 1, #s do
	c[i] = string.sub(s, i, i)
end
-- print_r(c)

local num = 0
local i = 1
while i <= #c - 2 do
	if c[i] == "A" then
		-- print(222, i, c[i], c[i+1], c[i+2])
		if c[i+1] == "B" and c[i+2] == "C" then
			c[i] = "B"
			c[i+1] = "C"
			c[i+2] = "A"
			i = i - 1
			if i <= 0 then
				i = 1
			end
			num = num + 1
		else
			i = i + 1
		end
	else
		i = i + 1
	end
end
print(num)


local s = io.read()

local i = 1
local counts = {}
local all = 0
while(true) do
	if string.byte(s, i) == 65 then
		i = i + 1
		table.insert(counts, 0)
	elseif string.byte(s, i) == 66 and string.byte(s, i + 1) == 67 then
		i = i + 2
		for t, v in ipairs(counts) do
			counts[t] = counts[t] + 1
		end
	else
		i = i + 1
		for _, v in ipairs(counts) do
			all = all + v
		end
		counts = {}
	end
	if i > string.len(s) + 1 then
		break
	end
end
print(all)


local s=io.read():gsub("BC","D")
local counter=0
local answer=0
for i=1,#s do
    if s:byte(i)==65 then
        counter=counter+1
    elseif s:byte(i)==68 then
        answer=answer+counter
    else
        counter=0
    end
end
print(answer)


local s = io.read()
local ret = 0
local cura = 0
local n = #s
local i = 1
while i <= n do
  local ss = s:sub(i, i)
  if ss == "A" then
    cura = cura + 1
    i = i + 1
  elseif ss == "B" then
    if i < n and s:sub(i + 1, i + 1) == "C" then
      ret = ret + cura
      i = i + 2
    else
      cura = 0
      i = i + 1
    end
  else
    cura = 0
    i = i + 1
  end
end
print(ret)


--[[
ABCACCBABCBCAABCB
[1] ABCACCBABCBCABCAB
[2] ABCACCBABCBCBCAAB
[5] ABCACCBBCBCBCAAAB
[6] BCAACCBBCBCBCAAAB

AAABCBC

BCBCAAA

]]
local s = io.read()
local n = #s
local sub = ""
local state = 1
local alen = 0
local ret = 0
for i = 1, n do
  sub = s:sub(i, i)
  if state == 1 then
    if sub == "A" then
      alen = alen + 1
    elseif sub == "B" then
      state = 2
    else
      alen = 0
    end
  elseif state == 2 then
    if sub == "C" then
      ret = ret + alen
    elseif sub == "A" then
      alen = 1
    else
      alen = 0
    end
    state = 1
  end
end
print(ret)


local h,w=io.read("n","n","l")
local a={}
local dist={}
for i=1,h do
    local input=io.read()
    a[i]={}
    dist[i]={}
    for j=1,w do
        a[i][j]=input:byte(j)
        dist[i][j]=-1
    end
end

local que={}
for i=1,h do
    for j=1,w do
        if a[i][j]==35 then
            dist[i][j]=0
            table.insert(que,{i,j})
        end
    end
end

local di={1,0,-1,0}
local dj={0,1,0,-1}
local l=1
while que[l] do
    local i=que[l][1]
    local j=que[l][2]
    for k=1,4 do
        local ni=i+di[k]
        local nj=j+dj[k]
        if dist[ni] and dist[ni][nj] and dist[ni][nj]==-1 then
            dist[ni][nj]=dist[i][j]+1
            table.insert(que,{ni,nj})
        end
    end
    l=l+1
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,dist[i][j])
    end
end
print(max)

Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3


--https://www.lua.org/pil/11.4.html
local List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local h,w=io.read("n","n","l")
local a={}
for i=1,h do
    local input=io.read()
    a[i]={}
    for j=1,w do
        a[i][j]=input:byte(j)
    end
end

local dist={}
local que=List.new()
for i=1,h do
    dist[i]={}
    for j=1,w do
        if a[i][j]==35 then
            dist[i][j]=0
            List.pushright(que,{i,j})
        else
            dist[i][j]=-1
        end
    end
end

local di={1,0,-1,0}
local dj={0,1,0,-1}
while true do
    local pop=List.popleft(que)
    if not pop then
        break
    end
    local i=pop[1]
    local j=pop[2]
    for k=1,4 do
        local ni=i+di[k]
        local nj=j+dj[k]
        if dist[ni] and dist[ni][nj] and dist[ni][nj]==-1 then
            dist[ni][nj]=dist[i][j]+1
            List.pushright(que,{ni,nj})
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,dist[i][j])
    end
end
print(max)


--https://www.lua.org/pil/11.4.html
local List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local h,w=io.read("n","n","l")
local a={}
for i=1,h do
    local input=io.read()
    a[i]={}
    for j=1,w do
        a[i][j]=input:byte(j)
    end
end

local dist={}
local que=List.new()
for i=1,h do
    dist[i]={}
    for j=1,w do
        if a[i][j]==35 then
            dist[i][j]=0
            List.pushright(que,{i,j})
        else
            dist[i][j]=-1
        end
    end
end

local di={1,0,-1,0}
local dj={0,1,0,-1}
while true do
    local pop=List.popleft(que)
    if not pop then
        break
    end
    local i=pop[1]
    local j=pop[2]
    for k=1,4 do
        local ni=i+di[k]
        local nj=j+dj[k]
        if dist[ni][nj] and dist[ni][nj]==-1 then
            dist[ni][nj]=dist[i][j]+1
            List.pushright(que,{ni,nj})
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,dist[i][j])
    end
end
print(max)


--https://www.lua.org/pil/11.4.html
local List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local h,w=io.read("n","n","l")
local a={}
for i=1,h do
    local input=io.read()
    a[i]={}
    for j=1,w do
        a[i][j]=input:byte(j)
    end
end

local dist={}
local que=List.new()
for i=1,h do
    dist[i]={}
    for j=1,w do
        if a[i][j]==35 then
            dist[i][j]=0
            List.pushright(que,{i,j})
        else
            dist[i][j]=-1
        end
    end
end

local di={1,0,-1,0}
local dj={0,1,0,-1}
while true do
    local pop=List.popleft(que)
    if not pop then
        break
    end
    local i=pop[1]
    local j=pop[2]
    for k=1,4 do
        local ni=i+di[k]
        local nj=j+dj[k]
        if ni<1 or ni>h or nj<1 or nj>w then
        else
            if dist[ni][nj]==-1 then
                dist[ni][nj]=dist[i][j]+1
                List.pushright(que,{ni,nj})
            end
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,dist[i][j])
    end
end
print(max)


--https://www.lua.org/pil/11.4.html
local List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local h,w=io.read("n","n","l")
local a={}
for i=1,h do
    local input=io.read()
    a[i]={}
    for j=1,w do
        a[i][j]=input:byte(j)
    end
end

local dist={}
for i=1,h do
    dist[i]={}
    for j=1,w do
        dist[i][j]=-1
    end
end

local iue=List.new()
local jue=List.new()
for i=1,h do
    for j=1,w do
        if a[i][j]==35 then
            dist[i][j]=0
            List.pushright(iue,i)
            List.pushright(jue,j)
        end
    end
end

local di={1,0,-1,0}
local dj={0,1,0,-1}
while true do
    local i=List.popleft(iue)
    if not i then
        break
    end
    local j=List.popleft(jue)
    for k=1,4 do
        local ni=i+di[k]
        local nj=j+dj[k]
        if ni<1 or ni>h or nj<1 or nj>w then
        else
            if dist[ni][nj]==-1 then
                dist[ni][nj]=dist[i][j]+1
                List.pushright(iue,ni)
                List.pushright(jue,nj)
            end
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,dist[i][j])
    end
end
print(max)


--https://www.lua.org/pil/11.4.html
local List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local h,w=io.read("n","n","l")
local a={}
for i=1,h do
    local input=io.read()
    a[i]={}
    for j=1,w do
        a[i][j]=input:byte(j)
    end
end

local dist={}
for i=1,h do
    dist[i]={}
    for j=1,w do
        dist[i][j]=-1
    end
end

local iue=List.new()
local jue=List.new()
for i=1,h do
    dist[i]={}
    for j=1,w do
        if a[i][j]==35 then
            dist[i][j]=0
            List.pushright(iue,i)
            List.pushright(jue,j)
        end
    end
end

local di={1,0,-1,0}
local dj={0,1,0,-1}
while true do
    local i=List.popleft(iue)
    if not i then
        break
    end
    local j=List.popleft(jue)
    for k=1,4 do
        local ni=i+di[k]
        local nj=j+dj[k]
        if ni<1 or ni>h or nj<1 or nj>w then
        else
            if dist[ni][nj]==-1 then
                dist[ni][nj]=dist[i][j]+1
                List.pushright(iue,ni)
                List.pushright(jue,nj)
            end
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,dist[i][j])
    end
end
print(max)