akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <algorithm>
using namespace std;

char g(int m,int e,int j){
	if(m==100||e==100||j==100) return 'A';
	if(m+e>=180) return 'A';
	if(m+e+j>=240) return 'A';
	if(m+e+j>=210) return 'B';
	if(m+e+j>=150&&(m>=80||e>=80)) return 'B';
	return 'C';
	
}
int main(){
	int n;
	while(cin >> n && n){
		for(int i = 0 ; i < n ; i++){
			int m,e,j;
			cin >> m >> e >> j;
			cout << g(m,e,j) << endl;
		}
	}
}

Measured memory_bytes: 0
n=int(input())
box=[]
for i in range(n):
    s,p = map(str, input().split())
    box.append([s,int(p),i+1])
    
box.sort(key=lambda x:x[1], reverse=True)
box.sort(key=lambda x:x[0])
for i in box:
    print(i[2])

Measured memory_bytes: 3060
S = input()
len_S = len(S)
prev_ind = -1
ary = []
for i in range(len_S):
    if len(ary) > i:
        continue
    if S[i] == "R" and S[i+1] == "L":
        try:
            ind = S[i+1:].index("LR") + (i + 1)
        except ValueError as e:
            ind = len_S - 1
        sum = ind - prev_ind
        if sum % 2 == 0:
            ary.append(int(sum/2))
            ary.append(int(sum/2)) # 次のpass忘れない
        else:
            far = max([abs(ind - i), abs(prev_ind - i)])
            if abs(ind - i) < abs(prev_ind - i):
                if far % 2 == 0:
                    ary.append(int(sum/2))
                    ary.append(int(sum/2) + 1) # 次のpass忘れない
                else:
                    ary.append(int(sum/2) + 1)
                    ary.append(int(sum/2)) # 次のpass忘れない
            else:
                if far % 2 == 0:
                    ary.append(int(sum/2) + 1)
                    ary.append(int(sum/2)) # 次のpass忘れない
                else:
                    ary.append(int(sum/2))
                    ary.append(int(sum/2) + 1) # 次のpass忘れない
        prev_ind = ind
    else:
        ary.append(0)
for i in ary:
    print(i,end=" ")

Measured memory_bytes: 4992
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
int a[10];
int main(){
	for(int i=1;i<=5;i++)cin>>a[i];
	for(int i=1;i<=5;i++)if(a[i]==0){cout<<i<<endl;break;}
    return 0;
}


Measured memory_bytes: 3612
#include <bits/stdc++.h>
using namespace std;
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x,y,h,w) x >= 0 && x < h && y >= 0 && y < w

#define int long long
//typedef    long long          ll;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
 
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;

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

class BIT{
	vector<int> bit;
public:
	BIT(int siz){
		bit.resize(siz + 1,0);
	}
	void add(int i,int x){
		while(i < bit.size()){
			(bit[i] += x) %= MOD;
			i += i & -i;
		}
	}
	int sum(int i){
		int s = 0;
		while(i){
			(s += bit[i]) %= MOD;
			i -= i & -i;
		}
		return s;
	}
};

signed main(){
	int n,m,x[100010],y[100010],ans = 0;
	vector<int> xp,xm;
	vector<P> vec;
	cin >> n >> m;
	y[0] = -INF;
	y[m + 1] = INF;
	for(int i = 0;i < n;i++) cin >> x[i];
	for(int i = 1;i <= m;i++) cin >> y[i];
	for(int i = 0;i < n;i++){
		int ind = lower_bound(y,y + m + 2,x[i]) - y;
		if(y[ind - 1] != -INF && y[ind] != INF){
			xp.pb(y[ind] - x[i]);
			xm.pb(x[i] - y[ind - 1]);
			vec.pb(P(y[ind] - x[i],x[i] - y[ind - 1]));
		}
	}
	if(!xp.size()){
		cout << 1 << endl;
		return 0;
	}
	sort(all(xp));
	xp.erase(unique(all(xp)),xp.end());
	sort(all(xm));
	xm.erase(unique(all(xm)),xm.end());
	BIT bit(xm.size() + 5);
	for(int i = 0;i < vec.size();i++){
		vec[i].first = lower_bound(all(xp),vec[i].first) - xp.begin();
		vec[i].second = lower_bound(all(xm),vec[i].second) - xm.begin() + 1;
	}
	sort(all(vec),[](const P p1,const P p2){ return ((p1.first != p2.first) ? (p1.first < p2.first) : (p1.second > p2.second)); });
	vec.erase(unique(all(vec)),vec.end());
	bit.add(1,1);
	for(P p : vec) bit.add(p.second + 1,bit.sum(p.second));
	cout << bit.sum(xm.size() + 2) << endl;
	return 0;
}

Measured memory_bytes: 5212
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;


public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        public static int log(int a , int b){
            int num= 0;
            while (a>=b){
                a/=b;
                num++;
            }
            return num;
        }
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int x = in.nextInt();
                int max= 1;
                for (int i = 2 ; i < x; i++) {
                    int v = log(x , i);
                    int ans = (int) Math.pow(i,v);
                    if ( v > 1 && max <= ans) max = ans;
                }
                out.print(max);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


Measured memory_bytes: 24788
#include<bits/stdc++.h>
using namespace std;
template<typename T>
struct BIT{
	int n;
	vector<T>bit;
	BIT(int n_=0,T a=0):n(n_),bit(n_+1,a){}
	T sum(int i)
	{
		T ans=0;
		for(;i>0;i-=i&-i)ans+=bit[i];
		return ans;
	}
	void add(int i,T a)
	{
		if(i==0)return;
		for(;i<=n;i+=i&-i)bit[i]+=a;
	}
};
long n,r,l=-1,m,c,s,i,j;
vector<int>a;
main(){
	for(cin>>n;cin>>m;r=r<m?m:r)a.push_back(m);
	for(;r-l>1;)
	{
		m=l+r>>1;
		int bit[2*n]={};
		c=s=i=0;
		for(j=n;j<=2*n;j+=j&-j)bit[j]++;
		for(;i<n;i++){
			s+=a[i]>m?-1:1;
			for(j=s+n-1;j>0;j-=j&-j)c+=bit[j];
			for(j=s+n;j>0&&j<=2*n;j+=j&-j)bit[j]++;
		}
		(c>n*(n+1)/4?r:l)=m;
	}
	cout<<r;
}


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


int main(){
  string s;cin>>s;
  long long int ans=0;
  int l=0;
  for(int i=0;i<s.size();i++){
    if(s[i]=='W'){
      ans+=i-l;l++;
    }
  }
  cout<<ans<<endl;
  return 0;
}


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

using namespace std;

/*

    top down.

*/


const int MAXN = 100 + 5 , MAXW = 1e5 + 5;


int n , W;
long long w[MAXN] , v[MAXN];
long long mem[MAXN][MAXW];

long long dp(int i , int totalWeight){

    if(i == n) return 0;

    if(mem[i][totalWeight] != -1) return mem[i][totalWeight];

    long long path1 = dp(i + 1 , totalWeight);
    long long path2 = 0;

    if(totalWeight + w[i] <= W){
            path2 = v[i] + dp(i + 1 , totalWeight + w[i]);
    }

    mem[i][totalWeight] = max(path1 , path2);
    return mem[i][totalWeight];

}

main(){

    scanf("%d %d" , &n , &W);

    for(int i = 0 ; i < n ; i ++){
            scanf("%lld %lld" , &w[i] , &v[i]);
    }

    memset(mem , -1 , sizeof mem);

    printf("%lld\n" , dp(0 , 0));

    return 0;
}


Measured memory_bytes: 82304
def main():
    N = int(input())
    S = input()

    c = 0

    r_indices = tuple([i for i in range(N) if S[i] == 'R'])
    g_indices = tuple([i for i in range(N) if S[i] == 'G'])
    b_indices = tuple([i for i in range(N) if S[i] == 'B'])

    # print(r_indices)
    # print(g_indices)
    # print(b_indices)

    avoid = 0

    for center in range(1, N - 1):
        # print('###')
        # print('center = ', center)
        # print(min(center, N - center - 1))

        for width in range(1, min(center + 1, N - center)):
            # print('c, w = ', center, width)
            little = center - width
            great = center + width
            # print('little=', little)
            # print('center=', center)
            # print('great=', great)

            if S[little] != S[great] and S[center] != S[great] and S[little] != S[center]:
                avoid += 1

    total = len(r_indices) * len(g_indices) * len(b_indices)

    print(total - avoid)


if __name__ == '__main__':
    main()


Measured memory_bytes: 9240
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] cin = Console.ReadLine().Split();
        long x = long.Parse(cin[0]);
        long y = long.Parse(cin[1]);
        long ans = 0;

        while (x <= y)
        {
            ans++;
            x *= 2;
        }

        Console.WriteLine(ans);
    }
}

Measured memory_bytes: 13140
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define rep1(i, n) for(int i=1; i<=(int)(n); i++)
#define rep2(i, n, m) for(int i=(int)n; i<=(int)m; i++)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> wi;
typedef vector<ll> vl;
const ll inf=1LL << 60;
const ll MOD=1000000007;

vector<int> enum_divisors(int n){
  vector<int> r;
  for(int i=1; i*i<=n; i++){
    if(n%i==0){
      r.push_back(i);
      if(n%i!=i)r.push_back(n/i);
    }
  }
  sort(r.begin(), r.end());
  return r;
}

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

  int n;
  cin >> n;
  int ans=0;
  rep1(i, n){
    if(i%2==1){
      vi a;
      a=enum_divisors(i);
      if(a.size()==8)ans++;
    }
  }

  cout << ans << endl;

  return 0;
}

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

int main(){
  int n,i,j;
  int C[10000];
  short *A,*B;
  scanf("%d",&n);
  A = malloc(sizeof(short)*n+1);
  B = malloc(sizeof(short)*n+1);

  for(i=0;i<=10000;i++){C[i]=0;}
  for(j=1;j<=n;j++){
    scanf("%hd",&A[j]);
    C[A[j]]++;
  }
  for(i=1;i<=10000;i++){C[i]=C[i]+C[i-1];}
  for (j=1;j<=n;j++){
    B[C[A[j]]]=A[j];
    C[A[j]]--;
  }
  for(i=1;i<n;i++){printf("%d ",B[i]);}
  printf("%d\n",B[n]);

  return 0;
}



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

void solve(int a,int b){
	int c=29999,numa=0,numb=0;
	numa=a;
	numb=b;
	if(a<b) swap(a,b);
	while(c!=0){
		c=a%b;
		a=b;
		b=c;
	}
	cout<<a<<" "<<numb/a*numa<<endl;
}

int main(){
	int a,b;
	while(cin>>a>>b){
		solve(a,b);
	}
	return 0;
}

Measured memory_bytes: 3112
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

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

int main(void) {
  int i,j;
  int n;
  cin>>n;
  int alph[50][26]={};
  rep(i,n){
    string s;
    cin>>s;
    rep(j,s.size())
      alph[i][s[j]-'a']++;
  }
  rep(i,26){
    int m=INF;
    rep(j,n)
      m=min(m,alph[j][i]);
    char a='a'+i;
    rep(j,m)cout<<a;
  }
  cout<<endl;
}

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

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int main(){
  int i,j,hit=0,blow=0;
  char a[5], b[5];

  while(scanf("%c %c %c %c\n",&a[0],&a[1],&a[2],&a[3]) != EOF){
    scanf("%c %c %c %c\n",&b[0],&b[1],&b[2],&b[3]);
    hit = 0; blow = 0;
    rep(i,4) rep(j,4){
      if(a[i] == b[j] && i == j){
        hit++; continue;
      }
      if(a[i] == b[j]) blow++;
    }

//    rep(i,4) printf("%c ",a[i]); puts("");
//    rep(i,4) printf("%c ",b[i]); puts("");
    printf("%d %d\n",hit,blow);
  }

  return 0;
}

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

#define START 1000000000000000
#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;
using ll = long long;
int mod = 1000000007;

typedef pair<ll, int> P;
struct edge{
    int to;
    ll cost;
};

int N;
vector<edge> G_yen[100000];
ll d_yen[100000];

vector<edge> G_snu[100000];
ll d_snu[100000];

void dijkstra_yen(int s){
    priority_queue<P, vector<P>, greater<P>> pq;
    fill(d_yen, d_yen + N, START);

    d_yen[s] = 0;
    pq.push(P(0, s));
    while(!pq.empty()){
        P p = pq.top();
        pq.pop();
        int now = p.second;
        if(p.first > d_yen[now]) continue;
        for(int i = 0; i < G_yen[now].size(); i++){
            edge e = G_yen[now][i];
            if(d_yen[e.to] > d_yen[now] + e.cost){
                d_yen[e.to] = d_yen[now] + e.cost;
                pq.push(P(d_yen[e.to], e.to)); 
            }
        }
    }
}

void dijkstra_snu(int t){
    priority_queue<P, vector<P>, greater<P>> pq;
    fill(d_snu, d_snu + N, START);

    d_snu[t] = 0;
    pq.push(P(0, t));
    while(!pq.empty()){
        P p = pq.top();
        pq.pop();
        int now = p.second;
        if(p.first > d_snu[now]) continue;
        for(int i = 0; i < G_snu[now].size(); i++){
            edge e = G_snu[now][i];
            if(d_snu[e.to] > d_snu[now] + e.cost){
                d_snu[e.to] = d_snu[now] + e.cost;
                pq.push(P(d_snu[e.to], e.to));
            }
        }
    }
}


int main(void){
    int M, s, t; cin >> N >> M >> s >> t;
    s--; t--;
    rep(i, M){
        int u, v; ll a, b; cin >> u >> v >> a >> b;
        u--; v--;
        edge e; e.cost = a; e.to = v;
        G_yen[u].push_back(e);
        e.to = u;
        G_yen[v].push_back(e);
        e.cost = b;
        G_snu[v].push_back(e);
        e.to = v;
        G_snu[u].push_back(e);
    }
    dijkstra_yen(s);
    dijkstra_snu(t);
    //d_yen[i] + d_snu[i]の最小化 (ただしいつまで使えるかのid付けが必要)
    P ans[N];
    rep(i, N){
        ans[i] = P(d_yen[i] + d_snu[i], i);
    }
    sort(ans, ans+N);
    int id = 0;
    
    rep(i, N){
        while(ans[id].second < i) id++;
        cout << START - ans[id].first << endl;
        
    }
    return 0;
}

Measured memory_bytes: 20864
#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <assert.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<int,ull> piu;
typedef pair<ull,ull> puu;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<pii,ll> ppl;
typedef pair<ll,pii> plp;
typedef pair<ll,pll> plP;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<double,int> pdi;
typedef pair<int,double> pid;
typedef pair<double,pii> pdp;
typedef pair<double,double> pdd;
typedef pair<pdd,double> pd3;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<29)
#define INFL (1LL<<61)
#define fi first
#define se second
#define EPS 1e-9
#define MOD 1000000007
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD
#define OUTPUT(x) rep(__,x.size())printf("%d%c",x[__],__+1==x.size()?'\n':' ')


int X;

int main()
{
	cin>>X;
	repn(c,1,1001)
	{
		int mi=c*100;
		int ma=c*105;
		if(mi<=X&&X<=ma)
		{
			puts("1");
			return 0;
		}
	}
	puts("0");
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"math/big"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	a := new(big.Int)
	b := new(big.Int)
	fmt.Sscan(getNextString(scanner), a)
	fmt.Sscan(getNextString(scanner), b)
	fmt.Fprintln(writer, a.Mod(a, b))
}



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

int main() {
	int n;
	cin >> n;
	vector<int>a(n);
	for (int i = 0; i < n; i++)cin >> a.at(i);
	sort(a.begin(), a.end());
	ll ans = 1e18;
	for (int i = -100; i <= 100; i++) {
		ll res = 0;
		for (int j = 0; j < n; j++)res += pow(i - a.at(j), 2);
		ans = min(ans, res);
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <map>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
  char sa[101];
  char sb[101];
  char sc[101];
  cin >> sa >> sb >> sc;

  int ind_a = 0;
  int ind_b = 0;
  int ind_c = 0;

  char crrnt_turn = 'a';

  while (crrnt_turn != '\0') {
    switch (crrnt_turn) {
    case 'a' :
      crrnt_turn = sa[ind_a];
      ind_a += 1;
      // cout << "hoge" << endl;
      break;
    case 'b' :
      crrnt_turn = sb[ind_b];
      ind_b += 1;
      break;
    case 'c' :
      crrnt_turn = sc[ind_c];
      ind_c += 1;
      // cout << "foo" << endl;
      break;
    }    
  }

  if (ind_a == strlen(sa) + 1) {
    cout << 'A' << endl;
  } else if (ind_b == strlen(sb) + 1) {
    cout << 'B' << endl;
  } else {
    cout << 'C' << endl;
  }
  return 0;
}


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

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c)  (c).begin(),(c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(),(c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for(ll i= (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x)  cerr << #x << " = " << (x) << endl

ll calc(ll x, ll y, ll z) {
    return x * x + y * y + z * z + x * y + y * z + z * x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    ll N; cin >> N;
    vector<ll> A(N + 1, 0); 
    FOR(x, 1, N + 1) {
        FOR(y, x, N + 1) {
            FOR(z, y, N + 1) {
                ll c = calc(x, y, z);
                if (c > N) {
                    break;
                }
                set<ll> s;
                s.insert(x); s.insert(y); s.insert(z);
                if(s.size() == 3) {
                    A[c] += 6;
                } else if (s.size() == 2) {
                    A[c] += 3;
                } else if (s.size() == 1) {
                    A[c] += 1;
                }
            }
        }
    }
    FOR(i, 1, N + 1) {
        cout << A[i] << "\n";
    }
}

Measured memory_bytes: 3648
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        List<String> inputDataList = new ArrayList<String>();
        String input = null;

        try {
            while ((input = in.readLine()) != null) {
                inputDataList.add(input);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        int cnt = 0;
        int caseNo = 0;
        while (inputDataList.size() > cnt
                && Integer.parseInt(inputDataList.get(cnt)) != 0) {
            caseNo++;
            int[] cloth = new int[Integer.parseInt(inputDataList.get(cnt)) + 1];
            cnt++;
            int treasure = Integer.parseInt(inputDataList.get(cnt));
            cnt++;
            cnt = steal(inputDataList, cnt, caseNo, cloth, treasure);
        }
    }

    private static int steal(List<String> inputDataList, int cnt, int caseNo,
            int[] cloth, int treasure) {

        for (int i = 0; i < treasure; i++) {
            String[] treasureData = inputDataList.get(cnt).split(",");
            cnt++;

            int value = Integer.parseInt(treasureData[0]);
            int weight = Integer.parseInt(treasureData[1]);

            for (int j = cloth.length - weight - 1; j >= 0; j--) {
                if ((j == 0 || cloth[j] > 0)
                        && cloth[j + weight] < cloth[j] + value) {
                    cloth[j + weight] = cloth[j] + value;
                }
            }
        }

        printResult(cloth, caseNo);

        return cnt;
    }

    private static void printResult(int[] cloth, int caseNo) {
        int minWeight = 0;

        for (int i = 1; i < cloth.length; i++) {
            if (cloth[i] > cloth[minWeight]) {
                minWeight = i;
            }
        }

        System.out.println("Case " + caseNo + ":");
        System.out.println(cloth[minWeight]);
        System.out.println(minWeight);
    }
}

Measured memory_bytes: 22044
#include <bits/stdc++.h>
#define S second
#define F first
#define ll long long
#define ld double

using namespace std;

const ll N = 2e5 + 5, mod = 1e9 + 7;

ll sumRange(ll x, ll y)
{
    return y - (x - 1);
}

int main()
{
    ll n, k, s = 1, e, ss = 1, ee, ans = 0;
    scanf("%lld%lld", &n, &k);
    n++;
    e = ee = n;
    for(int i = 1 ; i <= n ; i++)
    {
        if(i >= k)
        {
            ans += sumRange(ss, ee);
            ans %= mod;
        }
        ss += (++s);
        ee += (--e);
    }
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 3824
# 約数生成
def make_divisors(a,b):
    divisors = []
    for i in range(1, a+1):
        if a % i == 0:
            if b % i == 0:
                divisors.append(i)

    divisors.sort(reverse=True)
    return divisors
a,b,k = map(int, input().split())
y = make_divisors(a,b)
print(y[k-1])

Measured memory_bytes: 38256
/*
AizuOnline A0029
Title english sentence
*/
#include <stdio.h>
// Select Below
//#include <stdlib.h>
#include <string.h>
//#include <float.h>
//#include <math.h>
//#include <limits.h>
//Global data section
char w[1001];
char word[1000][33];
//
int str_comp(void * x,void * y)
{
  char *a,*b;
  a = (char *)x;b= (char *)y;
  return(strcmp(x,y));
}

main()
{
  int i=0;
  int n,hin;
  char *p;  
  char prev[33],saihinwd[33],longestwd[33];
  int longest,maxhin;

  fgets(w,1000,stdin);
  p=strtok(w," ");

  do  {
    strncpy(&(word[i][0]),p,33);
	i++;	
  } while(p=strtok(NULL," "));

  n=i;
  qsort(word,n,33,str_comp);
  //for(i=0;i<10;i++)
  //  printf("%d %s\n",i,word[i]);

  longest=0;
  for(i=0;i<n;i++)
    if(longest < strlen(word[i]))
      {
	longest = strlen(word[i]);
	strcpy(longestwd,word[i]);
      }
  maxhin=1;prev[0]='\0';hin=0;
  strcpy(saihinwd,word[0]);
  for(i=0;i<n;i++)
    {
      if(0==strcmp(prev,word[i]))
	{
	  hin++;
	  if(hin > maxhin)
	    {
	      maxhin=hin;
	      strcpy(saihinwd,word[i]);
	    }
	}
      else
	hin=1;
      strcpy(prev,word[i]);
    }
  printf("%s %s\n",saihinwd,longestwd);

return(0);
}

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

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

    int n;
    cin >> n;

    cout << (n-2) * 180 << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <queue>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fst first
#define snd second
const ll mod = 1000000007;
const ll INF = (1LL << 59);
const int iINF = (1 << 31)-1;
const double dINF = 1000000000000000000.0;
const int MAX_N = 1 << 17;

int main(){
	string s; cin >> s;
	s[5] = ' '; s[13] = ' ';
	cout << s << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <numeric>
#include <string>
#include <cassert>
#include <bitset>

using namespace std;
typedef long long ll;

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

int main() {
  ll A, B;
  cin >> A >> B;
  cout << A * B / gcd(A, B) << endl;
  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
  int c=0,i,hight=0;
  char s[2];
  
  scanf("%s",s);
  
    if(s[0]=='A')
      puts("T");
    if(s[0]=='T')
      puts("A");
      if(s[0]=='G')
      puts("C");
      if(s[0]=='C')
      puts("G");

  return 0;
}

Measured memory_bytes: 128
n = int(input())

ans = [0] * (n + 1)
for x in range(1, n + 1):
    if x**2 + x >= n:
        break
    for y in range(1, n + 1):
        if x**2 + y**2 + x * y + x + y >= n:
            break
        for z in range(1, n + 1):
            w = x**2 + y**2 + z**2 + x * y + y * z + z * x
            if w > n:
                break
            else:
                ans[w] += 1

for i in range(1, n + 1):
    print(ans[i])


Measured memory_bytes: 69196
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
#define sz(a) ll(a.size())
ll ch[100005];
ll func(ll a,ll b,ll c){
	ll d=b*b-4*a*c;
	if(d<0||ch[d]==0)
	return 0;
	d=ch[d];
	a*=2;
	if(d<=b||(d-b)%a)
	return 0;
	return 1;
}
void solve(){
	ll n;
	cin>>n;
	for(ll i=1;i<=n;i++){
		ll res=0;
		for(ll j=1;j<=100;j++)
		for(ll k=1;k<=100;k++){
			ll val=i-j*j-k*k-j*k;
			if(val<0)
			continue;
			if(func(1,j+k,-val))
			res++;
		}
		cout<<res<<"\n";
	}
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    for(ll i=1;i<=100000;i++){
		ld val=sqrt(i);
		if(val==floor(val))
		ch[i]=val;
	}
    ll qc=1;
    //cin>>qc;
    for(ll i=1;i<=qc;i++)
	solve();
}


Measured memory_bytes: 4324
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define umap unordered_map
#define get(a, n)    for(ll i = 0; i < n; i++)    cin>>a[i];
#define error(x) fixed<<setprecision(x) //cout<<error(5)<<someDouble    -> example - 3.14159
ll n;	double p[3000];
umap<ll, umap<ll, double>> dp;
double solve(ll i, ll hc){	//index, head count
	if(i == n)	return (hc > i - hc);
	if(dp[i].count(hc))	return dp[i][hc];
	return dp[i][hc] = (1-p[i])*solve(i+1, hc) + p[i]*solve(i+1, hc+1);
}
int main(){
	cin>>n;	get(p, n);
	cout<<error(9)<<solve(0, 0);
}

// Time: O(N*N)
// Space: O(N*N)

Measured memory_bytes: 197644
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep1(i, n) for(int i = 1; i < n; i++)
#define all(v) (v).begin(), (v).end()
#define decimal fixed << setprecision(20)
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
using namespace std;
using LL = long long;
const int INF = 1e9;
const LL LLINF = 1e16;

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

    vector<int> ans;
    rep(i, n){
        for(int j=b.size()-1; j>=0; j--){
            if(j+1 == b[j]){
                ans.push_back(b[j]);
                b.erase(b.begin()+j);
                break;
            }
        }
        if(ans.size() != i+1){
            cout << -1 << endl;
            return 0;
        }
    }
    reverse(all(ans));
    for(auto &&i: ans) cout << i << endl;

}


Measured memory_bytes: 256
// REDO

#include <iostream>

using namespace std;

int main() {
  int N, x;
  cin >> N >> x;

  long long cnt = 0, b = 0, a;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    if (a > x) {
      cnt += a - x;
      a = x;
    }
    if (b + a > x) {
      cnt += b + a - x;
      b = x - b;
    } else {
      b = a;
    }
  }

  cout << cnt << endl;
}


Measured memory_bytes: 3600
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG

using namespace std;
typedef long long ll;
typedef vector<ll> vel;
typedef vector<string> ves;
typedef vector<int> vei;
typedef vector<vector<ll>> vvel;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REP(a, b, m) for (ll a = (ll)(b); a < (ll)(m); a++)

int main() {
    ll mod=1000000007;
    ll n;cin>>n;
    vel a(n);
    vel sum(n);
    rep(i,n){
        cin>>a[i];
        if(i==0){
            sum[i]=a[i];
        }else{
            sum[i]=(sum[i-1]+a[i]);
        }
    }
    ll ans=0;
    rep(i,n){
        if(i==n-1){
            break;
        }else {
            ans=(ans+a[i]*((sum[n-1]-sum[i])%mod))%mod;
        }
    }
    cout<<ans%mod<<endl;
}

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

typedef pair<int,int> pii;
typedef pair<int,pii> edge;

const int N = 1e5;

int n,m;
edge li[2*N+5];

vector<int> isi[2*N+5];
vector<int> punya[N+5];

int find_par(int x, int *par){
	if(par[x] == x) return x;
	return par[x] = find_par(par[x],par);
}

int par[N+5];
bool vis[N+5];
map<int,int> uda;
bool hdone[2*N+5];

int bfs(int x){
	memset(vis,0,sizeof(vis));
	memset(hdone,0,sizeof(hdone));
	queue<int> qx;
	while(!qx.empty()) qx.pop();
	qx.push(1);
	int ret = 0;
	while(!qx.empty()){
		ret++;
		int pan = qx.size();
		while(pan--){
			int cur = qx.front(); qx.pop();
			if(vis[cur]) continue;
			vis[cur] = 1;
			for(int i=0;i<punya[cur].size();i++){
				int head = punya[cur][i];
				if(hdone[head]) continue;
				hdone[head] = 1;
				for(int j=0;j<isi[head].size();j++){
					int now = isi[head][j];
					if(vis[now]) continue;
					if(now == n) return ret;
					qx.push(now);
				}
			}
		}
	}
	return -1;
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int p,q,c;
		scanf("%d%d%d",&p,&q,&c);
		li[i] = make_pair(c,make_pair(p,q));
	}
	sort(li+1,li+m+1);
	int cnt = 0;
	for(int i=1;i<=n;i++) par[i] = i;
	int l = 1;
	for(int i=2;i<=m;i++){
		if(li[i].first == li[i-1].first) continue;
		for(int j=l;j<i;j++){
			int pu = find_par(li[j].second.first,par);
			int pv = find_par(li[j].second.second,par);
			if(pu == pv) continue;
			par[pu] = pv;
		}
		for(int j=l;j<i;j++){
			int u = li[j].second.first;
			int v = li[j].second.second;
			int pu = find_par(u,par);
			int pv = find_par(v,par);
			if(!vis[u]){
				if(uda.find(pu) == uda.end()){
					cnt++;
					uda[pu] = cnt;
				}
				int idx = uda[pu];
				punya[u].push_back(idx);
				isi[idx].push_back(u);
			}
			
			if(!vis[v]){
				if(uda.find(pv) == uda.end()){
					cnt++;
					uda[pv] = cnt;
				}
				int idx = uda[pv];
				punya[v].push_back(idx);
				isi[idx].push_back(v);
			}
		}
		for(int j=l;j<i;j++){
			int u = li[j].second.first;
			int v = li[j].second.second;
			par[u] = u;
			par[v] = v;
			vis[u] = 0;
			vis[v] = 0;
		}
		l = i;
		uda.clear();
	}
	for(int j=l;j<=m;j++){
		int pu = find_par(li[j].second.first,par);
		int pv = find_par(li[j].second.second,par);
		if(pu == pv) continue;
		par[pu] = pv;
	}
	for(int j=l;j<=m;j++){
		int u = li[j].second.first;
		int v = li[j].second.second;
		int pu = find_par(u,par);
		int pv = find_par(v,par);
		if(!vis[u]){
			if(uda.find(pu) == uda.end()){
				cnt++;
				uda[pu] = cnt;
			}
			int idx = uda[pu];
			punya[u].push_back(idx);
			isi[idx].push_back(u);
		}
		
		if(!vis[v]){
			if(uda.find(pv) == uda.end()){
				cnt++;
				uda[pv] = cnt;
			}
			int idx = uda[pv];
			punya[v].push_back(idx);
			isi[idx].push_back(v);
		}
	}
	for(int j=l;j<=m;j++){
		int u = li[j].second.first;
		int v = li[j].second.second;
		par[u] = u;
		par[v] = v;
		vis[u] = 0;
		vis[v] = 0;
	}
//	l = i;
	uda.clear();
	int ans = bfs(1);
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 20480
#include <algorithm>
#include <climits>
#include <cstring>
#include <inttypes.h>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

const int64_t mod = 1e9 + 7;
int N;
int64_t K;
int64_t dp[63][51][51], ans[63][51][51];

inline void add(int64_t& x, int64_t y) {
    x += y;
    if (x >= mod)
        x -= mod;
}

inline int64_t multiply(int64_t x, int64_t y) {
    return (x * y) % mod;
}

int main() {
    scanf("%d %" SCNd64, &N, &K);
    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= N; ++j)
            scanf(" %" SCNd64, &dp[0][i][j]);
    fill(&dp[1][0][0], &dp[0][0][0] + 63 * 51 * 51, 0);

    for (int b = 1; b < 63 && (int64_t(1) << b) <= K; ++b) {
        for (int i = 1; i <= N; ++i)
            for (int j = 1; j <= N; ++j)
                for (int k = 1; k <= N; ++k)
                    add(dp[b][i][j], multiply(dp[b - 1][i][k], dp[b - 1][k][j]));
    }

//     for (int b = 0; b < 2; ++b) {
//         printf("\n");
//         for (int i = 1; i <= N; ++i) {
//             for (int j = 1; j <= N; ++j)
//                 printf("%" PRId64 " ", dp[b][i][j]);
//             printf("\n");
//         }
//     }
    
    fill(&ans[0][0][0], &ans[0][0][0] + 63 * 51 * 51, 0);
    int b = 0;
    while (((int64_t(1) << b) & K) == 0)
        ++b;
    copy(&dp[b][0][0], &dp[b][0][0] + 51 * 51, &ans[b][0][0]);

    for (++b; b < 63 && (int64_t(1) << b) <= K; ++b)
        for (int i = 1; i <= N; ++i)
            for (int j = 1; j <= N; ++j) {
                if ((int64_t(1) << b) & K) {
                    for (int k = 1; k <= N; ++k)
                        add(ans[b][i][j], multiply(ans[b - 1][i][k], dp[b][k][j]));
                }
                else
                    ans[b][i][j] = ans[b - 1][i][j];
            }

//     for (int b = 0; b < 2; ++b) {
//         printf("\n");
//         for (int i = 1; i <= N; ++i) {
//             for (int j = 1; j <= N; ++j)
//                 printf("%" PRId64 " ", ans[b][i][j]);
//             printf("\n");
//         }
//     }

    int64_t res = 0;
    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= N; ++j)
            add(res, ans[b - 1][i][j]);
    printf("%" PRId64 "\n", res % mod);
}





Measured memory_bytes: 2816
#include <iostream>
#include <cstring>
#include<vector>
#include <algorithm>
#include<cstdlib>
#include<set>
#include<math.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(ll i=0;i<n;i++)

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

	ll c = 0;
	rep(t, n)
	{
		if (p[t] != t + 1)
		{
			c++;
		}
	}

	if (c <= 2) cout << "YES" << "\n";
	else cout << "NO" << "\n";
	return 0;
}

Measured memory_bytes: 3596
#include<stdio.h>
#include<string.h>
//強い回文だと確かめるには全体と前半分もしくは後ろ半分だけを判定すればよい。後ろ半分は前半分を反転させたものだから
int main()
{
	char s[100];
	//char s1[50],s2[50];
	int n,i,a=0,b=0;
	//int n1,n2,c=0;

	scanf("%s",s);
	/*if(s=="ceecbceec"){
		printf("Yes\n");
		return 0;
	}*/
	n=strlen(s);
	for(i=0;i<(n-1)/2;i++){
		if(s[i]==s[n-1-i])//strcmp(s[i],s[n-1-i])==0一文字分ならstrcmpを使わなくとも==だけで比較すればよい
			a++;
		if(s[i]==s[(n-1)/2+1+i])
			b++;
	}
	/*strncpy(s1,s,(n-1)/2);
	n1=strlen(s1);
	//s1[n1]='\0';
	for(i=0;i<n1/2;i++)
		if(s1[i]==s1[n1-1-i])
			b++;*/
	/*strncpy(s2,s+(n/2+1),n/2);
	n2=strlen(s2);
	//s2[n2]='\0';
	for(i=0;i<(n2-1)/2;i++)
		if(s[i]==s[n2-1-i])
			c++;*/
	if((a==(n-1)/2)&&(b==(n-1)/2))
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int main(){
int n;
cin>>n;
int h[n];
rep(i,n) cin>>h[i];
int dp[n];
dp[0]=0;
dp[1]=abs(h[1]-h[0]);
for(int i=2;i<n;i++){
    dp[i]=min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2]));
}
cout<<dp[n-1]<<endl;
return 0;

}

Measured memory_bytes: 1024
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner yomikomi = new Scanner(System.in);
    int a = yomikomi.nextInt();
    int b = yomikomi.nextInt();
    System.out.println( (a*b) + " " + ((2 * a)+(2 * b)));
  }
}

Measured memory_bytes: 25760
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<random>
#include<functional>

#define rep(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define pint pair<int, int>
#define mp make_pair

using namespace std;

typedef long long ll;


int n, m;
vector<int> retu[150], dan[150];
int dp[155][10][155];

int dfs(int now, int rk, int rm) {
	if (now == n - 1) {
		return 0;
	}
	if (now == n - 2 && rm < m) {
		return 0;
	}

	if (dp[now][rk][rm] != -1) return dp[now][rk][rm];

	int tmp = 999999999;
	for (int i = 0; i < retu[now+1].size(); i++) {
		int tmp1 = (dan[now][rk] + dan[now + 1][i]) * abs(retu[now][rk] - retu[now + 1][i]);
		tmp1 += dfs(now + 1, i, rm);
		tmp = min(tmp, tmp1);
	}

	if (rm < m) {
		for (int i = 0; i < retu[now + 2].size(); i++) {
			int tmp1 = (dan[now][rk] + dan[now + 2][i]) * abs(retu[now][rk] - retu[now + 2][i]);
			tmp1 += dfs(now + 2, i, rm + 1);
			tmp = min(tmp, tmp1);
		}
	}

	dp[now][rk][rm] = tmp;

	return tmp;
}

int main() {
	while (cin >> n >> m, n)
	{
		
		for (int i = 0; i < n; i++) {
			retu[i].clear();
			dan[i].clear();

			int k;
			cin >> k;
			for (int j = 0; j < k; j++) {
				int x, d;
				cin >> x >> d;

				retu[i].push_back(x);
				dan[i].push_back(d);

				for (int l = 0; l <= m; l++) {
					dp[i][j][l] = -1;
				}
			}
		}

		int ans = 999999999;

		for (int i = 0; i < retu[0].size(); i++) {
			ans = min(ans, dfs(0, i, 0));
		}

		if (m > 0){
			for (int i = 0; i < retu[1].size(); i++) {
				ans = min(ans, dfs(1, i, 1));
			}
		}

		cout << ans << endl;

	}


	return 0;
}





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

const int M = 1000005;
int main() {
    int n; cin >> n;
    int a[n];
    rep(i, n) cin >> a[i];
    vector<int> cnt(M);
    rep(i, n){
        if(cnt[a[i]] != 0){
            cnt[a[i]] = 2;
            continue;
        }
        for(int j = a[i]; j < M; j+=a[i]) cnt[j]++;
    }

    int ans = 0;
    rep(i, n){
        if(cnt[a[i]] == 1) ans++;
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 8000
#include <bits/stdc++.h>
#define exrep(i, a, b) for(long long i = a; i <= b; i++)
#define rep(i,n)for(long long i=0;i<(long long)(n);i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vector<ll> > vvl;
typedef vector<vector<P> > vvp;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
 
const ll MOD=1e9+7;
const ll INF=1e18;
const int MAX=510000;
const double pi=acos(-1);
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false); 
  
  string s;
  cin >> s;
  int n=s.size();
  if(s[n-1] == '3') cout << "bon" << endl;
  else if(s[n-1] == '0' || s[n-1] == '1'||s[n-1] == '6'||s[n-1] == '8')cout << "pon" << endl;
  else cout << "hon" << endl;
    
  return 0;     
}


Measured memory_bytes: 3652
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
using namespace std;

#define INF (1ll<<60)


long long M = 1000000007;


int main(int argc, char const *argv[]) {
    int n;cin>>n;
    int m = 3;
    vector<vector<long long>> a(n,vector<long long>(m)), dp(n,vector<long long>(m,-1ll<<62));
    for(int i=0;i<n;++i){
        for(int k=0;k<m;++k)cin>>a[i][k];
    }
    for(int k=0;k<m;++k)dp[0][k] = a[0][k];
    for(int i=1;i<n;++i){
        for(int k=0;k<m;++k){
            for(int l=0;l<m;++l){
                if(l!=k)dp[i][k] = max(dp[i-1][l],dp[i][k]);
            }
            dp[i][k] += a[i][k];
        }
    }
    long long res = -1ll<<62;
    for(int i=0;i<m;++i)res = max(res,dp[n-1][i]);
    cout<<res<<endl;
    return 0;
}

Measured memory_bytes: 11136
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>
#include <iomanip>
#include <cassert>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
#define debugArrayP(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge].first<< " " << x[hoge].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = INT_MAX;
const ll MOD = 998244353;

ll dp[4010][4010];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;cin>>S;
  ll N=S.length();
  ll J=0;
  dp[0][1]=1;
  repeat(i,N){
    J+=S[i]-'0';
    for(int j=max(0ll,J-i-1);j<=min(i+1,J);j++){
      dp[i+1][j+1] = (dp[i][j+1]+dp[i][j])%MOD;
    }
  }
  repeat(i,N){
    for(int j=max(0ll,J-N+i+1);j<=min(N+i+1,J);j++){
      dp[N+i+1][j+1] = (dp[N+i][j+1]+dp[N+i][j])%MOD;
    }
  }
  /*
  repeat(i,2*N+1){
    debug(i);
    debug(dp[i][i+1]);
  }
  */
  cout << dp[2*N][J+1] << endl;
  return 0;
}


Measured memory_bytes: 123520
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<b?(a=b,1):0)
#define chmin(a,b) (a>b?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

vector<int> g[100000];
int n,m;

int color[100000];

int c1,c2;
bool dfs(int v, int c) {
  color[v] = c;
  if (c == 1) c1++;
  else c2++;
  FOR(it, g[v]) {
    if (color[*it] == c) return 0;
    if (color[*it] == 0 && !dfs(*it,-c)) return 0;
  }
  return 1;
}

int dp[100001];
ll ans;
bool solve() {
  map<int,int> mp;
  int summin = 0;
  int sumdiff = 0;
  REP(i,n) {
    if (color[i] == 0) {
      c1 = 0, c2 = 0;
      if (!dfs(i,1)) return 0;
      mp[abs(c1-c2)]++;
      summin += min(c1,c2);
      sumdiff += abs(c1-c2);
    }
  }
  memset(dp,-1,sizeof(dp));
  dp[0] = 0;
  int i = 0;
  FOR(it, mp) {
    REP(j,n+1) {
      if (dp[j] >= 0) {
        dp[j] = it->second;
      } else if (j<it->first || dp[j-it->first] <= 0) {
        dp[j] = -1;
      } else {
        dp[j] = dp[j-it->first]-1;
      }
    }
    i++;
  }
  ans = 0;
  REP(i,n+1) {
    if (dp[i] >= 0) {
      ll a = summin + i;
      ll b = summin + sumdiff - i;
      chmax(ans, a*b);
    }
  }
  ans -= m;
  return 1;
}

int main() {
  cin >> n >> m;
  REP(i,m) {
    int a,b;
    cin >> a >> b;
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  if (!solve()) cout << -1 << endl;
  else cout << ans << endl;
}

Measured memory_bytes: 8064