akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppiii;

const int MOD = 1000000007;
ll modpow(ll b, ll e, ll m) {
  ll r = 1;
  while(e) {
    if(e%2) {
      r*=b;
      r%=m;
    }
    b*=b;
    b%=m;
    e/=2;
  }
  return r;
}
ll fact[200000];
ll ifact[200000];

ll choose(int n, int k) {
  assert(k >= 0 && k <= n);
  ll ret = fact[n];
  ret *= ifact[k];
  ret %= MOD;
  ret *= ifact[n-k];
  ret %= MOD;
  return ret;
}

void solve() {
  fact[0] = 1;
  for(int i = 1; i < 200000; i++) {
    fact[i] = i * fact[i-1];
    fact[i] %= MOD;
  }
  ifact[199999] = modpow(fact[199999], MOD-2, MOD);
  for(int i = 199998; i >= 0; i--) {
    ifact[i] = ifact[i+1] * (i+1);
    ifact[i] %= MOD;
  }
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  ll ret = 0;
  int x = h-a;
  int y = b+1;
  while(x >= 1 && y <= w) {
    ll inc = 1;
    inc *= choose(x-1+y-1, x-1);
    inc *= choose(h-x+w-y, h-x);
    ret += inc;
    ret %= MOD;
    x--;
    y++;
  }
  cout << ret << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  /*
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
  */
  solve();
}


Measured memory_bytes: 3328
#!/usr/bin/env python3
import sys


def solve(H: int, W: int):
    if H == 1 or W == 1:
        print(1)
        return
        
    first = W//2 if W%2 == 0 else (W//2)+1
    second = W//2

    if H%2 == 0:
        print(first*(H//2)+second*(H//2))
    else:
        print(first*(H//2+1)+second*(H//2))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	@SuppressWarnings("unused")
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(); // 県の個数
		int m = in.nextInt(); // 市の個数

		// 県、年でソート
		PriorityQueue<City> city = new PriorityQueue<>(m, new Comparator<City>() {

			@Override
			public int compare(City o1, City o2) {
				if ( o1.pref != o2.pref ) {		// 県でソート
					return o1.pref - o2.pref;
				} else {						// 同じ県の時は年でソート
					return o1.year - o2.year;
				}
			}
		});
		for ( int id = 1; id <= m; id++ ) {
			city.add(new City(id, in.nextInt(), in.nextInt()));
		}

		// 県ごとに年でナンバー付けしてidでソート
		String citycode[] = new String[m + 1];
		int p = 0;
		int num = 0;
		while ( city.size() > 0 ) {
			City c = city.poll();
			// 新出の県の場合
			if ( p != c.pref ) {
				p = c.pref;
				num = 1;
			} else {
				num++;
			}

			c.num = num;
			citycode[c.id] = String.format("%06d%06d", c.pref, c.num);
			// System.out.println("id:" + c.id + " pref:" + c.pref + " year:" + c.year);
		}

		PrintWriter pw = new PrintWriter(System.out);
		for ( int i = 1; i<=m; i++ ) {
			pw.println(citycode[i]);
		}
		pw.flush();
		in.close();
	}
}

class City {

	int	id		= 0;
	int	pref	= 0;
	int	year	= 0;
	int	num		= 0;

	public City(int id, int pref, int year) {
		this.id = id;
		this.pref = pref;
		this.year = year;
	}
}


Measured memory_bytes: 141160
#include <iostream>

using namespace std;

int main() {
    int H, W;
    cin >> H >> W;
    
    while (!(H == 0 && W == 0)) {
        for (int i = 0; i < W; i++)
            cout << '#';
        cout << endl;
        
        for (int i = 0; i < H - 2; i++) {
            cout << '#';
            for (int j = 0; j < W - 2; j++)
                cout << '.';
            cout << '#';
            cout << endl;
        }
        
        for (int i = 0; i < W; i++)
            cout << '#';
        cout << endl << endl;
        
        cin >> H >> W;
    }
    
    return 0;
}


Measured memory_bytes: 3100
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Node
{
	int key;
	struct Node *prev, *next;
};

struct Node *nil;

struct Node *listSearch(int key)
{
	struct Node *cur = nil->next;
	while(cur != nil && cur->key != key)
	{
		 cur = cur->next;
	}
	return cur;
}

void init()
{
	nil = (struct Node *)malloc(sizeof(struct Node));
	nil->next = nil;
	nil->prev = nil;
}

void printList()
{
	struct Node *cur =nil->next;
	int isf = 0;
	while(cur != nil)
	{
		if(isf++) putchar_unlocked(' ');
		printf("%d",cur->key);
		cur = cur->next;
	}
	putchar_unlocked('\n');
}


void deleteNode(struct Node *t)
{
	if(t ==nil) return;
	t->prev->next = t->next;
	t->next->prev = t->prev;
	free(t);
}

void deleteFirst()
{
	deleteNode(nil->next);
}

void deleteLast()
{
	deleteNode(nil->prev);
}

void deleteKey(int key)
{
	deleteNode(listSearch(key));
}


void insert(int key){
	struct Node *x = (struct Node *)malloc(sizeof(struct Node));
	x->key = key;
	x->next = nil->next;
	nil->next->prev = x;
	nil->next = x;
	x->prev = nil;
}

int main()
{
	int key,n,i;
	char com[20];
	nil = (struct Node*)malloc(sizeof(struct Node));
	nil->next = nil;
	nil->prev = nil;
	
	scanf("%d",&n);

	for(i=0;i<n;i++)
	{
		scanf("%s",com);
		if(strlen(com)>6)
		{
			if(com[6]=='F')
			{
				deleteFirst();
			}
			else
			{
				deleteLast();
			}
		}
		else
		{
			scanf("%d",&key);
			if(com[0] =='d')
			{
				deleteKey(key);
			}
			else
			{
				insert(key);
			}
		}
	}
	
	printList();
	
	return 0;
}
	


Measured memory_bytes: 32972
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int A,B,C,K;
    cin >> A >> B >> C >> K;
    int max;
    if(A >= K){
        max = K;
    }
    else if(A+B >= K){
        max = A;
    }
    else{
        max = A - (K - A - B);
    }
    cout << max << endl;
    return 0;
}

Measured memory_bytes: 3616
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9+7;

int main(){
    ll i,n,m;
    cin >> n >> m;
    ll a[m],b[n+1000];
    for(i = 0; i < m; i++){
        cin >> a[i];
    }
    b[0]= 1;
    b[1] = 1;
    //b[2] = 2;
    ll step = 0;
  if(a[step] == 1){
    b[1] = 0;
    step++;
  }
    for(i = 2; i < n+1;i++){
        if(i == a[step]){
            b[i] = 0;
            step++;
        }
        else{
          b[i] = b[i-1] + b[i-2] % mod;
        }
    }

    ll ans = b[n] % mod;
  	cout << ans << endl;
    return 0;

}

Measured memory_bytes: 1792
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mod 1000000007
typedef long long ll;
using namespace std;

int main(){
  string s;
  cin >> s;
  int ssize = (int)s.size()-2;
  cout << s[0] << ssize << s[(int)s.size()-1] << endl;
  return 0;
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		SC sc = new SC(System.in);
		int N=sc.nextInt();
		int M=sc.nextInt();
		UnionFind uft=new UnionFind(N);
		for(int i=0; i<M; i++) {
			int x=sc.nextInt()-1;
			int y=sc.nextInt()-1;
			int z=sc.nextInt();
			uft.unite(x, y);
		}
		HashSet<Integer> set = new HashSet<Integer>();
		for(int i=0; i<N; i++) {
			set.add(uft.root(i));
		}
		pl(set.size());
	}
	static class SC {
		private BufferedReader reader = null;
		private StringTokenizer tokenizer = null;
		public SC(InputStream in) {
			reader = new BufferedReader(new InputStreamReader(in));
		}
		public String next() {
			if (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new UncheckedIOException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}
	}
	public static void pl(Object o) {
		System.out.println(o);
	}
	public static void pl() {
		System.out.println();
	}
	public static void p(Object o) {
		System.out.print(o);
	}
}
class UnionFind {
	int[] par;
	int[] num;

	UnionFind(int n) {
		par = new int[n];
		num = new int[n];
		for(int i = 0; i < n; i++) {
			par[i] = i;
			num[i] = 1;
		}
	}

	int root(int x) {
		if (par[x] == x) return x;
		return par[x] = root(par[x]);
	}

	void unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);
		if (rx == ry) return;

		par[rx] = ry;
		num[ry] += num[rx];
	}

	boolean same(int x, int y) {
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}

	int getNum(int n) {
		return num[root(n)];
	}
}


Measured memory_bytes: 44828
#include <malloc.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//abc135C
int main(void) {
  int n, k, i, r, d, cou=0, tmp, len, m, max=0, premax=0;
  long long mod=1000000007;
  scanf("%d", &n);
  int a[n];

  for(i=0; i < n; i++){
    scanf(" %d", &a[i]);
    if(max < a[i]) premax=max, max=a[i];
    else if(premax < a[i]) premax=a[i];
  }
  for(i=0; i < n; i++){
    if(a[i] == max) printf("%d\n", premax);
    else printf("%d\n", max);
  }
  return 0;
}

Measured memory_bytes: 2304
using System;
using System.Linq;
using System.Collections.Generic;

class MyClass
{
    public static void Main(string[] args)
    {
        var n = int.Parse(Console.ReadLine());
        var x = Console.ReadLine().Split().Select(long.Parse).ToArray();
        var y = Console.ReadLine().Split().Select(long.Parse).ToArray();
        var z = new long[n];
        for (int i = 0; i < n; i++)
        {
            z[i] = Math.Abs(x[i] - y[i]);
        }
        Console.WriteLine(z.Sum());
        Console.WriteLine(Math.Sqrt(z.Sum(a => a * a)));
        Console.WriteLine(Math.Pow(z.Sum(a => a * a * a), (double)1 / 3));
        Console.WriteLine(z.Max());
    }
}


Measured memory_bytes: 20448
# -*- coding: utf-8 -*-

def main():
    n, m, x, y = list(map(int, input().split()))
    Xs = list(map(int, input().split()))
    Ys = list(map(int, input().split()))
    Xs.append(x)
    Ys.append(y)
    Xs.sort()
    Ys.sort()

    if Xs[-1]+1 <= Ys[0]:
        print("No War")
        exit()
    print("War")


if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
#define _CRT_SECURE_NO_WARNINGS
#define MOD 998244353 
#define INF 100000000
//#define MOD 922337203685419LL
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <bitset>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <functional>
#include <algorithm>
#include <iterator>
#include <cstdio>
#include <utility>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define RFOR(i,a,b) for(ll i=(a);i>(b);i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,n-1,-1)
#define ALL(obj) (obj).begin(), (obj).end()

inline ll madd(ll a, ll b) {
	a %= MOD;b %= MOD;
	return (a + b) % MOD;
}
inline ll msub(ll a, ll b) {
	a %= MOD;b %= MOD;
	a += MOD;
	return (a - b) % MOD;
}
inline ll mmul(ll a, ll b) {
	a %= MOD;b %= MOD;
	return (a*b) % MOD;
}
inline ll mpow(ll a, ll r) {
	long long res = 1;
	while (r > 0) {
		if (r & 1) res = res * a % MOD;
		a = a * a % MOD;
		r >>= 1;
	}
	return res;
}
inline ll minv(ll a) {
	a %= MOD;
	ll b = MOD, u = 0, v = 1;
	while (a) {
		ll t = b / a;
		b -= t * a; swap(a, b);
		u -= t * v; swap(u, v);
	}
	if (u < 0) u += MOD;
	return u;
}
inline ll mdiv(ll a, ll b) {
	a %= MOD;
	a *= minv(b);
	return a%MOD;
}


//ll fact[]
//inline ll combi(int a, int b) {
//	return mmul(fact[a], mmul(fact_inv[b], fact_inv[a - b]));
//}

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unite(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};

int res[10001];
int main() {
	int h, n, a, b;
	cin >> h >> n;
	vector<pi> v;
	REP(i, n) {
		cin >> a >> b;
		v.push_back(pi(a, b));
	}
	FOR(i, 1, 10001) res[i] = 200000000;
	REP(i, 10000) {
		for (auto p : v) {
			res[min(10000, int(i) + p.first)] = min(res[min(10000, int(i) + p.first)], res[i] + p.second);
		}
	}
	int result = res[h];
	FOR(i, h, 10001) result = min(result, res[i]);
	cout << result;
	return 0;
}
	

Measured memory_bytes: 256
use proconio::{input, fastout};
use proconio::marker::{Chars};
use proconio::marker::Usize1;
#[fastout]
fn main() {
    input!{
        n: usize,
    }

    let mut sum = 0;
    for i in 1..n+1 {
        let d = n / i;
        let e = d * i;
        sum += (i+e)*d/2;
        // println!("{}", d);
        // println!("{}", e);
        // println!("{}", sum);
    }

    println!("{}", sum);
}


Measured memory_bytes: 2140
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i = 0; i < (n); i++)
typedef pair<ll, ll> P;


// 座標 corrdinate と深さを保持するための構造体
struct Corr {
    ll y;
    ll x;
    ll depth;
};

ll H, W;  // 迷路の縦，横
ll s_y, s_x, g_y, g_x;  // スタート，ゴールの座標
ll y, x, depth;  // キューから取り出した座標
vector<string> maze;  // 座標を受け取る配列
queue<Corr> q;

vector<ll> y_vec = {0, 0, -1, +1};  // 進む方向の簡便化，テクニックとして覚えておく．
vector<ll> x_vec = {-1, +1, 0, 0};  // ループで回すことで上下左右に移動できるようになっている

void bfs(vector<vector<ll>> &path) {
    while (!q.empty()) {
        Corr now = q.front(); q.pop();  // キューの先頭を取り出す
        y = now.y, x = now.x, depth = now.depth;  // 行，列が y, x になっているので注意

        // path[now.y][now.x] = now.depth;

        // // 停止条件：現在の座標がゴールなら深さを返す
        // if (y == g_y && x == g_x) return;



        // 上下左右 4 方向を探索．進むことができればキューに格納する．
        for (ll i = 0; i < 4; ++i) {
            Corr next = {y + y_vec[i], x + x_vec[i], depth + 1};
            if (0 <= next.y && next.y < H && 0 <= next.x && next.x < W && maze[next.y][next.x] == '.') {
                // maze[next.y][next.x] = 'x';  // 同じ場所を探索しないようにする
                if(path[next.y][next.x]!=-1){
                    continue;
                }else{
                    path[next.y][next.x] = next.depth;
                    q.push(next);
                }
            }
        }
    }
}

int main() {
    // input
    cin >> H >> W;
    s_y = 0; s_x = 0;
    g_y = H-1; g_x = W-1;
    rep(i,H){
        string s;
        cin >> s;
        maze.push_back(s);
    }

    vector<vector<ll>> path(H, vector<ll> (W, -1));
    path[0][0] = 0;

    // calculation
    Corr start = {s_y, s_x, 0};  // 初期位置の深さ(depth)は 0
    q.emplace(start);  // 初期位置をキューに格納

    bfs(path);
    ll cost = path[H-1][W-1];
    ll ans = 0;
    rep(i,H){
        rep(j,W){
            if(maze[i][j]=='.'){
                ans ++;
            }
        }
    }

    if(cost == -1){
        cout << -1 << endl;
    }else{
        cout << ans - cost - 1 << endl;
    }

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
#define EPS (1e-10)
#define equals(a, b) (fabs(a - b) < EPS)

class Point{
public:
  double x, y;

  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator + (Point p){return Point(x + p.x, y + p.y);}
  Point operator - (Point p){return Point(x - p.x, y - p.y);}
  Point operator * (double a){return Point(x * a, y * a);}
  Point operator / (double a){return Point(x / a, y / a);}

  double abs(){return sqrt(norm());}
  double norm(){return x * x + y * y;}

  bool operator < (const Point &p) const{
    return x != p.x ? x < p.x : y < p.y;
  }
  bool operator == (const Point &p) const{
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }

};

typedef Point Vector;

class Segment{
public:
  Point p1, p2;
  Segment(Point p1, Point p2): p1(p1), p2(p2){}
};

typedef Segment Line;

class Circle{
public:
  Point c;
  double r;
  Circle(Point c = Point(), double r = 0): c(c), r(r){}
};

typedef vector<Point> Polygon;

double dot(Vector a, Vector b){
  return a.x * b.x + a.y * b.y;
}
double cross(Vector a, Vector b){
  return a.x * b.y - a.y * b.x;
}

Point reflect(Point p0, Point p1, Point p2){
  Vector v01 = Vector(p0.x - p1.x, p0.y - p1.y);
  Vector v21 = Vector(p2.x - p1.x, p2.y - p1.y);
  double t = -1 * ((p2.x - p1.x) * (p1.x - p0.x) + (p2.y - p1.y) * (p1.y - p0.y)) / (pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
  return (v21 * t - v01) * 2 + p0;
}

Point project(Point p0, Point p1, Point p2){
  Vector v01 = Vector(p0.x - p1.x, p0.y - p1.y);
  Vector v21 = Vector(p2.x - p1.x, p2.y - p1.y);
  double t = -1 * ((p2.x - p1.x) * (p1.x - p0.x) + (p2.y - p1.y) * (p1.y - p0.y)) / (pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
  return v21 * t + p1;
}

void solve(){
  double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
  Point p1 = Point(x1, y1), p2 = Point(x2, y2);
  int q; cin >> q;
  vector<Point> ans(q);
  for(int i = 0; i < q; i++){
    double x0, y0; cin >> x0 >> y0;
    Point p0 = Point(x0, y0);
    ans[i] = project(p0, p1, p2);
  }
  for(int i = 0; i < q; i++){
    cout << fixed << setprecision(10) << ans[i].x << " " << ans[i].y << endl;
  }
  return;
}

int main(){
  solve();
  return 0;
}



Measured memory_bytes: 3296
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww
{
    cww()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts)
{
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v)
{
    for (auto &e : t)
        fill_v(e, v);
}

using P = pair<int, int>;
int main()
{
    int N, M, L;
    cin >> N >> M >> L;
    auto d = make_v<int>(N, N);
    fill_v(d, INT_MAX/2);
    int A, B, C;
    for (size_t i = 0; i < M; i++)
    {
        cin >> A >> B >> C;
        A--;
        B--;
        d[A][B] = C;
        d[B][A] = C;
    }
    for (size_t k = 0; k < N; k++)
    {
        for (size_t i = 0; i < N; i++)
        {
            for (size_t j = 0; j < N; j++)
            {
                if (d[i][j] > d[i][k] + d[k][j])
                    d[i][j] = d[i][k] + d[k][j];
            }
        }
    }
    auto a = make_v<int>(N,N);
    fill_v(a,INT_MAX/2);
    for (size_t i = 0; i < N; i++)
    {
        for (size_t j = i+1; j < N; j++)
        {
            if(d[i][j]<=L)
            {
                a[i][j]=1;
                a[j][i]=1;
            }
        }
    }
    for (size_t k = 0; k < N; k++)
    {
        for (size_t i = 0; i < N; i++)
        {
            for (size_t j = 0; j < N; j++)
            {
                if (a[i][j] > a[i][k] + a[k][j])
                    a[i][j] = a[i][k] + a[k][j];
            }
        }
    }
    int Q,s,t;
    cin>>Q;
    for (size_t i = 0; i < Q; i++)
    {
        cin>>s>>t;
        s--;t--;
        if(a[s][t]>=INT_MAX/2)
        cout<<-1<<endl;
        else
        cout<<a[s][t]-1<<endl;
    }
    
    return 0;
}

Measured memory_bytes: 1280
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define endl "\n"

int main(){
	//
    //
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;
    int m;
    cin>>m;
    int total=0;
    for (int i=0; i<m; ++i){
    	int x;
    	cin>>x;
    	total+=x;
    }
    if(n-total<0){
    	cout<<-1;
    }
    else{
    	cout<<n-total;
    }
    return 0;
}

Measured memory_bytes: 3644
//
// Created by moono on 2018/10/27.
//

#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;



int main() {
    string A, B, C;
    cin >> A;
    cin >> B;
    cin >> C;

    //
    int a=0, b=0, c=0;
    char turn = 'a';
    while(a <= A.size() && b <= B.size() && c <= C.size()){
        if(turn == 'a'){
            if(a == A.size()){
                cout << "A" << endl;
                break;
            }
            turn = A[a++];

        }else if(turn == 'b'){
            if(b == B.size()){
                cout << "B" << endl;
                break;
            }
            turn = B[b++];

        }else if(turn == 'c'){
            if(c == C.size()){
                cout << "C" << endl;
                break;
            }
            turn = C[c++];
        }

//        cout << turn << endl;
    }




    return 0;
}

Measured memory_bytes: 256
from itertools import product
a,b,c,d=map(int,input())
for op1,op2,op3 in product((1,-1),repeat=3):
    if a+op1*b+op2*c+op3*d==7:
        x='+'if op1==1else'-'
        y='+'if op2==1else'-'
        z='+'if op3==1else'-'
        print('{}{}{}{}{}{}{}=7'.format(a,x,b,y,c,z,d))
        exit()


Measured memory_bytes: 9152
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <numeric>
#define range(i, r) for(int i=0;i<r;i++)
#define ranges(i, l, r) for(int i=l;i<r;i++)
#define INF 1e9
typedef long long int ll;
using namespace std;

int main(int argc, char const *argv[]) {
  int N, W, ans=0;
  std::cin >> N >> W;
  std::vector<int> w(N+1), v(N+1);
  w[0] = v[0] = 0;
  range(i, N) std::cin >> w[i+1] >> v[i+1];
  int mV = accumulate(v.begin(), v.end(), 0);
  std::vector<std::vector<ll> > dp(N + 1, std::vector<ll> (mV + 1, INF));
  dp[0][0] = 0;
  for(int i=1;i<N+1;i++){
    for(int j=0;j<mV+1;j++){
      if(v[i]>j){
        dp[i][j] = dp[i-1][j];
      }else{
        dp[i][j] = min(dp[i-1][j], dp[i-1][j-v[i]] + w[i]);
        if(dp[i][j]<=W){
          ans =  max(ans, j);
        }
      }
    }
  }
  std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 80256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long int ll;

const int INF = 1e9;

int main()
{
  int n, m;
  cin >> n >> m;
  
  vector<vector<int>> e(n, vector<int>(0));
  rep(i, m)
  {
    int a, b;
    cin >> a >> b;
    a--; b--;
    e[a].emplace_back(b);
    e[b].emplace_back(a);
  }
  
  //rep(i,n){ for(auto x : e[i]) cout<<x<<" "; cout<<endl;}
  
  vector<int> d(n, INF);
  queue<int> q;
  q.push(0);
  d[0] = 0;
  
  while(q.size())
  {
    int a = q.front();
    q.pop();
    
    for(auto x : e[a])
    {
      if(d[x] == INF)
      {
        d[x] = d[a] + 1;
        q.push(x);
      }
    }
    //rep(i, n) cout << d[i] << " "; cout << endl;
  }
  //rep(i, n) cout << d[i] << endl;
  
  if(d[n-1] == 2) cout << "POSSIBLE" << endl;
  else cout << "IMPOSSIBLE" << endl;
  
  
  
  return 0;
}

Measured memory_bytes: 13168
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repo(i,N) for(int i = 1; i < (N); i++)
//#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
//#define num 1000000007
#define pi acos(-1.0)
//cout << fixed << setprecision (10);   小数点以下１０桁まで
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//string s = to_string(int数);
//int n = stoi(string文字列);

int main() {
  int n;
  cin >>n;
  
  vector<int> omo(n);
  
  int zen = 0;
  rep(i,n) {
  cin >> omo.at(i);
  zen += omo.at(i);
  }
  
  int gou = 0;
  int ans = 10000;
  rep(i,n) {
  gou += omo.at(i);
  ans=min(ans,abs(zen-2*gou));
  }
  
  cout << ans << endl;
  
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
	
int w,a,b;
cin>>w>>a>>b;
if(a<b){
	
	if(a+w<b)
	cout<<b-(a+w);
	else
	cout<<"0";	
}
	else 
	{
		
	if(b+w<a)
	cout<<a-(b+w);
	else
	cout<<"0";	
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

Measured memory_bytes: 256
#include <iostream>
#include <string>

using namespace std;

int main() {
    string S;
    cin >> S;
    int count = 0;
    int i = 0;
    while(i < S.size() - 1) {
        if(S[i] != S[i + 1]) {
            if(S.size() == 2) {
                count += 2;
                break;
            }
            S.erase(i, 2);
            count += 2;
            i = 0;
        }
        else i++;
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 512
#include <vector>
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#include<stdio.h>
#include<list>
#include<iterator>
using namespace std;

int main() {

    map<string, int>m;
    int n,q,x;
    string k,s;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>q>>k;
        if(q==0){
            cin>>x;
            m[k]=x;
        }
        else if (q==1){
            cout<<m[k]<<endl;
        }
        else if(q==2){
            m[k]=0;
        }
        else{
            cin>>s;
            map<string,int>::iterator l=m.lower_bound(k),r=m.upper_bound(s);
            for(;l!=r;l++){
                if(l-> second!=0){
                    cout<<l->first<<" " <<l->second<<endl;
                }
            }
        }
    }

    return 0;
}



Measured memory_bytes: 27860
import java.util.*

fun main(args: Array<String>) {
    val (N, K) = readLine()!!.split(" ").map{ it.toInt()}
    val ps = Array(N) { readLine()!!.split(" ").map { it.toLong() } }

    val xs = TreeSet(ps.map { it[0] }).toTypedArray()
    val ys = TreeSet(ps.map { it[1] }).toTypedArray()

    val f = Array(xs.size + 2) { Array(ys.size + 2) {0} }

    for (p in ps) {
        val xi = xs.binarySearch(p[0]) + 1
        val yi = ys.binarySearch(p[1]) + 1

        f[xi][yi]++
    }

    for (i in 1 until xs.size + 2) for (j in 1 until ys.size + 2) {
        f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1]
    }

    var res = (1e19).toLong()

    for (i in 1..xs.size) for (j in i .. xs.size) for(k in 1 .. ys.size) for(l in k .. ys.size) {
        val c = f[j][l] - f[j][k - 1] - f[i - 1][l] + f[i - 1][k - 1]
        if (c < K) continue

        res = Math.min(res, (xs[j - 1] - xs[i - 1]) * (ys[l - 1] - ys[k - 1]))
    }

    println(res)
}


Measured memory_bytes: 38560
n=(ns=gets.chomp).to_i
nl=ns.size
sum=0
3.upto(nl) do |i|
  [3,5,7].repeated_permutation(i){|num|
    if num.uniq.size==3
      if num.join("").to_i<=n then sum+=1 end
    end
  }
end
puts sum


Measured memory_bytes: 1916
N,M = map(int,input().split())

c_l = [0 for i in range(N)]
a = 0
w = 0

for i in range(M):
  p,s = input().split()
  p = int(p)
  if c_l[p-1] != -1:
    if s == "AC":
      a += 1
      w += c_l[p-1]
      c_l[p-1] = -1
    else:
      c_l[p-1] += 1
print("{} {}".format(a,w))



Measured memory_bytes: 3928
//#include "stdafx.h"
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <functional>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <tuple>
#include <numeric>
#include <assert.h>
#include <functional>
#include <unordered_map>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n)				for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n)			for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define INF						2e9
#define MOD						(1000 * 1000 * 1000 + 7)
#define Ceil(x, n)				(((((x))+((n)-1))/n))		/* Nの倍数に切り上げ割り算 */
#define CeilN(x, n)				(((((x))+((n)-1))/n)*n)		/* Nの倍数に切り上げ */
#define FloorN(x, n)			((x)-(x)%(n))				/* Nの倍数に切り下げ */
#define IsOdd(x)				(((x)&0x01UL) == 0x01UL)			
#define IsEven(x)				(!IsOdd((x)))						
#define	BitSetV(Val,Bit)		((Val) |= (Bit))			
#define	BitTstV(Val,Bit)		((Val) & (Bit))				
#define ArrayLength(x)			(sizeof( x ) / sizeof( x[ 0 ]))
#define	MAX_QWORD				((QWORD)0xFFFFFFFFFFFFFFFF)
#define	MIN_QWORD				((QWORD)0x0000000000000000)
#define	MAX_SQWORD				((SQWORD)0x7FFFFFFFFFFFFFFF)
#define	MIN_SQWORD				((SQWORD)0x8000000000000000)
#define M_PI					3.14159265358979323846
typedef long long				ll;
typedef unsigned long			DWORD;
typedef unsigned long long int	QWORD;
typedef pair<ll, ll>			P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
// メイン
int main()
{
	// 入力
	int N;
	map<string, int> S;
	cin >> N;
	REP(i, N) {
		string tmp;
		cin >> tmp;
		S[tmp]++;
	}
	cin >> N;
	REP(i, N) {
		string tmp;
		cin >> tmp;
		S[tmp]--;
	}

	int ans = 0;
	for(auto itr = S.begin(); itr != S.end(); ++itr) {
		ans = max(ans, itr->second);
	}
	cout << ans << endl;
	return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

int main(void){
    int n, m; cin >> n >> m;
    ll dp[100010];
    bool ana[100010];
    REP(i, 100010) dp[i] = 0;
    REP(i, 100010) ana[i] = true;

    REP(i, m){
        int num; cin >> num;
        ana[num] = false;
    }
    
    dp[0] = 1;
    FOR(i, 1, n + 1){
        if(ana[i - 1]) dp[i] += dp[i - 1];
        if(i > 1 and ana[i - 2]) dp[i] += dp[i - 2];
        dp[i] %= 1000000007;
    }

    cout << dp[n] << endl;

}

Measured memory_bytes: 1152
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for(ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main(){
  ll s, n, m;
  bool t;
  in2(n, m);
  vector<vector<char>> a(n, vector<char> (n));
  vector<vector<char>> b(m, vector<char> (m));
  rep(i, n){
    rep(j, n)
      in(a[i][j]);
  }
  rep(i, m){
    rep(j, m)
      in(b[i][j]);
  }
  rep(i, n - m + 1){
    rep(j, n - m + 1){
      t = true;
      rep(k, m){
        rep(l, m){
          if(a[i + k][j + l] != b[k][l])
            t = false;
        }
      }
      if(t){
        out("Yes");
        memi;
        return 0;
      }
    }
  }
  out("No");
  memi;
}

Measured memory_bytes: 256
//
// Created by Harshit on 20-08-2020.
//

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,W;
    cin>>n>>W;
    vector <int> weight(n);
    vector <int> value(n);
    for(int i=0;i<n;i++){
        cin>>weight[i]>>value[i];
    }
    vector <vector <long long> > dp(n+1,vector <long long > (W+1,0));
   /* for(int i=1;i<n+1;i++){
        for(int j=0;j<=W;j++){
            if(j>=weight[i-1])
                dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i-1]]+value[i-1]);
            else
                dp[i][j]=dp[i-1][j];
        }
    }*/
    for(int i=0;i<n;i++){
        for(int j=0;j<=W;j++){
            dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
            if(j+weight[i]<=W)
                dp[i+1][j+weight[i]]=max(dp[i+1][j+weight[i]],dp[i][j]+value[i]);
        }
    }
   cout<<dp[n][W]<<endl;
}

Measured memory_bytes: 83168
#include <bits/stdc++.h>
using namespace std;
int main() {
	int n,i,j,k;
	cin>>n>>k;
	int a[n],b[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	b[0]=0;
	for(i=1;i<n;i++)
	{
	    b[i]=INT_MAX;
	    for(j=1;j<=k;j++)
	    {
	        if(i-j>=0)
	        {
	            b[i]=min(b[i],b[i-j]+abs(a[i]-a[i-j]));
	        }
	    }
	}
	cout<<b[n-1]<<endl;
	return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
  int n; cin >> n;

  vector<long long> a(n), b(n), c(n);
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < n; j++) {
      long long temp; cin >> temp;
      if(i == 0) a[j] = temp;
      if(i == 1) b[j] = temp;
      if(i == 2) c[j] = temp;
    }
  }

  sort(a.begin(), a.end());
  sort(c.begin(), c.end());

  long long answer = 0;
  vector<long long>::iterator low, up;
  for(int i = 0; i < n; i++) {
    low = lower_bound(a.begin(), a.end(), b[i]);
    up = upper_bound(c.begin(), c.end(), b[i]);
    answer += (low-a.begin()) * (c.end()-up);
  }

  cout << answer << endl;

  return 0;
}

Measured memory_bytes: 2688
N,M=input().split()
N=int(N)
M=int(M)

#配列の定義
arr = [[0 for c in range(N)] for r in range(N)]

#配列へ入力
for i in range(M):
    a,b=input().split()
    a=int(a)
    b=int(b)
    #表の、aの列のbの行に1を追加
    arr[a-1][b-1]+=1
    #表の、bの列のaの行に1を追加
    arr[b-1][a-1]+=1

#取り出し
for i in range(N):
    print(sum(arr[i]))

Measured memory_bytes: 3064
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;

void main() {
    while (true) {
        int N = readln.chomp.to!int;
        if (N == 0) break;
        long[] k = readln.split.to!(long[]);

        if (k.filter!(a=>a>1).empty) {
            writeln("NA");
        } else {
            writeln(k.filter!(a=>a>0).array.length+1);
        }
    }
}

Measured memory_bytes: 6776
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <atcoder/all>
using namespace atcoder;
#pragma warning (disable: 4996)
using namespace std;

using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

struct UnionFind {
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

long long modpow(long long a, long long n, long long mod) {
    if (n < 0)return 0;
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

ll merge_cnt(vector<ll>& a) {
    int n = a.size();
    if (n <= 1) { return 0; }

    ll cnt = 0;
    vector<ll> b(a.begin(), a.begin() + n / 2);
    vector<ll> c(a.begin() + n / 2, a.end());

    cnt += merge_cnt(b);
    cnt += merge_cnt(c);

    int ai = 0, bi = 0, ci = 0;
    while (ai < n) {
        if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
            a[ai++] = b[bi++];
        }
        else {
            cnt += n / 2 - bi;
            a[ai++] = c[ci++];
        }
    }
    return cnt;
}

int main() {
    ll n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        ll a, b, c, d;
        cin >> a >> b >> c >> d;
        cout << floor_sum(a, b, c, d) << endl;
    }
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main()
{
	cin.sync_with_stdio(false);
	int n;
	while (cin >> n, n)
	{
		vector<set<int>> st(n);
		REP(i, n)
		{
			int m; cin >> m;
			REP(j, m)
			{
				int a; cin >> a;
				st[i].insert(a);
			}
		}
		int m; cin >> m;
		set<int> w;
		REP(i, m)
		{
			int a; cin >> a;
			w.insert(a);
		}
		vi ans;
		REP(i, n)
		{
			bool f = true;
			for (auto j : w)
			{
				if (!st[i].count(j)) f = false;
			}
			if (f) ans.push_back(i);
		}
		if (ans.size() != 1) cout << -1 << endl;
		else cout << ans[0] + 1 << endl;
	}
	return 0;
}

Measured memory_bytes: 3512
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
 
int main() {
  long n,m,a,b,ans;
  cin >> n >> m;
  ans=n-1;
  dsu d(n);
  for(long i=0;i<m;i++) {
    cin >> a >> b;
    if(!(d.same(a-1,b-1))) {
      ans--;
      d.merge(a-1,b-1);
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 3780
#include <bits/stdc++.h>
    using namespace std;
    #define rep(i,n) for(int i=0; i<(n); i++)
    #define MAX_SIZE 1000005

    int main(){
    	int N;
    	cin >> N;
    	vector<int> A(N);
    	rep(i,N) cin >> A[i];
    	sort(A.begin(),A.end());
    	vector<bool> check(MAX_SIZE,true);
     
    	for(int i=0; i<N; i++){
    		if(!check[A[i]]) continue;
    		if(i!=N-1 && A[i]==A[i+1]){ check[A[i]]=false; }

    		for(int j=2; j*A[i]<=MAX_SIZE; j++){
    			check[j*A[i]] = false;
    		}
    	}

    	int ans=0;
    	for(int u : A){
    		if(check[u]){ ans++; }
    	}

    	cout << ans << endl;
    	return 0;
     
    }

Measured memory_bytes: 4284
#include<bits/stdc++.h>
using namespace std;
     
#define ll long long
#define loop(i,a,n) for(ll i=a;i<n;++i)
#define revloop(i,a,b) for(ll i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define INF 9999999999999999
#define MOD 1000000007

void func()
{
    ll x,k,d;
    cin>>x>>k>>d;
    x=abs(x);
    ll ans;
    ll y=x % d;
    ll mov=x / d;
    ans=y;
    if(abs(y-d)<y){
        ans=abs(y-d);
        y=y-d;
        mov++;
    }
    if(k>=mov){
        k=k-mov;
        if(k%2==0){
            cout<<ans<<"\n";
        }
        else{
            if(y<=0){
                ans=abs(y+d);
                cout<<ans<<"\n";
            }
            else{
                ans=abs(y-d);
                cout<<ans<<"\n";
            }
        }
    }
    else{
        ll ans=abs(x-(k*d));
        cout<<ans<<"\n";
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
 
    ll t=1;
    //cin>>t;
    while(t--)
    {
        func();
    }

}


Measured memory_bytes: 3584
#pragma GCC optimize("Ofast")
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <limits>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
#include <iomanip>
#include <functional>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

template<class T,class U>constexpr bool chmin(T&a,const U b){if(a<=b)return false;a=b;return true;}
template<class T,class U>constexpr bool chmax(T&a,const U b){if(a>=b)return false;a=b;return true;}
#define bit(n,k) ( (n>>k)&1 )

//デバッグ
template<class T>
void Vout(vector<T> &V){
    cout<<"\nstart\n";
    const int sz=V.size();
    for(int i=0;i<sz;i++){
        cout<<i<<" "<<V[i]<<'\n';
    }
    cout<<"end\n"<<endl;
}

template<class T>
void VPout(vector<T> &V){
    cout<<"\nstart\n";
    const int sz=V.size();
    for(int i=0;i<sz;i++){
        cout<<i<<" "<<V[i].first<<" "<<V[i].second<<'\n';
    }
    cout<<"end\n"<<endl;
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


constexpr int MAX=1<<30;
constexpr ll INF=1LL<<62;
constexpr int MOD=1e9+7;
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

//__builtin_popcount(S);
//#define int ll
//vector<vector<int>> data(3, vector<int>(4));
//vector.resize(a,vector<int>(b,-1));
//vector<vector<vector<要素の型>>> 変数名(要素数1, vector<vector<要素の型>>(要素数2, vector<要素の型>(要素数3, 初期値)));


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr); 
    cout<<fixed<<setprecision(10);

    int n,k;
    cin>>n>>k;
    vector<ll> aa(n);
    for(int i=0;i<n;i++) cin>>aa[i];
    ll ans=INF;
    for(int S=0;S<(1<<n);S++){
        if(__builtin_popcount(S)<k) continue;
        ll pre=0,cnt=0;
        vector<ll> a=aa;
        for(int i=0;i<n;i++){
            if(bit(S,i)){
                if(pre>=a[i]){
                    cnt+=(pre+1)-a[i];
                    a[i]=pre+1;
                }
            }
            chmax(pre,a[i]);
        }
        chmin(ans,cnt);
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define digit(x) floor(log10(x))+1
#define mod 1000000007
#define L_MAX 1e18
using ll = long long;
using ld = long double;
using ii = pair<ll,ll>;
using iii = tuple<ll,ll,ll>;
using matrix = vector<vector<ll>>;
using arr = vector<ll>;
using vs = vector<string>;
using pv = vector<ii>;
using umap = unordered_map<ll,ll>;
#define in1(x) scanf("%lld",&x)
#define in2(x,y) scanf("%lld %lld",&x,&y)
#define in3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define all(x) x.begin(),x.end()
#define debug(x) cerr << #x << " is " << x << endl;
void showArr(arr a){
    for(auto i : a) cout<<i<<" ";
    cout<<endl;
}
ll dx[]={-1,1,-1,1,0,0,-1,1};
ll dy[]={-1,1,0,0,-1,1,1,-1};
int main(){
    fast;
    ld r;
    cin>>r;
    cout<<2*3.14*r<<endl;
    
    
    return 0;
}

Measured memory_bytes: 3736
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using P = pair<ll,ll>;
#define fl cout<<flush;
#define endl '\n'
template <typename T> inline void prt(T v){cout<<v<<'\n';}
template <typename T> inline bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> inline bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ld pi=3.14159265358979323846;
const ld eps=1e-10;

#define debug(v) cout<<#v<<": ";prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T> 
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}

inline ll F(ll from, ll to){
    ll cost;
    if(to==1){
        if(from%2)cost=0;
        else cost=1;
    }
    else{
        if(from==0)cost=2;
        else if(from%2==0)cost=0;
        else cost=1;
    }
    return from-cost;
}

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll L;
    cin >> L;
    vector<ll> A(L,0);
    for(ll i=0;i<L;++i)cin>>A[i];
    ll sum=0;
    for(ll i=0;i<L;++i)sum+=A[i];
    ll ans=sum;
    ll dp0=0,dp2=0,dp12=0,dp21=0,dp212=0,dp1=0;
    for(ll i=0;i<L;++i){
        ll ep2=max(dp2+F(A[i],2),dp0+F(A[i],2));
        ll ep12=max(dp12+F(A[i],2),dp1+F(A[i],2));
        ll ep21=max(dp2+F(A[i],1),dp21+F(A[i],1));
        ll ep212=max(dp21+F(A[i],2),dp212+F(A[i],2));
        ll ep1=max(dp1+F(A[i],1),dp0+F(A[i],1));
        dp2=ep2;dp12=ep12;dp21=ep21;dp212=ep212;dp1=ep1;
        chmin(ans,sum-max({dp2,dp12,dp21,dp212,dp1}));
        //debug(dp2)debug(dp12)debug(dp21)debug(dp212)debug(dp1)
    }
    prt(ans);
    return 0;
}



Measured memory_bytes: 4740
def ABC068C():
    N, M = map(int, input().split())
    flag = [[False, False] for _ in range(N)]

    for _ in range(M):
        a, b = map(int, input().split())
        if a == 1:
            flag[b][0] = True
        elif b == N:
            flag[a][1] = True

    for i in range(N):
        if flag[i][0] and flag[i][1]:
            print('POSSIBLE')
            return

    print('IMPOSSIBLE')
    return

ABC068C()


Measured memory_bytes: 23412