var
  a, h, f, w, id, hs, sum: array [0..200000] of longint;
  u, v: array [0..400000] of longint;
  s, t: array [1..800000] of longint;
  n, q, i, x, y, num: longint;
procedure swap(var x, y: longint); inline;
var
  t: longint;
begin
  t := x;
  x := y;
  y := t;
end;
procedure add(x, y: longint); inline;
begin
  inc(num);
  u[num] := h[x];
  v[num] := y;
  h[x] := num;
end;
procedure pushup(x: longint); inline;
begin
  s[x] := s[x shl 1] + s[x shl 1 or 1];
end;
procedure pushdown(x, l, r: longint); inline;
var
  m: longint;
begin
  m := (l + r) shr 1;
  inc(s[x shl 1], t[x] * (m - l + 1));
  inc(s[x shl 1 or 1], t[x] * (r - m));
  inc(t[x shl 1], t[x]);
  inc(t[x shl 1 or 1], t[x]);
  t[x] := 0;
end;
procedure build(x, l, r: longint);
var
  m: longint;
begin
  if l = r then
  begin
    s[x] := w[l];
    exit;
  end;
  m := (l + r) shr 1;
  build(x shl 1, l, m);
  build(x shl 1 or 1, m + 1, r);
  pushup(x);
end;
procedure update(x, l, r, tl, tr, k: longint);
var
  m: longint;
begin
  if (l >= tl) and (r <= tr) then
  begin
    inc(s[x], k * (r - l + 1));
    inc(t[x], k);
    exit;
  end;
  pushdown(x, l, r);
  m := (l + r) shr 1;
  if tl <= m then
    update(x shl 1, l, m, tl, tr, k);
  if tr > m then
    update(x shl 1 or 1, m + 1, r, tl, tr, k);
  pushup(x);
end;
function query(x, l, r, t: longint): longint;
var
  m: longint;
begin
  if l = r then
    exit(s[x]);
  pushdown(x, l, r);
  m := (l + r) shr 1;
  if t <= m then
    exit(query(x shl 1, l, m, t))
  else
    exit(query(x shl 1 or 1, m + 1, r, t));
end;
procedure dfs1(x, y, z: longint);
var
  i, max: longint;
begin
  f[x] := y;
  sum[x] := 1;
  hs[x] := 0;
  max := 0;
  i := h[x];
  while i > 0 do
  begin
    if v[i] <> y then
    begin
      dfs1(v[i], x, z + 1);
      if sum[v[i]] > max then
      begin
        max := sum[v[i]];
        hs[x] := v[i];
      end;
      inc(sum[x], sum[v[i]]);
    end;
    i := u[i];
  end;
end;
procedure dfs2(x, y: longint);
var
  i: longint;
begin
  inc(num);
  id[x] := num;
  w[id[x]] := a[x];
  if hs[x] = 0 then
    exit;
  dfs2(hs[x], y);
  i := h[x];
  while i > 0 do
  begin
    if (v[i] <> f[x]) and (v[i] <> hs[x]) then
      dfs2(v[i], v[i]);
    i := u[i];
  end;
end;
procedure updatesubtree(x, k: longint); inline;
begin
  update(1, 1, n, id[x], id[x] + sum[x] - 1, k);
end;
begin
  read(n, q);
  num := 0;
  for i := 1 to n - 1 do
  begin
    read(x, y);
    add(x, y);
    add(y, x);
  end;
  num := 0;
  dfs1(1, 0, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for i := 1 to q do
  begin
    read(x, y);
    updatesubtree(x, y);
  end;
  for i := 1 to n do
    write(query(1, 1, n, id[i]), ' ');
end.

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


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

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.


//ex16night398 's code

var pnt,cnt:array[1..200000] of longint;
    a,b,p,x,i,n,q:longint;

begin
	readln(n,q);
  for i := 2 to n do begin
    readln(a,b);
    pnt[b]:=a
  end;
  for i := 1 to q do begin
    readln(p,x);
    cnt[p]:=cnt[p]+x
  end;
  write(cnt[1]);
	for i := 2 to n do begin
    cnt[i]:=cnt[i]+cnt[pnt[i]];
    write(' ',cnt[i])
  end;
  writeln;
end.


const NMAX=200010;
type intary=array[1..NMAX]of integer;
var a,b,c,p,x:intary;
    i,n,q :int64;

procedure sort(s,t:int64);
  var i,j:int64;
      tmp,pivot:integer;
begin
  i:=s; j:=t; pivot:=a[(s+t)div 2];
  repeat
    while a[i]<pivot do i:=i+1;
    while a[j]>pivot do j:=j-1;
    if i<=j then begin
      tmp:=a[i]; a[i]:=a[j]; a[j]:=tmp;
      tmp:=b[i]; b[i]:=b[j]; b[j]:=tmp;
      i:=i+1; j:=j-1;
    end;
  until i>j;
  if j>s then sort(s,j);
  if i<t then sort(i,t);
end;

begin
  readln(n,q);
  for i:=1 to n-1 do readln(a[i],b[i]);
  for i:=1 to q do readln(p[i],x[i]);

  sort(1,n-1);
  for i:=1 to n do c[i]:=0;
  for i:=1 to q do c[p[i]]:=c[p[i]]+x[i];
  for i:=1 to n-1 do c[b[i]]:=c[b[i]]+c[a[i]];
  for i:=1 to n do write(c[i],' ');
end.


const NMAX=200010;
type intary=array[1..NMAX]of integer;
var a,b,c,p,x:intary;
    i,n,q :int64;

procedure sort(s,t:int64);
  var i,j:int64;
      tmp,pivot:integer;
begin
  i:=s; j:=t; pivot:=a[(s+t)div 2];
  repeat
    while a[i]<pivot do i:=i+1;
    while a[j]>pivot do j:=j-1;
    if i<=j then begin
      tmp:=a[i]; a[i]:=a[j]; a[j]:=tmp;
      tmp:=b[i]; b[i]:=b[j]; b[j]:=tmp;
      i:=i+1; j:=j-1;
    end;
  until i>j;
  if j>s then sort(s,j);
  if i<t then sort(i,t);
end;

begin
  readln(n,q);
  for i:=1 to n-1 do readln(a[i],b[i]);
  for i:=1 to q do readln(p[i],x[i]);

  sort(1,n-1);
  for i:=1 to n do c[i]:=0;
  for i:=1 to q do inc(c[p[i]],x[i]);
  for i:=1 to n-1 do begin
    inc(c[b[i]],c[a[i]]);
  end;
  for i:=1 to n do write(c[i],'');
end.


const NMAX=200010;
type intary=array[1..NMAX]of integer;
var a,b,c,p,x:intary;
    i,n,q :int64;

procedure sort(s,t:int64);
  var i,j:int64;
      tmp,pivot:integer;
begin
  i:=s; j:=t; pivot:=a[(s+t)div 2];
  repeat
    while a[i]<pivot do i:=i+1;
    while a[j]>pivot do j:=j-1;
    if i<j then begin
      tmp:=a[i]; a[i]:=a[j]; a[j]:=tmp;
      tmp:=b[i]; b[i]:=b[j]; b[j]:=tmp;
      i:=i+1; j:=j-1;
    end;
  until i>j;
  if j>s then sort(s,j);
  if i<t then sort(i,t);
end;

begin
  readln(n,q);
  for i:=1 to n-1 do readln(a[i],b[i]);
  for i:=1 to q do readln(p[i],x[i]);

  sort(1,n-1);
  for i:=1 to n do c[i]:=0;
  for i:=1 to q do inc(c[p[i]],x[i]);
  for i:=1 to n-1 do begin
    inc(c[b[i]],c[a[i]]);
  end;
  for i:=1 to n do write(c[i],'');
end.


var pnt,cnt:array[1..200000] of longint;
    a,b,p,x,i,n,q:longint;

begin
	readln(n,q);
  for i := 2 to n do begin
    readln(a,b);
    pnt[b]:=a
  end;
  for i := 1 to q do begin
    readln(p,x);
    cnt[p]:=cnt[p]+x
  end;
  write(cnt[1]);
	for i := 2 to n do begin
    cnt[i]:=cnt[i]+cnt[pnt[i]];
    write(' ',cnt[i])
  end;
  writeln;
end.


var
	N,i:Longint;
	H:Array[0..100000]of Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(H[i]);
		if H[i-1]>H[i] then begin
			writeln('No');
			exit;
		end else if H[i-1]<H[i] then dec(H[i]);
	end;
	writeln('Yes');
end.

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


{$R-,S-,Q-,I-,O+}
var n,h,left:int64;
begin
  readln(n);
  while n>0 do begin
    read(h);
    if left < h then
      left:=h-1
    else if left = h then
    else begin
      writeln('No');
      exit;
    end;
    dec(n);
  end;
  writeln('Yes');
end.


var n,h,left:int64;
begin
  readln(n);
  left:=0;
  while n>0 do begin
    read(h);
    if left < h then
      left:=h-1
    else if left = h then
    else begin
      writeln('No');
      exit;
    end;
    dec(n);
  end;
  writeln('Yes');
end.


var h:array[1..100009]of int64;
    n,i,max,j:int64;
    flag:boolean;
begin
  max:=0;
  readln(n);
  for i:=1 to n do read(h[i]);
  for i:=1 to n do
    if max<h[i] then begin
      max:=h[i];
      j:=i;
    end;
  flag:=true;
  for i:=j to n do
    if h[j]-h[i]>1 then flag:=false;
  if flag then write('Yes')
  else write('No')
end.


var h:array[1..100009]of int64;
    n,i,max,min:int64;
begin
  max:=0;min:=10000000000;
  readln(n);
  for i:=1 to n do read(h[i]);
  for i:=1 to n do
    if max<h[i] then max:=h[i];
  for i:=1 to n do
    if min<h[i] then min:=h[i];
  if max-min>1 then write('No')
  else write('Yes')
end.


var n,i:longint;
    f,a:array[1..100000] of integer;
begin
	readln(n);
	for i:=1 to n do read(a[i]);
	// 0 la co the thay doi... 1 la khong thay doi duoc
	fillchar(f,n,0);
	for i:=2 to n do
	  if a[i] < a[i-1] then
	    begin
	      if (a[i] = a[i-1] - 1) and (f[i-1] = 0) then f[i]:=1
	      else
	        begin
	          write('No');
	          halt;
	        end;
	    end
	  else if (a[i] = a[i-1]) and (f[i-1] = 1) then f[i]:=1;
	write('Yes');
end.


var n,i,h,hmax:int64;
    flag:boolean;
begin
read(n);flag:=true;
read(h);hmax:=h;
for i := 2 to n do
begin
read(h);
if hmax <= h then hmax:=h
else if hmax-h>=2 then flag:=false;
end;
if flag then write('Yes') else write('No');
end.


uses math;
var
        l,r,i,j,o,p:longint;
begin
        read(l,r);
        if r-l>2020 then writeln(0) else
        begin
                o:=maxlongint;
                for i:=l to r do
                        for j:=i+1 to r do
                        o:=min(o,i*j mod 2019);
                writeln(o);
        end;
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).


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+670<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 50 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L<3 then begin
		writeln(0);
		exit;
	end;
	if L+2017<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 50 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L<3 then begin
		if R<673 then writeln(1)
		else writeln(0);
		exit;
	end;
	if L+2017<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 50 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+2017<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 40 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+2017<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 50 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+2018<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 50 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+2018<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 10 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
			if(i-j>R)or(i-j<L)then break;
			tmp:=i mod M*(i-j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+2018<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 10 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+2018<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 100 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


var
	L,R,ans,tmp,M,i,j:Longint;
begin
	read(L,R);
	M:=2019;
	ans:=M;
	if L+2018<=R then begin
		writeln(0);
		exit;
	end;
	for i:=L to R do begin
		for j:=1 to 500 do begin
			if i+j>R then break;
			tmp:=i mod M*(i+j)mod M;
			if ans>tmp then ans:=tmp;
		end;
	end;
	writeln(ans);
end.


Program Catcoder4;
//uses crt;
var l,r,i,j,min :longint;
//--------------------------
begin
readln(l,r);
min:=2019;
if r-l+1 >=2019 then Writeln(0) else begin
                                     for i:=l to r-1 do
                                     for j:=i+1 to r do begin
                                                        if ((i mod 2019)*(j mod 2019)) mod 2019 < min then min :=((i mod 2019)*(j mod 2019)) mod 2019;
                                                        end;
                                     Writeln(min);
                                     end;
//readln;
end.


var l,r,min,re,t:int64;
    i,j:longint;
begin
	readln(l,r);
    if r - l + 1 < 2019 then
      begin
        min:=2018; re:=0;
        for i:= l to r - 1 do
          for j:= i + 1 to r do
            begin
              re:= ((i mod 2019) * (j mod 2019)) mod 2019;
              if re < min then min:=re;
            end;
        t:=r div 2019;
        if (t * 2019 >= l) and (t * 2019 <= r) then min:=0;
      end
    else min:=0;
    write(min);
end.


var t:longint;        //A的个数在32位范围内
    a:int64;          //结果会爆int
    c:char;           //滚动省空间
begin
  while not eoln do   //一直读到换行
  begin
    read(c);          //读入一个
    if c='A' then     //如果是A
      inc(t);         //记录值加一
    if c='B' then     //如果是B
    begin
      read(c);        //读入
      if c='C' then   //如果是C（有效字符）
      begin
        inc(a,t);     //结果加记录值
        continue;     //下一个循环
      end
      else            //如果不是C（无效字符）
        if c='A' then //如果是A（因为A被吞了）所以要分开计算
          t:=1        //记录值为1
        else          //否则
          t:=0;       //记录值为0
    end;
    if c='C' then     //如果是单个的C（无效字符）
      t:=0;           //记录值为0
  end;
  writeln(a);         //输出结果
end.

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


var t:longint;
    a:int64;
    c:char;
begin
  while not eoln do
  begin
    read(c);
    if c='A' then
      inc(t);
    if c='B' then
    begin
      read(c);
      if c='C' then
      begin
        inc(a,t);
        continue;
      end
      else
        if c='A' then
          t:=1
        else
          t:=0;
    end;
    if c='C' then
      t:=0;
  end;
  writeln(a);
end.


var t:longint;
    a:int64;
    c:char;
begin
  while not eoln do
  begin
    read(c);
    if c='A' then
      inc(t);
    if c='B' then
    begin
      read(c);
      if c='C' then
      begin
        inc(a,t);
        continue;
      end
      else
        if c='A' then
          inc(t)
        else
          t:=0;
    end;
    if c='C' then
      t:=0;
  end;
  writeln(a);
end.


var t:longint;
    a:int64;
    c:char;
begin
  while not eoln do
  begin
    read(c);
    if c='A' then
      inc(t);
    if c='B' then
    begin
      read(c);
      if (ord(c)<>13)and(c='C') then
      begin
        inc(a,t);
        continue;
      end
      else
        if c='A' then
          inc(t)
        else
          t:=0;
    end;
    if c='C' then
      t:=0;
  end;
  writeln(a);
end.


var t:longint;
    a:int64;
    c:char;
begin
  while not eoln do
  begin
    read(c);
    if c='A' then
      inc(t);
    if c='B' then
    begin
      read(c);
      if (not eoln)and(c='C') then
      begin
        inc(a,t);
        continue;
      end
      else
        if c='A' then
          inc(t)
        else
          t:=0;
    end;
    if c='C' then
      t:=0;
  end;
  writeln(a);
end.


var s,p:ansistring;
    i,j,t:longint;
    a:int64;
begin
  readln(s);
  i:=1;
  while i<=length(s) do
  begin
    if s[i]='A' then
      inc(t);
    if s[i]='B' then
      if (i<>length(s))and(s[i+1]='C') then
      begin
        inc(a,t);
        inc(i,2);
        continue;
      end
      else
        t:=0;
    if s[i]='C' then
      t:=0;
    inc(i);
  end;
  writeln(a);
end.


var c:char;
    i,j,t:longint;
    a:int64;
begin
  while not eoln do
  begin
    read(c);
    if c='A' then
      inc(t);
    if c='C' then
      t:=0;
    if c='B' then
    begin
      read(c);
      if c='C' then
      begin
        inc(a,t);
        read(c);
      end
      else
        t:=0;
    end;
  end;
  writeln(a);
end.


var s,p:ansistring;
    i,j,t:longint;
    a:int64;
begin
  readln(s);
  i:=1;
  while i<=length(s) do
  begin
    if s[i]='A' then
      p:=p+'A';
    if s[i]='B' then
      if (i<>length(s))and(s[i+1]='C') then
      begin
        p:=p+'B';
        inc(i,2);
        continue;
      end
      else
        p:=p+'C';
    if s[i]='C' then
      p:=p+'C';
    inc(i);
  end;
  s:=p;
  for i:=1 to length(s) do
    case s[i] of
      'A':inc(t);
      'B':inc(a,t);
      'C':t:=0;
    end;
  writeln(a);
end.


var s,p:ansistring;
    i,j,a,t:longint;
begin
  readln(s);
  i:=1;
  while i<=length(s) do
  begin
    if s[i]='A' then
      p:=p+'A';
    if s[i]='B' then
      if (i<>length(s))and(s[i+1]='C') then
      begin
        p:=p+'B';
        inc(i,2);
        continue;
      end
      else
        p:=p+'C';
    if s[i]='C' then
      p:=p+'C';
    inc(i);
  end;
  s:=p;
  for i:=1 to length(s) do
    case s[i] of
      'A':inc(t);
      'B':inc(a,t);
      'C':t:=0;
    end;
  writeln(a);
end.


var s,p:ansistring;
    i,j,a,t:longint;
begin
  readln(s);
  i:=1;
  while i<=length(s) do
  begin
    if s[i]='A' then
      p:=p+'A';
    if s[i]='B' then
      if (i<>length(s))and(s[i+1]='C') then
      begin
        p:=p+'B';
        inc(i,2);
        continue;
      end
      else
        p:=p+'C';
    if s[i]='C' then
      p:=p+'C';
    inc(i);
  end;
  s:=p;
  for i:=1 to length(s) do
    case s[i] of
      'A':inc(t);
      'B':inc(a,t);
      'C':t:=0;
    end;
  writeln(a+t);
end.


var s,p:ansistring;
    i,j,a:longint;
begin
  readln(s);
  i:=1;
  while i<=length(s) do
  begin
    if s[i]='A' then
      p:=p+'A';
    if s[i]='B' then
      if (i<>length(s))and(s[i+1]='C') then
      begin
        p:=p+'B';
        inc(i,2);
        continue;
      end
      else
        p:=p+'C';
    if s[i]='C' then
      p:=p+'C';
    inc(i);
  end;
  s:=p;
  for i:=1 to length(s) do
    if s[i]='B' then
    begin
      for j:=i-1 downto 1 do
        if s[j]='C' then
          break
        else
        if s[j]='A' then
          inc(a);
    end;
  writeln(a);
end.


var s,p:ansistring;
    t,x,l,n:longint;
begin
  readln(s);
  for x:=1 to length(s) do
  begin
     if s[x]='A' then
       p:=p+'A';
     if (s[x]='B')and(x<>length(s))and(s[x+1]='C') then
       p:=p+'B';
     if (s[x]='C')and(x<>1)and(s[x-1]='B') then
       p:=p+'C';
  end;
  s:=p;
  for x:=1 to length(s)-2 do
    if s[x]+s[x+1]+s[x+2]='ABC' then
      break;
  if s[x]+s[x+1]+s[x+2]<>'ABC' then
    x:=0;
  l:=length(s)+1;
  while x<>0 do
  begin
    s[x]:='B';
    s[x+1]:='C';
    s[x+2]:='A';
    if (x<>0) and (s[x-1]='A') then
      dec(x)
    else
    if (x+4<l) and (s[x+3]+s[x+4]='BC') then
      inc(x,2)
    else
    begin
      for x:=x+1 to length(s)-2 do
        if s[x]+s[x+1]+s[x+2]='ABC' then
          break;
      if s[x]+s[x+1]+s[x+2]<>'ABC' then
        x:=0;
    end;
    inc(t);
  end;
  writeln(t);
end.


var s:ansistring;
    t,x,l,n:longint;
begin
  readln(s);
  for x:=1 to length(s)-2 do
    if s[x]+s[x+1]+s[x+2]='ABC' then
      break;
  if s[x]+s[x+1]+s[x+2]<>'ABC' then
    x:=0;
  l:=length(s)+1;
  while x<>0 do
  begin
    s[x]:='B';
    s[x+1]:='C';
    s[x+2]:='A';
    if (x<>0) and (s[x-1]='A') then
      dec(x)
    else
    if (x+4<l) and (s[x+3]+s[x+4]='BC') then
      inc(x,2)
    else
    begin
      for x:=x+1 to length(s)-2 do
        if s[x]+s[x+1]+s[x+2]='ABC' then
          break;
      if s[x]+s[x+1]+s[x+2]<>'ABC' then
        x:=0;
    end;
    inc(t);
  end;
  writeln(t);
end.


var s:ansistring;
    t,x,l:longint;
begin
  readln(s);
  x:=pos('ABC',s);
  l:=length(s)+1;
  while x<>0 do
  begin
    s[x]:='B';
    s[x+1]:='C';
    s[x+2]:='A';
    if (x<>0) and (s[x-1]='A') then
      dec(x)
    else
    if (x+4<l) and (s[x+3]+s[x+4]='BC') then
      inc(x,2)
    else
      x:=pos('ABC',s);
    inc(t);
  end;
  writeln(t);
end.


var s:ansistring;
    t,x:longint;
begin
  readln(s);
  x:=pos('ABC',s);
  while x<>0 do
  begin
    s[x]:='B';
    s[x+1]:='C';
    s[x+2]:='A';
    inc(t);
    x:=pos('ABC',s);
  end;
  writeln(t);
end.


var a,ans:int64;
    q,r,s:char;

begin
  q := 'C';
  r := 'C';
  a := 0;
  ans := 0;
  while not eoln do begin
    read(s);
    if (q='A') and (r='A') then a:=a+1;
    if (q='A') and (r='B') and (s='C') then begin
      q:='B';
      r:='C';
      s:='A';
      ans:=ans+a+1;
    end;
    if (s='C') or ((r='B') and (s<>'C')) or ((r='C') and (s='B')) then a:=0;
    q:=r;
    r:=s;
  end;
  writeln(ans);
end.


var a,ans:int64;
    q,r,s:char;

begin
  q := 'C';
  r := 'C';
  a := 0;
  ans := 0;
  while not eoln do begin
    read(s);
    if (q='A') and (r='A') then a:=a+1;
    if (q='A') and (r='B') and (s='C') then begin
      q:='B';
      r:='C';
      s:='A';
      ans:=ans+a+1;
    end;
    if (s='C') or ((r='B') and (s='B')) or ((r='C') and (s='B')) then a:=0;
    q:=r;
    r:=s;
  end;
  writeln(ans);
end.


var a,ans:int64;
    q,r,s:char;

begin
  q := 'C';
  r := 'C';
  a := 0;
  while not eoln do begin
    read(s);
    if (r='A') and (s='A') then a:=a+1;
    if (q='A') and (r='B') and (s='C') then begin
      q:='B';
      r:='C';
      s:='A';
      ans:=ans+a+1;
    end;
    if (s='C') or ((r='B') and (s='B')) or ((r='C') and (s='B')) then a:=0;
    q:=r;
    r:=s;
  end;
  writeln(ans);
end.


var
	H,W,i,j,ans,X,Y:Longint;
	D:Array[0..1001,0..1001]of Longint;
	S:String;
begin
	readln(H,W);
	for i:=0 to 1001 do begin
		D[0,i]:=1000000000;
		D[i,0]:=1000000000;
		D[H+1,i]:=1000000000;
		D[i,W+1]:=1000000000;
	end;
	for i:=1 to H do begin
		readln(S);
		for j:=1 to W do begin
			if S[j]='#'then D[i,j]:=0 else begin
				X:=D[i-1,j];
				Y:=D[i,j-1];
				if X<Y then D[i,j]:=X+1 else D[i,j]:=Y+1;
			end;
		end;
	end;
	for i:=H downto 1 do for j:=W downto 1 do begin
		X:=D[i+1,j];
		Y:=D[i,j+1];
		if X<Y then inc(X)else X:=Y+1;
		if D[i,j]>X then D[i,j]:=X;
		if ans<D[i,j]then ans:=D[i,j];
	end;
	writeln(ans);
end.

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


var
	n,i,ans,now:Longint;
	L,R:Array[0..100000]of Longint;
	A:Array[1..100000]of Longint;
function gcd(a,b:Longint):Longint;
begin
	if b=0 then gcd:=a else gcd:=gcd(b,a mod b);
end;
begin
	read(n);
	for i:=1 to n do read(A[i]);
	for i:=1 to n do L[i]:=gcd(L[i-1],A[i]);
	for i:=n downto 1 do R[i-1]:=gcd(R[i],A[i]);
	for i:=1 to n do begin
		now:=gcd(L[i-1],R[i]);
		if ans<now then ans:=now;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.


uses math;
var
    n, i, j : longint;
    h, dp : array[1..100005] of longint;

begin
    readln(n);
    for i := 1 to n do read(h[i]);
    dp[1] := 0;
    for i := 2 to n do
    if (i = 2) then dp[i] := dp[i - 1] + abs(h[2] - h[1])
    else dp[i] := min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] + abs(h[i - 2] - h[i]));
    writeln(dp[n]);
end.

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 30 40 20

Sample Output 1

30

If we follow the path 1 → 2 → 4, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

2
10 10

Sample Output 2

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

If we follow the path 1 → 3 → 5 → 6, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.


var
f:array[0..100000] of longint;
n,i,k:longint;  s,z:ansistring;
a:array[1..100000] of longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a);
    exit(b);
end;
begin
readln(n);
readln(s);
s:=s+' ';
i:=1;
k:=0;
while pos(' ',s)>0 do
begin
   z:=copy(s,1,pos(' ',s)-1);
   val(z,k);
   a[i]:=k;
   inc(i);
   delete(s,1,pos(' ',s));
end;
f[0]:=0;
f[1]:=0;
f[2]:=abs(a[2]-a[1]);
   for i:=3 to n do
   f[i]:=min(f[i-2]+abs(a[i]-a[i-2]),f[i-1]+abs(a[i]-a[i-1]));
   writeln(f[n]);
readln
end.


var
f:array[0..1000] of longint;
n,i,k:longint;  s,z:ansistring;
a:array[1..1000] of longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a);
    exit(b);
end;
begin
readln(n);
readln(s);
s:=s+' ';
i:=1;
k:=0;
while pos(' ',s)>0 do
begin
   z:=copy(s,1,pos(' ',s)-1);
   val(z,k);
   a[i]:=k;
   inc(i);
   delete(s,1,pos(' ',s));
end;
f[0]:=0;
f[1]:=0;
f[2]:=abs(a[2]-a[1]);
   for i:=3 to n do
   f[i]:=min(f[i-2]+abs(a[i]-a[i-2]),f[i-1]+abs(a[i]-a[i-1]));
   writeln(f[n]);
readln
end.


var
f:array[0..1000] of longint;
n,i,k:longint;  s,z:ansistring;
a:array[1..1000] of longint;
function min(a,b:longint):longint;
begin
    if a>b then exit(a);
    exit(b);
end;
begin
readln(n);
readln(s);
s:=s+' ';
i:=1;
k:=0;
while pos(' ',s)>0 do
begin
   z:=copy(s,1,pos(' ',s)-1);
   val(z,k);
   a[i]:=k;
   inc(i);
   delete(s,1,pos(' ',s));
end;
f[0]:=0;
f[1]:=0;
f[2]:=abs(a[2]-a[1]);
   for i:=3 to n do
   f[i]:=min(f[i-2]+abs(a[i]-a[i-2]),f[i-1]+abs(a[i]-a[i-1]));
   writeln(f[n]);
readln
end.


uses crt;
var
f:array[0..1000] of longint;
n,i,k:longint;  s,z:ansistring;
a:array[1..1000] of longint;
function min(a,b:longint):longint;
begin
    if a>b then exit(a);
    exit(b);
end;
begin
clrscr;
readln(n);
readln(s);
s:=s+' ';
i:=1;
k:=0;
while pos(' ',s)>0 do
begin
   z:=copy(s,1,pos(' ',s)-1);
   val(z,k);
   a[i]:=k;
   inc(i);
   delete(s,1,pos(' ',s));
end;
f[0]:=0;
f[1]:=0;
f[2]:=abs(a[2]-a[1]);
   for i:=3 to n do
   f[i]:=min(f[i-2]+abs(a[i]-a[i-2]),f[i-1]+abs(a[i]-a[i-1]));
   writeln(f[n]);
readln
end.


var
f:array[0..100000] of longint;
n,i,k:longint;  s,z:ansistring;
a:array[1..100000] of longint;
function min(a,b:longint):longint;
begin
    if a>b then exit(a);
    exit(b);
end;
begin
readln(n);
readln(s);
s:=s+' ';
i:=1;
k:=0;
while pos(' ',s)>0 do
begin
   z:=copy(s,1,pos(' ',s)-1);
   val(z,k);
   a[i]:=k;
   inc(i);
   delete(s,1,pos(' ',s));
end;
writeln(a[1],' ',a[2]);
f[0]:=0;
f[1]:=0;
f[2]:=a[2]-a[1];
   for i:=3 to n do
   f[i]:=min(f[i-2]+abs(a[i]-a[i-2]),f[i-1]+abs(a[i]-a[i-1]));
   write(f[n]);
readln
end.


uses math;
var i, n : longint;
    h, dp : array[1..100005] of longint;
const inf = 2000000000;

begin
    readln(n);
    for i := 1 to n do
    begin
        read(h[i]);
        dp[i] := inf;
    end;
    dp[1] := 0;
    for i := 2 to n do
    begin
        dp[i] := min(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
        if (i > 2) then dp[i] := min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
    end;
    writeln(dp[n]);
end.


var h, dp: array[0..100010] of longint;
    n, i: longint;
begin
    readln(n);
    for i:=1 to n do
        read(h[i]);
    h[0]:=h[1];
    dp[1]:=0;
    for i:=2 to n do
    begin
        if (dp[i-1]+abs(h[i]-h[i-1]))>(dp[i-2]+abs(h[i]-h[i-2])) then
            dp[i]:=dp[i-2]+abs(h[i]-h[i-2])
        else
            dp[i]:=dp[i-1]+abs(h[i]-h[i-1]);
    end;
    writeln(dp[n]);
end.


uses crt;
const m = 100000;
var
        g,s: array [0..m,0..3] of longint;
        i,j,k,n,s1: longint;

begin
        clrscr;
        readln (n);
        for i:=1 to n do
        readln ( g[i, 1], g[i,2], g[i,3]);

        for i:=1 to 3 do s[1,i]:= g[1,i];
        for i:=2 to n do
        begin
                j:=0;
                repeat
                        inc(j); k:=0; s1:=0;

                        repeat
                                inc(k);
                                if ( k <> j) and ( s[i-1,k] >= s1) then s1:= s[i-1,k];
                        until k = 3;

                        s[i,j]:= s[i,j] + s1 + g[i,j];
                until j=3;

        end;

        s1:= 0;
        for i:=1 to 3 do if  s[n,i] > s1 then s1:= s[n,i];
        write (s1);
        readln;
end.


uses crt;
var
        g,h: array [0..100000] of longint;
        i,j,m,s,n: longint;

begin
        clrscr;
        readln (n);
        for i:=1 to n do read (g[i]);
        readln;

        fillchar (h, sizeof (h), 0);
        h[1]:=0;
        h[2]:= abs ( g[1] - g[2]);
        for i:=3 to  n do
        begin
                if (abs(g[i] - g[i-1]) + h[i-1]) > (abs(g[i] - g[i-2]) + h[i-2]) then
                h[i]:= abs(g[i] - g[i-2]) + h[i-2] else h[i]:= abs(g[i] - g[i-1]) + h[i-1];
        end;

        write (h[n]);
        readln;
end.


uses crt;
var
        g,h: array [0..10000] of longint;
        i,j,m,s,n: longint;

begin
        clrscr;
        readln (n);
        for i:=1 to n do read (g[i]);
        readln;

        fillchar (h, sizeof (h), 0);
        h[1]:=0;
        h[2]:= abs ( g[1] - g[2]);
        for i:=3 to  n do
        begin
                if (abs(g[i] - g[i-1]) + h[i-1]) > (abs(g[i] - g[i-2]) + h[i-2]) then
                h[i]:= abs(g[i] - g[i-2]) + h[i-2] else h[i]:= abs(g[i] - g[i-1]) + h[i-1];
        end;

        write (h[n]);
        readln;
end.


uses math;
var
        g,h: array [0..10000] of longint;
        i,j,m,s,n: longint;
begin
        readln (n);
        for i:=1 to n do read (g[i]);
        fillchar (h, sizeof (h), 0);
        h[1]:=0; h[2]:= abs ( g[1] - g[2]);
        for i:=3 to  n do
        begin
                h[i]:= abs(g[i] - g[i-2]) + h[i-2];
                for j:= i-2 to i-1 do
                h[i] := min( h[i], abs(g[i] - g[j]) + h[j]);
        end;
        write (h[n]);
        readln;
end.


var cost:array[0..100005] of int64;
dp:array[0..100005] of int64;
i,o,p,l,k,m,n,h,indexx:longint;
begin
read(n);
for i:=1 to n do begin
        read(cost[i]);
        end;
dp[0]:=0;
dp[1]:=0;
dp[2]:=abs(cost[2]-cost[1]);
for i:=3 to n do begin
        if abs(cost[i]-cost[i-1]) < abs(cost[i]-cost[i-2]) then indexx:=i-1
        else indexx:=i-2;
        dp[i]:=abs(cost[i]-cost[indexx])+dp[indexx];
        //writeln('dp[',i,']= ',dp[i]);
        end;
writeln(dp[n]);
end.


var cost:array[0..100005] of longint;
dp:array[0..100005] of longint;
i,o,p,l,k,m,n,h,indexx:longint;
begin
read(n);
for i:=1 to n do begin
        read(cost[i]);
        end;
dp[0]:=0;
dp[1]:=0;
dp[2]:=abs(cost[2]-cost[1]);
for i:=3 to n do begin
        if abs(cost[i]-cost[i-1]) <= abs(cost[i]-cost[i-2]) then indexx:=i-1
        else indexx:=i-2;
        dp[i]:=abs(cost[i]-cost[indexx])+dp[indexx];
        //writeln('dp[',i,']= ',dp[i]);
        end;
writeln(dp[n]);
end.


var cost:array[0..100005] of longint;
dp:array[0..100005] of longint;
i,o,p,l,k,m,n,h,indexx:longint;
begin
read(n);
for i:=1 to n do begin
        read(cost[i]);
        end;
dp[0]:=0;
dp[1]:=0;
dp[2]:=abs(cost[2]-cost[1]);
for i:=3 to n do begin
        if abs(cost[i]-cost[i-1]) < abs(cost[i]-cost[i-2]) then indexx:=i-1
        else indexx:=i-2;
        dp[i]:=abs(cost[i]-cost[indexx])+dp[indexx];
        //writeln('dp[',i,']= ',dp[i]);
        end;
writeln(dp[n]);
end.


Program Frog1;
uses math;
var F,H:Array[0..1000005] of longint;
    i,n :longint;
    minn:int64;
    fi,fo:text;
//--------------------------------
begin
//assign(fi,'Frog1.inp');reset(Fi);
readln(n);
for i:=1 to n do begin
                 Read(H[i]);
                 end;
F[1]:=0;
F[0]:=0;
H[0]:=1000000000;
for i:=2 to n do begin
                 F[i]:=min(F[i-1]+abs(H[i]-H[i-1]),F[i-2]+abs(H[i]-H[i-2]));
                 end;
Writeln(F[n]);
//readln;
end.


uses crt;
var a,b,c:array[1..100000] of longint;
    i,n:longint;
function max(a,b:longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n);
    for i := 1 to n do readln(a[i],b[i],c[i]);
    for i:=2 to n do
      begin
          a[i]:=max(a[i-1],max(b[i-1],c[i-1])+a[i]);
          b[i]:=max(b[i-1],max(a[i-1],c[i-1])+b[i]);
          c[i]:=max(c[i-1],max(a[i-1],b[i-1])+c[i]);
      end;
      writeln(max(a[i],max(c[i],b[i])));
      readln;
end.


uses crt;
var h,qh:array[0..100000] of longint;
     i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
   clrscr;
   readln(n);
   qh[1]:=0;
   for i := 1 to n do read(h[i]);
   qh[2]:=abs(h[2]-h[1]);
   h[0]:=0;
   for i:=3 to n do
          qh[i]:=min(abs(h[i]-h[i-1])+qh[i-1],abs(h[i]-h[i-2])+qh[i-2]);
      writeln(qh[n]);
      readln;
end.


uses crt;
var h,qh:array[0..100000] of longint;
     i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
   clrscr;
   readln(n);
   qh[0]:=0;
   qh[1]:=0;
   for i := 1 to n do read(h[i]);
   h[0]:=0;
   for i:=2 to n do
          qh[i]:=min(abs(h[i]-h[i-1])+qh[i-1],abs(h[i]-h[i-2])+qh[i-2]);
      writeln(qh[n]);
      readln;
end.


uses crt;
var h,qh:array[0..1000] of longint;
     i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
   clrscr;
   readln(n);
   qh[0]:=0;
   qh[1]:=0;
   for i := 1 to n do read(h[i]);
   h[0]:=0;
   for i:=2 to n do
          qh[i]:=min(abs(h[i]-h[i-1])+qh[i-1],abs(h[i]-h[i-2])+qh[i-2]);
      writeln(qh[n]);
      readln;
end.


var h,qh:array[0..100000] of int64;
     i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
   readln(n);
   qh[0]:=0;
   qh[1]:=0;
   for i :=1 to n do read(h[i]);
   qh[2]:=abs(h[1]-h[2]);
   for i:=3 to n do
          qh[i]:=min(abs(h[i]-h[i-1])+qh[i-1],abs(h[i]-h[i-2])+qh[i-2]);
      writeln(qh[n]);
end.


var h,qh:array[0..1000] of longint;
     i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
   readln(n);
   qh[0]:=0;
   qh[1]:=0;
   for i := 1 to n do readln(h[i]);
   h[0]:=0;
   for i:=2 to n do
          qh[i]:=min(abs(h[i]-h[i-1])+qh[i-1],abs(h[i]-h[i-2])+qh[i-2]);
      writeln(qh[n]);
end.


uses crt;
var h,qh:array[0..1000] of longint;
     i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
   clrscr;
   readln(n);
   qh[0]:=0;
   qh[1]:=0;
   for i := 1 to n do readln(h[i]);
   h[0]:=0;
   for i:=2 to n do
          qh[i]:=min(abs(h[i]-h[i-1])+qh[i-1],abs(h[i]-h[i-2])+qh[i-2]);
      writeln(qh[n]);
      readln;
end.


uses crt;
var h,qh:array[0..1000] of longint;
     i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
   clrscr;
   readln(n);
   qh[0]:=0;
   qh[1]:=0;
   for i := 1 to n do readln(h[i]);
   h[0]:=0;
   for i:=2 to n do qh[i]:=min(abs(h[i]-h[i-1])+qh[i-1],abs(h[i]-h[i-2])+qh[i-2]);
      writeln(qh[n]);
      readln;
end.


var dp,h:array[1..1000] of longint;
    i,n:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    readln(n);
    dp[1]:=0;
    for i := 1 to n do readln(h[i]);
    for i := 3 to n do dp[i]:=dp[i-1]+min(abs(h[i]-h[i-1]),abs(h[i]-h[i-2]));
    writeln(dp[n]);
end.


var n,i,j,ans  :int64;
    dp,h   :array[1..100010]of int64;

procedure chmin(var a:int64;b:int64);
begin
  if a>b then a:=b
end;

function abs(a:int64):int64;
begin
  if a>=0 then abs:=a
  else abs:=-a
end;

begin
  readln(n);
  for i:=1 to n do read(h[i]);
  j:=1<<50;
  for i:=1 to 100010 do dp[i]:=j;
  dp[1]:=0;
  for i:=1 to n-1 do begin
    chmin(dp[i+1],dp[i]+abs(h[i+1]-h[i]));
    chmin(dp[i+2],dp[i]+abs(h[i+2]-h[i]));
  end;
  writeln(dp[n])
end.


Uses math;
var N,i:integer;
    H:array[1..100010] of integer;
    dp:array[1..100010] of integer;
begin
 read(N);
for i := 1 to N do read(H[i]);
 dp[1]:=0;dp[2]:=abs(H[2]-H[1]);
for i := 1 to N do
 begin
  dp[i+1]:=min(dp[i+1],dp[i]+abs(H[i+1]-H[i]));

  dp[i+2]:=dp[i]+abs(H[i+2]-H[i]);
 end;
write(dp[N])
end.


var
	llc,lltc,lc,ltc,tc,c,n,i:longint;

begin
	readln(n);
	read(llc);
	read(lc);
    lltc := 0;
	ltc := abs(llc - lc);
	for i := 3 to n do
	begin
		read(c);
        tc := lltc + abs(c - llc);
		if ltc + abs(c - lc) < tc then
        	tc := ltc + abs(c - lc);
		lltc := ltc;
		llc := lc;
		ltc := tc;
		lc := c;
	end;
	writeln(ltc);
end.


type
	node=record
				sa,sb,sc:longint;
			end;
var
	a,b,c:array[0..100010] of longint;
	f1,f2:node;
	i,n,ans:longint;
function max(x,y:longint):longint;
begin
	if x>y then exit(x) else exit(y);
end;
begin
	readln(n);
	for i:=1 to n do read(a[i],b[i],c[i]);
	f1.sa:=a[1];
	f1.sb:=b[1];
	f1.sc:=c[1];
	for i:=2 to n do
	begin
		f2.sa:=a[i]+max(f1.sb,f1.sc);
		f2.sb:=b[i]+max(f1.sa,f1.sc);
		f2.sc:=c[i]+max(f1.sa,f1.sb);
		f1:=f2;
	end;
	ans:=max(f1.sa,f1.sb);
	ans:=max(ans,f1.sc);
	writeln(ans);
end.


var
	f,a:array[0..100010] of longint;
	n,i:longint;
function g(x,y:longint):longint;
begin
	g:=abs(a[x]-a[y]);
end;
function min(x,y:longint):longint;
begin
	if x<y then exit(x) else exit(y);
end;
begin
	readln(n);
	for i:=1 to n do read(a[i]);
	f[1]:=0;
	f[2]:=g(1,2);
	for i:=3 to n do
		f[i]:=min(f[i-1]+g(i,i-1),f[i-2]+g(i,i-2));
	writeln(f[n]);
end.


const fi='';
      fo='';
      maxn=10000;
var   f,H:array[0..maxn] of longint;
      n,i:longint;
      f1:text;
procedure Init;
var i:word;
begin
 assign(f1,fi);
 reset(f1);
 readln(f1,n);
 for i:=1 to n do read(f1,H[i]);
 assign(f1,fo);
 rewrite(f1);
end;
function g(x,y:longint):longint;
begin
	g:=abs(H[x]-H[y]);
end;
function min(x,y:longint):longint;
begin
	if x<y then exit(x) else exit(y);
end;
procedure QHD;
var i:word;
begin
	f[1]:=0;
	f[2]:=g(1,2);
	for i:=3 to n do
		f[i]:=min(f[i-1]+g(i,i-1),f[i-2]+g(i,i-2));
end;
begin
 Init;
 QHD;
 writeln(f1,f[n]);
 close(F1);
end.


 var A,F:array[0..1000003] of longint;
     i,n,a1,b:longint; f1:text;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n);
  for i:=1 to n do read(f1,A[i]);
  F[1]:=0;
  F[2]:=abs(A[1]-A[2]);
  for i:=3 to n do
   BEGIN
      a1:=F[i-1]+abs(A[i-1]-A[i]);
      b:=F[i-2]+abs(A[i-2]-A[i]);
      if a1<b then F[i]:=a1 else F[i]:=b;
   END;
  write(F[n]);
  close(f1);
 END.


uses math;
const
    inf = 2000000000;
var
    i, j, n, k : longint;
    h, dp : array[1..100005] of longint;

begin
    readln(n, k);
    for i := 1 to n do
    begin
        read(h[i]);
        dp[i] := inf;
    end;
    dp[1] := 0;
    for i := 2 to n do
    for j := max(1, i - k) to i - 1 do dp[i] := min(dp[i], dp[j] + abs(h[i] - h[j]));
    writeln(dp[n]);
end.

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, i + K. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 100

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 3
10 30 40 50 20

Sample Output 1

30

If we follow the path 1 → 2 → 5, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

3 1
10 20 10

Sample Output 2

20

If we follow the path 1 → 2 → 3, the total cost incurred would be |10 - 20| + |20 - 10| = 20.

Sample Input 3

2 100
10 10

Sample Output 3

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 4

10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4

40

If we follow the path 1 → 4 → 8 → 10, the total cost incurred would be |40 - 70| + |70 - 70| + |70 - 60| = 40.


uses math;
var i, j, n, k : longint;
    h, dp : array[1..100005] of longint;
const inf = 2000000000;

begin
    readln(n, k);
    for i := 1 to n do
    begin
        read(h[i]);
        dp[i] := inf;
    end;
    dp[1] := 0;
    for i := 2 to n do
    begin
        for j := max(1, i - k) to i - 1 do
        begin
            dp[i] := min(dp[i], dp[j] + abs(h[j] - h[i]));
        end;
    end;
    writeln(dp[n]);
end.


var h, dp: array[-1000..100010] of longint;
    findmin: array[1..1000] of longint;
    n, i, j, k: longint;
begin
    readln(n, k);
    for i:=1 to n do
        read(h[i]);
    for i:=1 to k do
    begin
        dp[1-i]:=0;
        h[1-i]:=h[1];
    end;
    for i:=2 to n do
    begin
        for j:=1 to k do
        begin
            findmin[j]:=dp[i-j]+abs(h[i]-h[i-j]);
            if j=1 then
                dp[i]:=findmin[j]
            else
            begin
                if findmin[j]<dp[i] then
                    dp[i]:=findmin[j];
            end;
        end;
    end;
    writeln(dp[n]);
end.


var h, dp: array[0..100010] of longint;
    findmin: array[1..1000] of longint;
    n, i, j, k: longint;
begin
    readln(n, k);
    for i:=1 to n do
        read(h[i]);
    h[0]:=h[1];
    dp[1]:=0;
    for i:=2 to n do
    begin
        for j:=1 to k do
        begin
            findmin[j]:=dp[i-j]+abs(h[i]-h[i-j]);
            if j=1 then
                dp[i]:=findmin[j]
            else
            begin
                if findmin[j]<dp[i] then
                    dp[i]:=findmin[j];
            end;
        end;
    end;
    writeln(dp[n]);
end.


uses crt;
var
        g,h: array [0..100000] of longint;
        n,k, carry,i,j,f: longint;
begin

        read (n); readln (k);
        for i:=1 to n do read(g[i]);
        readln;
        fillchar (h, sizeof (h), 0);
        h[1]:= 0;
        h[2]:= abs (g[2] - g[1]);
        for i:=3 to n do
        begin
                f:= i-k; if (i-k < 1) then f:= 1;
                h[i]:= abs(g[i] - g[i-1]) + h[i-1];
                while f  < i do
                begin
                        carry:= abs(g[i] - g[f]) + h[f];
                        if carry  <= h[i] then h[i]:= carry;
                        inc(f);
                end;
        end;
        write (h[n]);
        readln;
end.


//uses crt;
var cost:array[0..100005] of int64;
dp:array[0..100005] of int64;
i,o,p,l,k,m,n,h,indexx,minindex,j:longint;
min:int64;
begin
//clrscr;
read(n,k);
for i:=1 to n do begin
        read(cost[i]);
        end;
dp[0]:=0;
dp[1]:=0;
dp[2]:=abs(cost[2]-cost[1]);
for i:=3 to n do begin
        min:=10000000000000000;
        for j:=i-1 downto i-k do begin
               // writeln('wda');
                if j=0 then break;
                if abs(cost[j]-cost[i])+dp[j]< min then begin
                        min:=abs(cost[j]-cost[i])+dp[j];
                        indexx:=j;
                        end;
                end;
        dp[i]:=min;
        //writeln('dp[',i,']= ',dp[i],' min=',min,' indexx=',indexx);
        end;
writeln(dp[n]);
//readln;
//readln;
end.


//uses crt;
var cost:array[0..100005] of int64;
dp:array[0..100005] of int64;
i,o,p,l,k,m,n,h,indexx,minindex,j:longint;
min:int64;
begin
//clrscr;
read(n,k);
for i:=1 to n do begin
        read(cost[i]);
        end;
dp[0]:=0;
dp[1]:=0;
dp[2]:=abs(cost[2]-cost[1]);
if k=1 then begin
        for i:=1 to n-1 do
                dp[n]:=dp[n]+abs(cost[i]-cost[i+1]);
        end
else
for i:=3 to n do begin
        min:=100000000000;
        for j:=i-1 downto i-k+1 do begin
               // writeln('wda');
                if j=0 then break;
                if abs(cost[i]-cost[j])+dp[j] <= abs(cost[i]-cost[j-1])+dp[j-1]then begin
                        if abs(cost[i]-cost[j])+dp[j]< min then begin
                                min:=abs(cost[i]-cost[j]+dp[j]);
                                indexx:=j;
                                end;
                        end
                else
                        if abs(cost[i]-cost[j-1])+dp[j-1] < min then begin
                                min:=abs(cost[i]-cost[j-1])+dp[j-1];
                                indexx:=j-1;
                                end;
                end;
        dp[i]:=abs(cost[i]-cost[indexx])+dp[indexx];
        //writeln('dp[',i,']= ',dp[i],' min=',min,' indexx=',indexx);
        end;
writeln(dp[n]);
//readln;
//readln;
end.


//uses crt;
var cost:array[0..100005] of int64;
dp:array[0..100005] of int64;
i,o,p,l,k,m,n,h,indexx,min,minindex,j:longint;
begin
read(n,k);
for i:=1 to n do begin
        read(cost[i]);
        end;
dp[0]:=0;
dp[1]:=0;
dp[2]:=abs(cost[2]-cost[1]);
if k=1 then begin
        for i:=1 to n-1 do
                dp[n]:=dp[n]+abs(cost[i]-cost[i+1]);
        end
else
for i:=3 to n do begin
        min:=1000000;
        for j:=i-1 downto i-k+1 do begin
               // writeln('wda');
                if j=0 then break;
                if abs(cost[i]-cost[j])+dp[j] <= abs(cost[i]-cost[j-1])+dp[j-1]then begin
                        if abs(cost[i]-cost[j])+dp[j]< min then begin
                                min:=abs(cost[i]-cost[j]+dp[j]);
                                indexx:=j;
                                end;
                        end
                else
                        if abs(cost[i]-cost[j-1])+dp[j-1] < min then begin
                                min:=abs(cost[i]-cost[j-1])+dp[j-1];
                                indexx:=j-1;
                                end;
                end;
        dp[i]:=abs(cost[i]-cost[indexx])+dp[indexx];
        //writeln('dp[',i,']= ',dp[i],' min=',min,' indexx=',indexx);
        end;
writeln(dp[n]);
//readln;
//readln;
end.


//uses crt;
var cost:array[0..100005] of int64;
dp:array[0..100005] of int64;
i,o,p,l,k,m,n,h,indexx,min,minindex,j:longint;
begin
read(n,k);
for i:=1 to n do begin
        read(cost[i]);
        end;
dp[0]:=0;
dp[1]:=0;
dp[2]:=abs(cost[2]-cost[1]);
if k=1 then begin
        for i:=1 to n-1 do
                dp[n]:=dp[n]+abs(cost[i]-cost[i+1]);
        end
else
for i:=3 to n do begin
        min:=1000000;
        for j:=i-1 downto i-k+1 do begin
               // writeln('wda');
                if j=1 then break;
                if abs(cost[i]-cost[j])+dp[j] <= abs(cost[i]-cost[j-1])+dp[j-1]then begin
                        if abs(cost[i]-cost[j])+dp[j]< min then begin
                                min:=abs(cost[i]-cost[j]+dp[j]);
                                indexx:=j;
                                end;
                        end
                else
                        if abs(cost[i]-cost[j-1])+dp[j-1] < min then begin
                                min:=abs(cost[i]-cost[j-1])+dp[j-1];
                                indexx:=j-1;
                                end;
                end;
        dp[i]:=abs(cost[i]-cost[indexx])+dp[indexx];
        //writeln('dp[',i,']= ',dp[i],' min=',min,' indexx=',indexx);
        end;
writeln(dp[n]);
//readln;
//readln;
end.


Program Frog1;
uses math;
var F,H:Array[-1000..100005] of longint;
    i,n,k,j :longint;
    minn:int64;
    fi,fo:text;
//--------------------------------
begin
//assign(fi,'Frog1.inp');reset(Fi);
readln(n,k);
for i:=1 to n do begin
                 Read(H[i]);
                 end;
F[1]:=0;
F[0]:=0;
for i:=0 downto -1000 do begin
                         H[i]:=1000000;
                         end;
for i:=2 to n do begin
                 F[i]:=1000000000;
                 for j:=1 to k do begin
                                  F[i]:=min(F[i],F[i-j]+abs(H[i]-H[i-j]));
                                  end;
                 end;
Writeln(F[n]);
//readln;
end.


uses crt;
var dp,h:array[1..100000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=maxlongint;
    for i:=2 to n do
    for j := i-k to i-1 do if j>0 then dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[1..100000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=100000;
    for i:=2 to n do
    for j := i-k to i-1 do if j>0 then dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[1..100000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=10000;
    for i:=2 to n do
    for j := i-k to i-1 do if j>0 then dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[1..100000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    fillchar(dp,sizeof(dp),100000);
    dp[1]:=0;
    for i:=2 to n do
    for j := i-k to i-1 do if j>0 then dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[-100000..100000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    fillchar(dp,sizeof(dp),100000);
    dp[1]:=0;
    for i:=2 to n do
    for j := i-k to i-1 do if j>=0 then dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[-100000..100000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=10000;
    for i:=-100000 to 0 do dp[i]:=100000;
    for i:=2 to n do
    for j := i-k to i-1 do dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[0..10000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=10000;
    for i:=-100000 to 0 do dp[i]:=0;
    for i:=2 to n do
    for j := i-k to i-1 do dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[-100000..100000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=10000;
    for i:=2 to n do
    for j := i-k to i-1 do if j>0 then dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[-10000..10000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=10000;
    for i:=2 to n do
    for j := i-k to i-1 do dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[0..10000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=10000;
    for i:=2 to n do
    for j := i-k to i-1 do if j>0 then dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
      readln;
end.


uses crt;
var dp,h:array[1..10000] of longint;
    i,j,n,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    read(n,k);
    for i := 1 to n do read(h[i]);
    dp[1]:=0;
    for i := 2 to n do dp[i]:=10000;
    for i:=2 to n do
      for j := i-k to i-1 do dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
      writeln(dp[n]);
      readln;
end.


uses crt;
var dp,h:array[0..1000] of longint;
   n,i,j,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    read(n,k);
    fillchar(dp,sizeof(dp),10000);
    dp[1]:=0;
    for i := 1 to n do read(h[i]);
    for i:=2 to n do
       for j:=i-k to i-1 do
          dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
     write(dp[n]);
end.


uses crt;
var dp,h:array[0..1000] of longint;
   n,i,j,k:longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    read(n,k);
    fillchar(dp,sizeof(dp),10000);
    dp[1]:=0;
    for i := 1 to n do read(h[i]);
    for i:=2 to n do
       for j:=i-k to i-1 do
          dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
     writeln(dp[n]);
     readln;
end.


uses crt;
var dp,h:array[0..1000] of longint;
   n,i,j:longint;
   k:byte;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    read(n,k);
    readln;
    fillchar(dp,sizeof(dp),10000);
    dp[1]:=0;
    for i := 1 to n do read(h[i]);
    for i:=2 to n do
       for j:=i-k to i-1 do
          dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
     writeln(dp[n]);
     readln;
end.


uses crt;
var dp,h:array[0..1000] of longint;
   n,i,j:longint;
   k:byte;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    read(n,k);
    fillchar(dp,sizeof(dp),10000);
    dp[1]:=0;
    for i := 1 to n do read(h[i]);
    for i:=2 to n do
       for j:=i-k to i-1 do
          dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
     writeln(dp[n]);
     readln;
end.


uses crt;
var dp,h:array[0..1000] of integer;
   n,i,j:integer;
   k:byte;
   function min(a,b:integer):integer;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    clrscr;
    read(n,k);
    fillchar(dp,sizeof(dp),10000);
    dp[1]:=0;
    for i := 1 to n do readln(h[i]);
    for i:=2 to n do
       for j:=i-k to i-1 do
            dp[i]:=min(dp[i],dp[j]+abs(h[i]-h[j]));
    writeln(dp[n]);
    readln;
end.


Uses math;
var N,K,i,j:integer;
    H:array[1..100100] of integer;
    dp:array[1..100100] of integer;
begin
 read(N,K);
for i := 1 to N do read(H[i]);

 dp[1]:=0;
for i := 2 to K do
 dp[i]:=abs(H[i]-H[1]);

for i := 1 to N-1 do //i段目からのジャンプ
 begin
 for j := 1 to K do dp[i+j]:=min(dp[i+j],dp[i]+abs(H[i+j]-H[i]));
  dp[i+K]:=dp[i]+abs(H[i+K]-H[i]);
 end;
write(dp[N])
end.


var
	f,a:array[0..100010] of longint;
	n,i,j,k:longint;
function g(x,y:longint):longint;
begin
	g:=abs(a[x]-a[y]);
end;
function min(x,y:longint):longint;
begin
	if x<y then exit(x) else exit(y);
end;
begin
	readln(n,k);
	for i:=1 to n do read(a[i]);
	f[1]:=0;
	for i:=2 to n do
	begin
		f[i]:=2147483640;
		for j:=1 to k do
			if i-j>0 then f[i]:=min(f[i],f[i-j]+g(i,i-j));
	end;
	writeln(f[n]);
end.


const fi='';
      fo='';
      maxn=10000;
var   f,H:array[0..maxn] of word;
      n,i,k:word;
      f1:text;
procedure Init;
var i:word;
begin
 assign(f1,fi);
 reset(f1);
 readln(f1,n,k);
 for i:=1 to n do read(f1,H[i]);
 assign(f1,fo);
 rewrite(f1);
end;
function g(x,y:longint):longint;
begin
	g:=abs(H[x]-H[y]);
end;
function min(x,y:longint):longint;
begin
	if x<y then exit(x) else exit(y);
end;
procedure QHD;
var i,j:word;
begin
	f[1]:=0;
	for i:=2 to n do
         begin
          F[i]:=maxn;
          for j :=1 to k do
           if i-j>0 then
		f[i]:=min(f[i],F[i-j]+g(i,i-j));
          end;
end;
begin
 Init;
 QHD;
 writeln(f1,f[n]);
 close(F1);
end.


 var  A,F:array[0..1000000] of longint;
      k,n,i,min,j,tam:longint; f1:text;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n,k);
  for i:=1 to n do read(f1,A[i]);
  F[1]:=0;
  F[2]:=abs(A[2]-A[1]);
  for i:=3 to k do
   BEGIN
    min:=1000000000;
    for j:=1 to i-1 do
     BEGIN
      tam:=F[i-j]+abs(A[i-j]-A[i]);
      if tam<min then
       BEGIN
        min:=tam;
       END;
     END;
    F[i]:=min;
   END;

  for i:=k+1 to n do
   BEGIN
    min:=1000000000;
    for j:=1 to k do
     BEGIN
      tam:=F[i-j]+abs(A[i-j]-A[i]);
      if tam<min then
       BEGIN
        min:=tam;
       END;
     END;
    F[i]:=min;
   END;
  write(F[n]);
  readln;
  close(f1);
 END.


uses math;
var
  a,dp:array[0..101010] of int64;
  n,i,j,k:longint;
BEGIN
  ///assign(input,'a.in'); reset(input);
  readln(n,k);
  for i:=1 to n do read(a[i]);
  for i:=0 to n+1 do dp[i] := high(int64);
  dp[1]:=0;
  for i:=2 to n do
    for j:=max(1,i-k) to i-1 do dp[i] := min(dp[i], dp[j] + abs(a[i]-a[j]));
  writeln(dp[n]);
END.


var
	f,a:array[0..100010] of longint;
	n,i,j,k:longint;
function g(x,y:longint):longint;
begin
	g:=abs(a[x]-a[y]);
end;
function min(x,y:longint):longint;
begin
	if x<y then exit(x) else exit(y);
end;
begin
	readln(n,k);
	for i:=1 to n do read(a[i]);
	f[1]:=0;
	for i:=2 to n do
	begin
		f[i]:=$7f;
		for j:=1 to k do
			if i-j>0 then f[i]:=min(f[i],f[i-j]+g(i,i-j));
	end;
	writeln(f[n]);
end.


uses math;
var
    n, limw, i, j : longint;
    w, v : array[1..105] of longint;
    dp : array[0..105, 0..100005] of int64;

begin
    readln(n, limw);
    for i := 1 to n do readln(w[i], v[i]);
    fillchar(dp, sizeof(dp), 0);
    for i := 1 to n do
    for j := 0 to limw do
    begin
        dp[i, j] := dp[i - 1, j];
        if (j >= w[i]) then dp[i, j] := max(dp[i, j], dp[i - 1, j - w[i]] + v[i])
    end;
    writeln(dp[n, limw]);
end.

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^5

1 \leq w_i \leq W

1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


uses math;
var
    n, limw, i, j : longint;
    w, v : array[1..105] of longint;
    dp : array[0..105, 0..100005] of int64;
    ans : int64;

begin
    readln(n, limw);
    for i := 1 to n do readln(w[i], v[i]);
    fillchar(dp, sizeof(dp), 0);
    for i := 1 to n do
    for j := 0 to limw do
    if (j >= w[i]) then dp[i, j] := max(dp[i, j], dp[i - 1, j - w[i]] + v[i])
    else dp[i, j] := max(dp[i, j], dp[i - 1, j]);
    ans := 0;
    for i := 0 to n do
    for j := 0 to limw do ans := max(ans, dp[i, j]);
    writeln(ans);
end.


uses math;
var
    n, limw, i, j : longint;
    w, v : array[1..105] of longint;
    dp : array[0..100005, 0..105] of int64;

begin
    readln(n, limw);
    for i := 1 to n do readln(w[i], v[i]);
    fillchar(dp, sizeof(dp), 0);
    for i := 1 to limw do
    for j := 1 to n do
    if (i >= w[j]) then dp[i, j] := max(dp[i, j], dp[i - w[j], j - 1] + v[j])
    else dp[i, j] := dp[i, j - 1];
    writeln(dp[limw, n]);
end.


uses math;
var
    n, limw, i, j : longint;
    w, v : array[1..105] of longint;
    dp : array[0..100005, 0..105] of int64;

begin
    readln(n, limw);
    for i := 1 to n do readln(w[i], v[i]);
    for i := 0 to limw do
    for j := 1 to n do
    if (i >= w[j]) then dp[i, j] := max(dp[i, j], dp[i - w[j], j - 1] + v[j])
    else dp[i, j] := dp[i, j - 1];
    writeln(dp[limw, n]);
end.


uses math;
var
    n, i, limw : longint;
    w, v : array[1..105] of longint;
    curw, curv, ans : int64;

procedure backtrack(i : longint);
var
    j : longint;
begin
    if (i = n + 1) then
    begin
        ans := max(ans, curv);
        exit;
    end;
    for j := 0 to 1 do
    begin
        curw := curw + j * w[i];
        curv := curv + j * v[i];
        if (curw <= limw) then backtrack(i + 1);
        curw := curw - j * w[i];
        curv := curv - j * v[i];
    end;
end;

begin
    readln(n, limw);
    ans := 0; curw := 0; curv := 0;
    for i := 1 to n do readln(w[i], v[i]);
    backtrack(1);
    writeln(ans);
end.


var
	n,mw: Longint;
	w,v:array[1..100] of Longint;
	bang:array[0..100,0..100] of Longint;

function max(a,b:Longint): Longint;
	begin
		if a>=b then max:=a else max:=b;
	end;

procedure nhap();
	var
		i: Longint;
	begin
		readln(n,mw);
		for i:=1 to n do readln(w[i],v[i]);
	end;

procedure taobang();
	var
		i,j: Longint;
	begin
		for i:=0 to n do
		for j:=0 to mw do
		begin
			if (i=0) then bang[i,j]:=0
			else
				if j<w[i] then bang[i,j]:=bang[i-1,j]
				else bang[i,j]:=max(bang[i-1,j],bang[i-1,j-w[i]]+v[i])
		end;
		for i:=0 to n do
		begin
			for j:=0 to mw do write(bang[i,j]:12);
			writeln;
		end;
	end;

begin
	nhap;
	taobang;
	writeln(bang[n,mw])
end.


var
	n,mw: Longint;
	w,v:array[1..100] of Longint;
	bang:array[0..100,0..100] of Longint;

function max(a,b:Longint): Longint;
	begin
		if a>b then max:=a else max:=b;
	end;

procedure nhap();
	var
		i: Longint;
	begin
		readln(n,mw);
		for i:=1 to n do readln(w[i],v[i]);
	end;

procedure taobang();
	var
		i,j: Longint;
	begin
		for i:=0 to n do
		for j:=0 to mw do
		begin
			if (i=0) or (j=0) then bang[i,j]:=0
			else
				if j<w[i] then bang[i,j]:=bang[i-1,j]
				else bang[i,j]:=max(bang[i-1,j],bang[i-1,j-w[i]]+v[i])
		end;
	end;

begin
	nhap;
	taobang;
	writeln(bang[n,mw])
end.


var
	n,mw: Longint;
	w,v:array[0..100] of Longint;
	bang:array[0..100,0..100] of Longint;

function max(a,b:Longint): Longint;
	begin
		if a>b then max:=a else max:=b;
	end;

procedure nhap();
	var
		i: Longint;
	begin
		readln(n,mw);
		for i:=1 to n do readln(w[i],v[i]);
	end;

procedure taobang();
	var
		i,j: Longint;
	begin
		for i:=0 to n do
		for j:=0 to mw do
		begin
			if (i=0) or (j=0) then bang[i,j]:=0
			else
				if j<w[i] then bang[i,j]:=bang[i-1,j]
				else bang[i,j]:=max(bang[i-1,j],bang[i-1,j-w[i]]+v[i])
		end;
	end;

begin
	nhap;
	taobang;
	writeln(bang[n,mw])
end.


function max(a, b:int64):int64;
begin
    if a>b then
        max:=a
    else
        max:=b;
end;
var dp: array[-1..100, -100000..100000] of int64;
    N, W, i, j: longint;
    ww, v: array[1..100000] of longint;
begin
    readln(N, W);
    for i:=1 to N do
        readln(ww[i], v[i]);

    for i:=1 to N do
    begin
        for j:=1 to W do
        begin
            if j-ww[i]<0 then
                dp[i,j]:=dp[i-1,j]
            else
                dp[i,j]:=max(dp[i-1,j], dp[i-1,j-ww[i]]+v[i]);
        end;
    end;
    writeln(dp[N, W]);
end.


uses crt;
const m = 100000;
var
        g: array [0..100,0..m] of int64;
        c,a: array [0..100] of longint;
        n,k,i,j,w: longint;
begin
        clrscr;
        read (n); readln (w);
        for i:=1 to n do
        readln (a[i], c[i]);

        i:=0;
        repeat
                inc(i);
                for j:=1 to w do
                begin
                        if j < a[i] then g[i,j]:= g[i-1,j] else
                        if g[i-1,j] >= g[i-1, j- a[i]] + c[i] then g[i,j]:= g[i-1,j] else
                        g[i,j]:= g[i-1, j- a[i]] + c[i];
                end;
        until i=n;

        write (g[n,w]);
        readln;
end.


uses crt;
const m = 100000;
var
        g: array [0..100,0..m] of longint;
        c,a: array [0..100] of longint;
        n,k,i,j,w: longint;
begin
        clrscr;
        read (n); readln (w);
        for i:=1 to n do
        readln (a[i], c[i]);

        i:=0;
        repeat
                inc(i);
                for j:=1 to w do
                begin
                        if j < a[i] then g[i,j]:= g[i-1,j] else
                        if g[i-1,j] >= g[i-1, j- a[i]] + c[i] then g[i,j]:= g[i-1,j] else
                        g[i,j]:= g[i-1, j- a[i]] + c[i];
                end;
        until i=n;

        write (g[n,w]);
        readln;
end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';
  oo=trunc(1e9);

var
  n,m,va: longint;
  v,w: array[0..100000] of int16;
  f:array[0..100,0..100000] of int32;

procedure nhap;
var i: longint;
begin

  read(n,m);
  for i:=1 to n do begin
    read(w[i],v[i]);
    inc(va,v[i]);
  end;

end;

procedure xuli;
var i,j,ans: longint;
begin

  ans:=0;
  for i:=0 to n do begin
    for j:=0 to va do f[i][j]:=oo;
  end;

  f[0][0]:=0;

  for i:=1 to n do begin
    for j:=1 to va do begin
      if j>=v[i] then f[i][j]:=min(f[i-1][j],f[i-1][j-v[i]]+w[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  for j:=1 to va do begin
    ans:=ifthen((f[n][j]<=m),j,ans);
  end;

  write(ans+1);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.


const fi='CAITUI.INP';
      fo='CAITUI.OUT';
      maxn=100; maxw=trunc(1e5);

var F:array[0..maxn,0..maxw] of int64;

    n,w:longint;
    a,c:array[1..maxn] of longint;
//================================
procedure Doc;
var i:longint;
begin
  readln(n,w);
  for i:=1 to n do readln(a[i],c[i]);
end;
//================================
procedure Xuli;
var i,j:longint; x:longint;
begin
  fillchar(f,sizeof(f),0);

  for i:=1 to n do
    for j:=0 to w do
      begin
        f[i,j]:=f[i-1,j];

        if (j>=a[i]) and (f[i,j]<f[i-1,j-a[i]]+c[i]) then
          f[i,j]:=f[i-1,j-a[i]]+c[i];
      end;

  writeln(f[n,w]);
end;
//================================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc; xuli;
  //close(input); close(output);
END.


const fi='CAITUI.INP';
      fo='CAITUI.OUT';
      maxn=100; maxw=trunc(1e5);
var F:array[0..maxn,0..maxw] of int64;
    n,w:longint;
    a,c:array[1..maxn] of longint;
//================================
procedure Doc;
var i:byte;
begin
  readln(n,w);
  for i:=1 to n do readln(a[i],c[i]);
end;
//================================
procedure Xuli;
var i,j:byte; x:longint;
begin
  fillchar(f,sizeof(f),0);
  for i:=1 to n do
    for j:=0 to w do
      begin
        f[i,j]:=f[i-1,j];
        if (j>=a[i]) and (f[i,j]<f[i-1,j-a[i]]+c[i]) then
          f[i,j]:=f[i-1,j-a[i]]+c[i];
      end;
  writeln(f[n,w]);
end;
//================================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc; xuli;
  //close(input); close(output);
END.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';

var
  n,m: longint;
  v,w: array[0..100000] of int64;
  f:array[0..100,0..100000] of int64;

procedure nhap;
var i: longint;
begin

  read(n,m);
  for i:=1 to n do read(w[i],v[i]);

end;

procedure xuli;
var i,j: longint;
begin

  fillchar(f,sizeof(f),0);
  for i:=1 to n do begin
    for j:=1 to m do begin
      if j>=w[i] then f[i][j]:=max(f[i-1][j],f[i-1][j-w[i]]+v[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  write(f[n][m]);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.


uses math;
const fi='knapsack1.inp';
      fo='knapsack1.out';
      maxn=100;
      maxw=100000;
var n,w:int32;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxw] of uint64;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w);
   for i:=1 to n do readln(we[i],v[i]);
end;
//=============================
procedure xuli;
var i,j:int32;
begin
   for i:=1 to w do f[0,i]:=0;
   for i:=1 to n do
    for j:=1 to w do
     begin
        f[i,j]:=f[i-1,j];
        if (j>=we[i]) and (v[i]+f[i-1,j-we[i]]>f[i,j]) then
         f[i,j]:=f[i-1,j-we[i]]+v[i];
     end;
   write(f[n,w]);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


uses math;
const fi='knapsack1.inp';
      fo='knapsack1.out';
      maxn=100;
      maxw=100000;
var n,w:int32;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxw] of uint64;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w);
   for i:=1 to n do readln(we[i],v[i]);
end;
//=============================
procedure xuli;
var i,j:int32;
begin
   for i:=1 to w do f[0,i]:=0;
   for i:=1 to n do
    for j:=1 to w do
     begin
        f[i,j]:=f[i-1,j];
        if (j>=we[i]) and (v[i]+f[i-1,j-1]>f[i,j]) then
         f[i,j]:=f[i-1,j-we[i]]+v[i];
     end;
   write(f[n,w]);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


uses crt;
var a:array[0..100000] of int64;
    n,i,w,wmax,j:longint;
    v:int64;
begin
    clrscr;
    read(n,wmax);
    fillchar(a,sizeof(a),0);
    for i:=1 to n do
      begin
          readln(w,v);
          for j := wmax downto w do if v+a[j-w]>a[j] then a[j]:=v+a[j-w];
      end;
      writeln(a[wmax]);
      readln;
end.


uses crt;
var a:array[1..100100] of int64;
    n,i,w,wmax,j:byte;
    v:int64;
begin
    clrscr;
    read(n,wmax);
    fillchar(a,sizeof(a),0);
    for i:=1 to n do
      begin
          readln(w,v);
          for j := wmax downto w do if v+a[j-w]>a[j] then a[j]:=v+a[j-w];
      end;
      writeln(a[wmax]);
      readln;
end.


uses crt;
var a:array[1..100100] of int64;
    n,i,w,wmax,j:byte;
    v:int64;
begin
    clrscr;
    readln(n,wmax);
    fillchar(a,sizeof(a),0);
    for i:=1 to n do
      begin
          readln(w,v);
          for j := wmax downto w do if v+a[j-w]>a[j] then a[j]:=v+a[j-w];
      end;
      writeln(a[wmax]);
      readln;
end.


uses crt;
var a:array[1..100000] of int64;
    w,n,wmax,v,i,j:longint;
begin
    clrscr;
    readln(n,wmax);
    fillchar(a,sizeof(a),0);
    for i:=1 to n do
      begin
          readln(w,v);
          for j := wmax downto w do if v+a[j-w]>a[j] then a[j]:=v+a[j-w];
      end;
      writeln(a[wmax]);
      readln;
end.


uses crt;
var dp:array[0..100,0..100000] of int64;
    v,w:array[0..100] of int64;
    i,j,n,vmax:longint;
function max(a,b:int64):int64;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,vmax);
    for i := 1 to n do readln(w[i],v[i]);
    for i := 0 to n do dp[0,i]:=0;
    for j := 0 to vmax do dp[i,0]:=0;
    for i:=1 to n do
      for j := 1 to vmax do if j<w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j])
        else dp[i,j]:=max(dp[i-1,j],dp[i-1,j-w[i]]+v[i]);
    writeln(dp[n,vmax]);
    readln;
end.


uses crt;
var dp:array[0..100,0..10000] of int64;
    v,w:array[0..100] of int64;
    i,j,n,vmax:longint;
function max(a,b:int64):int64;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,vmax);
    for i := 1 to n do readln(w[i],v[i]);
    for i := 0 to n do dp[0,i]:=0;
    for j := 0 to vmax do dp[i,0]:=0;
    for i:=1 to n do
      for j := 1 to vmax do if j<w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j])
        else dp[i,j]:=max(dp[i-1,j],dp[i-1,j-w[i]]+v[i]);
    writeln(dp[n,vmax]);
    readln;
end.


uses crt;
var dp:array[0..100,0..100] of int64;
    v,w:array[0..100] of int64;
    i,j,n,vmax:longint;
function max(a,b:int64):int64;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,vmax);
    for i := 1 to n do readln(w[i],v[i]);
    for i := 0 to n do dp[0,i]:=0;
    for j := 0 to vmax do dp[i,0]:=0;
    for i:=1 to n do
      for j := 1 to vmax do if j<w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j])
        else dp[i,j]:=max(dp[i-1,j],dp[i-1,j-w[i]]+v[i]);
    writeln(dp[n,vmax]);
    readln;
end.


uses crt;
var dp:array[0..100,0..100] of longint;
    v,w:array[0..100] of longint;
    i,j,n,vmax:longint;
function max(a,b:Longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,vmax);
    for i := 1 to n do readln(w[i],v[i]);
    for i := 0 to n do dp[0,i]:=0;
    for j := 0 to vmax do dp[i,0]:=0;
    for i:=1 to n do
      for j := 1 to vmax do if j<w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j])
        else dp[i,j]:=max(dp[i-1,j],dp[i-1,j-w[i]]+v[i]);
    writeln(dp[n,vmax]);
    readln;
end.


uses crt;
var dp:array[0..100,0..100] of longint;
    v,w:array[0..100] of longint;
    i,j,n,vmax:longint;
function max(a,b:Longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,vmax);
    for i := 1 to n do readln(w[i],v[i]);
    for i := 0 to n do dp[0,i]:=0;
    for j := 0 to vmax do dp[i,0]:=0;
    for i:=1 to n do
      for j := 1 to vmax do if j<w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j])
        else dp[i,j]:=max(dp[i-1,j],dp[i-1,j-w[i]]+v[i]);
    for i:=0 to n do
       begin
           for j := 0 to vmax do write(dp[i,j],' ');
           writeln;
       end;
    writeln(dp[n,vmax]);
    readln;
end.


uses crt;
var dp:array[0..100,0..100] of longint;
    v,w:array[0..100] of longint;
    i,j,n,vmax:longint;
function max(a,b:Longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,vmax);
    for i := 1 to n do readln(w[i],v[i]);
    for i := 0 to n do dp[0,i]:=0;
    for j := 0 to vmax do dp[i,0]:=0;
    for i:=1 to n do
      for j := 1 to vmax do if j<w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j])
        else if j>=w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j-w[i]]+v[i]);
    for i:=0 to n do
       begin
           for j := 0 to vmax do write(dp[i,j],' ');
           writeln;
       end;
    writeln(dp[n,vmax]);
    readln;
end.


uses crt;
var dp:array[0..100,0..100] of longint;
    v,w:array[0..100] of longint;
    i,j,n,vmax:longint;
function max(a,b:Longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n,vmax);
    for i := 1 to n do readln(w[i],v[i]);
    for i := 0 to n do dp[0,i]:=0;
    for j := 0 to vmax do dp[i,0]:=0;
    for i:=1 to n do
      for j := 1 to vmax do if j<w[i] then dp[i,j]:=max(dp[i,j],dp[i-1,j])
        else dp[i,j]:=max(dp[i,j],dp[i-1,j-w[i]]+v[i]);
    writeln(dp[n,vmax]);
    readln;
end.


Program KnapSack1;
uses math;
var V,W:Array[1..105] of longint;
    F:array[0..105,0..100005] of int64;
    n,i,j,maxx,k :longint;
    fi,fo:text;
//--------------------------------
begin
//assign(fi,'KnapSack1.inp');reset(fi);
readln(n,k);
for i:=1 to n do begin
                 Readln(W[i],V[i]);
                 end;
for j:=1 to k do begin
                 for i:=1 to n do begin
                                       if j-W[i]>=0 then F[i,j]:=max(F[i-1,j],V[i]+F[i-1,j-W[i]])
                                      else F[i,j]:=F[i-1,j];
                                      end;
                 end;
Writeln(F[n,k]);
//readln;
end.


Program KnapSack1;
uses math;
var V,W:Array[1..105] of longint;
    F:array[0..105,-100005..100005] of int64;
    n,i,j,maxx,k :longint;
    fi,fo:text;
//--------------------------------
begin
//assign(fi,'KnapSack1.inp');reset(fi);
readln(n,k);
for i:=1 to n do begin
                 Readln(W[i],V[i]);
                 end;
for j:=-1 downto -100005 do begin
                           for i:=0 to 105 do F[i,j]:=-1000000000;
                           end;
for j:=1 to k do begin
                 for i:=n downto 1 do begin
                                      if j-W[i]>=0 then F[i,j]:=max(F[i-1,j],V[i]+F[i-1,j-W[i]])
                                      else F[i,j]:=F[i-1,j];
                                      end;
                 end;
Writeln(F[n,k]);
//readln;
end.


Program KnapSack1;
uses math;
var V,W:Array[1..105] of longint;
    F:array[0..105,-100005..100005] of int64;
    n,i,j,maxx,k :longint;
    fi,fo:text;
//--------------------------------
begin
readln(n,k);
for i:=1 to n do begin
                 Readln(W[i],V[i]);
                 end;
for j:=-1 downto -100005 do begin
                           for i:=0 to 105 do F[i,j]:=-1000000000;
                           end;
for j:=1 to k do begin
                 for i:=n downto 1 do begin
                                  F[i,j]:=max(F[i-1,j],V[i]+F[i-1,j-W[i]]);
                                  end;
                 end;
Writeln(F[n,k]);
end.


Program KnapSack1;
uses math;
var V,W:Array[1..105] of longint;
    F:array[0..105,-100005..100005] of int64;
    n,i,j,maxx,k :longint;
    fi,fo:text;
//--------------------------------
begin
//assign(fi,'KnapSack1.inp');reset(fi);
readln(n,k);
for i:=1 to n do begin
                 Readln(W[i],V[i]);
                 end;
for j:=-1 downto -10005 do begin
                           for i:=0 to 105 do F[i,j]:=-1000000000;
                           end;
for j:=1 to k do begin
                 for i:=1 to n do begin
                                  F[i,j]:=max(F[i-1,j],V[i]+F[i-1,j-W[i]]);
                                  end;
                 end;
Writeln(F[n,k]);
//readln;
end.


var
	a:array[0..100010] of int64;
	i,j,n,v,w,c,max:longint;
begin
	readln(n,v);
	fillchar(a,sizeof(a),0);
	for i:=1 to n do
	begin
		read(c,w);
		for j:=v downto c do
			if w+a[j-c]>a[j] then a[j]:=w+a[j-c];
	end;
	writeln(a[v]);
end.


//uses crt;
const fi='';
      fo='';
      maxn=10000;
var W,V:array[1..maxn]of int64;
    F:array[0..100,0..maxn]of int64;
    n,m:longint;
    f1:text;
procedure Init;
var i,j:longint;
begin
 assign(f1,fi);
 reset(f1);
 readln(f1,n,m);
 for i:=1 to n do read(f1,w[i],v[i]);
 for i:=1 to m do F[0,i]:=0;
 for i:=0 to n do F[i,0]:=0;
 assign(f1,fo);
 rewrite(f1);
end;
function max(a,b:int64):int64;
begin
 if a<b then exit(b)
 else exit(a);
end;
procedure QHD;
var i,j:longint;
begin
 for i:=1 to n do begin
  for j:=0 to m do
   begin
    F[i,j]:=F[i-1,j];
    if (j>=W[i]) then F[i,j]:=max(F[i-1,j],F[i-1,j-w[i]]+V[i]);
	write(F[i,j]:3);
   end;
  writeln;
  end;
end;
begin
 Init;
 QHD;
 writeln(f1,F[n,m]);
 close(f1);
end.


 var W,V:array[0..100000] of longint;
     F:array[0..100,0..100000] of int64;
     n,m,i,j:longint;   f1:text;

 function max(x,y:int64):int64;
 BEGIN
  if x<y then exit(y) else exit(x);
 END;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n,m);
  for i:=1 to n do readln(f1,w[i],v[i]);
  for i:=0 to m do F[0,i]:=0;
  for i:=0 to n do F[i,0]:=0;
  //fillchar(f,sizeof(f),0);
  //n:=3; m:=8;
  for i:=1 to n do
   for j:=0 to m do
    BEGIN
     F[i,j]:=F[i-1,j];
     {if (j>=W[i]) and (F[i,j]<F[i-1,j-W[i]]+V[i]) then
      F[i,j]:=F[i-1,j-W[i]]+V[i]; }
     if j>=W[i] then F[i,j]:=max(F[i-1,j],F[i-1,j-W[i]]+V[i]);
    // writeln(i,' ',j,' ',F[i,j]);
    END;

  write(F[n,m]);
  readln;
  close(f1);
 END.


 var W,V:array[0..100000] of longint;
     F:array[0..100,0..100000] of longint;
     n,m,i,j:longint;   f1:text;

 function max(x,y:longint):longint;
 BEGIN
  if x<y then exit(y) else exit(x);
 END;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n,m);
  for i:=1 to n do readln(f1,w[i],v[i]);
  for i:=0 to m do F[0,i]:=0;
  for i:=0 to n do F[i,0]:=0;
  //fillchar(f,sizeof(f),0);
  n:=3; m:=8;
  for i:=1 to n do
   for j:=1 to m do
    BEGIN
     F[i,j]:=F[i-1,j];
     {if (j>=W[i]) and (F[i,j]<F[i-1,j-W[i]]+V[i]) then
      F[i,j]:=F[i-1,j-W[i]]+V[i]; }
     if j>=W[i] then F[i,j]:=max(F[i-1,j],F[i-1,j-W[i]]+V[i]);
    // writeln(i,' ',j,' ',F[i,j]);
    END;

  write(F[n,m]);
  readln;
  close(f1);
 END.


 var W,V:array[0..100000] of longint;
     F:array[0..100,0..100000] of longint;
     n,m,i,j:longint;   f1:text;

 function max(x,y:longint):longint;
 BEGIN
  if x<y then exit(y) else exit(x);
 END;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n,m);
  for i:=1 to n do readln(f1,w[i],v[i]);
  for i:=0 to m do F[0,i]:=0;
  for i:=0 to n do F[i,0]:=0;
  //fillchar(f,sizeof(f),0);
 // n:=3; m:=8;
  for i:=1 to n do
   for j:=1 to m do
    BEGIN
     F[i,j]:=F[i-1,j];
     {if (j>=W[i]) and (F[i,j]<F[i-1,j-W[i]]+V[i]) then
      F[i,j]:=F[i-1,j-W[i]]+V[i]; }
     if j>=W[i] then F[i,j]:=max(F[i-1,j],F[i-1,j-W[i]]+V[i]);
    // writeln(i,' ',j,' ',F[i,j]);
    END;

  write(F[n,m]);
  readln;
  close(f1);
 END.


 var W,V:array[0..1000000] of longint;
     F:array[0..10000,0..10000] of longint;
     n,m,i,j:longint;   f1:text;

 function max(x,y:longint):longint;
 BEGIN
  if x<y then exit(y) else exit(x);
 END;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n,m);
  for i:=1 to n do readln(f1,w[i],v[i]);
  for i:=0 to m do F[0,i]:=0;
  for i:=0 to n do F[i,0]:=0;
  //fillchar(f,sizeof(f),0);
  n:=3; m:=8;
  for j:=1 to m do
   for i:=1 to n do
    BEGIN
     F[i,j]:=F[i-1,j];
     {if (j>=W[i]) and (F[i,j]<F[i-1,j-W[i]]+V[i]) then
      F[i,j]:=F[i-1,j-W[i]]+V[i]; }
     if j>=W[i] then F[i,j]:=max(F[i-1,j],F[i-1,j-W[i]]+V[i]);
    // writeln(i,' ',j,' ',F[i,j]);
    END;

  write(F[n,m]);
  readln;
  close(f1);
 END.


 var W,V:array[0..1000000] of longint;
     F:array[0..100000,0..100000] of longint;
     n,m,i,j:longint;   f1:text;

 function max(x,y:longint):longint;
 BEGIN
  if x<y then exit(y) else exit(x);
 END;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n,m);
  for i:=1 to n do readln(f1,w[i],v[i]);
  for i:=0 to m do F[0,i]:=0;
  for i:=0 to n do F[i,0]:=0;
  //fillchar(f,sizeof(f),0);
  n:=3; m:=8;
  for j:=1 to m do
   for i:=1 to n do
    BEGIN
     F[i,j]:=F[i-1,j];
     {if (j>=W[i]) and (F[i,j]<F[i-1,j-W[i]]+V[i]) then
      F[i,j]:=F[i-1,j-W[i]]+V[i]; }
     if j>=W[i] then F[i,j]:=max(F[i-1,j],F[i-1,j-W[i]]+V[i]);
    // writeln(i,' ',j,' ',F[i,j]);
    END;

  write(F[n,m]);
  readln;
  close(f1);
 END.


var
  f:array[0..111,0..111111] of int64;
  w,v,a:array[1..111] of int64;
  n,m,j,i:longint;
BEGIN
 /// assign(input,'a.in'); reset(input);
  readln(n,m);
  for i:=1 to n do readln(w[i],v[i]);
  fillchar(f,sizeof(f),0);
  for i:=1 to n do
    for j:=1 to m do
      if (w[i]>j) then f[i,j]:=f[i-1,j] else
      if f[i-1,j]>=f[i-1,j-w[i]]+v[i] then f[i,j]:=f[i-1,j] else f[i,j]:=f[i-1,j-w[i]]+v[i];
  writeln(f[n,m]);
  Readln;
END.


uses math;
var
  v, w:array[0..1010] of qword;
  dp:array[1..101,0..101010] of qword;
  n, m, i, j:longint;
function max(u,v:qword):qword;
  BEGIN
    if u>v then exit(u);
    exit(v);
  end;

BEGIN
  ///assign(input,'a.in'); reset(input);
  readln(n, m);
  for i:=1 to n do read(w[i], v[i]);

  fillchar(dp,sizeof(dp),0);
  for i:=1 to n do
    for j:=1 to m do begin
      dp[i,j] := dp[i-1,j];
      if j >= w[i] then dp[i,j] := max(dp[i,j], dp[i-1,j-w[i]] + v[i]);
    end;
  writeln(dp[n][m]);
END.


var dp, back: array[0..5000, 0..5000] of longint;
    c: array[1..3000] of char;
    s, t: ansistring;
    i,j,k: longint;
begin
    readln(s);
    readln(t);
    for i:=1 to length(s) do
    begin
        for j:=1 to length(t) do
        begin
            if s[i]=t[j] then
            begin
                dp[i,j]:=dp[i-1][j-1]+1;
                back[i,j]:=0;
            end
            else
            begin
                if dp[i,j-1]>dp[i-1,j] then
                begin
                    dp[i,j]:=dp[i,j-1];
                    back[i,j]:=1;
                end
                else
                begin
                    dp[i,j]:=dp[i-1,j];
                    back[i,j]:=2;
                end;
            end;
        end;
    end;
    k:=0;
    while k<dp[length(s),length(t)] do
    begin
        if back[i,j]=2 then
            dec(i)
        else if back[i,j]=1 then
            dec(j)
        else
        begin
            inc(k);
            c[k]:=s[i];
            dec(i);
            dec(j);
        end;
    end;
    for i:=k downto 1 do
        write(c[i]);
    writeln;
end.

Score : 100 points

Problem Statement

You are given strings s and t.
Find one longest string that is a subsequence of both s and t.

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

Constraints

s and t are strings consisting of lowercase English letters.

1 \leq |s|, |t| \leq 3000

Input

Input is given from Standard Input in the following format:

s
t

Output

Print one longest string that is a subsequence of both s and t.
If there are multiple such strings, any of them will be accepted.

Sample Input 1

axyb
abyxb

Sample Output 1

axb

The answer is axb or ayb; either will be accepted.

Sample Input 2

aa
xayaz

Sample Output 2

aa

Sample Input 3

a
z

Sample Output 3

The answer is (an empty string).

Sample Input 4

abracadabra
avadakedavra

Sample Output 4

aaadara


uses math;
var
        f:text;
        x,y,h: ansistring;
        g: array [0..3000,0..3000] of longint;
        i,j,s,t,a: longint;

begin

        readln ( x);
        readln ( y);

        fillchar(g, sizeof (g),0);

        for i:=1 to length(x) do
        for j:=1 to length(y) do
        if x[i] = y[j] then g[i,j]:= g[i-1,j-1] + 1
        else g[i,j]:= max(g[i-1,j], g[i, j-1]);

        a:=0;
        i:= length (x); j:= length (y);
        while (i>0) and (j>0) do
        begin

                if  x[i] = y[j] then
                begin
                        h:= x[i] + h;
                        dec(i); dec(j);
                end
                else
                if g[i-1,j] > g[i, j-1] then dec (i) else dec (j);
        end;

        writeln (h);
        readln;

end.


uses math;
var
        f:text;
        x,y: ansistring;
        g: array [0..3000,0..3000] of longint;
        h: array [0..3000] of string;
        i,j,s,t,a: longint;

begin

        readln ( x);
        readln ( y);

        fillchar(g, sizeof (g),0);

        for i:=1 to length(x) do
        for j:=1 to length(y) do
        if x[i] = y[j] then g[i,j]:= g[i-1,j-1] + 1
        else g[i,j]:= max(g[i-1,j], g[i, j-1]);

        a:=0;
        i:= length (x); j:= length (y);
        while i>0 do
        begin

                if  x[i] = y[j] then
                begin
                        inc(a); h[a]:= x[i];
                        dec(i); dec(j);
                end
                else
                if g[i-1,j] > g[i, j-1] then dec (i) else dec (j);
        end;

        for i:=a downto 1 do write (h[i]);
        readln;

end.


uses
  math;
var
  f:array[0..3033,0..3033] of longint;
  n,m,j,i:longint;
  s,r,kq:ansistring;
BEGIN
  ///assign(input,'a.in'); reset(input);
  readln(s);  n := length(s);
  readln(r);  m := length(r);

  fillchar(f,sizeof(f),0);
  for i:=1 to n do
    for j:=1 to m do
      if s[i] <> r[j] then f[i,j] := max(f[i-1,j], f[i,j-1])
      else f[i,j] := 1 + f[i-1,j-1];
  kq := '';
  i:=n; j:=m;
  while((i>0) and (j>0)) do begin
    if (f[i,j] = f[i-1,j-1] + 1) and (s[i]=r[j]) then begin kq:=s[i] + kq; dec(i); dec(j); end
    else
      if f[i,j] = f[i-1,j] then dec(i) else dec(j);
  end;
  writeln(kq);
END.


uses
  math;
var
  f:array[0..3033,0..3033] of longint;
  n,m,j,i:longint;
  s,r,kq:ansistring;
BEGIN
  ///assign(input,'a.in'); reset(input);
  readln(s);  n := length(s);
  readln(r);  m := length(r);

  fillchar(f,sizeof(f),0);
  for i:=1 to n do
    for j:=1 to m do
      if s[i] <> r[j] then f[i,j] := max(f[i-1,j], f[i,j-1])
      else f[i,j] := 1 + f[i-1,j-1];
  kq := '';
  i:=n; j:=m;
  while((i>0) and (j>0)) do begin
    if f[i,j] = f[i-1,j-1] + 1 then begin kq:=s[i] + kq; dec(i); dec(j); end
    else
      if f[i,j] = f[i-1,j] then dec(i) else dec(j);
  end;
  writeln(kq);
END.


var
	s,t,ans:ansistring;
	i,j,n,m:longint;
	a:array[0..3001,0..3001] of longint;
begin
	readln(s); n:=length(s);
	readln(t); m:=length(t);
	fillchar(a,sizeof(a),0);
	for i:=1 to n do
		for j:=1 to m do
			if s[i]=t[j] then a[i,j]:=a[i-1,j-1]+1
			else if a[i-1,j]>a[i,j-1] then a[i,j]:=a[i-1,j]
					else a[i,j]:=a[i,j-1];
	ans:='';
	i:=n; j:=m;
	while (i>0) and (j>0) do
		if s[i]=t[j] then
		begin
			ans:=s[i]+ans;
			dec(i); dec(j);
		end
		else if a[i-1,j]>a[i,j-1] then dec(i)
				else dec(j);
	writeln(ans);
end.


var
	s,t,ans:ansistring;
	i,j,n,m:longint;
	a:array[0..3001,0..3001] of longint;
begin
	readln(s); n:=length(s);
	readln(t); m:=length(t);
	fillchar(a,sizeof(a),0);
	for i:=1 to n do
		for j:=1 to m do
			if s[i]=t[j] then a[i,j]:=a[i-1,j-1]+1
			else if a[i-1,j]>a[i,j-1] then a[i,j]:=a[i-1,j]
					else a[i,j]:=a[i,j-1];
	ans:='';
	i:=1; j:=1;
	while (i<=n) and (j<=m) do
		if s[i]=t[j] then
		begin
			ans:=ans+s[i];
			inc(i); inc(j);
		end
		else if a[i+1,j]<a[i,j+1] then inc(j)
				else inc(i);
	writeln(ans);
end.


var
	s,t,ans:ansistring;
	i,j,n,m:longint;
	a:array[0..3001,0..3001] of longint;
begin
	readln(s); n:=length(s);
	readln(t); m:=length(t);
	fillchar(a,sizeof(a),0);
	for i:=1 to n do
		for j:=1 to m do
			if s[i]=t[j] then a[i,j]:=a[i-1,j-1]+1
			else if a[i-1,j]>a[i,j-1] then a[i,j]:=a[i-1,j]
					else a[i,j]:=a[i,j-1];
	ans:='';
	i:=1; j:=1;
	while (i<=n) and (j<=m) do
		if s[i]=t[j] then
		begin
			ans:=ans+s[i];
			inc(i); inc(j);
		end
		else if a[i+1,j]<a[i,j+1] then inc(j)
				else inc(i);
	if ans<>'' then writeln(ans);
end.


var
	s,t,ans:ansistring;
	i,j,n,m:longint;
	a:array[0..3001,0..3001] of longint;
begin
	readln(s); n:=length(s);
	readln(t); m:=length(t);
	fillchar(a,sizeof(a),0);
	for i:=1 to n do
		for j:=1 to m do
			if s[i]=t[j] then a[i,j]:=a[i-1,j-1]+1
			else if a[i-1,j]>a[i,j-1] then a[i,j]:=a[i-1,j]
					else a[i,j]:=a[i,j-1];
	writeln(a[n,m]);
	ans:='';
	i:=1; j:=1;
	while (i<=n) and (j<=m) do
		if s[i]=t[j] then
		begin
			ans:=ans+s[i];
			inc(i); inc(j);
		end
		else if a[i+1,j]<a[i,j+1] then inc(j)
				else inc(i);
	if ans<>'' then writeln(ans);
end.


type inputunit = record
    x,y,h:integer;
  end;
var r : array[1..100] of inputunit;
    i,n,x,y,h,th : longint;
    t,f : boolean;

function height:integer;
begin
  height := abs(x - r[i].x) + abs(y - r[i].y) + r[i].h
end;

begin
  readln(n);
  for i := 1 to n do with r[i] do readln(x,y,h);
  for x := 0 to 100 do begin
    for y := 0 to 100 do begin
      h := 1000000201;
      f := true;
      t := true;
      for i := 1 to n do begin
        if t then begin
          if r[i].h > 0 then t := false;
          th := height;
          if h >= th then h := th else if not t then f := false
        end else if h <> height then f := false
      end;
      if f = true then break
    end;
    if f = true then break
  end;
  writeln(x,' ',y,' ',h)
end.

Score: 300 points

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.

The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.

Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (x_i, y_i) is h_i."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

N is an integer between 1 and 100 (inclusive).

x_i and y_i are integers between 0 and 100 (inclusive).

h_i is an integer between 0 and 10^9 (inclusive).

The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.

The center coordinates and the height of the pyramid can be uniquely identified.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 h_1
x_2 y_2 h_2
x_3 y_3 h_3
:
x_N y_N h_N

Output

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

Sample Input 1

4
2 3 5
2 1 5
1 2 5
3 2 5

Sample Output 1

2 2 6

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

Sample Input 2

2
0 0 100
1 1 98

Sample Output 2

0 0 100

In this case, the center coordinates and the height can be identified as (0, 0) and 100.

Note that C_X and C_Y are known to be integers between 0 and 100.

Sample Input 3

3
99 1 191
100 1 192
99 0 192

Sample Output 3

100 0 193

In this case, the center coordinates and the height can be identified as (100, 0) and 193.


type inputunit = record
    x,y,h:integer;
  end;
var r : array[1..100] of inputunit;
    i,n,x,y,h,th : longint;
    t,f : boolean;

function height:integer;
begin
  height := abs(x - r[i].x) + abs(y - r[i].y) + r[i].h
end;

begin
  readln(n);
  for i := 1 to n do with r[i] do readln(x,y,h);
  for x := 0 to 100 do begin
    for y := 0 to 100 do begin
      h := 1000000201;
      f := true;
      t := true;
      for i := 1 to n do begin
        if t then begin
          if r[i].h > 0 then t := false;
          th := height;
          if h >= th then h := th else if t then f := false
        end else if h <> height then f := false
      end;
      if f = true then break
    end;
    if f = true then break
  end;
  writeln(x,' ',y,' ',h)
end.