akhauriyash/Code-Regression huggingface dataset:
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();

    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let n: i32 = read();
    let r: i32 = read();

    if n >= 10 {
        println!("{}", r);
    } else {
        println!("{}", r + (100 * (10 - n)));
    }
}


Measured memory_bytes: 4352
//Code by 27.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<fstream>
#include<stdlib.h>
#include<set>
#include<climits>
#include<cmath>  
#include<memory.h> 
#include<sstream>
#include<time.h>
#include<iomanip>
using namespace std;
const unsigned long long BIGEST=1000000000000000000+1000000000000000000;
const long long BIGER=1000000000000000000;
const int BIG=1000000000;
const int MOD=1000000007;
long long a,b,c;
int main()
{
	cin>>a>>b>>c;
	cout<<min(c,a+b+1)+b;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main()
{
    int a, b, c;
    char read;
    a = b = c = 0;
    for(int i = 0;i < 3;i++){
      cin >> read;
      if(read == 'a'){
        a++;
      }else if(read == 'b'){
        b++;
      }else{
        c++;
      }
    }
    if(a == 1 && b == 1 && c == 1){
      cout << "Yes";
    }else{
      cout << "No";
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n,k;
  cin >> n >> k;
  vector <long long> h(n);
  for(int i=0;i<n;++i) cin >> h[i];
  sort(h.begin(),h.end());
  long long ans=100000000000;
  for(int i=0;i<n-k+1;++i) {
    ans=min(ans,h[i+k-1]-h[i]);
  }
  cout << ans << endl;
}

Measured memory_bytes: 1024
use std::collections::HashSet;
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}
fn main(){
    let n: i64 = read();
    let mut v = Vec::new();
    let mut set = HashSet::new();
    for _ in 0..n{
        v.push(read::<String>());
    }
    for x in &v{
        set.insert(x);
    }
    let len = set.len() as i64;
    if (n != len){
        println!("No");
        return;
    }
    let sub = v.clone();
    let cs :Vec<Vec<char>>= sub.into_iter().map(|s| s.chars().collect()).collect();
    for j in 0..n-1{
        if cs[j as usize].iter().last().unwrap() != &cs[(j + 1) as usize][0]{
            println!("No");
            return;
        }
    }
    println!("Yes");
}

Measured memory_bytes: 4352
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.rmi.server.UnicastRemoteObject;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main{
    static long mod=1000000007;
//    static int dx[]={-1,0,1,0};
//    static int dy[]={0,-1,0,1};

    public static void main(String[] args)   throws Exception, IOException{        
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);
        
//        int n=sc.nextInt();
        char c[]=sc.nextString().toCharArray();
        int a[]=new int[c.length];

        int st=-1;
        for (int i = 0; i < c.length; i++) {
			if(c[i]=='L') {
				if(c[i-1]=='L')continue;
//				db(st);
				a[i-1]=(i-st)/2;
				a[i]=(i-st)/2;
				if((i-st)%2==1)a[i-1]++;
				st=-1;
			} else {
				if(st<0)st=i;
			}
		}

//        db(a);
        st=-1;
        for (int i = c.length-1; -1 < i; i--) {
			if(c[i]=='R') {
				if(c[i+1]=='R')continue;
//				db(st,i);
				a[i+1]+=(st-i)/2;
				a[i]+=(st-i)/2;
				if((st-i)%2==1)a[i+1]++;
				st=-1;
			} else {
				if(st<0)st=i;
			}
		}

//        db(a);
        for (int i = 0; i < a.length; i++) {
            out.print(a[i]+" ");			
		}
        out.println();
        out.flush();
    }



    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
}

class P {
	int x, y;
	P(int  x, int y) {
	  this.x=x;
	  this.y=y;
	}
}


//class P {
//    int id, d;
//    P(int  id, int d) {
//        this.id=id;
//        this.d=d;
//    }
//}

//class P implements Comparable<P>{
//    int a,b;
//    P(int a, int b) {
//    	this.b=b;
//    	this.a=a;
//    }
//
//    public int compareTo(P p){
//        return b-p.b;
//    }
//}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public int[] nextIntArray(int size) throws IOException{
        int r[] = new int[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextInt(); 
        }
        return r;
    }
    public long[] nextLongArray(int size) throws IOException{
        long r[] = new long[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextLong(); 
        }
        return r;
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}


Measured memory_bytes: 37860
a, b, k = map(int, input().strip().split())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

c = gcd(a, b)
cnt = 1
if k == 1:
    print(c)
    exit()
for i in range(2, c):
    if c % i == 0:
        cnt += 1
    if cnt == k:
        print(c // i)
        exit()
print(1)


Measured memory_bytes: 3060
x = int(input())

def is_prime(n):
    for i in range(2, n + 1):
        if i * i > n:
            break
        if n % i == 0:
            return False
    return n != 1


sosu = False

while sosu==False:
    sosu = is_prime(x)
    x += 1

print(x-1)

Measured memory_bytes: 2940
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstring>
#include <math.h>
#include <bitset>
#include <queue>
#include <set>
#include <iomanip>
#include <math.h>
#include <assert.h>
// #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr long long int INFLL = 1001001001001001LL;
constexpr long long int infll = 1001001001001001LL;
constexpr int INF = 1000000007;
constexpr int inf = 1000000007;
const int mod = 1000000007;

template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

ll gcd(ll a, ll b)
{
    if (a % b == 0)
    {
        return (b);
    }
    else
    {
        return (gcd(b, a % b));
    }
}

ll lcm(ll a, ll b)
{
    return a / gcd(a, b) * b;
}

inline double nCr(const int n, int r)
{
    if (n == 0)
    {
        return 0;
    }
    if (r == 0)
    {
        return 1;
    }
    if (r == 1)
    {
        return n;
    }
    if (n == r)
    {
        return 1;
    }

    if (r > n / 2)
    {
        r = n / 2;
    }

    double result = 1;
    for (double i = 1; i <= r; i++)
    {
        result *= (n - i + 1) / i;
    }

    return (result);
}

template <typename T>
T seinomi(T a)
{
    if (a > 0)
    {
        return a;
    }
    else
    {
        return 0;
    }
}

template <typename T>
map<T, T> soinsuubunkai(T n) //連想配列[素因数f.first][個数f.second]
{
    map<T, T> ret;
    for (T i = 2; i * i <= n; i++)
    {
        while (n % i == 0)
        {
            ret[i]++;
            n /= i;
        }
    }
    if (n != 1)
        ret[n] = 1;
    return ret;
}

template <typename T>
T ketasuu(T num)
{
    return std::to_string(num).length();
}

int kaizyou(int k)
{
    int sum = 1;
    for (int i = 1; i <= k; ++i)
    {
        sum *= i;
    }
    return sum;
}

template <class ForwardIt, class T>
void iota(ForwardIt first, ForwardIt last, T value)
{
    while (first != last)
    {
        *first++ = value;
        ++value;
    }
}

//nの階乗を計算
int factorial(int n)
{
    int answer = 1;
    while (n > 1)
    {
        answer *= n;
        n--;
    }
    return answer;
}
int comb(int n, int r)
{
    return factorial(n) / (factorial(r) * factorial(n - r));
}

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


template <typename T>
T amarinasi(T a,T b){
    if(a%b==0)
{
    return a/b;
}
else if(a%b>0)
{
    return a/b+1;
}
else
{
    return a/b-1;
}
}

//小数点以下10桁テンプレート（main関数内の最初に貼付け）
//std::cout << std::fixed << std::setprecision(10);

//----------------------------------------------------------------

int main(){
string s;
cin>>s;
ll cnt1=0,cnt2=0;
for(ll i=0;i<s.size();i++){
    if(s[i]=='0'){
        cnt1++;
    }else{
        cnt2++;
    }
}
cout<<min(cnt1,cnt2)*2<<endl;
}










Measured memory_bytes: 512
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(int i=0;i<N;++i)
typedef long long int LL;

LL N, M, ans;

std::vector<LL>divisor(LL num)
{
	std::vector<LL>res;
	for (LL i = 1; i*i <= num; ++i)
	{
		if (num % i == 0)
		{
			res.push_back(i);
			if ((num / i) != i)
			{
				res.push_back(num / i);
			}
		}
	}
	return res;
}

int main()
{
	in >> N >> M;
	auto res = divisor(M);
	for (auto i : res)
	{
		if (N*i <= M) ans = std::max(ans, i);
	}
	out << ans << std::endl;
	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#define ll long long
#define zahyoutype double

typedef struct Point{zahyoutype x,y;}P;
typedef struct line{P p1,p2;}L;
int sgn(zahyoutype x){return x<0?-1:x>0;}

int iscross(L s,L t){
	//点(p,q)が直線f(x,y)=ax+by+c=0のどっち側にあるかはf(p,q)を見ればよい
	//点(x1,y1),(x2,y2)を結ぶ直線の式は(y2-y1)*(x-x1)+(x2-x1)*(y1-y)=0
	zahyoutype t1=(s.p2.y-s.p1.y)*(t.p1.x-s.p1.x)+(s.p2.x-s.p1.x)*(s.p1.y-t.p1.y);
	zahyoutype t2=(s.p2.y-s.p1.y)*(t.p2.x-s.p1.x)+(s.p2.x-s.p1.x)*(s.p1.y-t.p2.y);
	zahyoutype t3=(t.p2.y-t.p1.y)*(s.p1.x-t.p1.x)+(t.p2.x-t.p1.x)*(t.p1.y-s.p1.y);
	zahyoutype t4=(t.p2.y-t.p1.y)*(s.p2.x-t.p1.x)+(t.p2.x-t.p1.x)*(t.p1.y-s.p2.y);
	//同一直線上にない
	if(t1 || t2)return sgn(t1)!=sgn(t2) && sgn(t3)!=sgn(t4);
	//同一直線上にある
	zahyoutype c1=(s.p2.x-s.p1.x)*(s.p2.x-s.p1.x)+(s.p2.y-s.p1.y)*(s.p2.y-s.p1.y);
	zahyoutype c2=(t.p1.x-s.p1.x)*(s.p2.x-s.p1.x)+(t.p1.y-s.p1.y)*(s.p2.y-s.p1.y);
	zahyoutype c3=(t.p2.x-s.p1.x)*(s.p2.x-s.p1.x)+(t.p2.y-s.p1.y)*(s.p2.y-s.p1.y);
	if(0<=c2&&c2<=c1 || 0<=c3&&c3<=c1 || c2<=0&&c1<=c3 || c3<=0&&c1<=c2)return 1;
	return 0;
}

P crosspoint(L s,L t){
	double deno=(s.p1.x-s.p2.x)*(t.p1.y-t.p2.y)-(t.p1.x-t.p2.x)*(s.p1.y-s.p2.y);
	zahyoutype xnume=  s.p1.x*t.p1.x*(s.p2.y-t.p2.y)-s.p1.x*t.p2.x*(s.p2.y-t.p1.y)-s.p2.x*t.p1.x*(s.p1.y-t.p2.y)+s.p2.x*t.p2.x*(s.p1.y-t.p1.y);
	zahyoutype ynume=-(s.p1.y*t.p1.y*(s.p2.x-t.p2.x)-s.p1.y*t.p2.y*(s.p2.x-t.p1.x)-s.p2.y*t.p1.y*(s.p1.x-t.p2.x)+s.p2.y*t.p2.y*(s.p1.x-t.p1.x));
	P p={xnume/deno,ynume/deno};
	return p;
}

void readpoint(P*p){
	zahyoutype x=1,y;
	if(x/2)scanf("%lf%lf",&x,&y);
	else scanf("%ld%ld",&x,&y);	
	p->x=x;
	p->y=y;
}
void printpoint(P p){
	zahyoutype x=1;
	if(x/2)printf("%.9lf %.9lf\n",p.x,p.y);
	else printf("%ld %ld\n",p.x,p.y);	
}

int main(){
	int q;
	scanf("%d",&q);
	while(q--){
		L s,t;
		for(int i=0;i<2;i++){
			readpoint(&s.p1);
			readpoint(&s.p2);
			if(!i)t=s;
		}
		printpoint(crosspoint(s,t));
	}
}


Measured memory_bytes: 2128
def solve(x):
    x -= 400
    return 8 - x / 200

assert solve(400) == 8
assert solve(401) == 8
assert solve(599) == 8
assert solve(1599) == 3
assert solve(1800) == 1
assert solve(1999) == 1

x = int(raw_input())
print solve(x)


Measured memory_bytes: 64204
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter

val br = BufferedReader(InputStreamReader(System.`in`))
val out = PrintWriter(System.out)

fun main() {
    br.use { out.use {
        solve()
        out.flush()
    } }
}

fun solve() {
    val (n, k) = readLongList()
    var ans = k
    repeat(n.toInt() - 1) {
        ans *= (k - 1)
    }
    out.print(ans)
}

fun readInteger() = Integer.parseInt(br.readLine())
fun readLong() = java.lang.Long.parseLong(br.readLine())
fun readStringList() = br.readLine()!!.split(' ')
fun readIntegerList() = readStringList().map(Integer::parseInt)
fun readLongList() = readStringList().map(java.lang.Long::parseLong)
fun readDoubleList() = readStringList().map(java.lang.Double::parseDouble)


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

int main() {
    int n;
    cin >> n;
    vector<int> a(3*n);
    for (int i=0; i<3*n; i++) cin >> a[i];
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());

    long long ans = 0;
    for (int i=1; i<2*n; i+=2) ans += a[i];
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1536
#include <iostream>
using namespace std;
int main(void){
    int a, b;
    while(cin >> a >> b&&a||b){
        int cnt = 0;
        for(int i=1; i<=a; i++){
        for(int j=i+1;j<=a;j++){
        for(int k=j+1; k<=a; k++){
            if(i+j+k==b)cnt++;
        }
        }
        }
    cout << cnt << endl;
    }
}



Measured memory_bytes: 3096
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
	int count;
	vector<int> length;
	
	string input_str = "";
	string str;
	std::string::size_type first, last;
	
	while(true)
	{
		cin >> count;
		cin.ignore();
		getline(cin, input_str);
		if(cin.eof())
		{
			break;
		}
		first = 0;
		last = input_str.find(' ');
		
		while(last != std::string::npos)
		{
			last = input_str.find(" ", first);
			length.push_back(atoi(input_str.substr(first, last - first).c_str()));
			first = last + 1;
		}
		sort(length.begin(), length.end());
		vector<int>::iterator itr = length.begin();
		
		count = 0;
		
		for(itr = length.begin(); itr != length.end(); itr += 2)
		{
			count += *itr;
		}
		
		cout << count << endl;
		length.clear();
	}
	return 0;
}


Measured memory_bytes: 256
def main():
    N = int(input())
    A = [0] + list(map(int, input().split())) + [0]

    sum = 0
    for i in range(1, N+2): sum += abs(A[i] - A[i-1])

    for i in range(1, N+1):
        if A[i-1] <= A[i] <= A[i+1]: print(sum)
        elif A[i-1] >= A[i] >= A[i+1]: print(sum)
        elif A[i-1] < A[i+1] < A[i] or A[i-1] > A[i+1] > A[i]: print(sum - abs(A[i+1] - A[i]) * 2)
        else: print(sum - abs(A[i] - A[i-1]) * 2)


if __name__ == '__main__':
    main()


Measured memory_bytes: 14048
#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(i, (v).size()) { cout << v[i]; if (i != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)///
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int a, b, c;
	cin >> a >> b >> c;
	if (c >= a&&c <= b)cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}


Measured memory_bytes: 256
program atcoder
    implicit none
    integer :: i,j,n,k
    integer,allocatable,dimension(:) :: dp
    integer,allocatable,dimension(:) :: a
    read*,n,k
    allocate(dp(0:k+1))
    ! dp(i) := はじめ石がi個の時、先攻の人が勝てるかどうか
    allocate(a(n))
    read(*,*)(a(i),i=1,n)
    dp = 0
    do i = 0,k
        do j = 1,n
            if(i-a(j)>=0 .and. dp(i-a(j))==0)then
                !石がi個のとき、a[j]個取って相手が勝てない状態にできるなら勝てる
                dp(i) = 1
            endif
        enddo
    enddo
    if(dp(k)==1)then
        write(*,'(a)')"First"
    else
        write(*,'(a)')"Second"
    endif
    stop 
contains

end program atcoder


Measured memory_bytes: 640
#include <bits/stdc++.h>
#include <cmath>

const double PI = 3.14159265358979323846;
//using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 1e15;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
    ll a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
const int mod = 1000000007;
//const ll mod = 1e10;
typedef priority_queue<string, vector<string>, greater<string> > PQ_ASK;

struct V {
    char v;
    int seq;
};

class CumulativeSum {
    vector<ll> numbers;
    vector<ll> sums;

public:
    CumulativeSum(int n) {
        numbers.resize(n);
        sums.resize(n);
    }

    void set(int i, ll value) {
        numbers[i] = value;
    }

    ll getSum(int i) {
        if (i == -1) return 0;
        if (i == sums.size()) return 0;
        return sums[i];
    }

    ll getSectionSum(int start, int end) {
        return getSum(end) - getSum(start - 1);
    }

    void calculate() {
        for (int i = 0; i < numbers.size(); i++) {
            sums[i] = getSum(i - 1) + numbers[i];
        }
    }

};

int main() {

    int n, k;
    cin >> n >> k;

    vector<V> vs;
    string s;
    cin >> s;

    for (char c : s) {
        if (vs.empty()) vs.push_back({c, 1});
        else if (vs.back().v == c) vs.back().seq++;
        else vs.push_back({c, 1});
    }

    CumulativeSum cs(vs.size());
    rep(i, vs.size()) {
        cs.set(i, vs[i].seq);
    }
    cs.calculate();


    int ans = 0;
    for (int i = 0; i < vs.size(); i++) {
        int seq = vs[i].seq;
        char v = vs[i].v;

        if (v == '0') {
            int ma = min(i + k * 2 - 1, (int) vs.size() - 1);
            int now = cs.getSectionSum(i, ma);
//            printf("%d %d\n", i, ma - i);
            cmax(ans, now);
        } else {
            int ma = min(i + k * 2, (int) vs.size() - 1);
            int now = cs.getSectionSum(i, ma);
//            printf("%d %d\n", i, ma - i);
            cmax(ans, now);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 2932
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

void main() {
    int N, K;
    scan(N, K);

    if (K == 1) {
        writeln(N == 1 ? 1 : 0);
        return;
    }

    auto adj = new int[][](N, 0);

    foreach (i ; 0 .. N - 1) {
        int ai, bi;
        scan(ai, bi);
        ai--, bi--;
        adj[ai] ~= bi;
        adj[bi] ~= ai;
    }

    long dfs(int v, int p, int dep) {
        long res = 1;
        long t = dep > 0 ? K - 2 : K - 1;

        foreach (u ; adj[v]) if (u != p) {
            res *= t * dfs(u, v, dep + 1) % mod;
            res %= mod;
            t--;
            if (t < 0) t = 0;
        }

        debug {
            writefln("v: %d, res: %d", v, res);
        }

        return res;
    }

    auto ans = dfs(0, -1, 0) * K % mod;
    writeln(ans);
}








void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x > arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x < arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}


Measured memory_bytes: 15228
/*  -*- coding: utf-8 -*-
 *
 * 1579.cc: Array Update 2
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_M = 200000;

/* typedef */

struct Op {
  int x, y, z;
  Op() {}
  Op(int _x, int _y, int _z): x(_x), y(_y), z(_z) {}
};

/* global variables */

Op ops[MAX_M];
int xs[MAX_M];

/* subroutines */

/* main */

int main() {
  int n, a, d, m;
  scanf("%d%d%d%d", &n, &a, &d, &m);

  for (int i = 0; i < m; i++)
    scanf("%d%d%d", &ops[i].x, &ops[i].y, &ops[i].z);

  int k;
  scanf("%d", &k);

  int l = 0;
  for (int i = m - 1; i >= 0; i--) {
    Op &op = ops[i];
    if (op.y <= k && k <= op.z) {
      if (op.x != 0)
	xs[l++] = op.x;
      else
	k = op.z - (k - op.y);
    }
  }
  //printf("l=%d, k=%d\n", l, k);

  int v = a + d * (k - 1);
  for (int i = l - 1; i >= 0; i--) {
    if (xs[i] == 1) v++;
    else v >>= 1;
  }

  printf("%d\n", v);
  return 0;
}



Measured memory_bytes: 5412
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve();
    }
    public void solve() {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int N = scan.nextInt();
        int M = scan.nextInt();
        List<Key> list = new ArrayList<>();
        for (int i = 0; i < M; i++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            int keys = 0;
            for (int j = 0; j < b; j++) {
                keys |= pow(scan.nextInt() - 1);
            }
            Key key = new Key(i, a, b, keys);
            list.add(key);
        }
        list.sort(new KeyComparator());
        int max = pow(N)-1+1;
        int[] dp = new int[max];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 0; i < M; i++) {
            Key key = list.get(i);
            if (i == 0) {
                dp[key.keys] = key.cost;
                continue;
            }
            int[] next_dp = new int[max];
            Arrays.fill(next_dp, Integer.MAX_VALUE);
            next_dp[0] = 0;
            for (int j = 0; j < max; j++) {
                next_dp[j] = Math.min(next_dp[j], dp[j]);
                if (dp[j] == Integer.MAX_VALUE) {
                    continue;
                }
                if (j < key.keys) {
                    next_dp[j|key.keys] = Math.min(next_dp[j|key.keys], dp[j]+key.cost);
                    continue;
                }
                if (j == key.keys) {
                    next_dp[key.keys] = Math.min(key.cost, dp[key.keys]);
                    continue;
                }
                next_dp[j | key.keys] = Math.min(next_dp[j | key.keys], dp[j] + key.cost);
            }
            dp = next_dp;
        }
        if (dp[max-1] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(dp[max-1]);
        }
    }
    private int pow(int N) {
        if (N == 0) {
            return 1;
        }
        if (N % 2 == 1) {
            return 2 * pow(N - 1);
        }
        int half = pow(N / 2);
        return half * half;
    }
    class Key {
        int id;
        int cost;
        int keys;
        Key(int id, int cost, int total, int keys) {
            this.id = id;
            this.cost = cost;
            this.keys = keys;
        }
    }
    class KeyComparator implements Comparator<Key> {
        @Override
        public int compare(Key o1, Key o2) {
            if (o1.cost != o2.cost) {
                return Integer.compare(o1.cost, o2.cost);
            }
            return Integer.compare(o1.id, o2.id);
        }
    }
}


Measured memory_bytes: 41900
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << endl
typedef long long ll;
typedef vector<int> V;

int main() {
  int h, w;
  cin >> h >> w;
  int m[550][550] = {};
  for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> m[i][j];
  int i = 0;
  int cnt = 0;
  vector<pair<int, int>> a, b;
  while( true ) {
    for (int j = 0; j < w-1; j++) {
      if( m[i][j]%2 == 1 ) {
	cnt++;
	m[i][j]--;
	m[i][j+1]++;
	a.push_back(make_pair(i+1, j+1));
	b.push_back(make_pair(i+1, j+2));
      }
    }
    i++;
    if( i == h ) break;
    if( m[i-1][w-1]%2 == 1 ) {
      cnt++;
      m[i-1][w-1]--;
      m[i][w-1]++;
      a.push_back(make_pair(i, w));
      b.push_back(make_pair(i+1, w));
    }
    for (int j = w-1; j > 0; j--) {
      if( m[i][j]%2 == 1 ) {
	cnt++;
	m[i][j]--;
	m[i][j-1]++;
	a.push_back(make_pair(i+1, j+1));
	b.push_back(make_pair(i+1, j));
      }
    }
    i++;
    if( i == h ) break;
    if( m[i-1][0]%2 == 1 ) {
      cnt++;
      m[i-1][0]--;
      m[i][0]++;
      a.push_back(make_pair(i, 1));
      b.push_back(make_pair(i+1, 1));
    }
  }
  dump(cnt);
  for (int i = 0; i < cnt; i++) cout << a[i].first << " " << a[i].second << " " << b[i].first << " " << b[i].second << endl;
  return 0;
}


Measured memory_bytes: 5484
import sys
def input(): return sys.stdin.readline().strip()
def prime_factors(n):
    i = 2
    factors = {}
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            if i in factors.keys():
                factors[i] += 1
            else:
                factors[i] = 1
    if n > 1:
        if n in factors.keys():
            factors[n] += 1
        else:
            factors[n] = 1
    return factors

i  = lambda: int(input())

N = i()

pfd = prime_factors(N)

check_num = []

for pf in pfd.keys():
    tmp_num = []
    for i in range(1, pfd[pf] + 1):
        tmp_num.append(pf ** i)
        for n in check_num:
            tmp_num.append(n * (pf ** i))
    check_num += tmp_num

count = 0
for n in check_num:
    if N // (n-1) == N % (n-1):
        count += n-1

print(count)

Measured memory_bytes: 3316
#include<bits/stdc++.h>
#define LL long long
#define PB push_back
#define POP pop_back()
#define PII pair<int,int>
#define FI first
#define SE second
#define ULL unsigned long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
const int maxn=1<<17;
const int N=1e6+10,M=4e7+10;
int A,B,m;
int a[N],b[N];
int main()
{
    cin>>A>>B>>m;
    int ma=INF,mb=INF;
    for(int i=1;i<=A;i++){
        scanf("%d",&a[i]);
        ma=min(ma,a[i]);
    }
    for(int i=1;i<=B;i++){
        scanf("%d",&b[i]);
        mb=min(mb,b[i]);
    }
    int ans=ma+mb;
    for(int i=1,c,x,y;i<=m;i++){
        scanf("%d%d%d",&x,&y,&c);
        ans=min(ans,a[x]+b[y]-c);
    }
    cout<<ans;
    return 0;
}
/*
*/


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

int main() {
  long H, W;
  cin >> H >> W;

  long ans;
  if(H == 1 || W == 1){
    ans = 1;
  } else {
    ans = (H * W + 1) / 2;
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;

int n;
int a[100005];

int main() {
    scanf("%d", &n);
    forn(i, n) scanf("%d", a + i);
    sort(a, a + n);
    if (a[n - 1] == 0) {
        puts("Yes");
        return 0;
    }
    if (n % 3 != 0) {
        puts("No");
        return 0;
    }
    int x = n / 3;
    int y = x * 2;
    if (a[0] != a[x - 1] || a[x] != a[y - 1] || a[y] != a[n - 1]) {
        puts("No");
        return 0;
    }
    if ((a[0] ^ a[x]) != a[y]) {
        puts("No");
        return 0;
    }
    puts("Yes");
    return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005


ll N,C;
ll loc[SIZE],value[SIZE];
ll CLOCK[SIZE],rev[SIZE];
ll max_CLOCK[SIZE],max_rev[SIZE];


int main(){

	scanf("%lld %lld",&N,&C);

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

		scanf("%lld %lld",&loc[i],&value[i]);
	}
	loc[0] = 0;
	loc[N+1] = C;

	//時計回りを計算(片道)
	ll add = 0;

	CLOCK[0] = 0;
	max_CLOCK[0] = 0;

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

		add += value[i];
		ll tmp = add-loc[i];

		CLOCK[i] = tmp;
		max_CLOCK[i] = max(CLOCK[i],max_CLOCK[i-1]); //それまでで最大の差分を取得
		//printf("時計[%lld]:%lld\n",i,max_CLOCK[i]);
	}

	//半時計周りを計算(片道)
	add = 0;

	rev[0] = 0;
	max_rev[0] = 0;

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

		add += value[(N+1)-i];
		ll tmp = add-(C-loc[(N+1)-i]);

		rev[i] = tmp;
		max_rev[i] = max(rev[i],max_rev[i-1]);
		//printf("半時計[%lld]:%lld\n",i,max_rev[i]);
	}

	ll maximum = 0;

	//時計回りを全探索(引き返す)
	for(ll i = 0; i <= N; i++){

		ll tmp = CLOCK[i];
		tmp -= loc[i];

		//printf("i:%lld CLOCK:%lld loc:%lld tmp:%lld\n",i,CLOCK[i],loc[i],tmp);

		tmp += max_rev[N-i];
		//printf("max_rev[%lld]:%lld\n",N-i,max_rev[N-i]);

		maximum = max(maximum,tmp);
	}

	//半時計周りを全探索(引き返す)
	for(ll i = 0; i <= N; i++){

		ll tmp = rev[i];
		tmp -= (C-loc[(N+1)-i]);

		tmp += max_CLOCK[N-i];

		maximum = max(maximum,tmp);
	}

	printf("%lld\n",maximum);

	return 0;
}


Measured memory_bytes: 8452
// Virtual, WIP, WA

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

// Utility
#define ALL(X) (X).begin(), (X).end()
#define RANGE(I, X, Y) for (int I = (X); I < (Y); I++)
using ll = int64_t;
using ull = uint64_t;
namespace std {
template<class ...Ts>        istream& operator>>(istream& i, tuple<Ts...>& x) { apply([&](auto&&... ys){ ((i >> ys), ...); }, x); return i; }
template<class T1, class T2> istream& operator>>(istream& i, pair<T1, T2>& x) { i >> x.first >> x.second; return i; }
template<class T>            istream& operator>>(istream& i, vector<T>& x)    { for (auto& y : x) { i >> y; } return i; }
}

// Debugging
#define DD(X) std::cout << #X ": " << (X) << std::endl << std::flush
namespace std {
template<class ...Ts>        ostream& operator<<(ostream& o, const tuple<Ts...>& x) { o << "("; apply([&](auto&& y, auto&&... ys){ o << y; ((o << ", " << ys), ...); }, x); o << ")"; return o; }
template<class T1, class T2> ostream& operator<<(ostream& o, const pair<T1, T2>& x) { o << tie(x.first, x.second); return o; }
template<class T>            ostream& operator<<(ostream& o, const vector<T>& x)    { o << "{"; for (auto it = x.begin(); it != x.end(); it++) { if (it != x.begin()) { o << ", "; } o << *it; } o << "}"; return o; }
template<class T1, class T2> ostream& operator<<(ostream& o, const set<T1, T2>& x)  { o << "{"; for (auto it = x.begin(); it != x.end(); it++) { if (it != x.begin()) { o << ", "; } o << *it; } o << "}"; return o; }
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& x)  { o << "{"; for (auto it = x.begin(); it != x.end(); it++) { if (it != x.begin()) { o << ", "; } o << *it; } o << "}"; return o; }
}

// Main
tuple<ull, bool> mul(ull x, ull y) {
  ull ones32 = 0xffffffffULL;
  ull ones64 = 0xffffffffffffffffULL;

  ull xl = x & ones32;
  ull xh = x >> 32;
  ull yl = y & ones32;
  ull yh = y >> 32;

  ull z0 = xl * yl;
  ull z1 = xh * yl;
  ull z2 = xl * yh;
  ull z3 = xh * yh;

  if ((z1 >> 32) || (z2 >> 32) || z3) {
    return {0, 1};
  }

  ull w1 = z1 << 32;
  ull w2 = z2 << 32;

  ull res = 0;
  if (res > ones64 - z0) { return {0, 1}; }
  res += z0;

  if (res > ones64 - w1) { return {0, 1}; }
  res += w1;

  if (res > ones64 - w2) { return {0, 1}; }
  res += w2;

  return {res, 0};
}

void mainCase() {
  int n;
  cin >> n;
  vector<ull> ls(n, 0);
  cin >> ls;
  sort(ALL(ls));

  if (ls[0] == 0) {
    cout << 0 << endl;
    return;
  }

  ull lim = 1000'000'000'000'000'000ULL;
  ull res = 1;
  RANGE(i, 0, n) {
    auto [tmp, overflow] = mul(res, ls[i]);
    if (overflow || tmp > lim) {
      cout << -1 << endl;
      return;
    }
    res = tmp;
  }
  cout << res << endl;
}

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

Measured memory_bytes: 4056
#include<cstdio>
#include<cstring>

long long int gcd(long long int p, long long int q) {
    if(!q) return p;
    if(q>p) return gcd(q, p);
    return gcd(q, p%q);
}

int main(void) {
    long long int p,q;
    scanf("%lld%lld",&p,&q);
    long long int g = gcd(p,q);
    p/=g; q/=g;

    long long int b=1;
    for(long long int i=2; q-1 && i*i<=q; i++) {
        if(q%i) continue;
        b*=i;
        while(!(q%i)) q/=i;
    }
    b*=q;
    printf("%lld\n",b);

}

Measured memory_bytes: 2728
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define db double
#define inf 0x3f3f3f3f
using namespace std;

const int N=5e5+10,mod=1e9+7;


ll fac[N],inv[N];
ll ksm(ll a,ll n){
	ll ans=1;
	while(n){
		if(n&1) ans=ans*a%mod;
		a=a*a%mod;
		n>>=1;
	}
	return ans;
}
ll C(int m,int n){
	return fac[m]*inv[m-n]%mod*inv[n]%mod;
}
ll A(int m,int n){
	return fac[m]*inv[m-n]%mod;
}


int main(){
	int n,m;
	cin>>n>>m;
	fac[0]=1;
	for(int i=1;i<=m;i++) fac[i]=fac[i-1]*i%mod;
	inv[m]=ksm(fac[m],mod-2);
	for(int i=m-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
	ll ans=0;
	for(int i=0;i<=n;i++) ans+=C(n,i)*((i&1)?-1:1)*A(m-i,n-i)%mod;
	ans=(ans%mod+mod)%mod;
	ans=(ans*A(m,n)%mod);
	cout<<ans<<endl;	
}
 

Measured memory_bytes: 11388
#include <iostream>
#include <numeric>
#include <math.h>
#include <algorithm>
#include <float.h>
#include <limits>
#include <vector> 
#include <string.h>
#include <iomanip>

#define rep(i,a,n) for(ll int (i) = (a);(i) < (n);(i)++)
#define urep(i,a,n) for(ll int (i) = (a);(i) > (n);(i)--)
#define MOD 1000000007
#define ll long long
#define asort(a) sort(a.begin(),a.end());

using namespace::std;

int jo(ll int jxo, ll int jyo){//累乗
	ll int jhogeo = 1;
	rep(jinto,0,jyo) jhogeo=jhogeo*jxo;
	return jhogeo;
}
char tobig(char toxbig){//char大文字化
  return (toxbig-0x20);
}
char tolow(char toxlow){//char小文字化
  return (toxlow+0x20);
}
int ctoi(char cctoi){//char->int
  if('0' <= cctoi && cctoi <= '9') return (cctoi-'0');
  return -1;
}

int gcd(int gcad, int gcbd) {//最大公約数
  if(gcad < gcbd) return gcd(gcbd, gcad);
  int gcrd;
  while ((gcrd=gcad%gcbd)) {
    gcad = gcbd;
    gcbd = gcrd;
  }
  return gcbd;
}
int lcm(int lcam, int lcbm){//最小公倍数
 return (lcam*lcbm)/gcd(lcam,lcbm);
}


int main(){
  int N;
  cin>>N;
  vector<pair<int,int>> data(N);
  rep(i,0,N) cin>>data[i].second>>data[i].first;
  asort(data);
  int temp=0;
  int check=0;
  rep(i,0,N){
    temp+=data[i].second;
    if(temp>data[i].first){
      check++;
      break;
    }
  }
  cout<<(check?"No":"Yes");
  
 
}
// int *p = new int[N];

//  vector<int> 変数名(要素数);
//  sort(変数名.begin(),変数名.end());//昇順ソート

// vector<vector<int>> 変数名(左の個数, vector<int>(右の個数));

//cout<<fixed<<setprecision(n);  //小数点n桁まで表示

//  vector<pair<int,int>> data(N);  //2つのデータを格納、.first/.secondで参照
// pairのソートはfirst優先の昇順

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

int main(){
  vector<vector<int>> a(3, vector<int>(3));
  for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
      cin >> a.at(i).at(j);
    }
  }
  bool ok = true;
  for(int i = 0; i < 2; i++){
    for(int j = 0; j < 2; j++){
      if(a[i][j+1] - a[i][j] != a[i+1][j+1] - a[i+1][j]) ok = false;
    }
  }
  for(int i = 0; i < 2; i++){
    for(int j = 0; j < 2; j++){
      if(a[j+1][i] - a[j][i] != a[j+1][i+1] - a[j][i+1]) ok = false;
    }
  }
  if(ok) cout << "Yes" << endl;
  else cout << "No" << endl;
}
  

Measured memory_bytes: 256
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<cstdio>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;

const int inf = 99999999;
const ll linf = 999999999999999999;
const double pi = acos(-1);
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};

#define p_queue(i) priority_queue<i> 
#define rp_queue(i) priority_queue<i, vector<i>, greater<i>> 
#define P(p) cout<<(p)<<endl
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define mod(i) ((i)%(ll)(1e9 + 7))
#define divm(a,b) (mod(a * modpow((ll)b,(ll)(1e9+5))))
#define rsort(a,b,c) sort(a, b, greater<c>())
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.end(), v.begin());
#define ft first
#define sd second
#define pb push_back
#define it insert
#define sz(x) ((int)(x).size())
#define lb(a,n,k) (lower_bound(a,a+n,k) - a) 
#define vlb(a,k)  (lower_bound(a.begin(),a.end(),k) - a.begin())
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return

ll modpow(ll i,ll j){ ll tmp=1; while(j){ if(j%2)tmp=mod(tmp*i);i=mod(i*i);j/=2;}return tmp;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

//ll ncr[100][100];
//ll nCr(int n, int r){if(n==r) ret ncr[n][r] = 1; if(r==0) ret ncr[n][r] = 1; if(r==1) ret ncr[n][r] = n;if(ncr[n][r]) ret ncr[n][r]; ret ncr[n][r] = nCr(n-1,r) + nCr(n-1,r-1);}

//ll npr[100][100]={};
//ll nPr(int n,int r){if(npr[n][r])ret npr[n][r];if(r==0)ret npr[n][r] = 1;if(r==1)ret npr[n][r] = n;ret npr[n][r] = n * nPr(n-1,r-1);}

//ll nHr(int n,int r){ret nCr(n+r-1,r);}

///////////////////////////////////////////////////////////////////////////



int main(){
	string t = "CODEFESTIVAL2016";
	string a;
	cin >> a;

    int cnt = 0;
	rep(i,0,sz(t)){
        if(t[i] != a[i])cnt++;
	}
	P(cnt);
    
	return  0;
}

Measured memory_bytes: 256
#include<stdio.h>

int main()
{
    long long int a,b,i,c=1;
    scanf("%lld",&a);
    b=a;
    for(i=1;i<=a;i++)
    {
        c=c*b;
        b--;

    }
printf("%lld\n",c);


    return 0;
}



Measured memory_bytes: 2044
#include <iostream>
#include <stdlib.h>
#include <utility>
#include <stdio.h>
#include <vector>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, j , k) for(int i = j; i < k; i++)
#define ll long long

using namespace std;

int main(){
	ll X, K, D;
	cin >> X >> K >> D;
	X = abs(X);
	ll ans = 0;
	
	if(K <= X/D) {
		ans = X - D * K;
	}else{
		K = K - X/D;
		X = X - ((X/D) * D);
		if(K % 2 != 0) ans = D - X;
		else ans = X;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3556
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>
#include <queue>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cmath>
using namespace std;

typedef long long ll;

#define INF 10e17 // 4倍しても(4回足しても)long longを溢れない
#define rep(i,n) for(int i=0; i<n; i++)
#define rep_r(i,n,m) for(int i=m; i<n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())

int main() {
  int n,a,b;
  cin >> n >> a >> b;
  int total = 0, ans = 0;

  for (int i = 1; i <=n; ++i) {
    int cpy = i;
    while (cpy > 0) {
      total += cpy % 10;
      cpy /= 10;
    }
    if (total >= a and total <= b) {
      ans += i;
    }
    total = 0;
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
h,w,k = map(int, input().split())
c = [list(input()) for _ in range(h)]
ans = 0
for ib in range(1<<h):
    for jb in range(1<<w):
        cc = 0
        for i in range(h):
            if ib&(1<<i):
                continue
            for j in range(w):
                if jb&(1<<j):
                    continue
                cc += c[i][j]=="#"
        if cc==k:
            ans += 1
print(ans) 

Measured memory_bytes: 9208
#include<bits/stdc++.h>
typedef long long     ll;
#define fast          ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define fr            first
#define sc            second
#define com(i,n)      for(int i=0;i<n;i++)
#define dom(i,n)      for(int i=1;i<=n;i++)
#define mom(i,n)      for(int i=n;i>=0;i--)
#define sortI(a,n)    sort(a,a+n)
#define sortD(a,n)    sort(a,a+n,greater<int>())
#define sortvi(a)     sort(a.begin(),a.end())
#define sortvd(a)     sort(a.begin(),a.end(),greater<int>())
#define sumall(a,x)   accumulate(a.begin(),a.end(),x)
#define pi            3.14159265358979323846264338327950
using namespace std;
int main()
{
    int a[5];
    com(i,5)
    {
        cin>>a[i];
    }
    com(i,5)
    {
        if(a[i]==0)
        {
            cout << i+1 << endl;
            break;
        }
    }
}


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

using namespace std;

inline bool bound_check(int x) {
  return 0 <= x && x <= 5000;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (!n) return 0;
    pair<int, int> poles[n];
    vector<vector<bool> > plane(5001, vector<bool>(5001));
    int result = 0;
    for (int i = 0; i < n; i++) {
      cin >> poles[i].first >> poles[i].second;
      plane[poles[i].first][poles[i].second] = true;
    }
    for (int i = 0; i < n-1; i++)
      for (int j = i+1; j < n; j++) {
        int dx = poles[j].first - poles[i].first,
            dy = poles[j].second - poles[i].second,
            a = poles[j].first + dy, b = poles[j].second - dx,
            c = a - dx, d = b - dy;
        if (bound_check(a) && bound_check(b) && bound_check(c) && bound_check(d)
            && plane[a][b] && plane[c][d])
          result = max(result, dx * dx + dy * dy);
      }
    cout << result << endl;
  }
}

Measured memory_bytes: 4116
#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define min(p,q)((p)<(q)?(p):(q))
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}

ll a[100010];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)scanf("%lld",a+i);
	sortup(a,n);
	int ans=1e9;
	for(int i=0;i+k-1<n;i++){
		if(a[i+k-1]<0)ans=min(ans,-a[i]);
		else if(a[i]<0)ans=min(ans,a[i+k-1]-a[i]+min(a[i+k-1],-a[i]));
		else ans=min(ans,a[i+k-1]);
	}
	printf("%d",ans);
}


Measured memory_bytes: 1276
#include <iostream>
using namespace std;
#define Nmax 500000
#define INF 1000000001

int a[Nmax];
int L[Nmax / 2 + 2];
int R[Nmax / 2 + 2];
int cnt;

void merge(int left, int mid, int right)
{
	int n1 = mid - left;
	int n2 = right - mid;
	for (int i = 0; i < n1; i++) {
		L[i] = a[left + i];
	}
	for (int i = 0; i < n2; i++) {
		R[i] = a[mid + i];
	}
	L[n1] = INF;
	R[n2] = INF;

	int i, j;
	i = j = 0;
	for (int k = left; k < right; k++) {
		cnt++;
		if (L[i] <= R[j]) {
			a[k] = L[i];
			i++;
		}
		else
		{
			a[k] = R[j];
			j++;
		}
	}
}

void mergeSort(int left, int right)
{
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		mergeSort(left, mid);
		mergeSort(mid, right);
		merge(left, mid, right);
	}
}

int main()
{
	int n;

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	cnt = 0;
	mergeSort(0, n);

	for (int i = 0; i < n; i++) {
		cout << a[i];
		cout << ((i != (n - 1)) ? " " : "\n");
	}
	cout << cnt << endl;

	return 0;
}


Measured memory_bytes: 7004
// #include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<set>
#define rep(i,n) for(int i=0;i<n;i++)
#define exf(x)	 for(auto&& tmp : x) { cout << tmp << endl; }	// 拡張for
#define testP	 cout << "------------------ test ------------------" << endl;	// 動作確認テスト
#define testX(x) cout << "testout : " << x << endl;	// テストアウト
typedef long long ll;
using namespace std;
const int MOD{ int(1e9 + 7) };	// int 1000000007
const int inf{ int(1e9) };	// inf = 1000000000

// ------------------ ベクター表示 ------------------ //
void printVec(vector<ll> & vec) {
	cout << "";
	for (auto it = vec.begin(); it != vec.end(); it++) cout << *it << " ";
	cout << endl;
}

// ------------------ ベクター合計 ------------------ //
int sumVec(vector<int> v) {
	int sum = 0;
	for (size_t i = 0; i < v.size(); i++) sum += v[i];
	return sum;
}

// ------------------ int -> Vector 変換 ------------------ //
vector<int> ItoVec(int x) {

	vector<int> xVec;

	while (x > 0) {
		int tmp = x % 10;
		xVec.insert(xVec.begin(), tmp);
		x /= 10;
	}

	return xVec;
}

// ------------------ 最大公約数関数 ------------------ //
int GCD(int x, int y) {
	if (x == 0) return y;
	else GCD(y % x, x);
}


// -------------------------------------------------- //
// ------------------ ここから本編 ------------------ //
// -------------------------------------------------- //

int main(void) {

	string S, T;
	cin >> S >> T;

	T.erase(--T.end());
	cout<< (S==T?"Yes":"No") <<endl;

	return 0;
}

Measured memory_bytes: 3600
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
	
	const ll mod = 1000000007;
	ll n, m; cin >> n >> m;
	if (n < m) {
		swap(n, m);
	}
	if (2 <= n - m) {
		cout << 0 << endl;
		return 0;
	}
	ll ans = 1;
	if (n - m == 1) {
		for (ll i = 0; i < n; i++) {
			ans *= (i + 1);
			ans %= mod;
		}
		for (ll i = 0; i < m; i++) {
			ans *= (i + 1);
			ans %= mod;
		}
	}
	else { // n = m
		ans *= 2;
		for (ll i = 0; i < n; i++) {
			ans *= (i + 1);
			ans %= mod;
		}
		for (ll i = 0; i < m; i++) {
			ans *= (i + 1);
			ans %= mod;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(i = 1; i <= n; i++)
//2D::rep(i,j)rep(j,j)cin>>bin[i][j];
//vector<int> a(n)
#define Graph vector<vector<int>>
#define A1 cout<<"No"<<endl
#define A2 cout<<"Yes"<<endl
#define A3 cout<<ans<<
#define AA(i) cout<<i<<endl;
const int U=2e5+5;
const ll MOD=1000000007;
const int INF = 1001001001;


int main(){
    int i,N; cin >> N;
    int b=0;
    int ans=0;
    rep(i,N){
      if(N%i!=0){
        b=1;
      }
      ans += N/i - 1 + b;
      b=0;
    }
    AA(ans);
    return 0;
}

Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define int long long
#define ld long double
#define fi first
#define se second
#define all(uiet) uiet.begin(),uiet.end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vpp vector<pair< int, int > >
#define pll pair<int , int >
#define ppll pair < pll , pll >
#define mod 1000000007
const int INF=(1LL<<60)-1;
int32_t main(){
    SPEED;
    int l, r, d;
    cin >> l >> r >> d;
    if(l % d == 0) cout << r/d - l/d + 1;
    else cout << r/d - l/d;
}

Measured memory_bytes: 3596
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair

int main(){
	ll n,k;
	cin >> n >> k;
	ll a[n];
	for(ll i=0;i<n;i++) cin >> a[i];
	sort(a,a+n);
	ll res=0;
	for(ll i=0;i<k;i++) res+=a[i];
	cout << res << endl;
}


Measured memory_bytes: 3644