akhauriyash/Code-Regression huggingface dataset:
// template version 1.12
using namespace std;
#include <iostream>
#include <bits/stdc++.h>

// varibable settings
#define infile "../test/sample-1.in"
#define int long long //{{{
const int INF=1e18;
const int MOD=1e9+7; //}}}

// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)((b)-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define each(i,a) for (auto&& i : a)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ub upper_bound
#define lb lower_bound
#define posl(A, x) (lower_bound(all(A), x)-A.begin())
#define posu(A, x) (upper_bound(all(A),x)-A.begin())
template<class T> inline void chmax(T &a, const T &b) { if((a) < (b)) (a) = (b); }
template<class T> inline void chmin(T &a, const T &b) { if((a) > (b)) (a) = (b); }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;

template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

#if defined(PCM) || defined(LOCAL)
  #include "lib/dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif
//}}}

int solve(){
  int x,a;cin>>x>>a;
  if (x<a){
    cout << 0 << endl;
  }
  else{
    cout << 10 << endl;
  }
  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}


Measured memory_bytes: 256
def reverse_append(nums):
    ans = [0] * len(nums)
    left = len(nums) % 2 == 1
    right_i = (len(nums)+1) // 2
    left_i = right_i - 1
    for i in range(len(nums)):
        if left:
            left = not left
            ans[left_i] = nums[i]
            left_i -= 1
        else:
            left = not left
            ans[right_i] = nums[i]
            right_i += 1
    return ans

if __name__ == "__main__":
    raw_input()
    nums = [int(i) for i in raw_input().split()]
    print(" ".join([str(num) for num in reverse_append(nums)]))

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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
	ll to;
 	ll cost;
 };
using Graph = vector<vector<Edge>>;

const int MAX = 10000000;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/****** define constant end ********/

/****** define variable start ******/
ll a, b, c, d;
/******* define variable end *******/
int solve() {
	cin >> a >> b >> c >> d;
	cout << max({a * c, a * d, b * c, b * d}) << endl;
	return 0;
}

int main() {
	// clock_t start = clock();
	cout << std::fixed << std::setprecision(10);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int end_solve = solve();
	/*
	clock_t end = clock();
	const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
	1000.0; printf("time %lf[ms]\n", time);
	*/

	return end_solve;
}


Measured memory_bytes: 3644
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main{
	Scanner sc=new Scanner(System.in);

	int INF=1<<28;
	double EPS=1e-12;

	int n;
	int[] a;

	void run(){
		n=sc.nextInt();
		a=new int[n];
		int n0=0, n1=0;
		for(int i=0; i<n; i++){
			a[i]=sc.nextInt();
			if(a[i]==0){
				n0++;
			}else if(a[i]==1){
				n1++;
			}
		}
		sort(a);
		int m=n-(n0+n1);
		int[] b=new int[m];
		for(int i=0; i<m; i++){
			b[i]=a[i+n0+n1];
		}

		if(n1==0&&n0%2==1){

			if(m>=3&&b[m-2]==2&&b[m-1]==3){
				b[m-2]=3;
				b[m-1]=2;
			}

			for(int i=1; i<m; i++){
				println(b[i]+"");
			}

			for(int i=0; i<n0-1; i+=2){
				println("0");
				println("0");
			}
			if(m>0){
				println(b[0]+"");
			}
			println("0");

		}else{

			if(m>=2&&b[m-2]==2&&b[m-1]==3){
				b[m-2]=3;
				b[m-1]=2;
			}

			for(int i=0; i<m; i++){
				println(b[i]+"");
			}

			// debug(n0,n1);

			for(int i=0; i<n0-1; i+=2){
				println("0");
				println("0");
			}
			if(n1>0){
				println("1");
			}
			if(n0%2==1){
				println("0");
			}
			for(int i=0; i<n1-1; i++){
				println("1");
			}
		}
	}

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

	void print(String s){
		System.out.print(s);
	}

	void println(String s){
		System.out.println(s);
	}

	public static void main(String[] args){
		// System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
		new Main().run();
	}
}

Measured memory_bytes: 12000
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
#include<functional>
#include<stack>
#include<tuple>
#include<cassert>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

int main() {
  int X;
  cin >> X;
  int temp = X / 200;
  cout << 10 - temp  << endl;
  return 0;
}

Measured memory_bytes: 3632
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
int main(){
    ll n,k;
    cin>>n>>k;
    int cnt=0;
    while(n){
       n/=k;
       cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}

Measured memory_bytes: 888
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

void yes() {
  cout << "Yes" << endl;
  exit(0);
}

void no() {
  cout << "No" << endl;
  exit(0);
}

int main() {
  vector<vector<ll>> c(3, vector<ll>(3));
  REP(i, 3) {
    REP(j, 3) { cin >> c[i][j]; }
  }
  bool ok = true;

  REP(j, 2) {
    set<ll> st;
    REP(i, 3) { st.insert(c[i][j + 1] - c[i][j]); }
    if (SZ(st) != 1) {
      no();
    }
  }

  REP(i, 2) {
    set<ll> st;
    REP(j, 3) { st.insert(c[i + 1][j] - c[i][j]); }
    if (SZ(st) != 1) {
      no();
    }
  }

  yes();
}

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

#define int long long

int N;
vector<int> G[10000];
vector<int> c;
vector<int> ans;
int now;

void dfs(int v, int u) {  
  for ( int i : G[v] ) {
    if ( i == u ) continue;
    ans[i] = c[--now];
    dfs(i, v);    
  }
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N;
  for ( int i = 0; i < N-1; i++ ) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  c = ans = vector<int>(N);
  int sum = 0;
  for ( int i = 0; i < N; i++ ) {
    cin >> c[i];
    sum += c[i];    
  }

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

  int reef = 0;
  for ( int i = 0; i < N; i++ ) {
    if ( G[i].size() == 1 ) reef = i;    
  }
  
  ans[reef] = c[N-1];
  now = N-1;

  dfs(reef, -1);

  cout << sum-c[N-1] << endl;;
  for ( int i = 0; i < N; i++ ) {
    cout << ans[i] << " ";
  }
  cout << endl;
  
  return 0;
}


Measured memory_bytes: 1536
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		// 整数の入力
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long d = sc.nextLong();

		long cd = lcm(c,d);

		// 出力
		System.out.println(b-a+1-
                           ((b/c-(a%c==0?a/c-1:a/c)+
                            (b/d-(a%d==0?a/d-1:a/d)-
                            (b/cd-(a%cd==0?a/cd-1:a/cd))))));
	}

	private static long lcm(long m, long n) {
		return m * n / gcd(m, n);
	}

	private static long gcd(long m, long n) {
		if(m < n) return gcd(n, m);
		if(n == 0) return m;
		return gcd(n, m % n);
	}
}

Measured memory_bytes: 22996
#include <iostream>
//解答解法
using namespace std;
int main(){
    int N;
    cin >> N;
    int res = 1e9;
    for(int i = 0; i <= N ; i++){//i円を6^kのみで引き出し、残りを9^lのみで引き出す
        int cnt = 0;
        int t = i;
        while(t > 0){//6進数の各桁を足す(tを6^kのみで引き出したときの最小回数)
            cnt += t % 6;
            t /= 6;
        }
        int s = N - i;
        while(s > 0){//9進数ver
            cnt += s % 9;
            s /= 9;
        }
        res = min(res, cnt);
    }
    cout << res << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int dfs(vector<vector<int>>&a,vector<int>b,int i,int n){
  int counter=0;
  for(int k;k<n+1;k++){
    counter+=b.at(k);
  }
  if(b.at(i)==0&&counter==n-1){
    return 1;
  }
  if(b.at(i)==0){
    b.at(i)=1;
    int sum=0;
    for(int j=0;j<a.at(i).size();j++){
      sum+=dfs(a,b,a.at(i).at(j),n);
    }
    return sum;
  }
  else return 0;
}
int main(){
  int n,m;
  cin>>n>>m;
  vector<int> a(m);
  vector<int> b(m);
  for(int i=0;i<m;i++){
    cin>>a.at(i)>>b.at(i);
  }
  vector<vector<int>> c(n+1);
  for(int i=0;i<m;i++){
    c.at(a.at(i)).push_back(b.at(i));
    c.at(b.at(i)).push_back(a.at(i));
  }
  vector<int> f(n+1);
  cout<<dfs(c,f,1,n)<<endl;
}

Measured memory_bytes: 256
N,A,B,c,d=map(int,input().split())
n_time=N-1
_A=abs(A-B)
for i in range((N+1)//2):
    if (n_time-i)*d-i*c>=_A>=(n_time-i)*c-i*d:
        print('YES')
        break
else:
    print('NO')

Measured memory_bytes: 3060
S = input()
alp = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ans = ''
flg = False
for s in alp:
  if s not in S:
    ans = s
    flg = True
    break
if flg:
  print(ans)
else:
  print('None')
    

Measured memory_bytes: 3188
#include<stdio.h>

int H, W;
char S[51][51]; //文字列の２次元配列は初めに数で定義、変数定義はうまくいかない

int count(int x, int y) {
  int c = 0;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      if (0 <= x+dx && x+dx < H && 0 <= y+dy && y+dy < W && S[x+dx][y+dy] == '#') {
        c++;
      }
    }
  }
  return c;
}


int main()
{
  scanf("%d%d", &H, &W);
  for (int i = 0; i < H; i++) {
    scanf("%s", S[i]);
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.') {
        S[i][j] = (char)count(i, j) + '0';
      }
    }
  }
  
  for (int i = 0; i < H; i++) {
    printf("%s\n", S[i]);
  }
  return 0;
}


Measured memory_bytes: 128
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int age = sc.nextInt();
        int price = sc.nextInt();
        
        if (age >= 13) {
            System.out.println(price);
        } else if (age <= 5) {
            System.out.println(0);
        } else {
            System.out.println(price / 2);
        }
    }
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
ld PI=3.14159265358979323846;

ll N;
string s;
vector<char> t(100010,'a');

void solve(){
    REP(i,1,N-1){
        if((s[i] == 'o' && t[i] == 'S') || (s[i] == 'x' && t[i] == 'W' )){
            t[(i+1)] = t[(i-1)];
        }else{
            t[(i+1)] = ((t[(i-1)] == 'S')?'W':'S');
        }
    }
    /*
    if((s[N-1] == 'o' && t[N-1] == 'S') || (s[N-1] == 'x' && t[N-1] == 'W' )){
        if(t[N-2] == t[0]){
            if((s[0] == 'o' && t[0] == 'S') || (s[0] == 'x' && t[0] == 'W' )){
                return (t[N-1] == t[1]);
            }else{
                return (t[N-1] != t[1]);
            }
        }
    }else{
        return (t[N-2] != t[0]);
    }
    */
}

bool check(){
    REP(i,1,N+1){
        if((s[i%N] == 'o' && t[i%N] == 'S') || (s[i%N] == 'x' && t[i%N] == 'W' )){
            if(t[(i+1)%N] == t[(i-1)%N]){
                continue;
            }else{
                return false;
            }
        }else{
            if(t[(i+1)%N] == ((t[(i-1)%N] == 'S')?'W':'S')){
                continue;
            }else{
                return false;
            }
        }
    }
    return true;
}

char x[4] = {'S','S','W','W'};
char y[4] = {'S','W','S','W'};

int main(){
    cin >> N;
    cin >> s;
    rep(i,4){
        t[0] = x[i];
        t[1] = y[i];
        solve();
        if(check()){
            rep(i,N){
                cout << t[i];
            }
            cout << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}

Measured memory_bytes: 640
#include <iostream>
#include <cstdio>
using namespace std;
int n,k,s;
int main()
{
	scanf("%d%d%d",&n,&k,&s);
	if(s < 1e9)
	{
		for(int i = 1; i <= k; i++)
			printf("%d ",s);
		for(int i = k + 1; i <= n; i++)
			printf("%d ",s+1);
	}
	if(s == 1e9)
	{
		for(int i = 1; i <= k; i++)
			printf("%d ",s);
		for(int i = k + 1; i <= n; i++)
			printf("%d ",s-1);
	}
	return 0;
}

Measured memory_bytes: 1280
import java.util.HashMap;
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int counter = 0;
		HashMap<Integer, Integer> hash = new HashMap<>();

		int[] array = new int[N];
		for (int i = 0; i < N; i++) {

			array[i] = scan.nextInt();
			if (hash.get(array[i]) == null)
				hash.put(array[i], 1);
			else
				hash.put(array[i], (hash.get(array[i])) + 1);

			if (hash.get(array[i] - 1) == null)
				hash.put(array[i] - 1, 1);
			else
				hash.put(array[i] - 1, hash.get(array[i] - 1) + 1);

			if (hash.get(array[i] + 1) == null)
				hash.put(array[i] + 1, 1);
			else
				hash.put(array[i] + 1, hash.get(array[i] + 1) + 1);

		}
		Object[] array2 = hash.keySet().toArray();
		int max = 0;
		int j= 0;
		for (int i = 0; i < array2.length; i++) {
			if ((int) hash.get(array2[i]) > max){
				max = (int) hash.get(array2[i]);
			j = i;	
			}
			
		}
		System.out.println(max);
//		System.out.println(array2[j]);

	}

}


Measured memory_bytes: 63668
#include <stdio.h>

int main(void) {
  long long int number, max, min;
  long long int i, n;
    long long int sum = 0;
    scanf("%lld",&n);
    scanf("%lld",&number);
    max = number;
    min = number;
    sum += number;
    for(i = 1; i < n; i++ ) {
        scanf("%lld",&number);
        sum += number;
        if(number > max) {
            max = number;
        }
        if(number < min) {
            min = number;
        }
    }
    printf("%lld %lld %lld\n", min, max, sum);
    return 0;
}

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

int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int j = n - 1; 0 <= j; j--) {
    cout << a[j];
    if (j == 0) break;
    cout << " ";
  }
  cout << endl;
  return 0;
}

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

int main(){
  int W, H, N;
  cin >> W >> H >> N;
  int x, y;
  cin >> x >> y;
  long long ans = 0;
  for(int i = 1; i < N; ++i){
    int x_, y_;
    cin >> x_ >> y_;
    int t = abs(x-x_)+abs(y-y_);
    if(x > x_ and y > y_) t -= min(x-x_,y-y_);
    if(x < x_ and y < y_) t -= min(x_-x,y_-y);
    ans += t;
    x = x_;
    y = y_;
  }
  cout << ans << endl;
}



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

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

ans=[0]*N
uf1=UnionFind(N)
for i in range(M):  # range(m):
    a,b=map(int, input().split()) #a,b:つながっている辺
    uf1.union(a-1,b-1)
    ans[a-1]-=1
    ans[b-1]-=1

for i in range(N):
    uf1.find(i)  # 一周findすることによって接続漏れをなくす。

from collections import defaultdict
ll=defaultdict(int)
for i in uf1.par:
    ll[i]+=1

for i in range(N):
    ans[i]+=ll[uf1.par[i]]-1

for i in range(K):
    a,b=map(int, input().split()) #a,b:つながっている辺
    if uf1.is_same(a-1,b-1):
        ans[a-1]-=1
        ans[b-1]-=1

print(*ans)

Measured memory_bytes: 18208
#include<stdio.h>

int binarySearch(int,int);
int S[100000];

int main(){


  int T[50000];
  int q;
  int n,i,count=0;

  scanf("%d",&n);
  
  for(i=0; i<n; i++){
    scanf("%d",&S[i]);
  }  
  scanf("%d",&q);
 
  for(i=0 ; i<q ; i++){
    
    scanf("%d",&T[i]);
  }  

  for(i=0 ; i<q ; i++){    

    if( binarySearch(n,T[i]) == 1){
      count++;}
  }
  
    printf("%d\n",count);
    
    return 0;
  }


int binarySearch(int right,int data){

  int left=0,mid;

  while (left < right){
    mid = (left + right)/2;
      if(S[mid] == data){
        mid = 1;
	return mid;
      }else if(data < S[mid]){
	right = mid;
      }else{
	 left = mid + 1;
      }
  }
  mid=0;
  return mid;
}



Measured memory_bytes: 2324
import java.util.Scanner;

//Life Game
public class Main{

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true){
			int x = sc.nextInt();
			int n = sc.nextInt();
			int e = sc.nextInt();
			if((x|n|e)==0)break;
			int[] v = new int[x];
			for(int i=0;i<x;i++)v[i]=sc.nextInt();
			int[][] ev = new int[n+1][2];
			while(e--!=0){
				int p = sc.nextInt();
				int k = sc.nextInt();
				int val = sc.nextInt();
				ev[p][0] = k;
				ev[p][1] = val;
			}
			double[][] p = new double[5001][n+1];

			p[0][0] = 1;
			for(int i=0;i<n;i++){
				for(int j=0;j<5001;j++){
					if(p[j][i]==0)continue;
					for(int k=0;k<x;k++){
						int np = Math.min(n, i+v[k]);
						int nm = j;
						if(ev[np][0]==1){
							np = Math.min(n, np+ev[np][1]);
						}
						else if(ev[np][0]==2){
							nm += ev[np][1];
						}
						else if(ev[np][0]==3){
							nm = Math.max(0, nm-ev[np][1]);
						}
						p[nm][np] += p[j][i]/x;
					}
				}
			}
			double exp = 0;
			for(int i=0;i<5001;i++)exp+=i*p[i][n];
			System.out.println((int)exp);
		}
	}
}

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

int damerau_levenshtein_distance(const string &a,const string &b){
    //unordered_map<int,int> da;
    //unordered_map<int,map<int,int>> d;
    vector<int> da_(26);
    vector<vector<int>> d_(a.size()+2,vector<int>(b.size()+2));
    auto da=[&](int i)->int&{
        return da_[i-'a'];
    };
    auto d=[&](int i,int j)->int&{
        return d_[i+1][j+1];
    };
    int maxdist=a.size()+b.size();
    d(-1,-1)=maxdist;
    for(int i=0; i<=a.size(); ++i){
        d(i,-1)=maxdist;
        d(i,0)=i;
    }
    for(int i=0; i<=b.size(); ++i){
        d(-1,i)=maxdist;
        d(0,i)=i;
    }
    for(int i=1; i<=a.size(); ++i){
        int db=0;
        for(int j=1; j<=b.size(); ++j){
            int k=da(b[j-1]),l=db,cost;
            if(a[i-1]==b[j-1]){
                cost=0;
                db=j;
            }else cost=1;
            d(i,j)=min({d(i-1,j-1)+cost,d(i,j-1)+1,d(i-1,j)+1,d(k-1,l-1)+(i-k-1)+1+(j-l-1)});
        }
        da(a[i-1])=i;
    }
    return d(a.size(),b.size());
}

void solve(int n){
    int d;
    vector<string> name(n);
    cin >> d;
    rep(i,0,n) cin >> name[i];
    sort(name.begin(),name.end());

    int count=0;
    rep(i,0,n){
        rep(j,i+1,n){
            if(damerau_levenshtein_distance(name[i],name[j])>d) continue;
            ++count;
            cout << name[i] << "," << name[j] << endl;
        }
    }
    cout << count << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    for(;;){
        int n;
        cin >> n;
        if(!n) break;
        solve(n);
    }
    return 0;
}

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

int main(){
  string s;cin >> s;
  string ans = "No";
  if(s[0]!=s[2] && s[0]==s[1] && s[2]==s[3])ans = "Yes";
  if(s[0]!=s[3] && s[0]==s[2] && s[1]==s[3])ans = "Yes";
  if(s[0]!=s[1] && s[0]==s[3] && s[2]==s[1])ans = "Yes";
  cout << ans;
}

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

int main () {
  string A, B, C;
  cin >> A >> B >> C;
  int a, b;
  a = A.size(), b = B.size();
  if(A.at(a-1) == B.at(0) && B.at(b-1) == C.at(0)) cout << "YES" << endl;
  else cout << "NO" << endl;
}


Measured memory_bytes: 256
import java.io.*;

public class Main {

	final static long MOD = 1000000007;
	public static void main(String[] args) throws Exception {
		// TODO 自動生成されたメソッド・スタブ

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String W = in.readLine();
		String[] Ws = W.split(" ");
		int H = Integer.parseInt(Ws[0]);
		int N = Integer.parseInt(Ws[1]);

		W = in.readLine();
		Ws = W.split(" ");

		long[] A = new long[Ws.length];
		long sum=0;
		for (int i = 0; i < Ws.length; i++) {
			A[i] = Long.parseLong(Ws[i]);
			sum+=A[i];
		}

		System.out.println(H-sum<=0?"Yes":"No");
	}
}


Measured memory_bytes: 33620
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stdio.h>
#include<deque>

typedef long long ll;

#define debug(x) cout << #x << "=" << x << endl;
#define debug_arr(a, n) for(ll i = 0; i < n; i++)cout << a[i] << " "
#define inf 100000000000
#define loop(i,n) for(ll i = 0; i < n; i++)

using namespace std;

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

    int t,f,isReverse = -1;
    deque<char> dequeChar;
    char c; 

    for(char ch: str){
        dequeChar.push_back(ch);
    }

    loop(i,queryNum){
        cin >> t;
        if(t == 1) isReverse *= -1;
        else if(t == 2){
            cin >> f >> c;
            if(isReverse == -1){
                if(f == 1)dequeChar.push_front(c);
                if(f == 2)dequeChar.push_back(c);
            }else{
                if(f == 2)dequeChar.push_front(c);
                if(f == 1)dequeChar.push_back(c);
            }
        }
    }
    
    if(isReverse == -1){
        loop(i, dequeChar.size()){
            cout << dequeChar[i];
        }
    }else{
        for(ll i = dequeChar.size() - 1; i >= 0; i--){
            cout << dequeChar[i];
        }
    }
    cout << endl;

    return 0;
}

Measured memory_bytes: 896
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<int, P> E;
#define MOD (1000000007ll)
#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a += b;
	if(a>=MOD){
		a -= MOD;
	}
}

vector<int> v;

int main(void){
	int n,k,m,i,ans=0;
	string s;
	v.push_back(0);
	cin >> n >> k;
	cin >> s;
	if(s[0]=='0'){
		v.push_back(0);
	}
	for(i=1; i<n; ++i){
		if(s[i-1]!=s[i]){
			v.push_back(i);
		}
	}
	if(s[n-1]=='0'){
		v.push_back(n);
	}
	v.push_back(n);
	m = v.l_ength();
	for(i=0; i<v.l_ength(); i+=2){
		ans = max(ans,v[min(i+2*k+1,m-1)]-v[i]);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1020
import java.util.Scanner;
public class Main
{
	public static void main(String arg[])
	{
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		while(n-->0)
		{
			int s=in.nextInt();
			int t=in.nextInt();
			if(s<t)
			{
				for(int i=s;i<=t;i++)
					if(i!=t)
						System.out.print(i+" ");
					else
						System.out.println(i);
			}
			else if(s>t)
			{
				if(s<=5)
				{
					for(int i=s;i>=t;i--)
						if(i!=t)
							System.out.print(i+" ");
						else
							System.out.println(i);
				}
				else if(s>=6)
				{
					for(int i=s;i<=9;i++)
						System.out.print(i+" ");
					if(t>=0&&t<=5)
						for(int i=5;i>=t;i--)
						{
							if(i!=t)
								System.out.print(i+" ");
							else
								System.out.println(i);
						}
					else if(t>=6&&t<=9)
					{
						System.out.print("5 4 3 2 1 0 1 2 3 4 5 ");
						for(int i=6;i<=t;i++)
						if(i!=t)
							System.out.print(i+" ");
						else
							System.out.println(i);
					}
				}
			}
		}
	}
}

Measured memory_bytes: 12000
<?php

$input = trim(fgets(STDIN));

$numbers = '1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51';

$arr= explode(',', $numbers);
$arr = array_map('trim', $arr);

echo $arr[$input - 1];


Measured memory_bytes: 4728
def lotate(dic, dire):
    if dire == 'N':
        x,y,z,w = dic['up'], dic['back'], dic['bottom'], dic['front']
        dic['back'], dic['bottom'], dic['front'], dic['up'] = x,y,z,w
    elif dire == 'S':
        x, y, z, w = dic['up'], dic['back'], dic['bottom'], dic['front']
        dic['front'], dic['up'], dic['back'], dic['bottom'] = x, y, z, w
    elif dire == 'W':
        x, y, z, w = dic['up'], dic['left'], dic['bottom'], dic['right']
        dic['left'], dic['bottom'], dic['right'], dic['up'] = x, y, z, w
    elif dire == 'E':
        x, y, z, w = dic['up'], dic['left'], dic['bottom'], dic['right']
        dic['right'], dic['up'], dic['left'], dic['bottom'] = x, y, z, w
    return dic


a1,b1,c1,d1,e1,f1 = map(int, input().split())
a2,b2,c2,d2,e2,f2 = map(int, input().split())
dic1 = {'up': a1, 'front': b1, 'right': c1, 'left': d1, 'back': e1, 'bottom': f1}
dic2 = {'up': a2, 'front': b2, 'right': c2, 'left': d2, 'back': e2, 'bottom': f2}
ans = 'No'
if dic1 == dic2:
    ans = 'Yes'
else:
    s = 'EEENEEENEEESEEESEEENEEEN'
    for i in s:
        dic2 = lotate(dic2, i)
        if dic1 == dic2:
            ans = 'Yes'
            break
print(ans)



Measured memory_bytes: 5612
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=998244353;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

ll n,q;
vl a;

int main(){
	cin>>n>>q;
	a=vl(n);
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<q;i++){
		ll N,x,m;
		cin>>N>>x>>m;
		ll sum=x%m,res=N-1;
		for(int j=0;j<n;j++){
			ll num=(N-1)/n+(j<(N-1)%n?1:0);
//			cout<<num<<endl;
			if(a[j]%m==0) res-=num;
			else sum+=a[j]%m*num;
		}
		res-=sum/m;
		cout<<res<<endl;
	}
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(a) (a).begin(), (a).end()

#define INF 1000000000

using namespace std;

int main() {
    string str[3];

    REP(i, 3)
    cin >> str[i];

    REP(i, 3)
    cout << str[i][i];

    cout << endl;

    return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for(int i=0; i<N ;i++){
    cin >> vec.at(i);
  }
  int count=0;
  int suki;
  for(int i=0; i<N ;i++){
    suki=vec.at(i)-1;
    if(vec.at(suki)==i+1){
      count=count+1;
    }
    else{
    }
  }
  cout << count/2 << endl;
    
  
}

Measured memory_bytes: 640
from collections import defaultdict

def parse_expr(s,i,num):
    if i < len(s) and f_num[s[i]]:
        n,i = parse_num(s,i,num)
        if s[i] == "(":
            i += 1
            su = 0
            while i < len(s) and s[i] != ")":
                e,i = parse_expr(s,i,num)
                su += e
            return su*n,i+1
        else:
            k,i = parse_alp(s,i,n)
            return k+n-1,i
    else:
        k,i = parse_alp(s,i,num)
        return k,i

def parse_num(s,i,num):
    m = int(s[i])
    i += 1
    while i < len(s) and f_num[s[i]]:
        m *= 10
        m += int(s[i])
        i += 1
    return num*m,i

def parse_alp(s,i,num):
    k = 1
    i += 1
    while i < len(s) and f_alp[s[i]]:
        k += 1
        i += 1
    return k,i

def find(s,l,r,i):
    if l == r:
        return 0
    su,k = parse_expr(s,l,1)
    if i < su:
        if l < r and f_alp[s[l]]:
            return s[l+i]
        else:
            n,l = parse_num(s,l,1)
            if l < r and s[l] == "(":
                return find(s,l+1,k-1,i%(su//n))
            else:
                return find(s,l,k,i%(su//n))
    else:
        return find(s,k,r,i-su)
while 1:
    s,i = input().split()
    if s == i == "0":break
    i = int(i)
    f_alp = defaultdict(lambda : 0)
    f_num = defaultdict(lambda : 0)
    for a in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
        f_alp[a] = 1
    for a in range(10):
        f_num[str(a)] = 1
    print(find(s,0,len(s),i))



Measured memory_bytes: 6036
// package arc.arc091;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong();
        long m = in.nextLong();

        out.println(solve(n, m));
        out.flush();
    }

    private static long solve(long n, long m) {
        if (n > m) {
            return solve(m, n);
        }
        // n <= m
        if (n == 1) {
            if (m == 1) {
                return 1;
            }
            return m-2;
        }
        return (n-2)*(m-2);
    }


    public static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }

    public static class InputReader {
        private static final int BUFFER_LENGTH = 1 << 12;
        private InputStream stream;
        private byte[] buf = new byte[BUFFER_LENGTH];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int next() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public char nextChar() {
            return (char) skipWhileSpace();
        }

        public String nextToken() {
            int c = skipWhileSpace();
            StringBuilder res = new StringBuilder();
            do {
                res.append((char) c);
                c = next();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public int nextInt() {
            return (int) nextLong();
        }

        public long nextLong() {
            int c = skipWhileSpace();
            long sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble() {
            return Double.valueOf(nextToken());
        }

        int skipWhileSpace() {
            int c = next();
            while (isSpaceChar(c)) {
                c = next();
            }
            return c;
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }
}

Measured memory_bytes: 20948
#include<atcoder/all>
#include<iostream>
using namespace std;
using namespace atcoder;

int op(int a, int b) { return max(a, b); }
int e() { return 0; }

int main() {
  int N,K; cin>>N>>K;
  segtree<int, op, e> s(300010);
  int max_l = 0;
  for (int i = 0; i < N; i++) {
    int a;cin>>a;
    int prod = s.prod(max(0, a-K), min(300010, a+K+1));
    max_l = max(max_l, prod+1);
    s.set(a, prod+1);
  }
  cout<<max_l<<endl;
}

Measured memory_bytes: 8572
import math

n = int(input())
X = [list(map(int,input().split())) for i in range(n)]

X2 = []
for i in range(n):
    if X[i] != [0, 0]:
        X2.append(X[i][:])

X = X2[:]

n = len(X)

if n == 0:
    print(0)
else:

    for i in range(n):
        X[i].append(math.atan2(X[i][1], X[i][0]))

    X.sort(key=lambda x:x[2])

    X += X[:]
    #print(X)
    ans = 0

    X_sum = [[X[0][0], X[0][1]]]
    for i in range(1, 2*n):
        #print(i)
        X_sum.append([X_sum[i-1][0] + X[i][0], X_sum[i-1][1] + X[i][1]])

    #print(len(X_sum))

    for st in range(0, n):
        for en in range(st+1, st+n+1):
            dis = [0, 0]
            for i in range(st, en):
                #print(i)
                dis[0] = X_sum[en][0] - X_sum[st][0]
                dis[1] = X_sum[en][1] - X_sum[st][1]
            #print(st,en,(dis[0]**2 + dis[1]**2)**0.5)
            ans = max(ans, (dis[0]**2 + dis[1]**2))


    print(ans ** 0.5)

Measured memory_bytes: 39408
#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}

int h[101];
int N;
int main(){

  while(cin>>N&&N){
    for(int i=0;i<N+1;i++)cin>>h[i];
    for(int i=0;i<N+1;i++){
      vector<int> v;
      for(int j=0;j<N+1;j++){
        if(i==j)continue;
        v.push_back(h[j]);
      }
      int diff=v[1]-v[0];
      bool ok=true;
      for(int j=1;j<(int)v.size();j++){
        if(v[j]-v[j-1]!=diff){
          ok=false;
          break;
        }
      }
      if(ok){
        cout<<h[i]<<endl;
        break;
      }
    }
  }

  return 0;
}

Measured memory_bytes: 1220
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    String t = "";
    for(int i = 0; i < s.length(); i++) {
      if(i == 3) {
        t += "8";
      } else {
        t += String.valueOf(s.charAt(i));
      }
    }
    System.out.println(t);
  }
}


Measured memory_bytes: 21844
def main():
    from numpy import zeros
    from sys import stdin
    def input():
        return stdin.readline().strip()

    n, c = map(int, input().split())
    # v: cumulative sum
    x, v = zeros(n+2, dtype=int), zeros(n+2, dtype=int)
    for i in range(n):
        j, k = map(int, input().split())
        x[i+1] = j
        v[i+1] = v[i] + k
    x[n+1] = c
    v[n+1] = v[n]
    
    max_nut_clockwise = zeros(n+1, dtype=int)
    for i in range(1, n+1):
        max_nut_clockwise[i] = max(max_nut_clockwise[i-1], v[i] - x[i])
    max_nut_counterclockwise = zeros(n+1, dtype=int)
    for i in range(n-1, -1, -1):
        max_nut_counterclockwise[i] = max(max_nut_counterclockwise[i+1], v[-1] - v[i] - c + x[i+1])

    ans = 0
    # rotate clockwise, then rotate counterclockwise
    for i in range(n):
        if ans < v[i] - 2*x[i] + max_nut_counterclockwise[i]:
            ans = v[i] - 2*x[i] + max_nut_counterclockwise[i]
    # rotate counterclockwise, then rotate clockwise
    for i in range(2, n+2):
        if ans < v[-1] - v[i-1] - 2 * (c - x[i]) + max_nut_clockwise[i-1]:
            ans = v[-1] - v[i-1] - 2 * (c - x[i]) + max_nut_clockwise[i-1]
    
    print(ans)

main()


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

#define rep(i,n) for(int i=0; i<n; i++)
#define rept(i,a,b) for(int i=a; i<b; i++)
#define all(x) (x).begin(),(x).end()

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

const int mod = 1e9 + 7;


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

    int n;
    cin >> n;
    string s;
    cin >> s;

    if (n % 2 == 1){
        cout << "No" << endl;
        return 0;
    }

    rep(i, n/2) {
        if (s[i] != s[i + n/2]) {
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;

    return 0;
}



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


int main() {
int N,maxi = 0,mini = 101;
cin >> N;
vector<int> vec(N);
rep(i,N) {
    cin >> vec.at(i);
    if (vec.at(i) > maxi) maxi = vec.at(i);
    if(mini > vec.at(i)) mini = vec.at(i);
}
ll ans = INF;
for (int i = 1;i <= 100;i++) {
    ll cnt = 0;
    rep(j,N) {
        cnt += pow(vec.at(j)-i,2);
    }
    ans = min(ans,cnt);
}
cout << ans << endl;

return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
    ll n,k,i,ret=10000000000;
    scanf("%lld %lld",&n,&k);
    vector<ll> h(n);
    
    for(i=0;i<n;i++)
        scanf("%lld",&h[i]);
    
    sort(h.begin(),h.end());
    
    for(i=0;i<=n-k;i++)
        ret=min(ret,h[i+k-1]-h[i]);
    
    printf("%lld\n",ret);
}

Measured memory_bytes: 1024
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
int main() {
	ll x;
	cin >> x;
	for (int i = -300; i <= 300; i++) {
		for (int j = -300; j <= 300; j++) {
			
			if (pow(i,5) -pow(j,5)== x) {
				
				cout << i<< " " << j << endl;
				return 0;
			}
		}
	}
	return 0;
}

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

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

int N, M;

struct State
{
    ll pos, spos, dist;
};

bool operator<(const State &e, const State &f)
{
    return e.dist > f.dist;
}

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

    while (cin >> N >> M, N || M)
    {
        // 陸路での距離, 海路での距離
        ll road[N][N], ship[N][N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (i != j)
                {
                    road[i][j] = ship[i][j] = INF;
                }
                else
                {
                    road[i][j] = ship[i][j] = 0;
                }
            }
        }
        for (int i = 0; i < M; i++)
        {
            int x, y, t;
            string sl;
            cin >> x >> y >> t >> sl;
            x--;
            y--;
            if (sl == "S")
                ship[x][y] = ship[y][x] = t;
            else
                road[x][y] = road[y][x] = t;
        }
        int R;
        cin >> R;
        vector<int> z(R);
        for (int i = 0; i < R; i++)
        {
            cin >> z[i];
            z[i]--;
        }
        ll dp[R][N];
        for (int i = 0; i < R; i++)
        {
            for (int j = 0; j < N; j++)
            {
                dp[i][j] = INF;
            }
        }
        for (int k = 0; k < N; k++)
        {
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    road[i][j] = min(road[i][j], road[i][k] + road[k][j]);
                    ship[i][j] = min(ship[i][j], ship[i][k] + ship[k][j]);
                }
            }
        }
        priority_queue<State> q;
        dp[0][z[0]] = 0;
        q.push(State{0, z[0], 0});
        while (!q.empty())
        {
            State state = q.top();
            q.pop();
            int idx = state.pos;
            int st = z[idx], gt = z[idx + 1];
            int mt = state.spos, dis = state.dist;
            if (dp[idx][mt] < dis)
                continue;
            // 陸路のみ
            if (dp[idx][mt] + road[st][gt] < dp[idx + 1][mt])
            {
                dp[idx + 1][mt] = dp[idx][mt] + road[st][gt];
                if (idx + 2 < R)
                {
                    q.push(State{idx + 1, mt, dp[idx + 1][mt]});
                }
            }
            for (int i = 0; i < N; i++)
            {
                if (dp[idx][mt] + road[st][mt] + ship[mt][i] + road[i][gt] < dp[idx + 1][i])
                {
                    dp[idx + 1][i] = dp[idx][mt] + road[st][mt] + ship[mt][i] + road[i][gt];
                    if (idx + 2 < R)
                    {
                        q.push(State{idx + 1, i, dp[idx + 1][i]});
                    }
                }
            }
        }
        ll res = INF;
        for (int i = 0; i < N; i++)
            res = min(res, dp[R - 1][i]);
        cout << res << endl;
    }
}



Measured memory_bytes: 5392
#define _USE_NATH_DEFINES
#include<cstdio>
#include <iostream>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<complex>
#include <string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<numeric>
#include<limits>
#include<climits>
#include<cfloat>
#include<functional>
#include<iterator>
#include <random>

using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
#define EPS 1e-9
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;

int main() {
	string s;
	cin >> s;
	int n;
	int l, r;
	n = s.size();
	if (n % 2 == 0) {
		l = n / 2;
		r = n / 2;
	}
	else {
		l = n / 2 + 1;
		r = n / 2;
	}
	int l_cnt = 0;
	int r_cnt = 0;
	rep(i, n) {
		if (i % 2 == 0) {
			if (s[i] == 'R' || s[i] == 'U' || s[i] == 'D') l_cnt++;
		}
		else if(i % 2 == 1){
			if (s[i] == 'L' || s[i] == 'U' || s[i] == 'D') r_cnt++;
		}
	}
	if (l_cnt == l && r_cnt == r) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define MOD 1000000007
#define mod 1000000037
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
#define INF 2*1e9

int main()
{
    int n;
    cin >> n;
    vector<vector<int> >v(n);
    for(int i=1;i<n;i++)
    {
        int x;
        cin >> x;
        x-=1;
        v[x].push_back(i);
    }
    for(int i=0;i<n;i++)
        cout << v[i].size() << endl;
}

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

using namespace std;
typedef long long ll;
//typedef unsigned long long ll;

#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
//typedef pair<ll, int> P;
typedef pair<ll, ll> P;
//const double INF = 1e10;
const ll INF = LONG_LONG_MAX / 10;
//const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
//const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
//typedef pair<int, int> P;
//typedef pair<double, double> P;

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

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);


void print_line(vector<ll> &line) {
    if (line.size() == 0ll) {
        cout << endl;
        return;
    }
    for (ll i = 0; i < line.size(); i++) {
        cout << line[i];
        if (i == line.size() - 1) cout << endl;
        else cout << ' ';
    }
}

// http://algoogle.hadrori.jp/algorithm/segment-tree.html
struct SegTreeMin {
    ll N, dat[4 * 1000];

    SegTreeMin() {}

    SegTreeMin(ll n) {
        N = 1;
        while (N < n) N *= 2;
        for (ll i = 0; i < 2 * N - 1; i++)
            dat[i] = INF;
    }

    // update k th element
    void update(ll k, ll a) {
        k += N - 1; // leaf
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }

    // min [a, b)
    ll query(ll a, ll b) { return query(a, b, 0, 0, N); }

    ll query(ll a, ll b, ll k, ll l, ll r) {
        if (r <= a or b <= l) return INF;
        if (a <= l and r <= b) return dat[k];
        ll m = (l + r) / 2;
        return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
    }
};


struct SegTreeMax {
    ll N, dat[4 * 1000];

    SegTreeMax() {}

    SegTreeMax(ll n) {
        N = 1;
        while (N < n) N *= 2;
        for (ll i = 0; i < 2 * N - 1; i++)
            dat[i] = -INF;
    }

    // update k th element
    void update(ll k, ll a) {
        k += N - 1; // leaf
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }

    // max [a, b)
    ll query(ll a, ll b) { return query(a, b, 0, 0, N); }

    ll query(ll a, ll b, ll k, ll l, ll r) {
        if (r <= a or b <= l) return -INF;
        if (a <= l and r <= b) return dat[k];
        ll m = (l + r) / 2;
        return max(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
    }
};

typedef priority_queue<long long, vector<long long>, greater<long long>> PQ_ASK;
const int mod = 1000000007;


int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];

    int q;
    cin >> q;
    rep(i, q) {
        int b, e;
        cin >> b >> e;
        reverse(a.begin() + b, a.begin() + e);
    }

    print_line(a);

}


Measured memory_bytes: 3112