Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
34 lines (33 sloc) 580 Bytes
var
n, m, x, y, i: longint;
count, twos, threes, ans: longint;
begin
read(n, m, x);
count := 1;
twos := 0;
threes := 0;
for i := 1 to n - 1 do
begin
read(x, y);
if (x = y) then
inc(count)
else
count := 1;
if (count mod 3 = 0) then
begin
dec(twos);
inc(threes)
end;
if (count mod 3 = 2) then
inc(twos)
end;
if (m >= threes + twos) then
ans := threes * 2 + twos
else if (m <= threes) then
ans := m * 2
else
ans := threes + m;
if (ans + m > n) then
ans := n - m;
writeln(ans)
end.