puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 032

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

const int inf = 1e6;

void chmin(int &a, int b) { a = min(a, b); }

void print_vector_vector(vector<vector<int>> vv) {
  for (vector<int> v : vv) {
    for (int i : v) {
      cout << i << ",";
    }
    cout << endl;
  }
}

void tobit(int n, int d) {
  rep(i, d) {
    bool bit = (n >> i) & 1;
    cout << bit;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n, 0));
  rep(i, n) {
    rep(j, n) { cin >> a[i][j]; }
  }
  int m;
  cin >> m;
  // vector<vector<bool>> isHate(n, vector<bool>(n, false));
  vector<bool> isHate(1 << n, false);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    isHate[(1 << x) | (1 << y)] = true;
  }
  vector<vector<int>> dp(1 << n, vector<int>(n, inf));
  rep(i, n) {
    int next_pos = i;
    int next_state = 1 << next_pos;
    dp[next_state][next_pos] = a[next_pos][0];
  }
  rep(i, (1 << n) - 1) {
    int now_state = i;
    rep(j, n) {
      int now_pos = j;
      bool check = (now_state >> j) & 1;
      if (!check) {
        continue;
      }
      rep(k, n) {
        int next_pos = k;
        if (next_pos == now_pos) {
          continue;
        }
        bool check = (now_state >> next_pos) & 1;
        if (check) {
          continue;
        }
        if (isHate[(1 << next_pos) | (1 << now_pos)]) {
          continue;
        }
        int next_state = (1 << next_pos) | now_state;
        int step = __builtin_popcount(now_state);
        chmin(dp[next_state][next_pos],
              dp[now_state][now_pos] + a[next_pos][step]);
      }
    }
  }
  int ans = *min_element(all(dp[(1 << n) - 1]));
  if (ans == inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

Crystal solution: n = read_line.to_i
a = (1..n).map { read_line.split.map(&.to_i) }
m = read_line.to_i
flag = (1..n).map { [true] * n }
m.times do
  x, y = read_line.split.map(&.to_i.pred)
  flag[x][y] = flag[y][x] = false
end

puts (0...n).to_a.each_permutation.select { |perm|
  perm.each_cons(2).all? { |(i, j)| flag[i][j] }
}.min_of? { |perm|
  (0...n).sum { |i| a[perm[i]][i] }
} || -1
AtCoder problem 032

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

const int inf = 1e6;

void chmin(int &a, int b) { a = min(a, b); }

void print_vector_vector(vector<vector<int>> vv) {
  for (vector<int> v : vv) {
    for (int i : v) {
      cout << i << ",";
    }
    cout << endl;
  }
}

void tobit(int n, int d) {
  rep(i, d) {
    bool bit = (n >> i) & 1;
    cout << bit;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n, 0));
  rep(i, n) {
    rep(j, n) { cin >> a[i][j]; }
  }
  int m;
  cin >> m;
  // vector<vector<bool>> isHate(n, vector<bool>(n, false));
  vector<bool> isHate(1 << n, false);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    isHate[(1 << x) | (1 << y)] = true;
  }
  vector<vector<int>> dp(1 << n, vector<int>(n, inf));
  rep(i, n) {
    int next_pos = i;
    int next_state = 1 << next_pos;
    dp[next_state][next_pos] = a[next_pos][0];
  }
  rep(i, (1 << n) - 1) {
    int now_state = i;
    rep(j, n) {
      int now_pos = j;
      bool check = (now_state >> j) & 1;
      if (!check) {
        continue;
      }
      rep(k, n) {
        int next_pos = k;
        if (next_pos == now_pos) {
          continue;
        }
        bool check = (now_state >> next_pos) & 1;
        if (check) {
          continue;
        }
        if (isHate[(1 << next_pos) | (1 << now_pos)]) {
          continue;
        }
        int next_state = (1 << next_pos) | now_state;
        int step = __builtin_popcount(now_state);
        chmin(dp[next_state][next_pos],
              dp[now_state][now_pos] + a[next_pos][step]);
      }
    }
  }
  int ans = *min_element(all(dp[(1 << n) - 1]));
  if (ans == inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}


Python solution: #!/usr/bin/env python3

import itertools

n = int(input())
a = [[] for _ in range(n)]
for i in range(n):
    a[i] = list(map(int, input().split()))
m = int(input())
ng = [[False] * n for _ in range(n)]
for _ in range(m):
    x, y = map(int, input().split())
    ng[x - 1][y - 1] = ng[y - 1][x - 1] = True

inf = 1_000_000_000
ans = inf
for p in itertools.permutations(range(n)):
    cost = 0
    for i in range(n):
        cost += a[p[i]][i]
    if cost >= ans:
        continue
    ok = True
    for i in range(n - 1):
        x = p[i]
        y = p[i + 1]
        if ng[x][y]:
            ok = False
            break
    if ok:
        ans = cost
if ans == inf:
    print(-1)
else:
    print(ans)
AtCoder problem 032

C++ solution:

Crystal solution: n = read_line.to_i
a = (1..n).map { read_line.split.map(&.to_i) }
m = read_line.to_i
flag = (1..n).map { [true] * n }
m.times do
  x, y = read_line.split.map(&.to_i.pred)
  flag[x][y] = flag[y][x] = false
end

puts (0...n).to_a.each_permutation.select { |perm|
  perm.each_cons(2).all? { |(i, j)| flag[i][j] }
}.min_of? { |perm|
  (0...n).sum { |i| a[perm[i]][i] }
} || -1


Python solution: #!/usr/bin/env python3

import itertools

n = int(input())
a = [[] for _ in range(n)]
for i in range(n):
    a[i] = list(map(int, input().split()))
m = int(input())
ng = [[False] * n for _ in range(n)]
for _ in range(m):
    x, y = map(int, input().split())
    ng[x - 1][y - 1] = ng[y - 1][x - 1] = True

inf = 1_000_000_000
ans = inf
for p in itertools.permutations(range(n)):
    cost = 0
    for i in range(n):
        cost += a[p[i]][i]
    if cost >= ans:
        continue
    ok = True
    for i in range(n - 1):
        x = p[i]
        y = p[i + 1]
        if ng[x][y]:
            ok = False
            break
    if ok:
        ans = cost
if ans == inf:
    print(-1)
else:
    print(ans)
AtCoder problem 032

C++ solution:

Crystal solution: n = read_line.to_i
a = (1..n).map { read_line.split.map(&.to_i) }
m = read_line.to_i
flag = (1..n).map { [true] * n }
m.times do
  x, y = read_line.split.map(&.to_i.pred)
  flag[x][y] = flag[y][x] = false
end

puts (0...n).to_a.each_permutation.select { |perm|
  perm.each_cons(2).all? { |(i, j)| flag[i][j] }
}.min_of? { |perm|
  (0...n).sum { |i| a[perm[i]][i] }
} || -1





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: [][]int :: #[n, n]int
	for i(0, n - 1)
		for j(0, n - 1)
			do a[i][j] :: cui@inputInt()
		end for
	end for
	
	var m: int :: cui@inputInt()
	var xy: [][]bool :: #[n, n]bool
	for(1, m)
		var x: int :: cui@inputInt() - 1
		var y: int :: cui@inputInt() - 1
		do xy[x][y] :: true
		do xy[y][x] :: true
	end for
	
	var idxs: []int :: #[n]int
	for i(0, n - 1)
		do idxs[i] :: i
	end for
	
	var ans: int :: lib@intMax
	
	while loop(@nextPermutation(idxs), skip)
		var t: int :: a[idxs[0]][0]
		for i(1, n - 1)
			if(xy[idxs[i]][idxs[i - 1]])
				skip loop
			end if
			do t :+ a[idxs[i]][i]
		end for
		do ans :: [ans, t].min()
	end while
	
	if(ans = lib@intMax)
		do ans :: -1
	end if
	do cui@print("\{ans}\n")
end func

func nextPermutation(arr: []int): bool
	var len: int :: ^arr
	var left: int :: len - 2
	while(left >= 0 & arr[left] >= arr[left + 1])
		do left :- 1
	end while
	if(left < 0)
		ret false
	end if
	var right: int :: len - 1
	while(arr[left] >= arr[right])
		do right :- 1
	end while
	var tmp: int :: arr[left]
	do arr[left] :: arr[right]
	do arr[right] :: tmp
	do left :+ 1
	do right :: len - 1
	while(left < right)
		do tmp :: arr[left]
		do arr[left] :: arr[right]
		do arr[right] :: tmp
		do left :+ 1
		do right :- 1
	end while
	ret true
end func
AtCoder problem 032

C++ solution:


Python solution: #!/usr/bin/env python3

import itertools

n = int(input())
a = [[] for _ in range(n)]
for i in range(n):
    a[i] = list(map(int, input().split()))
m = int(input())
ng = [[False] * n for _ in range(n)]
for _ in range(m):
    x, y = map(int, input().split())
    ng[x - 1][y - 1] = ng[y - 1][x - 1] = True

inf = 1_000_000_000
ans = inf
for p in itertools.permutations(range(n)):
    cost = 0
    for i in range(n):
        cost += a[p[i]][i]
    if cost >= ans:
        continue
    ok = True
    for i in range(n - 1):
        x = p[i]
        y = p[i + 1]
        if ng[x][y]:
            ok = False
            break
    if ok:
        ans = cost
if ans == inf:
    print(-1)
else:
    print(ans)




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: [][]int :: #[n, n]int
	for i(0, n - 1)
		for j(0, n - 1)
			do a[i][j] :: cui@inputInt()
		end for
	end for
	
	var m: int :: cui@inputInt()
	var xy: [][]bool :: #[n, n]bool
	for(1, m)
		var x: int :: cui@inputInt() - 1
		var y: int :: cui@inputInt() - 1
		do xy[x][y] :: true
		do xy[y][x] :: true
	end for
	
	var idxs: []int :: #[n]int
	for i(0, n - 1)
		do idxs[i] :: i
	end for
	
	var ans: int :: lib@intMax
	
	while loop(@nextPermutation(idxs), skip)
		var t: int :: a[idxs[0]][0]
		for i(1, n - 1)
			if(xy[idxs[i]][idxs[i - 1]])
				skip loop
			end if
			do t :+ a[idxs[i]][i]
		end for
		do ans :: [ans, t].min()
	end while
	
	if(ans = lib@intMax)
		do ans :: -1
	end if
	do cui@print("\{ans}\n")
end func

func nextPermutation(arr: []int): bool
	var len: int :: ^arr
	var left: int :: len - 2
	while(left >= 0 & arr[left] >= arr[left + 1])
		do left :- 1
	end while
	if(left < 0)
		ret false
	end if
	var right: int :: len - 1
	while(arr[left] >= arr[right])
		do right :- 1
	end while
	var tmp: int :: arr[left]
	do arr[left] :: arr[right]
	do arr[right] :: tmp
	do left :+ 1
	do right :: len - 1
	while(left < right)
		do tmp :: arr[left]
		do arr[left] :: arr[right]
		do arr[right] :: tmp
		do left :+ 1
		do right :- 1
	end while
	ret true
end func
AtCoder problem 033

C++ solution:
#include <iostream>

using namespace std;



int H, W;



int main() {

	cin >> H >> W;

	if (H == 1 || W == 1) cout << H * W << endl;

	else cout << ((H + 1) / 2) * ((W + 1) / 2) << endl;

	return 0;

}

Crystal solution: a, b = read_line.split.map(&.to_i)
if a == 1 || b == 1
  puts a * b
else
  puts (a.succ // 2) * (b.succ // 2)
end
AtCoder problem 033

C++ solution:
#include <iostream>

using namespace std;



int H, W;



int main() {

	cin >> H >> W;

	if (H == 1 || W == 1) cout << H * W << endl;

	else cout << ((H + 1) / 2) * ((W + 1) / 2) << endl;

	return 0;

}


Python solution: import math

h, w = map(int, input().split())

if h == 1 or w == 1:
    print(h*w)
else:
    print(math.ceil(h/2) * math.ceil(w/2))
AtCoder problem 033

C++ solution:
#include <iostream>

using namespace std;



int H, W;



int main() {

	cin >> H >> W;

	if (H == 1 || W == 1) cout << H * W << endl;

	else cout << ((H + 1) / 2) * ((W + 1) / 2) << endl;

	return 0;

}





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var ans: int
	if(h = 1 | w = 1)
		do ans :: h * w
	else
		do ans :: ((h + 1) / 2) * ((w + 1) / 2)
	end if
	do cui@print("\{ans}\n")
end func
AtCoder problem 033

C++ solution:

Crystal solution: a, b = read_line.split.map(&.to_i)
if a == 1 || b == 1
  puts a * b
else
  puts (a.succ // 2) * (b.succ // 2)
end


Python solution: import math

h, w = map(int, input().split())

if h == 1 or w == 1:
    print(h*w)
else:
    print(math.ceil(h/2) * math.ceil(w/2))
AtCoder problem 033

C++ solution:

Crystal solution: a, b = read_line.split.map(&.to_i)
if a == 1 || b == 1
  puts a * b
else
  puts (a.succ // 2) * (b.succ // 2)
end





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var ans: int
	if(h = 1 | w = 1)
		do ans :: h * w
	else
		do ans :: ((h + 1) / 2) * ((w + 1) / 2)
	end if
	do cui@print("\{ans}\n")
end func
AtCoder problem 033

C++ solution:


Python solution: import math

h, w = map(int, input().split())

if h == 1 or w == 1:
    print(h*w)
else:
    print(math.ceil(h/2) * math.ceil(w/2))




Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var ans: int
	if(h = 1 | w = 1)
		do ans :: h * w
	else
		do ans :: ((h + 1) / 2) * ((w + 1) / 2)
	end if
	do cui@print("\{ans}\n")
end func
AtCoder problem 034

C++ solution:
#include <bits/stdc++.h>
using namespace std;

bool is_ok(int N, int K, vector<int> &a, int len)
{
    int num = 0;
    map<int, int> cnt_map;
    for (int i = 0; i < len; ++i) {
        auto ai = a.at(i);
        ++cnt_map[ai];
        if (cnt_map.at(ai) == 1)
            ++num;
    }
    if (num <= K)
        return true;
    for (int i = 0, j = len; j < N; ++i, ++j) {
        auto ai = a.at(i);
        auto aj = a.at(j);
        --cnt_map[ai];
        if (cnt_map.at(ai) == 0)
            --num;
        ++cnt_map[aj];
        if (cnt_map.at(aj) == 1)
            ++num;
        if (num <= K)
            return true;
    }
    return false;
}

int main()
{
    int N, K;
    cin >> N >> K;
    vector<int> a(N);
    for (auto &ai : a)
        cin >> ai;
    int ok = 0;
    int ng = N + 1;
    while (abs(ok - ng) > 1) {
        auto mid = (ok + ng) / 2;
        if (is_ok(N, K, a, mid))
            ok = mid;
        else
            ng = mid;
    }
    cout << ok << endl;
}


Crystal solution: n, k = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)

table = Hash(Int32, Int32).new(0)
cnt, r, ans = 0, 0, 0
(0...n).each do |l|
  while r < n && (cnt < k || table[a[r]] > 0)
    cnt += 1 if table[a[r]] == 0
    table[a[r]] += 1
    r += 1
  end

  ans = {ans, r - l}.max

  cnt -= 1 if table[a[l]] == 1
  table[a[l]] -= 1
end
puts ans
AtCoder problem 034

C++ solution:
#include <bits/stdc++.h>
using namespace std;

bool is_ok(int N, int K, vector<int> &a, int len)
{
    int num = 0;
    map<int, int> cnt_map;
    for (int i = 0; i < len; ++i) {
        auto ai = a.at(i);
        ++cnt_map[ai];
        if (cnt_map.at(ai) == 1)
            ++num;
    }
    if (num <= K)
        return true;
    for (int i = 0, j = len; j < N; ++i, ++j) {
        auto ai = a.at(i);
        auto aj = a.at(j);
        --cnt_map[ai];
        if (cnt_map.at(ai) == 0)
            --num;
        ++cnt_map[aj];
        if (cnt_map.at(aj) == 1)
            ++num;
        if (num <= K)
            return true;
    }
    return false;
}

int main()
{
    int N, K;
    cin >> N >> K;
    vector<int> a(N);
    for (auto &ai : a)
        cin >> ai;
    int ok = 0;
    int ng = N + 1;
    while (abs(ok - ng) > 1) {
        auto mid = (ok + ng) / 2;
        if (is_ok(N, K, a, mid))
            ok = mid;
        else
            ng = mid;
    }
    cout << ok << endl;
}



Python solution: from collections import deque

n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
q = deque()
ans = 0

kinds = 0
num_dict = {}

for i in range(n):
    now_app = a[i]
    q.append(now_app)
    if now_app not in num_dict:
        num_dict[now_app] = 1
        kinds += 1
    else:
        num_dict[now_app] += 1
    while kinds > k:
        now_rem = q.popleft()
        if now_rem in num_dict:
            if num_dict[now_rem] == 1:
                num_dict.pop(now_rem)
                kinds -= 1
            else:
                num_dict[now_rem] -= 1
    # print(q)
    # print(num_dict, kinds)
    ans = max(ans, len(q))

print(ans)
AtCoder problem 034

C++ solution:
#include <bits/stdc++.h>
using namespace std;

bool is_ok(int N, int K, vector<int> &a, int len)
{
    int num = 0;
    map<int, int> cnt_map;
    for (int i = 0; i < len; ++i) {
        auto ai = a.at(i);
        ++cnt_map[ai];
        if (cnt_map.at(ai) == 1)
            ++num;
    }
    if (num <= K)
        return true;
    for (int i = 0, j = len; j < N; ++i, ++j) {
        auto ai = a.at(i);
        auto aj = a.at(j);
        --cnt_map[ai];
        if (cnt_map.at(ai) == 0)
            --num;
        ++cnt_map[aj];
        if (cnt_map.at(aj) == 1)
            ++num;
        if (num <= K)
            return true;
    }
    return false;
}

int main()
{
    int N, K;
    cin >> N >> K;
    vector<int> a(N);
    for (auto &ai : a)
        cin >> ai;
    int ok = 0;
    int ng = N + 1;
    while (abs(ok - ng) > 1) {
        auto mid = (ok + ng) / 2;
        if (is_ok(N, K, a, mid))
            ok = mid;
        else
            ng = mid;
    }
    cout << ok << endl;
}






Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var map: dict<int, int> :: #dict<int, int>
	var l: int :: 0
	var r: int :: 0
	var ans: int :: 0
	while(l <> n)
		if(r = n | ^map > k)
			var key: int :: a[l]
			var val: int :: map.get(key, &) - 1
			if(val = 0)
				do map.del(key)
			else
				do map.add(key, val)
			end if
			do l :+ 1
		else
			var key: int :: a[r]
			var val: int :: map.get(key, &) + 1
			do map.add(key, val)
			do r :+ 1
		end if
		if(^map <= k)
			do ans :: [ans, r - l].max()
		end if
	end while
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 034

C++ solution:

Crystal solution: n, k = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)

table = Hash(Int32, Int32).new(0)
cnt, r, ans = 0, 0, 0
(0...n).each do |l|
  while r < n && (cnt < k || table[a[r]] > 0)
    cnt += 1 if table[a[r]] == 0
    table[a[r]] += 1
    r += 1
  end

  ans = {ans, r - l}.max

  cnt -= 1 if table[a[l]] == 1
  table[a[l]] -= 1
end
puts ans


Python solution: from collections import deque

n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
q = deque()
ans = 0

kinds = 0
num_dict = {}

for i in range(n):
    now_app = a[i]
    q.append(now_app)
    if now_app not in num_dict:
        num_dict[now_app] = 1
        kinds += 1
    else:
        num_dict[now_app] += 1
    while kinds > k:
        now_rem = q.popleft()
        if now_rem in num_dict:
            if num_dict[now_rem] == 1:
                num_dict.pop(now_rem)
                kinds -= 1
            else:
                num_dict[now_rem] -= 1
    # print(q)
    # print(num_dict, kinds)
    ans = max(ans, len(q))

print(ans)
AtCoder problem 034

C++ solution:

Crystal solution: n, k = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)

table = Hash(Int32, Int32).new(0)
cnt, r, ans = 0, 0, 0
(0...n).each do |l|
  while r < n && (cnt < k || table[a[r]] > 0)
    cnt += 1 if table[a[r]] == 0
    table[a[r]] += 1
    r += 1
  end

  ans = {ans, r - l}.max

  cnt -= 1 if table[a[l]] == 1
  table[a[l]] -= 1
end
puts ans





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var map: dict<int, int> :: #dict<int, int>
	var l: int :: 0
	var r: int :: 0
	var ans: int :: 0
	while(l <> n)
		if(r = n | ^map > k)
			var key: int :: a[l]
			var val: int :: map.get(key, &) - 1
			if(val = 0)
				do map.del(key)
			else
				do map.add(key, val)
			end if
			do l :+ 1
		else
			var key: int :: a[r]
			var val: int :: map.get(key, &) + 1
			do map.add(key, val)
			do r :+ 1
		end if
		if(^map <= k)
			do ans :: [ans, r - l].max()
		end if
	end while
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 034

C++ solution:


Python solution: from collections import deque

n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
q = deque()
ans = 0

kinds = 0
num_dict = {}

for i in range(n):
    now_app = a[i]
    q.append(now_app)
    if now_app not in num_dict:
        num_dict[now_app] = 1
        kinds += 1
    else:
        num_dict[now_app] += 1
    while kinds > k:
        now_rem = q.popleft()
        if now_rem in num_dict:
            if num_dict[now_rem] == 1:
                num_dict.pop(now_rem)
                kinds -= 1
            else:
                num_dict[now_rem] -= 1
    # print(q)
    # print(num_dict, kinds)
    ans = max(ans, len(q))

print(ans)




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var map: dict<int, int> :: #dict<int, int>
	var l: int :: 0
	var r: int :: 0
	var ans: int :: 0
	while(l <> n)
		if(r = n | ^map > k)
			var key: int :: a[l]
			var val: int :: map.get(key, &) - 1
			if(val = 0)
				do map.del(key)
			else
				do map.add(key, val)
			end if
			do l :+ 1
		else
			var key: int :: a[r]
			var val: int :: map.get(key, &) + 1
			do map.add(key, val)
			do r :+ 1
		end if
		if(^map <= k)
			do ans :: [ans, r - l].max()
		end if
	end while
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 036

C++ solution:
#include <iostream>

#include <cmath>

#include <algorithm>

using namespace std;



long long N, X[1 << 18], Y[1 << 18];

long long Q, T[1 << 18];

long long min_X = (1LL << 60), max_X = -(1LL << 60);

long long min_Y = (1LL << 60), max_Y = -(1LL << 60);



int main() {

	// Step #1. “ü—Í

	cin >> N >> Q;

	for (int i = 1; i <= N; i++) cin >> X[i] >> Y[i];

	for (int i = 1; i <= Q; i++) cin >> T[i];



	// Step #2. 45 “x‰ñ“]

	for (int i = 1; i <= N; i++) {

		long long p1 = X[i] + Y[i];

		long long p2 = Y[i] - X[i];

		X[i] = p1;

		Y[i] = p2;

		min_X = min(min_X, X[i]);

		max_X = max(max_X, X[i]);

		min_Y = min(min_Y, Y[i]);

		max_Y = max(max_Y, Y[i]);

	}



	// Step #3. ƒNƒGƒŠ‚É“š‚¦‚é

	for (int i = 1; i <= Q; i++) {

		long long ret1 = abs(X[T[i]] - min_X);

		long long ret2 = abs(X[T[i]] - max_X);

		long long ret3 = abs(Y[T[i]] - min_Y);

		long long ret4 = abs(Y[T[i]] - max_Y);

		cout << max({ ret1, ret2, ret3, ret4 }) << endl;

	}

	return 0;

}

Crystal solution: n, q = read_line.split.map(&.to_i)
x, y = (1..n).map {
  a, b = read_line.split.map(&.to_i64)
  {a - b, a + b}
}.transpose
xy = x.minmax + y.minmax

q.times do
  i = read_line.to_i - 1
  puts [x[i], x[i], y[i], y[i]].zip(xy).max_of { |a, b| (a - b).abs }
end
AtCoder problem 036

C++ solution:
#include <iostream>

#include <cmath>

#include <algorithm>

using namespace std;



long long N, X[1 << 18], Y[1 << 18];

long long Q, T[1 << 18];

long long min_X = (1LL << 60), max_X = -(1LL << 60);

long long min_Y = (1LL << 60), max_Y = -(1LL << 60);



int main() {

	// Step #1. “ü—Í

	cin >> N >> Q;

	for (int i = 1; i <= N; i++) cin >> X[i] >> Y[i];

	for (int i = 1; i <= Q; i++) cin >> T[i];



	// Step #2. 45 “x‰ñ“]

	for (int i = 1; i <= N; i++) {

		long long p1 = X[i] + Y[i];

		long long p2 = Y[i] - X[i];

		X[i] = p1;

		Y[i] = p2;

		min_X = min(min_X, X[i]);

		max_X = max(max_X, X[i]);

		min_Y = min(min_Y, Y[i]);

		max_Y = max(max_Y, Y[i]);

	}



	// Step #3. ƒNƒGƒŠ‚É“š‚¦‚é

	for (int i = 1; i <= Q; i++) {

		long long ret1 = abs(X[T[i]] - min_X);

		long long ret2 = abs(X[T[i]] - max_X);

		long long ret3 = abs(Y[T[i]] - min_Y);

		long long ret4 = abs(Y[T[i]] - max_Y);

		cout << max({ ret1, ret2, ret3, ret4 }) << endl;

	}

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var x: []int :: #[n]int
	var y: []int :: #[n]int
	for i(0, n - 1)
		do x[i] :: cui@inputInt()
		do y[i] :: cui@inputInt()
		var nx: int :: x[i] - y[i]
		var ny: int :: x[i] + y[i]
		do x[i] :: nx
		do y[i] :: ny
	end for
	
	var minX: int :: x[0]
	var maxX: int :: x[0]
	var minY: int :: y[0]
	var maxY: int :: y[0]
	for i(1, n - 1)
		do minX :: [minX, x[i]].min()
		do maxX :: [maxX, x[i]].max()
		do minY :: [minY, y[i]].min()
		do maxY :: [maxY, y[i]].max()
	end for
	
	for(1, q)
		var idx: int :: cui@inputInt() - 1
		var ans: int :: 0
		do ans :: [ans, (x[idx] - minX).abs()].max()
		do ans :: [ans, (x[idx] - maxX).abs()].max()
		do ans :: [ans, (y[idx] - minY).abs()].max()
		do ans :: [ans, (y[idx] - maxY).abs()].max()
		do cui@print("\{ans}\n")
	end for
end func