akhauriyash/Code-Regression huggingface dataset:
using System;
using System.Collections.Generic;
//using System.Linq;
class Program
{
	static void Main(string[] args)
	{
		// 入力フェーズ
        //string[] input = Console.ReadLine().Split(' ');
				int N = int.Parse(Console.ReadLine());
				List<point> coordinate = new List<point>();
				for(int i=0; i<N; i++)
				{
					string[] input = Console.ReadLine().Split(' ');
					point p = new point();
					p.x = int.Parse(input[0]);
					p.y = int.Parse(input[1]);
					p.h = int.Parse(input[2]);
					coordinate.Add(p);
				}

				// 座標1~100, 1~100について中心点となりうるか判定する
				bool isCenter	= true;
				point center = new point();
				for(int i=0; i<=100; i++)
				{
					for(int j=0; j<=100; j++)
					{
						// 座標(i, j)は中心点を満たすか？
						//Console.Write(i + " " + j + " is Center? ");
						int H = 0;
						isCenter = true;
						for(int k = 0; k<N; k++)
						{
							point pyramid = coordinate[k];
							if(pyramid.h > 0)
							{
								H = pyramid.h + Math.Abs(i - pyramid.x) + Math.Abs(j - pyramid.y);
								break;
							}
						}
						foreach(var py in coordinate)
						{
							int hh = Math.Max(H - Math.Abs(i-py.x) - Math.Abs(j-py.y), 0);
							if(hh != py.h)
							{
								isCenter = false;
								break;
							}
						}

						if(isCenter)
						{
							center.x = i;
							center.y = j;
							center.h = H;
							break;
						}
					}
					if(isCenter) break;
				}
        Console.WriteLine(center.x + " " + center.y + " " + center.h);
    }

		class point
		{
			public int x{set; get;}
			public int y{set; get;}
			public int h{set; get;}
		}
}


Measured memory_bytes: 13268
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod=1000000007;
int ncr(int n, int r, int p) 
{ 
    int C[r+1]; 
    memset(C, 0, sizeof(C)); 
    C[0] = 1;
    for (int i = 1; i <= n; i++) 
    { 
        for (int j = min(i, r); j > 0; j--) 
  
            C[j] = (C[j] + C[j-1])%p; 
    } 
    return C[r]; 
}
int main() {
    // your code goes here
    
    string s;
    cin>>s;
    bool f=0;
    for(int i=0;i<s.length();i++)
    {
        if((i+1)%2!=0)
        {
            if(s[i]=='R'||s[i]=='U'||s[i]=='D')continue;
            f=1;
        }
        else
        {
            if(s[i]=='L'||s[i]=='U'||s[i]=='D')continue;
            f=1;
        }
    }
    if(!f)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

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

int main() {
  int N,c,x;
  c=0;
  cin >> N;
  vector<int> a(N);
  for (int i=0;i<N;i++) {
    cin >> a.at(i);
    x=a.at(i);
    if ((i+1)%2!=0&&x%2!=0) {
      c++;
    }
  }
  cout << c << endl;
}

Measured memory_bytes: 3596
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

int main() {
  ll n;
  string str;
  cin >> str >> n;

  rep(i, (ll)str.size()) {
    if (str[i] == 'a') continue;
    ll c = 26 - (str[i] - 'a');
    if (c <= n) {
      n -= c;
      str[i] = 'a';
    }
  }
  if (n > 0) str[(ll)str.size()-1] += (n % 26);

  cout << str << endl;

  return 0;
}


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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<pair<int, int> >v(n);
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        v[i] = {x, i + 1};
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < n; i++) {
        cout << v[i].second << " ";
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 1664
from collections import *

def dfs(now):
    if len(now)==N:
        print(''.join(now))
        return

    for i in range(ord(max(now))-ord('a')+2):
        now.append(alpha[i])
        dfs(now)
        now.pop()

N = int(input())
alpha = 'abcdefghijklmnopqrstuvwxyz'
dfs(deque(['a']))

Measured memory_bytes: 55384
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			
			int N = scan.nextInt();
			int K = scan.nextInt();
			int[] h = new int[N];
			for(int i = 0;i<N;i++) {
				h[i] = scan.nextInt();
			}
			
			Arrays.sort(h);
			
			
			int min = Integer.MAX_VALUE;
			
			for(int i = 0;i<N-(K-1);i++) {
				int hmin = h[i];
				int hmax = h[i+(K-1)];
				
				if((hmax-hmin)<min) {
					min = hmax-hmin;
				}
				
				
			}
			
			System.out.println(min);
			
			
		}
	}
}

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

int main() {
    int n;
    ll ans = 0;
    cin >> n;

    string s;
    cin >> s;

    vector<int> r(n, 0), g(n, 0), b(n, 0);
    set<char> temp({'R', 'G', 'B'});
    r[0] = (s[0] == 'R');
    g[0] = (s[0] == 'G');
    b[0] = (s[0] == 'B');
    for (int i = 1; i < n; i++) {
        r[i] = r[i - 1] + (s[i] == 'R');
        g[i] = g[i - 1] + (s[i] == 'G');
        b[i] = b[i - 1] + (s[i] == 'B');
    }


    for (int i = 0; i < n - 2; i++) {
        for (int j = i + 1; j < n - 1; j++) {
            if (s[i] == s[j]) continue;
            set<char> cl = temp;
            cl.erase(s[i]);
            cl.erase(s[j]);
            char left = *cl.begin();
            ll val = 0;
            if (left == 'G') {
                val = g[n - 1] - g[j];
            } else if (left == 'R') {
                val = r[n - 1] - r[j];
            } else {
                val = b[n - 1] - b[j];
            }
            if (j + (j - i) < n && s[j + (j - i)] == left) {
                val--;
            }
            ans += val;
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 3688
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
  ll a,b;
  cin>>a>>b;
  ll sum=0;
  ll arr[b];
  for(int i=0;i<b;i++)
  {
    cin>>arr[i];
    sum+=arr[i];
  }
  if(sum<a)
  {
    cout<<"No";
  }
  else
  {
    cout<<"Yes";
  }
}


Measured memory_bytes: 1024
#include <stdio.h>

int main(){
    char S[101];

    scanf("%s", S);

    int start = 0;
    int end = 0;

    for(; S[end] != '\0'; end ++);

    end --;

    int ans = 0;

    for(; start < end; start ++, end --){
        if(S[start] != S[end]){
            ans ++;
        }
    }
    printf("%d\n", ans);
}

Measured memory_bytes: 128
import math
X  = int(input())

def check(n):
    for i in range(2, math.ceil(math.sqrt(n))+1):
        #print(i)
        if n%i==0:
            break
    else:
        return 1

if X==2:
    print(2)
else:
    for i in range(X, X+100):
        #print(i)
        if check(i)==1:
            print(i)
            break



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

//create arrays outside of main()
int N = 10;
int a[100000] = {};

stack<char> s;


int main() {

    pair<int,int> p[1000000];

    int n;
    cin >> n;

    for(int i=0; i<n; i++){
        cin>>p[i].first;
        p[i].second = i;
    }

    sort(p, p+n);

    for(int i=0; i<n; i++){
        cout << p[i].second + 1 << " ";
    }

    return 0;
}

Measured memory_bytes: 8576
from sys import stdin
readline = stdin.readline


h, w = map(int, readline().split())
w += 1
dp = [list(map(int, readline().split())) + [1] for _ in range(h)]

for i in range(h):
    for j in range(w):
        dp[i][j] ^= 1
    
for i in range(1, h):
    for j in range(w):
        if dp[i][j]:
            dp[i][j] += dp[i - 1][j]

max_s = 0
for i in range(h):
    stack = []
    for j in range(w):
        if (stack[-1][0] if stack else 0) < dp[i][j]:
            stack.append((dp[i][j], j))
        elif stack and stack[-1][0] > dp[i][j]:
            while stack and stack[-1][0] > dp[i][j]:
                height, l = stack.pop()
                s = height * (j - l)
                if max_s < s:
                    max_s = s
            if dp[i][j]:
                stack.append((dp[i][j], l))
print(max_s)

Measured memory_bytes: 53468
#include <algorithm>
#include <bitset>
#include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <numeric>
#include <sstream>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#include<iomanip>
namespace mp = boost::multiprecision;
using namespace std;
using ull = unsigned long long;
using ld = long double;
using lll = boost::multiprecision::cpp_int;
using ll = long long;
#define FOR(i, N) for(ll i = 0; i < N; ++i)

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
// using int = ll;
template <typename T>
T gcd(T m, T n) {
    if(n == 0)
        return abs(m);
    return (gcd(n, m % n));
}
void putYN(bool b) {
    if(b) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}
ll combi(ll n, ll k) {
    ll r = 1, rr = 1;
    for(ll i = 0; i < k; ++i) {
        r *= (n - i);
        rr *= (i + 1);
         r %= 1000000007;
         rr %= 1000000007;
    }
    r /= rr;
     r %= 1000000007;
    return r;
}
#define rep(a,b,c) for(ll a = b; a < c; ++i)
ll mod = 1000000007;
//---------------------------------------------------------------------------------------------------
ll f[101010];
void init() {
    f[0] = 1;
    rep(i, 1, 101010) f[i] = (f[i - 1] * i) % mod;
}
//---------------------------------------------------------------------------------------------------
ll inv(ll x) {
    ll res = 1;
    ll k = mod - 2;
    ll y = x;
    while (k) {
        if (k & 1) res = (res * y) % mod;
        y = (y * y) % mod;
        k /= 2;
    }
    return res;
}
//---------------------------------------------------------------------------------------------------
ll C(int n, int k) {
    ll a = f[n]; // = n!
    ll b = f[n-k]; // = (n-k)!
    ll c = f[k]; // = k!

    ll bc = (b * c) % mod;

    return (a * inv(bc)) % mod;
}
ll pow2_(ll n){
  
  ll div = 1000000007ll;
  ll ans = 0,k=2;
  {
    bitset<48> b(n);
    FOR(i,32){
      ans+=b[i]*k%div;
      ans%=div;
      k*=k;
      k%=div;
    }
  }
  return ans;
}
int main(){
  ll X,Y,Z;
  cin>>X>>Y>>Z;
  swap(X,Y);
  swap(X,Z);
  printf("%ld %ld %ld\n",X,Y,Z);
  return 0;
}

Measured memory_bytes: 256
package main

import (
  "bufio"
  "fmt"
  "io"
  "os"
  "strconv"
)

func Solve(stdin io.Reader, stdout io.Writer) {
  sc := bufio.NewScanner(stdin)

  /* 単語ごとに入力するならこれ */
  sc.Split(bufio.ScanWords)

  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())

  ret := true

  max := -1
  for i := 0; i < n; i++ {
    sc.Scan()
    n, _ := strconv.Atoi(sc.Text())
    if n > max {
      max = n - 1
    } else if n < max {
      ret = false
    }
  }

  if ret {
    fmt.Println("Yes")
  } else {
    fmt.Println("No")
  }


}

func main() {
  Solve(os.Stdin, os.Stdout)
  return
}


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

int main() {
    int N;
    cin >> N;
    vector<int> weight(N);
    int h = 0;
    int l = 0;

    int i;
    for(i = 0; i < N; i++){
        cin >> weight[i];
        h += weight[i];
    }

    int memory;
    for(i = N - 1; i >= 0; i--){
        memory = h - l;
        h -= weight[i];
        l += weight[i];
        if(l >= h) break;
    }

    cout << min(memory, l - h) << endl;

    return 0;
}



Measured memory_bytes: 256
/**********************************************************/
/**            Bismillahir-rahmanir-rahim                **/
/**  In the name of Allah, the Beneficent, the Merciful. **/
/**********************************************************/
/****@author: Sayed Sohan, CSE 13th batch(2016), MBSTU*****/
/**********************************************************/

#include <bits/stdc++.h>
using namespace std;
//              Typedefs
typedef long long               ll;
typedef unsigned long long      ull;

//              Templates
template<class T>               using min_heap = \
                                priority_queue<T, \
                                std::vector<T>, std::greater<T>>;

//              Basic-Defs
#define PB                      push_back
#define MP                      make_pair
#define SP                      " "
#define NL                      "\n"
#define all(v)                  v.begin(), v.end()

//              DEBUG--SECTION
#define DE                      cout<<"HERE\n";
#define DE1(x)                  cout<<x<<"###\n";
#define DE2(x,y)                cout<<x<<"####"<<y<<"\n";
#define Pause                   system("pause");

//              Functions
#define clr(arr,value)          memset(arr,value,sizeof(arr))
#define __FAST_IO               ios_base::sync_with_stdio(0);\
                                cin.tie(0);cout.tie(0);
#define NUM1(x)                 __builtin_popcount(x)
#define LCM(x,y)                (x*y)/__gcd(x,y)

//              Sizes--Math     0123456789
#define MOD                     1000000007   //1e9+7 
#define MAX                     100050      
#define INF                     1e18
#define PI                      acos(-1)
#define eps                     1e-6

//** your code goes from here


int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    __FAST_IO;
    
    ll i,j,k,n,m,T; //basic variables
    string s;
    cin>>k;
    cin>>s;

    if(s.size()<=k){
    	cout<<s<<NL;
    }
    else{
    	for(i=0;i<k;i++) cout<<s[i];
    	cout<<"...";
    }


    /// Don't forget to set array size....
    return 0;
}




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

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> A(M), B(M), C(M);
    vector<vector<int>> dist(N, vector<int>(N));
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i] >> C[i];
        A[i]--; B[i]--;
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i == j) {
                dist[i][j] = 0;
            } else {
                dist[i][j] = 1000000000;
            }
        }
    }

    for (int i = 0; i < M; i++) {
        dist[A[i]][B[i]] = min(dist[A[i]][B[i]], C[i]);
        dist[B[i]][A[i]] = min(dist[B[i]][A[i]], C[i]);
    }

    for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }

    int res = M;
    for (int i = 0; i < M; i++) {
        bool flag = false;
        for (int j = 0; j < N; j++) {
            if (dist[j][A[i]] + C[i] == dist[j][B[i]]) {
                flag = true;
            }
        }
        if (flag) {
            res--;
        }
    }
    cout << res << endl;
    return 0;
}

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

using namespace std;

int main () {
    int n;
    
    cin  >> n;
    int val = (n % 1000 == 0) ? 0 : (1000 - n % 1000) ;
    
    cout <<  val << endl;
    
    return 0;
}


Measured memory_bytes: 3608
use std::vec::Vec;
use std::cmp::*;

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 main() {
  let (n, l) = {
    let v: Vec<i64> = read_vec();
    (v[0], v[1])
  };

  let mut v: Vec<i64> = vec![0; n as usize];
  let mut sum = 0;
  let mut abs: usize = 0;
  for i in 0..n as usize {
    v[i] = l + i as i64;
    sum += v[i];
    if v[i].abs() < v[abs].abs() {
      abs = i;
    }
  }

  println!("{}", sum - v[abs]);

}


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

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#pragma GCC optimize("Ofast")
//1.0 * clock() / CLOCKS_PER_SEC

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const ll NN = 1e10 + 7;
const int N = 1e5 + 7;
const int M = 11;
const ll mod = 1e9 + 7;
const ll inf = 1e18 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);

void solve1()
{
    int k;
    cin >> k;
    int ans = 0;
    for( int i = 1; i <= k; i++ ){
        for( int j = 1; j <= k; j++ ){
            for( int h = 1; h <= k; h++ ){
                ans += __gcd(i , __gcd(j , h));
            }
        }
    }
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    int cghf = 1;//cin >> cghf;
    while( cghf-- ){
        solve1();
    }
}


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

using namespace std;
typedef long long ll;

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end() //sortなどの引数を省略したい
#define SIZE(x) ll(x.size()) //sizeをsize_tからllに直しておく
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define Umap unordered_map
#define Uset unordered_set

int main(){
    ll k, x;
    cin >> k >> x;
    for(int i = x - k + 1; i <= x + k - 1; i++) cout << i << " ";
    return 0;
}

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

using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); i++)

int main(){
  int n;
  cin >> n;
  if(n < 10) cout << n << endl;
  else if(n < 100) cout << 9 << endl;
  else if(n < 1000) cout << 9 + n - 99 << endl;
  else if(n < 10000) cout << 909 << endl;
  else if(n < 100000) cout << 909 + n - 9999 << endl;
  else if(n < 1000000) cout << 90909 << endl;
  return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[][] c = new int[3][3];
		int[] a = new int[3];
		int[] b = new int[3];

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				c[i][j] = sc.nextInt();
			}
		}

		// Initialize a & b
		for (int i = 0; i < 3; i++) {
			int max = 0;
			for (int j = 0; j < 3; j++) {
				max = Math.max(c[i][j], max);
			}
			a[i] = max;
		}
		for (int j = 0; j < 3; j++) {
			int max = 0;
			for (int i = 0; i < 3; i++) {
				max = Math.max(c[i][j], max);
			}
			b[j] = max;
		}

		while (true) {
			if (check(a, b, c)) {
				System.out.println("Yes");
				return;
			}

			for (int i = 0; i < 3; i++) {
				boolean[] bigger = new boolean[3];
				for (int j = 0; j < 3; j++) {
					bigger[j] = (a[i] + b[j] > c[i][j]);
				}
				if (bigger[0] == bigger[1] && bigger[1] == bigger[2]) {
					if (bigger[0]) {
						a[i]--;
					}
				}
			}
			for (int j = 0; j < 3; j++) {
				boolean[] bigger = new boolean[3];
				for (int i = 0; i < 3; i++) {
					bigger[i] = (a[i] + b[j] > c[i][j]);
				}
				if (bigger[0] == bigger[1] && bigger[1] == bigger[2]) {
					if (bigger[0]) {
						b[j]--;
					}
				} else {
					System.out.println("No");
					return;
				}
			}
		}
	}

	private static boolean check(int[] a, int[] b, int[][] c) {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (c[i][j] != a[i] + b[j]) {
					return false;
				}
			}
		}
		return true;
	}
}


Measured memory_bytes: 21844
#[allow(unused_imports)]
use std::cmp;

// Input macro from https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
  (source = $s:expr, $($r:tt)*) => {
    let mut iter = $s.split_whitespace();
    input_inner!{iter, $($r)*}
  };
  ($($r:tt)*) => {
    #[allow(unused_mut)]
    let mut s = {
      use std::io::Read;
      let mut s = String::new();
      std::io::stdin().read_to_string(&mut s).unwrap();
      s
    };
    let mut iter = s.split_whitespace();
    input_inner!{iter, $($r)*}
  };
}

macro_rules! input_inner {
  ($iter:expr) => {};
  ($iter:expr, ) => {};

  ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
    let $var = read_value!($iter, $t);
    input_inner!{$iter $($r)*}
  };
}

macro_rules! read_value {
  ($iter:expr, ( $($t:tt),* )) => {
    ( $(read_value!($iter, $t)),* )
  };

  ($iter:expr, [ $t:tt ; $len:expr ]) => {
    (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
  };

  ($iter:expr, chars) => {
    read_value!($iter, String).chars().collect::<Vec<char>>()
  };

  ($iter:expr, usize1) => {
    read_value!($iter, usize) - 1
  };

  ($iter:expr, $t:ty) => {
    $iter.next().unwrap().parse::<$t>().expect("Parse error")
  };
}

fn main() {
  input!{
    n: usize,
    k: usize,
    xs: [i32; n]
  }
  let mut ans = 1_000_000_000;
  for i in 0..n {
    if i + k - 1 < n {
      if xs[i] < 0 {
        ans = cmp::min(ans, xs[i + k - 1] - xs[i] * 2);
      } else {
        ans = cmp::min(ans, xs[i + k - 1]);
      }
    }
    if i + 1 >= k {
      if xs[i] > 0 {
        ans = cmp::min(ans, xs[i] * 2 - xs[i + 1 - k]);
      } else {
        ans = cmp::min(ans, -xs[i + 1 - k]);
      }
    }
  }
  println!("{}", ans);
}


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


int main(){
  
  int N;
  cin >> N;
  int a;
  bool denied = false;
  for(int i = 0; i <N; i++){
    cin >> a;
    if(a % 2 == 0){
      if(a % 5 != 0 && a % 3 != 0)denied = true;
    }
  }
  if(denied) cout << "DENIED" << endl;
  else cout << "APPROVED" << endl;
}

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

vector<long long int> a,b,c;

int main() {

    int n;

    cin >> n;

    a.resize(n);
    b.resize(n);
    c.resize(n);


    for(int i=0;i<n;i++){
        scanf(" %lld",&a[i]);
        a[i] = -a[i];
    }
    for(int i=0;i<n;i++){
        scanf(" %lld",&b[i]);
    }
    for(int i=0;i<n;i++){
        scanf(" %lld",&c[i]);
    }

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

    int ai=0,bi=0,ci=0;

    long long int ans = 0;

    for(int i=0; i<n; i++){

        vector<long long int>::iterator a_lower = upper_bound(a.begin(),a.end(),-b[i]);

        if(a_lower == a.end()){
            continue;
        }

        vector<long long int>::iterator c_upper = upper_bound(c.begin(),c.end(),b[i]);

        if(c_upper == c.end()){
            continue;
        }

        ans += (a.end() - a_lower) * (c.end()  - c_upper);

    }

    cout << ans << endl;



    return 0;
}

Measured memory_bytes: 2560
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define mod 1000000007
using namespace std;
typedef long long ll;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
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; }
//V,P(大文字)使用不可

const ll INF=1e18;

signed main(){
    int n;
    cin>>n;
    V<ll> a(n);
    cinf(n,a);
    ll ans=INF;
    sort(all(a));
    for(ll i=a[i];i<=a[n-1];i++){
        ll cnt=0;
        for(ll j=0;j<n;j++){
            cnt+=(i-a[j])*(i-a[j]);
        }
        chmin(ans,cnt);
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)

map<char,int> val;
map<char,function<int(int,int)>> op;

bool nextval(){
  val['R']++;
  if(val['R'] == 3){
    val['R'] = 0;
    val['Q']++;
    if(val['Q'] == 3){
      val['Q'] = 0;
      val['P']++;
      if(val['P'] == 3){
	val['P'] = 0;
	return false;
      }
    }
  }
  return true;
}

int solve(const string &s,int &i){
  if(isdigit(s[i])){
    return s[i++] - '0';
    
  }else if(val.count(s[i])){
    return val[s[i++]];
    
  }else if(s[i] == '-'){
    i++; // '-'
    return  2 - solve(s,i);

  }else if(s[i] == '('){
    i++; // '('
    int l = solve(s,i);
    char m = s[i];
    assert(op.count(m));
    i++; // '+' or '*'
    int r = solve(s,i);
    i++; // ')'
    return op[m](l,r);
  }
  assert(false);
}

int main(void){
  string s;
  val['P'] = val['Q'] = val['R'] = 0;
  op['+'] = [](int x,int y){
    if(x == 2 or y == 2) return 2;
    else if(x == 1 or y == 1) return 1;
    else return 0;
  };
  op['*'] = [](int x,int y){
    if(x == 0 or y == 0) return 0;
    else if(x == 1 or y == 1) return 1;
    else return 2;
  };
  
  while(cin >> s, s != "."){
    int res = 0;
    
    do{
      int index = 0;
      if(solve(s,index) == 2)
	res++;
    }while(nextval()); // 0,0,0で始まり0,0,0で終わる
    
    cout << res << endl;
  }
}


Measured memory_bytes: 3192
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e5+5;
vector<int> nei[maxn];
queue<int> que;
int dis[maxn],ans[maxn];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        cin>>u>>v;
        nei[u].push_back(v);
        nei[v].push_back(u);
    }
    memset(dis,0x3f,sizeof(dis));
    dis[1]=0;
    que.push(1);
    while(que.size())
    {
        int cur=que.front();
        que.pop();
        for(int i=0;i<(int)nei[cur].size();i++)
        {
            int next=nei[cur][i];
            if(dis[next]>dis[cur]+1)
            {
                dis[next]=dis[cur]+1;
                que.push(next);
            }
        }
    }
    for(int i=1;i<=n;i++)
        if(dis[i]==INF)
        {
            cout<<"No\n";
            return 0;
        }
    for(int i=2;i<=n;i++)
        for(int j=0;j<(int)nei[i].size();j++)
            if(dis[i]==dis[nei[i][j]]+1)
            {
                ans[i]=nei[i][j];
                break;
            }
    cout<<"Yes\n";
    for(int i=2;i<=n;i++)
        cout<<ans[i]<<'\n';
    return 0;
}


Measured memory_bytes: 10968
#include<bits/stdc++.h>
#define N 1000005 
#define LL long long 
using namespace std;
int A[N];
char str[N]; 
bool cmp(int a,int b){
	return a>b;
}
int main(){ 
	long long n,m,p;
	cin>>n>>m;
	p=n%m;
	cout<<min(p,m-p)<<endl;
	return 0; 
} 

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

int main(){
  int n;
  cin >> n;
  set<int> st;
  for(int i = 0 ; i < n ; i++){
    int d;
    cin >> d;
    st.insert(d);
  }
  cout << st.size() << endl;
  return 0;
}

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

a = 1
b = 0
y = 1

while y != x:
    if y > x:
        if a - b == 1:
            break
        b += 1
    else:
        a += 1
    y = a**5 - b**5

if y == x:
    print(a, b)
    exit()

num = 1
while num**5 < x:
    num += 1
num -= 1

# binary search
for a in range(num, 0, -1):
    start = 1
    stop = a
    while start < stop:
        b = (start + stop) // 2
        y = a**5 + b**5
        if y == x:
            print(a, -b)
            exit()
        elif y < x:
            start = b + 1
        else:
            stop = b - 1
    if a**5 + start**5 == x:
        print(a, -start)
        exit()

Measured memory_bytes: 9240
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000003;
inline int qp(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = 1ll * res * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return res;
}
int f[mod];
int main() {
	int q;
	scanf("%d", &q);
	f[0] = 1;
	for (int i = 1; i < mod; i++)
		f[i] = 1ll * f[i - 1] * i % mod;
	while (q--) {
		int x, d, n;
		scanf("%d%d%d", &x, &d, &n);
		if (!d) {
			printf("%d\n", qp(x, n));
			continue;
		}
		int inv = qp(d, mod - 2);
		int xd = 1ll * x * inv % mod;
		if (xd + (n - 1) >= mod) printf("0\n");
		else {
			int ans = 1ll * qp(d, n) * f[xd + (n - 1)] % mod * qp(f[xd - 1], mod - 2) % mod;
			printf("%d\n", ans);
		}
	}
	return 0;
}

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

int main(){
  int64_t a,b;
  cin >> a >> b;
  if((a<=0)&&(b>=0)){
    cout << "Zero" << endl;
  }else if(a>0){
    cout << "Positive" << endl;
  }else if(b<0){
    if((b-a)%2==0){
      cout << "Negative" << endl;
    }else{
      cout << "Positive" << endl;
    }
  }
  return 0;
}


Measured memory_bytes: 3596
ans = False
count, sum = map(int, input().split())
for x in range(count+1):
    for y in range(count-x+1):
        if sum == 10000 * x + 5000 * y + 1000 * (count-x-y) and not ans:
            print("{0} {1} {2}".format(x, y, (count-x-y)))
            ans = True
if not ans:
    print("-1 -1 -1")


Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()

int N , Q;
vector<i64> A;
i64 rs[101010];
int main() {
  cin >> N >> Q;
  A.resize(N);
  rep(i,0,N - 1) cin >> A[i];
  vector<i64> sum(101010);
  rep(i,0,N - 1) {
    if(i < 2) sum[i] = A[i];
    else sum[i] = sum[i - 2] + A[i];
  }
  for(int i = N - 1;i >= 0;i--) {
    rs[i]  = rs[i + 1] + A[i];
  }
  auto f = [&](i64 x, i64 i) {
    i64 d = A[i] - x;
    if(d < 0) return false;
    i64 r = A[i];
    i64 l = x - d;
    auto ite = lower_bound(all(A) , x);
    i64 left = ite - lower_bound(all(A) , l);
    i64 right = upper_bound(all(A) , r - 1) - ite;
    i64 xx = ite - A.begin();
    if(right + left >= N - i) return true;
    i64 j = N - right - left;
    i64 k = xx + right - 1;
    if(j - i <= i - k) return true;
    return false;
  };
  rep(i,0,Q - 1) {
    i64 x;
    cin >> x;
    i64 ok = N;
    i64 ng = -1;
    while(ok - ng > 1) {
      i64 mid = (ok + ng) / 2;
      if(f(x,mid)) {
        ok = mid;
      }
      else {
        ng = mid;
      }
    }
    i64 c = N - ok;
    i64 ans = rs[ok];
    if(N - 1 - c - c >= 0) ans += sum[N - 1 - c - c];
    cout << ans << endl;
  }
}


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

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for(int i = (int)(m); i < (int)(n); ++i)


int main()
{
    string S;
    cin >> S;

    int num_n = 0, num_s = 0, num_w = 0, num_e = 0;
    for(auto s: S){
        if(s == 'N') ++num_n;
        if(s == 'S') ++num_s;
        if(s == 'W') ++num_w;
        if(s == 'E') ++num_e;
    }
    if((num_n == 0 and num_s > 0) or (num_n > 0 and num_s == 0)
    or (num_w == 0 and num_e > 0) or (num_w > 0 and num_e == 0)) cout << "No" << endl;
    else  cout << "Yes" << endl;

    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<vector>
#include<algorithm>
const int size = 10000005;
bool sosu[size] = {false};
using namespace std;
int main(){
  vector<int> vec;
  for(int i=3;i<size;i+=2){
    if(!sosu[i]){
      if(i>=13 && !sosu[i] && !sosu[i-2] && !sosu[i-6] && !sosu[i-8]) vec.push_back(i);
      if((long long)i*i < size){
	for(int j=i*i;j<size;j+=i) sosu[j] = true;
      }
    }
  }
  int n;
  while(scanf("%d",&n),n!=0){
    printf("%d\n",*(upper_bound(vec.begin(),vec.end(),n)-1));
  }
  return(0);
}



Measured memory_bytes: 12384
use std::io;
use std::str::FromStr;
fn main() {
    let stdin = io::stdin();
    let mut s = String::new();
    stdin.read_line(&mut s).ok();
    s.trim();
    s.pop();
    let mut n : i64 = i64::from_str(&s).expect("e");
    let mut ans : Vec<i64> = Vec::new();
    let mut pow : i64 = 1;
    while n != 0 || ans.is_empty() {
        ans.push(if n % (pow * 2) == 0 {0} else {1});
        n -= pow * ans[ans.len() - 1];
        pow *= -2;
    }
    ans.reverse();
    for i in &ans {
        print!("{}",i);
    }
    println!();
}

Measured memory_bytes: 4352
N, A = tuple(map(int, input().split()))
X = list(map(lambda x: int(x) - A, input().split()))
D = {0:0}

for item in X:
  for k,v in list(D.items()):
    D[k+item]=D.get(k+item,0)+v
  D[item]=D.get(item,0)+1

print(D[0])

Measured memory_bytes: 3188
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define IN(v) int v;cin>>v;
#define OUT(v) cout<<v<<endl
const int INF = 1000000000;

int N, Q;
vvi edge;
vi parent;
vi counter;

vector<ll> dp;
ll solve(int i) {
  int j = i;
  if (dp[i] != -1)
    return dp[i];
  return dp[i] = solve(parent[i])+counter[i];
}
int main() {
  cin >> N >> Q;

  edge.resize(N, vi());
  parent.resize(N, INF);

  REP(i, N-1) {
    IN(a); IN(b);
    edge[a-1].push_back(b-1);
    edge[b-1].push_back(a-1);
  }

  counter.resize(N, 0);
  REP(i, Q) {
    IN(p); IN(x);
    counter[p-1] += x;
  }
  queue<int> q;
  q.push(0);
  vector<bool> visited(N, false);

  while (!q.empty()) {
    int p = q.front(); q.pop();
    visited[p] = true;
    for (const auto& e : edge[p]) {
      if (!visited[e]) {
        parent[e] = p;
        q.push(e);
      }
    }
  }

  vector<ll> ans;
  dp.resize(N, -1);
  dp[0] = counter[0];
  REP(i, N)
    ans.push_back(solve(i));

  REP(i, N-1)
    cout << ans[i] << " ";

  cout << ans[N-1] << endl;

  return 0;
}


Measured memory_bytes: 19952
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
#include <vector>
#include <iomanip>
#include <map>
#include <deque>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

const ll SIZE = 1e6 + 10, S2 = 1e7 * 2, MOD = 1e9 + 7, INF = 1e9 * 1e9 + 100;

ll check(ll vl, ll a, ll b) {
    /*vector<ll> vec1, vec2;
    for (int i = 1; i <= vl; i++) {
        if (i != a) vec1.push_back(i);
    }

    for (int i = 1; i <= vl; i++) {
        if (i != b) vec2.push_back(i);
    }
    if (vec1.size() < vl) vec1.push_back(vl + 1);
    if (vec2.size() < vl) vec2.push_back(vl + 1);
*/
    ll q = 0;
    ll q2 = 0, vl1 = (vl - 1) / 2, vl2 = (vl - 1) / 2;

    if (vl == 14) {
        cout << "";
    }
    if (vl % 2 != 0) {
        vl1 = (vl / 2) + 1;
        vl2 = vl1;
        if (vl1 >= a) vl1++;
        if (vl2 >= b) vl2++;
        q2 = vl1 * vl2;
    }
    else {
        vl1 = (vl - 1) / 2 + 1, vl2 = vl1 + 1;
        if (vl1 >= a) vl1++;
        if (vl2 >= b) vl2++;
        if (vl2 < b) {
            ll q = b - vl2, v1 = vl1, v2 = vl2;
            v1 -= q;
            v2 = b + 1;
            q2 = v2 * v1;
        }
        if (vl1 < a) {
            ll q = a - vl1, v1 = vl1, v2 = vl2;
            v1 = a + 1;
            v2 -= q;
            q2 = max(v2, v2 * v1);
        }
        q2 = max(q2, vl2 * vl1);
        vl1 = (vl - 1) / 2 + 1, vl2 = vl1 + 1;
        vl1--;
        //if (vl2 < b) {
        //    ll q = b - vl2, v1 = vl1, v2 = vl2;
        //    v1 -= q;
        //    v2 = b + 1;
        //    q2 = v2 * v1;
        //}
        //if (vl1 < a) {
        //    ll q = a - vl1, v1 = vl1, v2 = vl2;
        //    v1 = a + 1;
        //    v2 -= q;
        //    q2 = max(v2, v2 * v1);
        //}
        if (vl1 >= a) vl1++;
        if (vl2 >= b) vl2++;
        
        q2 = max(q2, vl2 * vl1);
    }

   /* for (int i = 0; i < vl; i++) q = max(q, vec1[i] * vec2[vl - i - 1]);
*//*
    if (q != q2) {
        cout << "sdhfb";
    }*/
    q = q2;
    return q;
}

int main() {
    fastInp;

    ll n, m;
    cin >> n >> m;

    if (n == 1) n = 3;
    if (m == 1) m = 3;
    cout << (n - 2) * (m - 2);

    return 0;
}



Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main(void){

  double v;
  while( cin >> v ){

    double t = v / 9.8;
    double y = 4.9*t*t;

    for( int N = 1 ; N <= 1000 ; N++ ){
      if( 5 * N - 5 >= y ){
	cout << N << endl;
	break;
      }
    }

  }
  return 0;
}

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

template<class T>
void sort_uniq(vector<T> &v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}

int main() {
    string s;
    cin >> s;
    int K;
    cin >> K;
    vector<string> V;
    for(int k=1; k<=K; k++) {
        for(int i=0; i+k<=s.length(); i++) {
            V.push_back(s.substr(i, k));
        }
    }
    sort_uniq(V);
    cout << V[K-1] << endl;
    return 0;
}

Measured memory_bytes: 1660
import sys
sys.setrecursionlimit(10**6)

N=int(input())
ab=[list(map(int, input().split())) for i in range(N-1)]
link=[[] for i in range(N+1)]
for i in range(0, N-1):
  link[ab[i][0]].append([ab[i][1], i])
  link[ab[i][1]].append([ab[i][0], i])
  
out=[0]*(N-1)
def f(num, prenum=0, prec=0):
  postc=1
  for postnum, linknum in link[num]:
    if postnum==prenum:
      continue
    if postc==prec:
      postc+=1
      
    out[linknum]=postc
    f(postnum, num, postc)
    postc+=1
    
f(1)

print(max(out))
for i in range(N-1):
  print(out[i])

Measured memory_bytes: 96792
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder.Abc
{
    //https://atcoder.jp/contests/abc141/tasks/abc141_a
    public class QuestionA
    {
        public static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            //// 文字列の入力
            //string s = Console.ReadLine();

            //// 整数の入力
            //long n = long.Parse(Console.ReadLine());

            //// 文字列配列の入力
            //string[] inputStrArray = Console.ReadLine().Split(' ');

            //// 整数配列の入力
            //var inputLongArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();

            var datas = Console.ReadLine().Split(' ');

            string result = "";

            if (datas[0] == datas[1]) {
                result = "=";
            } else if (int.Parse(datas[0], System.Globalization.NumberStyles.HexNumber) < int.Parse( datas[1], System.Globalization.NumberStyles.HexNumber)) {
                result = "<";
            } else {
                result = ">";
            }


            Console.WriteLine(result);

            Console.Out.Flush();
        }
    }
}


Measured memory_bytes: 11092
#ifdef loc
    #include "codes/loc_debug.h"
#else
    #include <bits/stdc++.h>
    #define pr(...)
    #define pra(a,n)
    #define praa(a,n,m)
    #define prl()
#endif
using namespace std;

typedef long long ll;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define sz(a) int(a.size())
const int N = 100005;
int a[N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    rep(i, 0, n) {
        cin >> a[i];
    }
    int ans = 0;
    rep(i, 1, n - 1) {
        vector<int> b = {a[i - 1], a[i], a[i + 1]};
        sort(b.begin(), b.end());
        if(b[1] == a[i]) {
            ans++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ac=0,wa=0,tle=0,re=0;
  string s;
  for(int i=0; i<n; i++) {
    cin >> s;
    if(s == "AC") {
      ac++;
    } else if(s == "WA") {
      wa++;
    } else if(s == "TLE") {
      tle++;
    } else if(s == "RE") {
      re++;
    }
  }
  cout << "AC x " << ac << endl;
  cout << "WA x " << wa << endl;
  cout << "TLE x " << tle << endl;
  cout << "RE x " << re << endl;
}

Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int siz=1e6+1;
struct edge{
	int v,w,nxt;
}e[siz];

int n,m;
ll ans,cnt,_cnt;
ll ccnt[3];
int col[siz],head[siz];
bool flag;
bitset<siz> vis;

inline
int posi(int x){return ((x%3)+3)%3;}

void dfs(int now);
void add_edge(int u,int v,int w);

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;i++){
		scanf("%d%d",&u,&v);
		add_edge(u,v,1);
		add_edge(v,u,-1);
	}
	for(int i=1;i<=n;i++){
		if(vis[i])
			continue;
		memset(ccnt,0,sizeof ccnt);
		_cnt=cnt=0;
		flag=false;
		dfs(i);
		if(flag)
			ans+=cnt*cnt;
		else
			if(ccnt[0]>0&&ccnt[1]>0&&ccnt[2]>0)
				ans+=ccnt[0]*ccnt[1]+ccnt[1]*ccnt[2]+ccnt[0]*ccnt[2];
			else
				ans+=_cnt;
	}
	printf("%lld\n",ans);
}

void dfs(int now)
{
	cnt++;
	vis[now]=true;
	ccnt[col[now]]++;
	for(int i=head[now];i!=0;i=e[i].nxt){
		if(e[i].w==1)
			_cnt++;
		if(!vis[e[i].v]){
			col[e[i].v]=posi(col[now]+e[i].w);
			dfs(e[i].v);
		}
		else
			if(col[e[i].v]!=posi(col[now]+e[i].w))
				flag=true;
	}
}

void add_edge(int u,int v,int w)
{
	static int cnt;
	e[++cnt]={v,w,head[u]};
	head[u]=cnt;
}

Measured memory_bytes: 14080
#include <bits/stdc++.h>
using namespace std;
 
//#define int         long long
#define endl        "\n"
#define pii         pair<int, int>
#define vi          vector<int>
#define sz(x)       (int)x.size()
#define dint        long double


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

const int mod = (int)1e9 + 7;
const int MX = 5e5+5;
int modpow(int a, int b);

inline int mul(int a, int b) {
    long long res = ((long long)a % mod * b % mod) % mod;
    return (int)res;
}

inline int add(int a, int b) {
    long long res = a + b;
    if(res >= mod) res -= mod;
    return (int)res;
}

inline int sub(int a, int b) {
    long long res = a - b;
    if(res < 0) res += mod;
    return (int)res;
}

long long pw(int a, int b) {
    long long res = 1;
    if(a < 2) return a;
    long long _a = a;
    while(b) {
        if(b % 2) res = res * _a;
        b /= 2;
        _a = _a * _a;
    }
    return res;
}

int modpow(int a, int b) {
    long long res = 1;
    if(a < 2) return a;
    while(b) {
        if(b % 2) res = mul(res, a);
        b /= 2;
        a = mul(a, a);
    }
    return res;
}

int MI(int a) {
    return modpow(a, mod - 2);
}

int fac[MX];
int ifac[MX];

void pre() {
    fac[0] = fac[1] = 1;
    ifac[0] = ifac[1] = 1;
    for(int i=2; i<MX; i++) fac[i] = mul(fac[i-1], i);
    for(int i=2; i<MX; i++) ifac[i] = MI(fac[i]);
}

long long C(int n, int k) {
    long long res = 1;
    res = mul(res, fac[n]);
    res = mul(res, ifac[k]);
    res = mul(res, ifac[n-k]);
    return res;
}

long long P(int n, int k) {
    long long res = 1;
    res = mul(res, fac[n]);
    res = mul(res, ifac[n-k]);
    return res;
}

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



void solve(int TC) { 
    int N, M; cin >> N >> M;
    int ans = 0;
    for(int i=0; i<=N; i++) {
        int cur = 1;
        cur = mul(cur, C(N, i));
        cur = mul(cur, P(M, i));
        int remPos = N - i;
        int remNum = M - i;
        cur = mul(cur, P(remNum, remPos));
        cur = mul(cur, P(remNum, remPos));
        if(i % 2) ans = sub(ans, cur);
        else ans = add(ans, cur);
    }
    cout << ans << endl;
}
 
signed main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(12);
    
    pre();

    int T = 1;
    //cin >> T;
    for(int i=1; i<=T; i++) {
        solve(i);
    }
    return 0;
 
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

void solve(int test_case) {
	int a,b,c;
	cin>>a>>b>>c;
	int m = c%b;
	set<int> s;
	for(int i=0;;i+=a) {
		int n = i%b;
		if(n==m) {
			cout<<"YES";
			return;
		}
		if(s.count(n))break;
		s.insert(n);
	}
	cout<<"NO";
}

int main() {
	
	////// FILE BASED IO////
	//freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
	///////////////
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
}	


Measured memory_bytes: 3656
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPLL(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
template<class T>inline bool chmax(T &a, const T &b){if(a < b){a = b; return 1;}return 0;}
template<class T>inline bool chmin(T &a, const T &b){if(a > b){a = b; return 1;}return 0;}
typedef long long ll;

// ABC130 B - Bounding
// 2020.06.19

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, X; cin >> N >> X;
  vector<int> L(N);
  REP(i, N){
    cin >> L[i];
  }
  int ans = 1, res = 0;
  REP(i, N){
    res += L[i];
    if(res > X){
      break;
    }
    ans++;
  }
  cout << ans << endl;
}

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

const int Maxn = 100005;

int n, c, k;
int a[Maxn];
int res;

int main()
{
    scanf("%d %d %d", &n, &c, &k);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    sort(a, a + n);
    int pnt = 0;
    while (pnt < n) {
        res++;
        int nxt = upper_bound(a, a + n, a[pnt] + k) - a;
        pnt = min(nxt, pnt + c);
    }
    printf("%d\n", res);
    return 0;
}


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

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  for (auto& e : a) cin >> e;

  vector<vector<int>> d(h, vector<int>(w, 1e9));
  queue<pair<int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        q.push({i, j});
        d[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int y = p.first + dy[i], x = p.second + dx[i];
      if (!(0 <= y && y < h && 0 <= x && x < w)) continue;
      if (d[p.first][p.second] + 1 >= d[y][x]) continue;
      d[y][x] = d[p.first][p.second] + 1;
      q.push({y, x});
    }
  }

  int r = 0;
  for (const auto& row : d) {
    for (auto e : row) {
      r = max(r, e);
    }
  }
  cout << r << endl;
}


Measured memory_bytes: 13456
#include "iostream"
#include "vector"
#include "algorithm"

long long damage(long long h) {
	if (h == 1)
		return 1;
	else
		return 1 + damage(h / 2) * 2;
}

int main(void) {
	long long h;
	std::cin >> h;
	long long ans = damage(h);
	std::cout << ans;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
typedef long long ll;
const ll longinf=1LL<<60;
const ll P=1e9+7;
using namespace std;

ll f[110000];
bool flag[110000];
ll cf[110000];
bool cflag[110000];

int l[110000];
int n=0;
char c;

void init()
{
    while (scanf("%c",&c)!=EOF)
    {
        if (c=='\n') break;
        l[n++] = (c=='1');
    }
}

ll my_pow(ll x, ll n)
{
    if (n==0) return 1;
    ll ans = my_pow(x,n/2);
    ans = (ans*ans)%P;
    if (n&1) ans=(ans*x)%P;
    return ans;
}

ll cal(ll x)
{
    if (x==0) return 1;
    if (x==1) return 3;
    if (cflag[x]==1) return cf[x];
    cflag[x]=1;
    cf[x] = (3*cal(x-1))%P;

    return cf[x];
}

ll solve(int x)
{
    if (x>=n) return 1;
    if (flag[x]) return f[x];
    flag[x]=1;
    f[x] = cal(n-x-1);
    for (int i=x+1; i<=n; ++i)
        if (l[i]==1 || i==n)
        {
            f[x] = (f[x] + 2*solve(i))%P;
            break;
        }
    return f[x];
}

int main()
{
    init();
    cout<<solve(0)<<endl;
    return 0;
}



Measured memory_bytes: 5504