akhauriyash/Code-Regression huggingface dataset:
import java.util.*;
import java.io.*;
import java.math.*;
 
 
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,-1,0,0};
    static int dy[]={0,0,1,-1};
    // static int dx[]={1,-1,0,0,1,1,-1,-1};
    // static int dy[]={0,0,1,-1,1,-1,1,-1};
    // PriorityQueue<Integer> que = new PriorityQueue<Integer>(); 
    // HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
    // ArrayList<Integer> lis = new ArrayList<Integer>();
    // ArrayList<Integer> map[]=new ArrayList[26];
    // static HashMap<Integer,Long> map = new HashMap<Integer,Long>();

    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();
        // long n=sc.nextLong();
        // char c[][] = new char[h][w];
        // long a[] = new long [n];
        // char c[]=sc.nextString().toCharArray();
        // for(int i=0; i<n; i++) {a[i]=sc.nextInt();}
        // int n=sc.nextInt();
        int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
        int d[]={a,b,c},ans=0;
        while(true){
            sort(d);
            if(d[0]==d[1]&&d[1]==d[2])break;
            ans++;
            if(d[0]==d[1]){d[0]++;d[1]++;}
            else d[0]+=2;
        }    

        out.println(ans);
        out.flush();
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
     
    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }
     
    static boolean bit(long x,int k){
        // weather k-th bit (from right) be one or zero
        return  ( 0 < ( (x>>k) & 1 )  )  ? true:false;
    }    
}

class XY {
    int x,y,d;
    XY(int x, int y, int d) {
        this.x=x;
        this.y=y;
        this.d=d;
    } 
}
 
class P implements Comparable<P>{
    int x,y;
    P(int x, int y) {
        this.x=x;
        this.y=y;
    } 
      
    public int compareTo(P p){
        return -x + p.x;//des
    } 
}
 
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 long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}

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

using namespace std;

#define ll long long
#define PI 3.14159265358979323846264338327950L

int s,w;

void solve(){

    cin >> s >> w;

    if(w >= s) cout << "unsafe" << endl;
    else cout << "safe" << endl;
}





int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();

}

Measured memory_bytes: 3604
#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;

char s[N];
int n;
int dp[N][13];
int mod = 1e9 + 7;

int go(int x, int y) {
    if(x == n) {
        return y == 5;
    }
    int& res = dp[x][y];
    if(res == -1) {
        res = 0;
        rep(i, 0, 10) {
            if(s[x] == '?' || i == (s[x] - '0')) {
                res += go(x + 1, (y * 10 + i) % 13);
                if(res >= mod) {
                    res -= mod;
                }
            }
        }
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    n = strlen(s);
    memset(dp, -1, sizeof(dp));
    cout << go(0, 0) << endl;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	string s, t;
	cin >> s >> t;

	if (!t.find(s))
		cout << "Yes" << "\n";
	else
		cout << "No" << "\n";

	return 0;
}


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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define debug3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define debugB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

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

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

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

int n;
int a[111111];

int main() {
   cin >> n;
   int four = 0, two = 0, other = 0;
   for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] % 4 == 0) {
         four++;
      } else if (a[i] % 2 == 0) {
         two++;
      } else {
         other++;
      }
   }

   vector<int> s, t;
   while (two--) {
      s.push_back(2);
      t.push_back(2);
   }

   int a = four, b = other;
   while (1) {
      if (a) {
         s.push_back(4);
         a--;
      }
      if (b) {
         s.push_back(1);
         b--;
      }
      if (!a && !b) break;
   }

   a = four, b = other;
   while (1) {
      if (b) {
         t.push_back(1);
         b--;
      }
      if (a) {
         t.push_back(4);
         a--;
      }
      if (!a && !b) break;
   }

   bool ans1 = true;
   for (int i = 1; i < n; i++) {
      if (s[i-1] * s[i] % 4 != 0) ans1 = false;
   }
   bool ans2 = true;
   for (int i = 1; i < n; i++) {
      if (t[i-1] * t[i] % 4 != 0) ans2 = false;
   }

   
   if (ans1 || ans2) {
      puts("Yes");
   } else {
      puts("No");
   }

   return 0;
}

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

using namespace std;
 
int main(){
    int n,m;
    int dx[4] = { 0 , 1 , 0 , -1 };
    int dy[4] = { 1 , 0 , -1 , 0 };
    int angle;
    string order;
    int x,y;
    int Go;
 
    while(1){
	angle = 0; x = y = 1;
	cin >> n >> m;
	if( n == 0 && m == 0 ) break;
	
	while(1){
	    cin >> order;
	    if( order == "RIGHT" ){
		angle++;
		if( angle == 4 ) angle = 0;
		continue;
	    }
	    else if( order == "LEFT" ){
		angle--;
		if( angle == -1 ) angle = 3;
		continue;
	    }
	    else if( order == "STOP" ){
		cout << x << " " << y << endl;
		break;
	    }
	    cin >> Go;
	    if( order == "FORWARD" ){
		x += dx[angle]*Go;
		y += dy[angle]*Go;
		if( x < 1 ) x = 1;
		if( y < 1 ) y = 1;
		if( x > n ) x = n;
		if( y > m ) y = m;
	    }
	    else if( order == "BACKWARD" ){
		x -= dx[angle]*Go;
		y -= dy[angle]*Go;
		if( x < 1 ) x = 1;
		if( y < 1 ) y = 1;
		if( x > n ) x = n;
		if( y > m ) y = m;
 
	    }
	}
    }
    return 0;
}

Measured memory_bytes: 3036
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template<class T> inline bool chmin(T& a, T b) { if (b < a) { 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; }

void execute();
int main() 
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);// cin と scanfの併用禁止
    cout << fixed << setprecision(15);
    execute();
    return 0;
}


void execute()
{
    ll X, K, D;
    cin >> X >> K >> D;
    ll D2 = 2 * D;

    // 0を超えることができない場合
    if(X < 0){
        X = -X;
        if(K < X / D) {cout << X - D * K << endl; return;}
    }else{
        if(K < X / D) {cout << X - D * K << endl; return;}
    }

    if(K % 2 == 1){
        // X + D + iD2
        X += D;
    }else{
        // X + iD2 
    }

    ll v = X % D2;
    if(v < 0) v += D2;
    if(D < v) v = -(v-D2);

    cout << v << endl; 
}


Measured memory_bytes: 3640
N = int(input())
A = map(int, input().split(' '))

import collections
n_frq_dict = collections.Counter(A)

ans = 0
negative = 0
for n, frq in n_frq_dict.items():
    if frq % 2 == 1:
        ans += 1
    else:
        ans += 1
        negative += 1

print(ans - negative % 2)

Measured memory_bytes: 58860
def main():
    n = int(input())
    h = [int(x) for x in input().split()]
    ans = 0
    kari =0
    for i in range(1,n):
        if h[i]<=h[i-1]:
            kari += 1
        else:
            ans = max(kari,ans)
            kari = 0
    ans = max(ans,kari)
    print(ans)
if __name__ =="__main__":
    main()

Measured memory_bytes: 63984
//
import java.math.*;
import java.util.*;
import java.io.*;
  
public class Main { 
  
    static BufferedReader in;
    static PrintWriter out = new PrintWriter(System.out);
    static String file = "../in";
    static int test = 10; // 0 for local testing, 1 for std input
    static int inf = 1_000_000, mod = 1_000_000_007;

    void swap(int[]ary, int i, int j)
    {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }
    
    String[] split() throws Exception
    {
        return in.readLine().split(" ");
    }

    int readInt() throws Exception
    {
        return Integer.valueOf(in.readLine());
    }

    long[] toLongArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    }

    int[] toIntArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    }

    String reverse(String str)
    {
        return new StringBuilder(str).reverse().toString();
    }


    long pow(long a, int pow)
    {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    }

    public static void main(String[] args) throws Exception
    {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        new Main().a();
        out.flush();
    }
    
    void a() throws Exception
    {
        String str = in.readLine();
        int n = str.length();
        boolean flag = true;
        if(n % 2 != 0) 
        {
            flag = false;
        }

        for(int i = 0; i < n; i++)
        {
            if(i % 2 == 0)
            {
                if(str.charAt(i) != 'h') flag = false;
            }
            else
            {
                if(str.charAt(i) != 'i') flag = false;
            }
        }
        if(flag) out.println("Yes");
        else out.println("No");
    }

}

Measured memory_bytes: 21204
function Main(input) {
	var N = parseInt(input.split(" ")[0]);
  var A = parseInt(input.split(" ")[1]);
  var B = parseInt(input.split(" ")[2]);
  
  while(A > 0 || B < N + 1){
        if(A + 1 != B){
           A++;
        }else{
           A--;
           if(A == 0){
              console.log("Borys");
             return;
              }
        }
    	if(B - 1 != A){
           B--;
        }else{
           B++;
          if(B == N + 1){
             console.log("Alice");
            return;
             }
        }
  }
  
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 7372
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		while(true){
			int n = sc.nextInt();
			int s = sc.nextInt();
			if(n==0 && s==0) break;
			int[] a = new int[n];
			for(int i=0;i<n;i++) a[i] = sc.nextInt();
			
			int cnt = 0;
			for(int i=0;i<n;i++){
				for(int j=i+1;j<n;j++){
					if(a[i]+a[j]>s) cnt++;
				}
			}
			
			System.out.println(cnt);
		}
	}

}

Measured memory_bytes: 64620
N = int(input())
xy = [[int(i) for i in input().split()] for j in range(N)]

def rec(v, length, memo):
    memo[v] = 1

    if -1 not in memo:
        memo[v] = -1
        return length

    length_sum = 0
    for next_v in range(N):
        if memo[next_v] == -1:
            length_sum += rec(next_v, length+((xy[v][0]-xy[next_v][0])**2 + (xy[v][1]-xy[next_v][1])**2) ** 0.5, memo)

    memo[v] = -1
    return length_sum

ans = 0
n = 1
memo = [-1 for i in range(N)]
for i in range(N):
    ans += rec(i, 0, memo)
    n *= i+1

print(ans / n)

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

using namespace std;

int main(void) {
	int n, d; cin >> n >> d;
	vector<vector<int>> x(n, vector<int>(d));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < d; j++) {
			cin >> x[i][j];
		}
	}

	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			int sum = 0;
			for (int k = 0; k < d; k++) {
				int diff = abs(x[i][k] - x[j][k]);
				sum += diff * diff;
			}

			bool flag = false;
			for (int k = 0; k <= sum; k++) {
				if (k * k == sum) {
					flag = true; break;
				}
			}
			if (flag) ans++;
		}
	}

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

Measured memory_bytes: 256
import java.io.*;
import java.math.*;
import java.util.*;
public class Main { 
 
	public static void main(String[] args) { 
		Scanner input = new Scanner(System.in);
		long N = input.nextLong();
		long P = input.nextLong(); //Product
		long maxGCD = 1;
		for (long prime = 2; prime <= Math.sqrt(P); prime++) {
			long numCurPrime = 0;
			while (P%prime==0) {
				P/=prime;
				numCurPrime++;
				if (numCurPrime%N==0) {
					maxGCD*=prime;
					numCurPrime=0; //Effectively floor division using simulation counters
				}
			}
		}
		if (P>1&&N==1) maxGCD*=P; //If P remains > 1, it is guaranteed to be prime
		//Only way a prime number increases GCD in this case is if array only contains 1 number
		//(i.e N==1)
		System.out.println(maxGCD);
	}
}

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



typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector< vector<ll> > vvint;
typedef pair<ll, ll> pint;


#define rep(i,n) for(ll i=0;i<n;i++)



void print(const vint& v)
{
  for(int i=0; i+1<v.size(); ++i){
    std::cout << v[i] << " ";
  }
  cout<<v.back()<<endl;
}


struct Card
{
  int value;
  char suite;

  Card(int value, char suite){
    this->value = value;
    this->suite = suite;
  }

  string toStr(){
    return this->suite + to_string(this->value);
  }

  bool operator ==(Card &obj){
    return this->value == obj.value && this->suite == obj.suite;
  }
  
};

void bubbleSort(vector<Card> &C){
  ll n = C.size();

  rep(i, n){
    for(ll j=n-1; j>=i+1; j--){
      if(C[j].value < C[j-1].value) swap(C[j], C[j-1]);
    }
  }
}

void selectionSort(vector<Card> &C){
  ll n = C.size();
  rep(i, n){
    ll minj = i;
    for(ll j=i; j<n; ++j){
      if(C[j].value < C[minj].value) minj = j;
    }
    swap(C[i], C[minj]);
  }
}



int main() {


  cout<<fixed<<setprecision(10);

  ll n; cin>>n;
  vector<Card> c1, c2;

  rep(i, n){
    string s;
    cin>>s;
    c1.emplace_back(s[1]-'0', s[0]);
    c2.emplace_back(s[1]-'0', s[0]);
  }

  bubbleSort(c1);
  selectionSort(c2);

  rep(i, n-1){
    cout<<c1[i].toStr()<<" ";
  }
  cout<<c1.back().toStr()<<endl;
  cout<<"Stable"<<endl;

  bool f = true;
  rep(i, n-1){
    cout<<c2[i].toStr()<<" ";
    f = f && (c1[i] == c2[i]);
  }
  cout<<c2.back().toStr()<<endl;
  if(f) cout<<"Stable"<<endl;
  else cout<<"Not stable"<<endl;


}





//




Measured memory_bytes: 3228
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<pair<ll,ll>> vep;
const ll mod=1000000007;
const ll INF=1e15;

int main(){
    int N;cin>>N;
    ll MaxL=-1,MinR=INF,MaxD=0;
    vep d;

    for(int i=0;i<N;i++){
        ll L,R; cin>>L>>R;
        R++;
        d.push_back(make_pair(L,R));
        MaxL=max(L,MaxL);
        MinR=min(R,MinR);
        MaxD=max(MaxD,R-L);
    }

    ll ans=MaxD;
    if(MinR>MaxL) ans+=(MinR-MaxL);
    //cout<<MaxL<<' '<<MinR<<' '<<ans<<endl;
    vep s;
    for(int i=0;i<N;i++){
        ll a=0,b=0;
        a=max(a,d.at(i).second-MaxL);
        b=max(b,MinR-d.at(i).first);
        s.push_back(make_pair(a,b));
    }

   

    //for(int i=0;i<N;i++) cout<<d.at(i).first<<' '<<d.at(i).second<<endl;
    //for(int i=0;i<N;i++) cout<<s.at(i).first<<' '<<s.at(i).second<<endl;

    sort(s.begin(),s.end());
    ll minb=s.at(0).second;
    for(int i=0;i<N-1;i++){
        minb=min(minb,s.at(i).second);
        ans=max(ans,minb+s.at(i+1).first);
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 4848
#include<cstdio>
#include<cstring>
int n,m,f[3005][3005],top,lst[3005][3005];
char ans[3005],s[3005],t[3005];
int main()
{
	scanf("%s%s",s+1,t+1);n=strlen(s+1),m=strlen(t+1);
	for(int i=1;i<=n;i++)
	 for(int j=1;j<=m;j++)
	 {
	 	if(f[i-1][j]>f[i][j-1])f[i][j]=f[i-1][j],lst[i][j]=0;
	 	else f[i][j]=f[i][j-1],lst[i][j]=1;
	 	if(s[i]==t[j]&&f[i-1][j-1]+1>f[i][j])
	 	 f[i][j]=f[i-1][j-1]+1,lst[i][j]=2;
	 }
	for(int i=n,j=m;i&&j;)
	 if(!lst[i][j])i--;else
	 if(lst[i][j]==1)j--;else
	 ans[++top]=s[i],i--,j--;
	while(top)printf("%c",ans[top--]);
}

Measured memory_bytes: 70656
#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(aa, (v).size()) { cout << v[aa]; if (aa != 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 t;
	while (cin >> t, t) {
		vi v(t);
		REP(i, t)cin >> v[i];
		int n; cin >> n;
		vi d(100,INF);
		REP(i, n) {
			int a, b;
			cin >> a >> b;
			if(d[a-1]==INF)d[a - 1] = b;
			else d[a - 1] = min(b, d[a - 1]);
		}
		int p = 100, cnt = 0;
		while (p != 0) {
			REP(i, p) {
				int flag = 1;
				FOR(j, i, p) {
					if (v[(j-i)%t] > d[j])flag = 0;
				}
				if (flag) {
					p = i;
					break;
				}
			}

			cnt++;
		}
		cout << cnt-1 << endl;
	}
}

Measured memory_bytes: 3108
n=int(input())
a=list(map(int,input().split()))
s=[0]*n
s[0]=a[0]
for i in range(1,n):
  s[i]=s[i-1]+a[i]
m=s[n-1]
d=10**15
for i in range(n-1):
  d=min(d,abs(m-(2*s[i])))
print(d)

Measured memory_bytes: 24832
#include <iostream>
#include <vector>

using namespace std;

vector<int> minMax(const vector<int>& v){
	vector<int> minMax;
	minMax.assign(2,v[0]);
	for(int i = 1; i < v.size();i++){
		if(minMax[0] > v[i]){
			minMax[0] = v[i];
		}
		if(minMax[1] < v[i]){
			minMax[1] = v[i];
		}
	}
	return minMax;
}


int main(){
	vector<int> v,tmp,_minMax;
	int n, m, a ,end;
	cin >> n >> m;
	for(unsigned int i = 0; i < n; i++){
		cin >> a;
		v.push_back(a);
	}
	int index = 0;
	int sum  = 0;
	do{
		end = index + m;
		if(end <= n){
			for(unsigned int i = index; i < end; i++){
				tmp.push_back(v[i]);
			}
		}else{
			end = end - n;
			for(unsigned int i = index; i < n; i++){
				tmp.push_back(v[i]);
			}
			for(unsigned int i = 0; i < end; i++){
				tmp.push_back(v[i]);
			}
		}
		_minMax = minMax(tmp);
		sum += (_minMax[1] - _minMax[0]);
		tmp.clear();
		index = end;
	}while(end != n);
	cout << sum << endl;
}

Measured memory_bytes: 3132
package main

import (
	"fmt"
)

func main() {
	n, q := 0, 0
	_, _ = fmt.Scan(&n, &q)
	var str string
	_, _ = fmt.Scan(&str)
	arr := make([]int, 0)
	current := 0
	a := false
	for _, s := range str {
		tmp := string(s)
		if tmp == "A" {
			arr = append(arr, current)
			a = true
		} else if a && tmp == "C" {
			current++
			arr = append(arr, current)
			a = false
		} else {
			arr = append(arr, current)
			a = false
		}
	}
	for i := 0; i < q; i++ {
		l, r := 0, 0
		_, _ = fmt.Scan(&l, &r)
		fmt.Println(arr[r - 1] - arr[l - 1])
	}
}

Measured memory_bytes: 7684
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, m, n) for (int i = m; i < n; ++i)
const int MOD = 1000000007;

int main() {
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  int dx = x2 - x1;
  int dy = y2 - y1;
  cout << x2 - dy << " ";
  cout << y2 + dx << " ";
  cout << x1 - dy << " ";
  cout << y1 + dx << endl;
  return 0;
}


Measured memory_bytes: 256
import math
N,K = map(int,input().split())
A = list(map(int,input().split()))

def cut(c):
    t = 0
    for a in A:
        if a<=c:
            continue
        t += math.ceil(a/c) - 1
    return t<=K

l=0
r=10**9
while((l+1)<r):
    c = (l+r)//2
    if cut(c):
        r = c
    else:
        l = c

print(r)

Measured memory_bytes: 31072
import itertools
n=int(input())
m=list("MARCH")
d=[0]*5
for i in range(n):
  s=input()
  for j in range(len(m)):
    if s[0]==m[j]:
      d[j]+=1
ans=0
for i in list(itertools.combinations(list(range(5)), 3)):
  k=list(i)
  ans+=d[k[0]]*d[k[1]]*d[k[2]]
print(ans)

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

int main(){
  int a[3][3];
  rep(i,3)rep(j,3)cin >> a[i][j];
  int n;
  cin >> n;
  rep(i,n){
    int b;
    cin >> b;
    rep(j,3)rep(k,3)if(a[j][k]==b)a[j][k]=0;
  }
  int flag = 0;
  rep(i,3)if(a[i][1]==a[i][0]&&a[i][2]==a[i][1])flag=1;
  rep(i,3)if(a[1][i]==a[0][i]&&a[2][i]==a[1][i])flag=1;
  if(a[0][0]==a[1][1]&&a[1][1]==a[2][2])flag = 1;
  if(a[2][0]==a[1][1]&&a[1][1]==a[0][2])flag=1;
  if(flag)cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep2(i,a,n) for(int i = (a);i < (n);i++)
#define rep(i,n) rep2(i,0,n)
#define INF (1LL << 60)

int main(void){
    int n;cin >> n;
    vector<int> f(n+1,0);
    
    for(int x = 1;x < 101;x++){
        for(int y = 1;y < 101;y++){
            for(int z = 1;z < 101;z++){
                int alina = x*x+y*y+z*z+x*y+y*z+z*x;
                
                if(alina > n)continue;
                f[alina]++;
            }
        }
    }
    
    rep(i,n)cout << f[i+1] << endl;
}

Measured memory_bytes: 3548
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        while(true) {
            int c = Math.max(a, b);
            int d = Math.min(a, b);
            a = c % d;
            b = d;
            if (a == 0) {
                System.out.println(d);
                break;
            }
        }
    }
}


Measured memory_bytes: 26204
#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;
}
int main(){
  string S;
  cin>>S;
  FOR(i,S.size()){
    S[i]='x';
  }
  cout<<S<<endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
vector<ll> divisor(ll n) {
    vector<ll> ret;
    for(ll i = 1; i * i <= n; i++) {
        if(n % i == 0) {
            ret.push_back(i);
            if(i * i != n)
                ret.push_back(n / i);
        }
    }
    return (ret);
}
int main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> mp(1000001, 0);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        mp[a[i]]++;
    }
    int ans = 0;
    for(int i = 0; i < n; i++) {
        if(mp[a[i]] >= 2) {
            continue;
        }
        auto tmp = divisor(a[i]);
        int flag = 0;
        for(auto e : tmp) {
            if(e != a[i] && mp[e]) {
                flag = 1;
                break;
            }
        }
        ans += (1 - flag);
    }
    cout << ans << endl;
}

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

int main()
{
	std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    long test=1;
// 	cin>>test;	
	while(test--)
	{
	    long a,b=0,c=0,k,i,n,j,temp,mini,maxi,ans,q,sum,x,y,count=0,next,prev;
	    string sa,sb,sc;
	    cin>>sa>>sb>>sc;
	    char win='a';
	    while(1)
	    {
	        if(win=='a')
	        {
	            if(sa.length()==0)
	            {
	                cout<<"A";
	                return 0;
	            }
	            win=sa[0];
	            //cout<<win;
	            sa=sa.substr(1,sa.length()-1);
	        }
	        else if(win=='b')
	        {
	            if(sb.length()==0)
	            {
	                cout<<"B";
	                return 0;
	            }
	            win=sb[0];
	   //         cout<<win;
	            sb=sb.substr(1,sb.length()-1);
	        }
	        else if(win=='c')
	        {
	            if(sc.length()==0)
	            {
	                cout<<"C";
	                return 0;
	            }
	            win=sc[0];
	           // cout<<win;
	            sc=sc.substr(1,sc.length()-1);
	        }
	    }
	    if(sa.length()==0)cout<<"A";
	    else if(sb.length()==0)cout<<"B";
	    else if(sc.length()==0)cout<<"C";
	    
	
    }
    
}


Measured memory_bytes: 256
//   _/                _/                 _/_/_/   _/
//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
// _/     _/    _/   _/     _/    _/  _/_/_/     _/
//_/     _/    _/   _/     _/    _/  _/    _/   _/
// _/_/   _/_/       _/_/   _/_/      _/_/     _/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=1e9;
const ll INF=5e18;
typedef pair<int,int> P;
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  ll a,b,x;
  cin >> a >> b >> x;
  if(a==0) {
    cout << b/x+1 << endl;
  }
  else {
    cout << b/x-(a-1)/x << endl;
  }
}



Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<queue>
#include<map>
#include<set>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

int n,ans;
string s;

map<string,bool> mp;

int main()
{
	n=read();
	while(n--)
	{
		cin>>s;
		if(!mp[s])
		{
			ans++;
			mp[s]=1;
		}
	}
	printf("%d",ans);
	return 0;
}

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

namespace C
{
    class Program
    {
        const long r = 1000000007;
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            int N = int.Parse(line);

            // Ai = 0なるiが存在する
            // Ai = 9なるiが存在する
            // 全体→10^N
            // 0が存在しない→9 ^ N
            // 9が存在しない→9 ^ N
            // 0も9も存在しない→8 ^ n
            // 0か9が存在しない→9 ^ N + 9 ^ N - 8 ^ N
            // 0か9が存在しないパターンを除く→10^N - 9^N - 9^N - 8^N
            long sum = powmod(10, N) - powmod(9, N) - powmod(9, N) + powmod(8, N);
            sum %= r;
            if (sum < 0 )
            {
                sum += r;
                sum %= r;
            }

            Console.WriteLine(sum);
        }

        static long powmod(long x, long y)
        {
            long result = 1;
            for(long i = 0; i < y; i++)
            {
                result *= x;
                result = result % r;
            }
            return result;
        }
    }
}


Measured memory_bytes: 27384
#include <iostream>
#include <algorithm>
#include <numeric>
#include <utility>
#include <map>
#include <vector>
#include <climits>
#include <cmath>
#include <stdlib.h>

#define max_n 100001

using namespace std;


int main()
{
    int d, t, s; cin >> d >> t >> s;
    cout << (((float) d / s <= t) ? "Yes" : "No") << endl;
    return 0;
}

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

typedef long long ll;
typedef pair<int, int> P;

#define frep(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n) frep(i, 0, (n))
#define rrep(i, n) for(int i = (n - 1); i >= 0; i--)
#define all(i, x) for(typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define pb push_back

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MAX = 100010;
const int INF = 1e9;
const int MOD = 1000000007;
const string END = "\n";

void solve(){
	ll n; cin >> n;
	ll x, y; cin >> x >> y;
	ll a[n];
	rep(i, n) cin >> a[i];
	if(n == 1){
		cout << abs(y - a[n-1]) << END;
	}else{
		cout << max(abs(y - a[n-1]), abs(a[n-2] - a[n-1])) << END;
	}
}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	solve();
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <bitset>
#define range(i, r) for(int i=0;i<r;i++)
#define ranges(i, l, r) for(int i=l;i<r;i++)
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int> >
#define vvl std::vector<std::vector<ll> >
#define MODs 1000000007;
#define MODn 1000000009;
typedef long long int ll;
using namespace std;

int main(int argc, char const *argv[]) {
  int N, tmp=-1, ul = 0, ans=1;
  std::cin >> N;
  std::vector<int> a(N);
  range(i, N) std::cin >> a[i];
  range(i, N){
    if(ul==1 && tmp>a[i]){
      ans++, tmp=-1, ul=0;
    }else if(ul==-1 && tmp<a[i]){
      ans++, tmp=-1, ul=0;
    }
    if(tmp==-1){
      tmp = a[i];
    }else if(ul==0 && tmp!=a[i]){
      ul = (tmp<a[i]?1:-1);
    }
    tmp=a[i];
  }
  std::cout << ans << '\n';
  return 0;
}


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

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

struct Edge {
    int from, to;
    cost_t cost;
    Edge(int from, int to, cost_t cost) : from(from), to(to), cost(cost) {}
};

using Edges = vector<Edge>;
using Graph = vector<Edges>;

int scc(Graph &g,vector<int>& cmp){
    int n=g.size();
    vector<int> used(n);
    vector<int> ord(n,-1);
    int k=0;
    function<void(int)> dfs=[&](int v){
        if(used[v]) return;
        used[v]=true;
        for(auto& e:g[v]){
            dfs(e.to);
        }
        ord[k++]=v;
    };
    for(int i=0;i<n;i++){
        if(!used[i]) dfs(i);
    }
    vector<vector<int>> rg(n);
    for(int i=0;i<n;i++){
        for(auto& e:g[i]) rg[e.to].push_back(i);
    }
    k=0;
    cmp.assign(n,-1);
    function<void(int)> rdfs=[&](int v){
        if(cmp[v]!=-1) return;
        cmp[v]=k;
        for(auto to:rg[v]) rdfs(to);
    };
    for(int i=n-1;i>=0;i--){
        if(cmp[ord[i]]==-1) rdfs(ord[i]),k++;
    }
    return k;
}

int main(){
    int V,E;
    cin>>V>>E;
    Graph g(V);
    for(int i=0;i<E;i++){
        int s,t;
        cin>>s>>t;
        g[s].push_back(Edge(s,t,0));
    }
    vector<int> cmp;
    int k=scc(g,cmp);
    assert(k==(*max_element(cmp.begin(),cmp.end())+1));
    int Q;
    cin>>Q;
    for(int i=0;i<Q;i++){
        int u,v;
        cin>>u>>v;
        cout<<(cmp[u]==cmp[v])<<endl;
    }
    return 0;
}



Measured memory_bytes: 4700
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vvp = vector<vector<pair<int,int>>>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}




int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    vi p(N);
    in(N,p);
    vi A(N),B(N);
    A[0] = 1;
    B[N-1] = 1;
    reps(i,1,N){
        A[i] = A[i-1] + 30000;
        B[N-i-1] = B[N-i] + 30000;
    }
    
    rep(i,N) B[p[i]-1] += i;
    
    rep(i,N-1) cout << A[i] << " ";;
    cout << A[N-1] << endl;
    
    rep(i,N-1) cout << B[i] << " ";;
    cout << B[N-1] << endl;
    
    

    return 0;
}


Measured memory_bytes: 896
#include <bits/stdc++.h>
#define cout16 cout << setprecision(16) 
#define rep(i,n) for(int i=0;i<n;i++ )
#define rep2(i,f,n) for(int i=f;i<n;i++ )
#define SORT(A) sort(A.begin(),A.end())
#define REV(A) reverse(A.begin(),A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int,int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second

int main(void) {
	int h,w; cin >> h >> w;
	vvi c(10,vi(10));
	rep(i,10)rep(j,10) cin >> c[i][j];
	rep(k,10)rep(i,10)rep(j,10) c[i][j] = min(c[i][j],c[i][k]+c[k][j]);
	ll ans = 0;
	rep(i,h)rep(j,w) {
		int a; cin >> a;
		if(a==-1) continue;
		ans += c[a][1];
	}
	cout << ans << endl;
}


Measured memory_bytes: 256
import java.io.IOException;
import java.util.NoSuchElementException;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        try (
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in))) {
            FastScanner fs = new FastScanner();

            final int N = fs.nextInt();
            final int M = fs.nextInt();

            int[] x = new int[N];
            Arrays.fill(x, 0);

            for (int i = 0; i < M; i++) {
                // 0-indexed
                int a = fs.nextInt() - 1;
                int b = fs.nextInt() - 1;

                x[a]++;
                x[b]++;
            }

            for (int i = 0; i < N; i++) System.out.println(x[i]);
        }
    }
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() {
        if (hasNextByte()) return buffer[ptr++];
        else return -1;
    }
    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }
    public boolean hasNext() {
        while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
        return hasNextByte();
    }
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if(b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
            throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }
}


Measured memory_bytes: 21076
#![allow(unused_imports)]

//input
use proconio::input;
use proconio::marker::{Bytes, Chars};
//use std::io::Read;

//collections
//use std::collections::BTreeMap;
//use std::collections::BTreeSet;
//use std::collections::HashMap;
//use std::collections::HashSet;
//use std::collections::VecDeque;
//use std::collections::BinaryHeap;

//use std::cmp::*;

fn main() {
    input! {
        k: usize,
        s: Chars,
    }

    let n = s.len();

    if n <= k {
        for i in 0..n {
            print!("{}", s[i]);
        }
        println!();
    } else {
        for i in 0..k {
            print!("{}", s[i]);
        }
        println!("...");
    }
}


Measured memory_bytes: 2168
#include <stdio.h>
#include <stdlib.h>

#define MAX_N 101000
#define SWAP( a, b ) do { int tmp; tmp = a; a = b; b = tmp; } while( 0 )

int isPrime[ MAX_N + 1 ] = { 0, 0, 1 };

void insertionSort( int *, int );
void quickSort( int *, int );
void quickSort_body( int *, int, int );
int median( int, int, int );

int main( void ) {
	int i, j, N, P, cnt, p[ 22 ], sum[ 253 ];

	for ( i = 3; i <= MAX_N; i += 2 )
		isPrime[ i ] = 1;

	for ( i = 3; i * i <= MAX_N; i += 2 )
		if ( isPrime[ i ] )
			for ( j = i * 2; j <= MAX_N; j += i )
				isPrime[ j ] = 0;

	for ( ; scanf( "%d %d", &N, &P ), N != -1; printf( "%d\n", sum[ P - 1 ] ) ) {
		cnt = 0;
		for ( i = N + 1; cnt < 22; i++ )
			if ( isPrime[ i ] )
				p[ cnt++ ] = i;

		cnt = 0;
		for ( i = 0; i < 22; i++ )
			for ( j = i; j < 22; j++ )
				sum[ cnt++ ] = p[ i ] + p[ j ];

		quickSort( sum, 200 );
	}

	return 0;
}

void insertionSort( int *data, int n ) {
	int i, j, tmp;

	for ( i = 1; i < n; i++ ) {
		tmp = data[ i ];

		if ( data[ i - 1 ] > tmp ) {
			j = i;
			do {
				data[ j ] = data[ j - 1 ];

				j--;
			} while ( j && data[ j - 1 ] > tmp );

			data[ j ] = tmp;
		}
	}
}

void quickSort( int *data, int n ) {
	quickSort_body( data, 0, n - 1 );
}

void quickSort_body( int *data, int l, int r ) {
	if ( l < r ) {
		int i = l, j = r, pivot = median( data[ i ], data[ ( i + j ) / 2 ], data[ j ] );

		while ( 1 ) {
			for ( ; data[ i ] < pivot; i++ ) ;
			for ( ; pivot < data [ j ]; j-- ) ;
			if ( i >= j )
				break;

			SWAP( data[ i ], data[ j ] );

			i++;
			j--;
		}

		if ( i - l < 20 )
			insertionSort( data + l, i - l );
		else
			quickSort_body( data, l, i - 1 );

		if ( r - j < 20 )
			insertionSort( data + j + 1, r - j );
		else
			quickSort_body( data, j + 1, r );
	}
}

int median( int x, int y, int z ) {
	if ( x < y )
		if ( y < z )
			return y;
		else if ( z < x )
			return x;
		else
			return z;
	else if ( z < y )
		return y;
	else if ( x < z )
		return x;
	else
		return z;
}

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

#define int long long
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  dp[i][j]
    白をi個 黒をj個
    そろえる操作回数
}*/

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

  vector<char> c(2*n);
  vi a(2*n);
  rep(i, 2*n){
    cin >> c[i] >> a[i];
    a[i]--;
  }

  vvi b(n, vi(n+1)), w(n, vi(n+1));
  vi bn(n), wn(n);
  rep(i, 2*n){
    vi bs(n+1), ws(n+1);
    for(int j = n; j > 0; j--){
      bs[j-1] = bs[j] + bn[j-1];
      ws[j-1] = ws[j] + wn[j-1];
    }

    for(int j = n+1; j > 0; j--){
      if(c[i] == 'B') b[a[i]][j-1] = ws[j-1] + bs[a[i]+1];
      if(c[i] == 'W') w[a[i]][j-1] = ws[a[i]+1] + bs[j-1];
    }

    if(c[i] == 'B') bn[a[i]]++;
    else wn[a[i]]++;
  }

  vvi dp(n+1, vi(n+1, inf));
  dp[0][0] = 0;
  rep(i, n+1) rep(j, n+1){
    if(i < n) dp[i+1][j] = min(dp[i+1][j], dp[i][j]+b[i][j]);
    if(j < n) dp[i][j+1] = min(dp[i][j+1], dp[i][j]+w[j][i]);
  }

  cout << dp[n][n] << endl;

  return 0;
}


Measured memory_bytes: 94336
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	
	int n; cin >> n;
	vi b(n), ans;
	rep(i, n) cin >> b[i];
	
	rep(i, n){
		for(int j = b.size() - 1; j >= 0; j--) if(b[j] == j + 1){
			ans.pb(j + 1);
			b.erase(b.begin() + j);
			goto NEXT;
		}
		cout << "-1" << endl;
		return 0;
		NEXT:;
	}
	reverse(all(ans));
	for(int i : ans) cout << i << endl;
	return 0;
}

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

using namespace std;

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(i, a) for (auto &i: (a))
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef long double ld;

void solve();

int32_t main () {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(20);
	#ifdef LOCAL
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int t = 1;
	// cin >> t;
	while (t--) { 
		solve();
	}
	return 0;
}
const int N = 1e5 + 55;
vector<int> f[N], b[N];
int p[N], sz[N];
int get (int a) {
	if (p[a] == a)
		return a;
	return p[a] = get(p[a]);
}
void merge (int a, int b) {
	a = get(a);
	b = get(b);
	if (a == b)
		return;
	if (sz[a] < sz[b])
		swap(a, b);
	p[b] = a;
	sz[a] += sz[b];
}
int n, m, k;
int ans[N];
void solve() {
	cin >> n >> m >> k;
	rep(i, 1, n + 1)
		p[i] = i, sz[i] = 1;
	rep(i, 0, m) {
		int a, b;
		cin >> a >> b;
		f[a].push_back(b);
		f[b].push_back(a);
		merge(a, b);
	}
	rep(i, 0, k) {
		int a, bb;
		cin >> a >> bb;
		b[a].push_back(bb);
		b[bb].push_back(a);
	}
	rep(i, 1, n + 1) {
		ans[i] = sz[get(i)] - (int)f[i].size() - 1;
		for (auto &j: b[i]) {
			if (get(j) == get(i))
				--ans[i];
		}
	}
	rep(i, 1, n + 1) 
		cout << ans[i] << ' ';
}	
	

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

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define repr(i,n) for(int i=(int)(n-1);i>=0;i--)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout << #x" = " << x << endl
#define print(x) cout << x << endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX/3
#define MAX_N 1000000001


int main(){
  cin.sync_with_stdio(false);
  int n, m, s, g1, g2;
  while(cin >> n >> m >> s >> g1 >> g2, n) {
    s--, g1--, g2--;
    int cost[n][n];
    rep(i, n)rep(j, n) cost[i][j] = (i == j) ? 0 : INF;
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c; a--, b--;
      cost[a][b] = c;
    }

    rep(i, n)rep(j, n)rep(k, n) minch(cost[j][k], cost[j][i] + cost[i][k]);
    int mini = INF;
    rep(i, n) minch(mini, cost[s][i] + cost[i][g1] + cost[i][g2]);
    cout << mini << endl;
  }

  return 0;
}

Measured memory_bytes: 3024