akhauriyash/Code-Regression huggingface dataset:
A = [[],[],[]]
A[0] = list(map(int, input().split()))
A[1] = list(map(int, input().split()))
A[2] = list(map(int, input().split()))

complete = [[False, False, False] for i in range(3)]

N = int(input())
for i in range(N):
    val = int(input())

    for y in range(3):
        for x in range(3):
            if A[y][x] == val: complete[y][x] = True

bingo = False
for y in range(3):
    if all(complete[y]): bingo = True

for x in range(3):
    if (all(complete[i][x] for i in range(3))): bingo = True

if all(complete[i][i] for i in range(3)): bingo = True
if all(complete[i][3-i-1] for i in range(3)): bingo = True

if bingo:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 38472
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <climits>
#include <algorithm>
using namespace std;
int main(void){
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    vector<int> a(n);
    for(int i=0; i<n-1; i++){
        if(s[i] == 'A' && s[i+1] == 'C'){
            a[i+1] = a[i] + 1;
        }else{
        	a[i+1] = a[i];
        }
    }
    for(int i=0; i<q; i++){
        int l,r;
        cin >> l >> r;
        int ans = a[r-1] - a[l-1];
        cout << ans << endl;
    }
}

Measured memory_bytes: 1536
/*|In The Name Of Allah|*/

#include <bits/stdc++.h>

using namespace std;

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

int n;
int x;
bool v[N];
int mem[N];

int go(int idx){
    if(v[idx])
        return 0;
    if(idx == n)
        return 1;
    if(idx > n)
        return 0;
    if(~mem[idx])
        return mem[idx];
    return mem[idx] = (go(idx + 1) + go(idx + 2)) % mod;
}

int main(){
    scanf("%d%d" , &n , &x);
    for(int i = 1; i <= x; i++){
        int a;
        scanf("%d" , &a);
        v[a] = 1;
    }
    memset(mem , -1 , sizeof mem);
    printf("%d\n" , go(0));
}


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

using namespace std;

int main(void) {
	int N;
	cin >> N;
	long long allOdd = 1;
	for(int i = 0; i < N; ++i) {
		int A;
		cin >> A;
		if(A % 2 == 0) {
			allOdd *= 2;
		}
	}
	cout << (pow(3, N) - allOdd) << '\n';
	return 0;
}



Measured memory_bytes: 256
n = int(input())
v_list = [int(x) for x in input().split()]
v_list.sort()
while len(v_list) > 1:
    a = v_list.pop(0)
    b = v_list.pop(0)
    v_list.insert(0, (a + b) / 2)
print(v_list[0])


Measured memory_bytes: 2940
#include<iostream>
#include<algorithm>
#include<climits>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(arg) cin>>arg
#define prin(arg) cout<<arg<<"\n"
#define prin2(arg1,arg2) cout<<arg1<<" "<<arg2<<"\n"
using namespace std;
typedef long long ll;
const int NMAX=1<<17;
int x,y,z;
int main(){
  scan(x);scan(y);scan(z);
  if(x%(y+z)>=z){
    prin(x/(y+z));
  }
  else{
    prin(x/(y+z)-1);
  }
    
  
  return 0;
}


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


int main()
{
	string str;
	int m, h, size;

	while (1){
		cin >> str;
		if (str[0] == '-'){
			break;
		}
		cin >> m;
		size = str.size();

		for (int i = 0; i < m; i++){
			cin >> h;
			for (int j = 0; j < h; j++){
				str = str + str[j];
			}
			str = str.substr(h, size);
		}
		cout << str << endl;
	}
	
	return 0;
}


Measured memory_bytes: 3140
from math import sqrt, ceil

def_val = 2 ** 31 - 1
n, q = map(int, input().split())
unit = ceil(sqrt(n))

l = [(def_val, [def_val] * unit) for _ in range(unit)]

while q:
    op, s, t = map(int, input().split())
    sd, sm = s // unit, s % unit
    if op:
        td, tm = t // unit, t % unit
        if sd == td:
            print(min(l[sd][1][sm:tm + 1]))
        else:
            unit_min = min((tup[0] for tup in l[sd + 1:td]), default=def_val)
            s_min = min(l[sd][1][sm:], default=def_val)
            t_min = min(l[td][1][:tm + 1], default=def_val)
            print(min(unit_min, s_min, t_min))
    else:
        unit_list = l[sd][1]
        unit_list[sm] = t
        l[sd] = (min(unit_list), unit_list)
    q -= 1

Measured memory_bytes: 9672
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();
		for(int i=0;i<W+2;i++) {
			System.out.print("#");
		}
		System.out.println();
		for(int j=0;j<H;j++) {
			System.out.print("#");
			String s = sc.next();
			System.out.print(s);
			System.out.print("#");
			System.out.println();
		}
		for(int i=0;i<W+2;i++) {
			System.out.print("#");
		}
		sc.close();
	}

}

Measured memory_bytes: 21972
#include <iostream>

using namespace std;

int main(){
  int N;
  cin >> N;
  char S[N];
  cin >> S;

  int count = 0;
  int sum_e[N];
  for (int i = 0; i < N; i++){
    if (S[i] == 'E'){
      count++;
    }
    sum_e[i] = count;
    //cout << sum_e[i] << endl;
  }
  int min = N;
  int tmp;
  for (int i = 0; i < N; i++){
    if (i == 0){
      tmp = sum_e[N-1] - sum_e[i];
    }else if (i == N-1){
      tmp = i - sum_e[i-1];
    }else{
      tmp = sum_e[N-1] - sum_e[i] + i - sum_e[i-1];
    }
    //cout << tmp << endl;
    if (tmp < min) min = tmp;
  }
  cout << min << endl;
  return 0;
}


Measured memory_bytes: 1664
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

// input に使うやつ
var rdr = bufio.NewReaderSize(os.Stdin, 10000)

func readLine() string {
	buf := make([]byte, 0, 10000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

// string から int へ
func atoi(s string) int {
	i, _ := strconv.Atoi(s)
	return i
}

// 一つの文字を int として読み込み
func readInt() int {
	return atoi(readLine())
}

// 複数の文字を int のスライスとして読み込み
func readInts() []int {
	in := readLine()
	ins := strings.Split(in, " ")
	out := make([]int, len(ins))
	for i, v := range ins {
		out[i] = atoi(v)
	}
	return out
}

// sortしたスライスを返す
func sorted(a []int) []int {
	sort.Ints(a)
	b := a
	return b
}

// 逆順のsort
func reverse(a []int) []int {
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	b := a
	return b
}

func judge(a []int) bool {
	mae := a[0]
	for i := 1; i < len(a); i++ {
		if mae+1 != a[i] {
			return false
		}
		mae = a[i]
	}
	return true
}

func main() {
	_ = readInt()
	p := readInts()

	if judge(p) {
		fmt.Println("YES")
		return
	}

	var a int
	var b int
	for i := 0; i < len(p); i++ {
		if p[i] != i+1 {
			a = i
		}
	}
	for i := 0; i < len(p); i++ {
		if p[i] != i+1 && p[i] != p[a] {
			b = i
		}
	}
	p[a], p[b] = p[b], p[a]
	if judge(p) {
		fmt.Println("YES")
		return
	}
	fmt.Println("NO")
}


Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include<random>
#include<math.h>
#include<limits.h>
#include<iterator>
#define rep(i,a) for(int i=0;i<a;i++)
#define nrep(i,a,b) for(int i=a;i<b;i++)
#define mrep(i,a) for(int i=a;i>=0;i--)
#define ll long long
#define vl vector<ll> 
#define vvl vector<vector<ll> > 
#define vb vector<bool> 
#define vvb vector<vector<bool> > 
#define INF 1123456789012345
using namespace std;

ll fun(ll a, ll b, ll c){
	ll maxi = max(a, max(b, c));
	ll mini = min(a, min(b, c));
	return maxi - mini;
}

ll gu_gu(ll w, ll h){
	ll ans = INF;
	nrep(i, 1, w){
		if ((w - i) % 2) ans = min(ans, fun(i*h, ((w - i) / 2)*h, ((w - i) / 2 + 1)*h));
		else ans = min(ans, abs(i*h - ((w - i) / 2)*h));
	}nrep(i, 1, h){
		if ((h - i) % 2) ans = min(ans, fun(i*w, ((h - i) / 2)*w, ((h - i) / 2 + 1)*w));
		else ans = min(ans, abs(i*w - ((h - i) / 2)*w));
	}
	ll t = w / 2;
	nrep(i, 1, h ) ans = min(ans, abs(t*i - (h - i)*w));
	t = h / 2;
	nrep(i, 1, w ) ans = min(ans, abs(t*i - (w - i)*h));
	return ans;
}

ll ki_gu(ll w, ll h){
	ll ans = INF;
	ll ki, gu;
	if (w % 2){
		ki = w; gu = h;
	}
	else{
		ki = h; gu = w;
	}
	nrep(i, 1, w){
		if ((w - i) % 2) ans = min(ans, fun(i*h, ((w - i) / 2)*h, ((w - i) / 2 + 1)*h));
		else ans = min(ans, abs(i*h - ((w - i) / 2)*h));
	}nrep(i, 1, h){
		if ((h - i) % 2) ans = min(ans, fun(i*w, ((h - i) / 2)*w, ((h - i) / 2 + 1)*w));
		else ans = min(ans, abs(i*w - ((h - i) / 2)*w));
	}
	ll t = gu / 2;
	nrep(i, 1, ki ) ans = min(ans, abs(t*i - (ki - i)*gu));
	t = ki / 2;
	nrep(i, 1, gu ) ans = min(ans, fun(t*i, (t + 1)*i, (gu - i)*ki));
	return ans;
}


ll ki_ki(ll w, ll h){
	ll ans = INF;
	nrep(i, 1, w){
		if ((w - i) % 2) ans = min(ans, fun(i*h, ((w - i) / 2)*h, ((w - i) / 2 + 1)*h));
		else ans = min(ans, abs(i*h - ((w - i) / 2)*h));
	}nrep(i, 1, h){
		if ((h - i) % 2) ans = min(ans, fun(i*w, ((h - i) / 2)*w, ((h - i) / 2 + 1)*w));
		else ans = min(ans, abs(i*w - ((h - i) / 2)*w));
	}
	ll t = w / 2;
	nrep(i, 1, h) ans = min(ans, fun(t*i, (t + 1)*i, (h - i)*w));
	t = h / 2;
	nrep(i, 1, w) ans = min(ans, fun(t*i, (t + 1)*i, (w - i)*h));
	return ans;
}

int main(){
	ll h, w; cin >> h >> w;
	ll ans = 0;
	if (!(h % 3) || !(w % 3)){
		cout << 0 << endl;
		return 0;
	}
	if (!(h % 2) && !(w % 2)) ans = gu_gu(w, h);
	else if (h % 2 && w % 2) ans = ki_ki(w, h);
	else ans = ki_gu(w, h);
	cout << ans << endl;
	return 0;
}

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

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

int main(){
    ll a, b;
    cin >> a >> b;
    if ( a < b ) swap(a, b);
    ll gcd_num = gcd(a,b);
    set<ll> st;
    ll max_num = gcd_num;
    for ( ll i = 2; i * i <= max_num; ++i ) {
        while(1) {
            if ( gcd_num == 1 ) break;
            if ( gcd_num % i != 0 ) break;
            gcd_num /= i;
            st.insert(i);
        }
    }
    if ( gcd_num != 1 ) {
        st.insert(gcd_num);
    }
//    cout << max_num << endl;
//    cout << st.size() << endl;
    cout << st.size() + 1 << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
using namespace std;

void bom(int x,int y,vector<string>&in){
in[x][y]='0';
loop(i,-3,4)
loop(j,-3,4){
if(i&&j)continue;
if(x+i>=0&&x+i<8&&y+j>=0&&y+j<8&&in[x+i][y+j]=='1')bom(x+i,y+j,in);
}
}

int main(){
int n;
cin>>n;
rep(i,n){
vector<string> in(8);
rep(j,8)cin>>in[j];
int x,y;
cin>>x>>y;
if(in[y-1][x-1]=='1')bom(y-1,x-1,in);
cout<<"Data "<<i+1<<':'<<endl;
rep(j,8)cout<<in[j]<<endl;
}
}

Measured memory_bytes: 1216
//Created by:Coding_lover(29-07-2020 14:32:16)
#include <bits/stdc++.h>
#define ll long long
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define INF LLONG_MAX
#define f first
#define s second
#define LB lower_bound
#define UB upper_bound
#define pi pair<long long,long long>
#define pii pair<long long,pi>
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define vc vector
#define vll vector<long long>
#define rep(i, a, n) for (ll i = a; i < (long long)n; ++i)
#define repe(i, a, n) for (ll i = a; i <= (long long)n; ++i)
#define repr(i, n, a) for (ll i = n; i > (long long)a; --i)
#define repre(i, n, a) for (ll i = n; i >= (long long)a; --i)
#define dbg(x) cout<<"Debugging "<<#x<<":"<<endl;cout<<#x<<" = "<<x<<endl
#define dbgv(v) cout<<"Debugging "<<#v<<":"<<endl; for(ll i=0;i<v.size();i++){cout<<v[i]<<" ";}cout<<endl;
using namespace std;
int main() {
  FASTER;
  ll t=1;
  //cin >> t;
  while (t--) {
  	ll n,k;
  	cin>>n>>k;
  	vll a(n);
  	rep(i,0,n)cin>>a[i];
  	vll dp(k+1);
  	repe(i,1,k){
  		for(auto j:a){
  			if(i-j<0)continue;
  			else
  			{
  				if(dp[i-j]==0)dp[i]=1;
  			}
  		}
  	}
  	if(dp[k]==1)cout<<"First"<<endl;
  	else cout<<"Second"<<endl;
  }
  return 0;
}

Measured memory_bytes: 3984
#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++)

int main() {

    string yohou;
    string jissai;
    cin >> yohou ;
    cin >> jissai;
    int count = 0;
    rep(i, 3)
    {
        if ( yohou[i] == jissai[i] )
            count++;
    }
  cout << count;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
  cin>>n;
  vector<long long >nos(n,0);
  for(int i=0;i<n;i++)cin>>nos[i];
  long long count=0,sum=0;
  for(int i=0;i<n;i++){
    if(nos[i]==0){count+=sum/2;sum=0;continue;}
    sum+=nos[i];
  }
  count+=sum/2;
  cout<<count;
}

Measured memory_bytes: 3960
#include <bits/stdc++.h>
#define maxn 100000
using namespace std;

typedef long long LL;
int fa[maxn], off[maxn];

int find(int x) {
    if (fa[x] == x) return x;
    int temp = fa[x];
    fa[x] = find(fa[x]);
    off[x] += off[temp];
    return fa[x];
}


int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) fa[i] = i;
    for (int i = 0; i < m; ++i) {
        int l, r, d;
        scanf("%d%d%d", &l, &r, &d);
        int fl = find(l), fr = find(r);
        if (fl == fr) {
            if (off[r] - off[l] == d) continue;
            else { puts("No"); return 0; }
        }
        else {
            fa[fr] = fl;
            off[fr] = d - off[r] + off[l];
        }
    }
    puts("Yes");
    return 0;
}

Measured memory_bytes: 4504
#include<bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
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; }


using namespace std;

int main(){
    int n;
    cin >> n;
    pair<string, int> m[51];
    string s;
    int tmp;
    rep(i, n) {
        cin >> s >> tmp;
        m[i] = make_pair(s, tmp);
    }
    cin >> s;
    ll ans = 0;
    bool flag = false;
    rep(i, n){
        if (flag) {
            ans += m[i].second;
        }
        if (m[i].first == s) {
            flag = true;
        }

    }

    cout << ans << endl;

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(long long i=(l);i<(r);++i)
#define REP(i,n) FOR(i,0,n)
#define int long long

signed main(){
    int N;
    cin>>N;
    int ans=0;
    int V[N],C[N];
    REP(i,N){
        cin>>V[i];
    }
    REP(i,N){
        cin>>C[i];
    }
    REP(i,N){
        if(V[i]-C[i]>0) ans+=V[i]-C[i];
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace AtCoder.ABC000
{
    class Program
    {
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split(' ').Select(elem => int.Parse(elem));
            var aEven = a.Where((elem, idx) => idx % 2 != 0).ToArray();
            var aOdd = a.Where((elem, idx) => idx % 2 == 0).ToArray();
            var b = new int[n];
            if (n % 2 == 0)
            {
                Array.Reverse(aEven);
                Array.Copy(aEven, b, aEven.Length);
                Array.Copy(aOdd, 0, b, aEven.Length, aOdd.Length);
            }
            else
            {
                Array.Reverse(aOdd);
                Array.Copy(aOdd, b, aOdd.Length);
                Array.Copy(aEven, 0, b, aOdd.Length, aEven.Length);
            }
            Console.WriteLine(String.Join(" ", b));
        }
    }
}

Measured memory_bytes: 50360
#include<stdio.h>
int main(void)
{
	int a,b,x,y,i;
	scanf("%d %d",&a,&b);
	for(i=0;i<=401;i++){
		x=(a-1)*i+1;
		if(x>=b)	break;
	}
	printf("%d\n",i);
	return 0;
}

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

int main() {
    ll x;
    cin>>x;
    for (int i=1; i<=x; i++) {
        ll sum=(i*(i+1))/2;
        if (sum>=x) {
            cout<<i<<endl;
            return 0;
        }
    }
}


Measured memory_bytes: 256
#include <iostream>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

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

	for (int i = 1; i < s.size(); i++) {
		int count = 0;
		for (char c = 'a'; c <= 'z'; c++) {
			bool right = false;
			bool left = false;
			for (int j = 0; j < i; j++)
				if(s[j]==c)
					left = true;
			for (int j = i; j < s.size(); j++)
				if (s[j] == c)
					right = true;
			if (right&&left)
				count++;
		}
		if (ans < count)
			ans = count;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
	
	String INPUT = "";
	
	public static void main(String[] args) throws Exception {
		new Main().run();
	}
	
	void run() {
		Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
		PrintWriter out = new PrintWriter(System.out);
		
		solve(sc, out);
		out.flush();
	}
	
	void solve(Scanner sc, PrintWriter out) {
		int flg[][] = new int[4][13];
		
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 13; j++) {
				flg[i][j] = 1;
			}
		}
		
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			String S = sc.next();
			int a = sc.nextInt();
			if (S.equals("S")) flg[0][a-1]=0;
			if (S.equals("H")) flg[1][a-1]=0;
			if (S.equals("C")) flg[2][a-1]=0;
			if (S.equals("D")) flg[3][a-1]=0;
		}
		
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 13; j++) {
				if (flg[i][j] == 1) {
					if (i == 0) System.out.print("S ");
					if (i == 1) System.out.print("H ");
					if (i == 2) System.out.print("C ");
					if (i == 3) System.out.print("D ");
					System.out.println(j+1);
				}
			}
		}
	}
}



Measured memory_bytes: 26172
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>


using namespace std;

bool graph[8][8];

int dfs(int v, int N, bool visited[8]) {
  bool all_visited = true;
  for (int i = 0; i < N; i++) {
    if (visited[i] == false) {
      all_visited = false;
    }
  }

  if (all_visited) {
    return 1;
  }

  int ret = 0;

  for (int i = 0; i < N; i++) {
    if (graph[v][i] == false)
      continue;
    if (visited[i])
      continue;
    visited[i] = true;
    ret += dfs(i, N, visited);
    visited[i] = false;
  }
  return ret;
}

int main(void) {
  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    graph[A - 1][B - 1] = graph[B - 1][A - 1] = true;
  }

  bool visited[8];
  for (int i = 0; i < N; i++) {
    visited[i] = false;
  }

  visited[0] = true;
  cout << dfs(0, N, visited) << endl;
  return 0;
}

Measured memory_bytes: 3604
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i)
#define _rep(i,x,y) for(int i=x,i##end=y;i>=i##end;--i)
#define ll long long

int main(){
	ll a,b,c,d;
	std::cin>>a>>b>>c>>d;
	std::cout<<std::max(a*c,std::max(a*d,std::max(b*c,b*d)))<<'\n';
	return 0;
}

Measured memory_bytes: 3608
#include <iostream>
#define INF 123456789
using namespace std;

int main() {
	// AOJ0189 Bellman-Ford
	for (;;)
	{
		struct edge {int from,to,cost;};
		int city = 0;//?????¨???????????????????°????(0~7?????§????????????city == 8??§??????)
		int n;
		cin >> n;
		edge es[2*n];
		if (n == 0) break;
		for (int i = 0;i < n;i++)
		{
			int a,b,c;
			cin >> a >> b >> c;
			es[2*i].from = a;
			es[2*i].to = b;
			es[2*i].cost = c;
			es[2*i+1].from = b;
			es[2*i+1].to = a;
			es[2*i+1].cost = c;
 			city = max (max(a,b)+1,city);
		}
		int min = INF;
		int mincity;
		for (int target_city = 0;target_city < city;target_city++)
		{
			//target_city?????????????°???????????¨??????????????????????
			int d[city];
			for (int i = 0;i < city;i++)
			{
				d[i] = INF;
			}
			d[target_city] = 0;
			for (;;)
			{
				bool update = false;
				for (int i = 0;i < 2 * n;i++)
				{
					//???????????????????????????
					if (d[es[i].from] != INF && d[es[i].from] + es[i].cost < d[es[i].to])
					{
						update = true;
						d[es[i].to] = d[es[i].from] + es[i].cost;
					}
				}
				if(!update) break;
			}
			int sum = 0;
			for (int i = 0;i < city;i++)
			{
				sum += d[i];
			}
			if (sum < min)
			{
				min = sum;
				mincity = target_city;
			}
		}
		cout << mincity << " " << min << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
#define repz(i, z, n) for(decltype(n) i = (z); i < (n); i++)
#define rep(i, n) for(decltype(n) i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define SIZE(v) ((ll)(v).size())
#define MAX(v) (*max_element(all(v)))
#define MIN(v) (*min_element(all(v)))
#define INF 1000000000000 //1E+12
#define MOD 1000000007    //合同式の法

// 略
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define READ cin >>
#define PRINT cout <<

typedef long long ll;
typedef unsigned long long ul;
// 1s間で可能なループ回数2E+8回
// intの最大値2147483647~2E+9
// long longの最大値9223372036854775807〜9E+18
// 総和accumulate
// 単純リストvector<> push_back
// 連想配列map<key,value> insert,
// pair キーとバリューを格納 firstとsecondでアクセス
// 小数点指定 setprecision()
// to_string<->stoi
// stoll(longlong)
// stod(double)
// 大文字65-90(-32)
// 小文字97-122(+32)
// mod:1E+9 + 7
const ll mod = 1000000000+7;
using namespace std;
ll modpow(ll a, ll n){
  ll res=1;
  while(n>0){
    if(n & 1) res = res*a%mod;
    a = a*a%mod;
    n >>= 1;
  }
  return res;
}
int main(){
  // faster
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n, k; cin >> n >> k;

  vector<ll> dp(k+1,0);
  for(ll i=k;i>=1;i--){
    dp[i]=modpow(k/i,n);
    for(ll j=2*i;j<=k;j+=i) dp[i]-=dp[j];
    dp[i]%=mod;
  }
  ll res = 0;
  for(ll i=1;i<=k;i++){
    res+=i%mod*dp[i];
    res%=mod;
  }
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 3996
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>

#define sp ' '
using namespace std;

int A, B;

int main(){
	cin >> A >> B;
	if (B%A) {
		cout << B - A << endl;
	}
	else {
		cout << A + B << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input  freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define zip(i,a,b) for(int i=a;i<b;i++)
#define rzip(i,a,b) for(int i=a;i>=b;i--)
#define ll unsigned long long int
#define test int t;cin>>t; while(t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int>     pii;
typedef pair<char, int>    pci;
typedef pair<char, char>   pcc;
typedef vector<pii>       vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18+5)
#define inf (1e9+5)
#define mod 1000000007


bool check_prime(long long n)
{
	int flag = 0;
	for (long long i = 2; i * i <= n; i++)
	{
		if (n % i == 0)
		{
			flag = 1;
			break;
		}
	}
	if (n == 1)
		return false;
	else if (flag == 0 || n == 2 || n == 3)
	{
		return true;
	}
	else
	{
		return false;
	}

}

int BE(int x, int n, int m) //function to calculate x raise to power n modulo m
{
	int result = 1;
	while (n > 0)
	{
		if (n % 2 == 1)
			result = result * x % m;
		x = x * x % m;
		n = n / 2;
	}
	return result;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	if (fopen("input.txt", "r"))
	{
		freopen ("input.txt" , "r" , stdin);
		freopen ("output.txt" , "w" , stdout);
	}
#endif
	//_______________________________-code starts-_______________________________________________

	int n;
	cin >> n;
	double x[n], y[n];
	zip(i, 0, n)
	{
		cin >> x[i] >> y[i];
	}
	cout << fixed << setprecision(10);
	ld var = 0;
	zip(i, 0, n)
	{
		zip(j, i + 1, n)
		{
			var += sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
		}
	}
	cout << 2 * var / n << en;




	return 0;
}



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

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) { ll d = euclid(b, a % b, y, x);
        return y -= a/b * x, d; }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1) ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1); return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e) return Mod(1);
        Mod r = *this ^ (e / 2); r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int main() {
    const int M = 256;
    typedef long double ld;
    for (;;) {
        int n; cin >> n; if (!n) break;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        ld mine = 1e100;
        int bs = 0, ba = 0, bc = 0;
        rep(s, 0, 16) rep(a, 0, 16) rep(c, 0, 16) {
            vector<int> freq(M);
            int r = (a * s + c) % M;
            rep(i, 0, n) {
                freq[(v[i] + r) % M]++;
                r = (a * r + c) % M;
            }
            double e = 0;
            rep(i, 0, 256) if (freq[i]) {
                e -= ld(1.0) * freq[i] / n * log10(ld(1.0) * freq[i] / n);
            }
            if (e + 1e-9 < mine) {
                mine = e, bs = s, ba = a, bc = c;
            }
        }
        cout << bs << " " << ba << " " << bc << endl;
    }

}


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

using namespace std;

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

#define MOD 1000000007
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REP1(i, N) for (int i = 1; i <= N; ++i)
#define RREP(i, N) for (int i = N - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()

int a[90][90], b[90][90];
bool dp[90][90][13000];

int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> b[i][j];
    }
  }
  dp[0][0][abs(a[0][0] - b[0][0])] = true;
  dp[0][0][abs(b[0][0] - a[0][0])] = true;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int k = 0; k <= 80 * (h + w); ++k) {
        dp[i + 1][j][abs(k + (a[i + 1][j] - b[i + 1][j]))] |= dp[i][j][k];
        dp[i + 1][j][abs(k + (b[i + 1][j] - a[i + 1][j]))] |= dp[i][j][k];
        dp[i][j + 1][abs(k + (a[i][j + 1] - b[i][j + 1]))] |= dp[i][j][k];
        dp[i][j + 1][abs(k + (b[i][j + 1] - a[i][j + 1]))] |= dp[i][j][k];
      }
    }
  }
  for (int k = 0; k <= 80 * (h + w); ++k) {
    if (dp[h - 1][w - 1][k]) {
      cout << k << endl;
      return 0;
    }
  }
  return 0;
}

Measured memory_bytes: 94336
#!/usr/bin/env python3
import sys
import math

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
"""
後ろから順番に漸化式でいけるかと思ったが、それぞれの駅での電車の間隔が一定でないので、むり
O(n**2)で順番に計算する。
"""


n = int(input())
c, s, f = [0] * (n - 1), [0] * (n - 1), [0] * (n - 1)
for i in range(n - 1):
    c[i], s[i], f[i] = map(int, input().split())
ans = [0] * (n - 1)

for i in range(n - 1):
    time = s[i] + c[i]  # 次の役につく時刻
    for j in range(i + 1, n - 1):
        time = max(math.ceil(time / f[j]) * f[j], s[j]) + c[j]
    ans[i] = time
    print(time)
print(0)


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

struct Point { double x, y; };

struct Circle { Point center; double radius; };

double distance(const Point p, const Point q)
{
    const auto dx = p.x-q.x, dy = p.y-q.y;
    return sqrt(dx*dx + dy*dy);
}

double distance(const Circle a, const Circle b)
{
    const auto d = distance(a.center, b.center);
    return max(0.0, d - a.radius - b.radius);
}

int main()
{
    cout << setprecision(9) << fixed;

    Point start, finish;
    cin >> start.x >> start.y >> finish.x >> finish.y;

    int N; cin >> N;

    auto circles = vector<Circle>(N);
    for (auto& c : circles)
        cin >> c.center.x >> c.center.y >> c.radius;
    circles.push_back({start, 0});
    circles.push_back({finish, 0});

    const auto s = N, t = N+1;
    N += 2;

    auto distances = vector<double>(N, numeric_limits<double>::infinity());
    distances[s] = 0;

    auto candidates = vector<int>(N);
    iota(begin(candidates), end(candidates), 0);
    while (!candidates.empty())
    {
        const auto compare = [&](const int v, const int w) { return distances[v] < distances[w]; };
        const auto v = *min_element(begin(candidates), end(candidates), compare);
        candidates.erase(remove(begin(candidates), end(candidates), v), end(candidates));

        for (auto w = 0; w < N; w++)
            distances[w] = min(distances[w], distances[v] + distance(circles[v], circles[w]));
    }

    cout << distances[t] << endl;
}


Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
	"strings"

)


func main() {
	solve(os.Stdin, os.Stdout)
}

type ReaderEx struct {
	baseScanner *bufio.Scanner
	words       []string
	nextWordIdx func() int
	initWordIdx func(int)
}

func NewScannerEx(stdin io.Reader) *ReaderEx {
	var this ReaderEx
	var idx int
	var wordsCnt int
	this.baseScanner = bufio.NewScanner(stdin)
	this.baseScanner.Buffer(make([]byte, 0), 1E+11)
	this.nextWordIdx = func() int {
		cur := idx
		idx = (idx + 1) % wordsCnt
		return cur
	}
	this.initWordIdx = func(wc int) {
		idx = 0
		wordsCnt = wc
	}
	return &this
}

func (me *ReaderEx) Scan(doSplit bool) (isScanned bool) {
	isScanned = me.baseScanner.Scan()
	if doSplit {
		me.words = strings.Fields(me.baseScanner.Text())
	} else {
		me.words = []string{me.baseScanner.Text()}
	}
	me.initWordIdx(len(me.words))
	return
}

func (me *ReaderEx) nextBytes() []byte {
	return []byte(me.words[me.nextWordIdx()])
}
func (me *ReaderEx) nextString() string {
	return me.words[me.nextWordIdx()]
}

type StringArray []string

func (me *ReaderEx) nextStringArray() StringArray {
	return me.words[me.nextWordIdx():]
}

func (me StringArray) toStrings() []string {
	return me
}

func (me StringArray) toInts() []int {
	strValues := me.toStrings()
	values := make([]int, len(strValues))
	for i, v := range strValues {
		n, err := strconv.Atoi(v)
		if err != nil {
			panic(err)
		}
		values[i] = n
	}
	return values
}
func (me *ReaderEx) nextInt() int {
	n, err := strconv.Atoi(me.words[me.nextWordIdx()])

	defer func() {
		if err != nil {
			panic(fmt.Sprintf("getInt(%d)", n))
		}
	}()
	return n
}

func pow(x, y, mod int) (ans int) {

	ans = 1
	for bit := 1 << uint(math.Floor(math.Log2(float64(y)))); ; bit >>= 1 {
		if y&bit != 0 {
			ans *= x
			if mod > 0 {
				ans %= mod
			}
		}
		if bit <= 1 {
			return
		}
		ans *= ans
		if mod > 0 {
			ans %= mod
		}
	}
}

type factType struct {
	prime int
	count int
}

func fact(n int) (facts []*factType) {

	for i := 2; i*i <= n; i++ {
		if n%i == 0 {
			fa := factType{prime: i}
			for n%fa.prime == 0 {
				n /= fa.prime
				fa.count++
			}
			facts = append(facts, &fa)
		}
	}
	if n > 1 {
		facts = append(facts, &factType{prime: n, count: 1})
	}
	return facts
}
func dec2bin(n int) (ret []bool) {
	for bit := 1 << uint(math.Floor(math.Log2(float64(n)))); ; bit >>= 1 {
		ret = append(ret, n&bit != 0)

		if bit <= 1 {
			return
		}
	}

}

func abs(v int) int {
	if v < 0 {
		v = -v
	}
	return v
}

func b2i(b bool) (i int) {
	if b {
		i = 1
	} else {
		i = 0
	}
	return
}

func solve(stdin io.Reader, stdout io.Writer) {

	r := NewScannerEx(stdin)
	r.Scan(true)
	n, a, b := r.nextInt(), r.nextInt(), r.nextInt()

	ans := 0
	if (b-a)%2 == 0 {
		ans = (b - a + 1) / 2
	} else {

		minInt := func(a int, vs ...int) int {
			for i := range vs {
				v := &vs[i]
				if a > *v {
					a = *v
				}
			}
			return a
		}

		ans = minInt((a+b-1)/2, (n+1-a+n+1-b-1)/2)
	}
	fmt.Fprintln(stdout, ans)
}


Measured memory_bytes: 640
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
typedef long long ll;

#define INF 1000000000

int main(){
    int m,mi,mx;
    while(cin>>m>>mi>>mx, m){
        vector<int> p(m);
        rep(i,m) cin>>p[i];

        sort(all(p), greater<int>());

        int n, gap=0;

        for(int i=mi-1; i<mx; i++){
            if(p[i]-p[i+1] >= gap){
                n=i+1;
                gap = p[i]-p[i+1];
            }
        }
        cout<<n<<endl;
    }

  return 0;
}

Measured memory_bytes: 1220
#include<bits/stdc++.h>
using namespace std;
typedef int sign;
typedef long long ll;
#define For(i,a,b) for(register sign i=(sign)a;i<=(sign)b;++i)
#define Fordown(i,a,b) for(register sign i=(sign)a;i>=(sign)b;--i)
const int N=1e5+5;
template<typename T>bool cmax(T &a,T b){return (a<b)?a=b,1:0;}
template<typename T>bool cmin(T &a,T b){return (a>b)?a=b,1:0;}
template<typename T>T read()
{
	T ans=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)&&ch!='-')ch=getchar();
	if(ch=='-')f=-1,ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch-'0'),ch=getchar();
	return ans*f;
}
template<typename T>void write(T x,char y)
{
	if(x==0)
	{
		putchar('0'),putchar(y);
		return;
	}
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	static char wr[20];
	int top=0;
	for(;x;x/=10)wr[++top]=x%10+'0';
	while(top)putchar(wr[top--]);
	putchar(y);
}
void file()
{
#ifndef ONLINE_JUDGE
	freopen("D.in","r",stdin);
	freopen("D.out","w",stdout);
#endif
}
int n,E,T;
ll p[N];
void input()
{
	n=read<int>(),E=read<int>(),T=read<int>();
	For(i,1,n)p[i]=read<ll>();
}
const ll inf=1e18;
ll dp[N];
void work()
{
	int now=0;
	ll Min=inf;
	For(i,1,n)
	{
		dp[i]=dp[i-1]+T;	
		while(T<=2*(p[i]-p[now+1]))
		{
			cmin(Min,dp[now]-2*p[now+1]);
			now++;
		}
		if(now<i)cmin(dp[i],dp[now]+T);
		cmin(dp[i],Min+2*p[i]);
	}
	write(dp[n]+E,'\n');
}
int main()
{
	//file();
	input();
	work();
	return 0;
}

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

int parse(string a) {
	int b = 0;
	for (char c : a) {
		b *= 10; b += c - 48;
	}
	return b;
}
vector<int>split(string a) {
	vector<int>b;
	string c = "";
	for (char d : a) {
		if (d == ',') { b.push_back(parse(c)); c = ""; }
		else c += d;
	}
	b.push_back(parse(c));
	return b;
}
int main() {
	int a, b;
	cin >> a >> b;
	int c[100][100];
	for (int g = 0; g<a; g++) {
		for (int h = 0; h < a; h++) {
			if (g == h)c[g][h] = 0;
			else c[g][h] = 1<<29;
		}
	}
	for (int d = 0; d < b; d++) {
		string e;
		cin >> e;
		vector<int>f = split(e);
		f[0]--; f[1]--;
		c[f[0]][f[1]] = f[2];
		c[f[1]][f[0]] = f[3];
	}
	for (int x = 0; x < a; x++) {
		for (int y = 0; y < a; y++) {
			for (int z = 0; z < a; z++) {
				c[y][z] = min(c[y][z], c[y][x] + c[x][z]);
			}
		}
	}
	string s;
	cin >> s;
	vector<int>q = split(s);
	q[0]--; q[1]--;
	cout << q[2] - q[3] - c[q[0]][q[1]] - c[q[1]][q[0]] << endl;
}

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

namespace abc144_a
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int a = Convert.ToInt32(input[0]);
            int b = Convert.ToInt32(input[1]);

            int product = a * b;

            if (a < 10 && b < 10)
            {
                Console.WriteLine(product);
            }
            else
            {
                Console.WriteLine("-1");
            }

            Console.Read();

        }
    }
}


Measured memory_bytes: 11220
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<functional>
#include<list>
#include<bitset>
#include<numeric>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> p;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	vector<vector<int>> data(n+1, vector<int>(n+1, 0));
	int l, r;
	for (int i = 0; i < m; i++) {
		cin >> l >> r;
		for (int j = r; j <= n; j++) {
			data[l][j]++;
		}
	}
	int pi, qi;
	int anss;
//	vector<int> ans(q);
	for (int i = 0; i < q; i++) {
		cin >> pi >> qi;
		anss = 0;
		for (int j = pi; j <= qi; j++) {
			anss += data[j][qi];
		}
		//cout << ans << endl;
//		ans[i] = anss;
		cout << anss << endl;
	}
	/*for (int i = 0; i < q; i++) {
		cout << ans[i] << endl;
	}*/
	return 0;
}

Measured memory_bytes: 1920
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <functional>
#include <math.h>
#include <map>
#include <set>
#include <string>
using namespace std;
#define fio ios::sync_with_stdio(false); cin.tie(NULL);

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;

const int INT_INF = 1e9;
const ll LL_INF = 1e18;
const int MAXN = 500003;

typedef struct GE{
	int to;
	ll w;
	GE(){};
}GE;

int n, m;
int A[MAXN];
int main() {
	int D;
	scanf("%d %d", &D, &n);
	
	int ans = 1;
	while(D--)
		ans *= 100;
	
	if(n==100)
		n++;
	printf("%d", ans*n);
	
	return 0;
}

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

typedef long long ll;

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

    ll N, K;
    cin >> N >> K;

    auto P = vector<ll>(N, -1);
    auto C = vector<ll>(N, -1);
    for (ll i = 0; i < N; i++) {
        cin >> P[i];
        P[i]--;
    }
    for (ll i = 0; i < N; i++) {
        cin >> C[i];
    }

    auto vis = vector<bool>(N, false);
    auto ans = vector<ll>(N, ll(-1e18));

    for (ll root = 0; root < N; root++) {
        if (vis[root]) {
            continue;
        }

        vis[root] = true;
        auto cycle = vector<ll>({root});
        auto costs = vector<ll>({C[root]});
        auto u = P[root];
        while (u != root) {
            cycle.push_back(u);
            costs.push_back(C[u]);
            vis[u] = true;
            u = P[u];
        }

        ll L = cycle.size();
        ll S = accumulate(costs.begin(), costs.end(), 0LL);

        for (ll i = 0; i < L; i++) {
            ll cnt = 0;
            for (ll step = 1; step <= min(L, K); step++) {
                cnt += costs[(i + step) % L];
                auto val1 = cnt;
                auto val2 = cnt + (K - step) / L * S;
                ans[cycle[i]] = max({ans[cycle[i]], val1, val2});
            }
        }
    }

    cout << *max_element(ans.begin(), ans.end()) << endl;


    return 0;
}

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

int main() {
  int a, b, c, x;
  cin >> a >> b >> c >> x;
  
  int ans = 0;

  for (int i = 0; i <= a; i++) {
    for (int j = 0; j <= b; j++) {
      for (int k = 0; k <= c; k++) {
         if (500*i + 100*j + 50*k == x) {
           ans++;
         }
       }
    }
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
using namespace std;

int main() {
	string s[10000],w;
	int n;
	int sum= 0;

	cin >> w;
	n = w.length();

	for (int i = 0; i < n; i++) {
		if (isalpha(w[i])) {
			if (isupper(w[i])) w[i] = tolower(w[i]);
		}
	}
	
	//Debug
	/*for (int i = 0; i <= n; i++) {
		cout << w[i];
	}
	cout << endl;
	*/

	//input
	int i = 0,nw;
	while (1) {
		cin >> s[i];
		if (s[i] == "END_OF_TEXT") {
			nw = i;
			break;
		}
		i++;
	}
	i = 0;

	//tolower
	for (int i = 0; i < nw; i++) {
		int a = s[i].length();
		for (int j = 0; j < a; j++) {
			char s0 = s[i][j];
			if (isalpha(s0)) {
				if (isupper(s0)) s[i][j] = tolower(s0);
			}
		}
	}

	//Debug
	/*
	for (int i = 0; i < nw; i++) {
		cout << s[i] << endl;
	}
	*/

	//count
	for (int i = 0; i < nw; i++) {
		if (w == s[i]) sum++;
	}

	cout << sum << endl;
	return 0;
}

Measured memory_bytes: 1280
from operator import itemgetter
n = int(input())
x =[]
y =[]
info = [list(map(int,input().split())) for i in range(n)]
for i in range(n):
  x.append([i,info[i][0]])
  y.append([i,info[i][1]])

x = sorted(x,key=itemgetter(1))
y = sorted(y,key=itemgetter(1))
tree = []

for i in range(n-1):
    tree.append([x[i][0],x[i+1][0],x[i+1][1]-x[i][1]])
    tree.append([y[i][0],y[i+1][0],y[i+1][1]-y[i][1]])

#UnionFindクラス
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]
  
  # xとyの根を結合させる
  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
  
  # x,yの根が同じかどうかを返す（根が同じ:True、根が異なる:False）
  def is_same(self, x, y):
    return self.find(x) == self.find(y)

uf = UnionFind(len(tree))
tree = sorted(tree,key=itemgetter(2))
ans = 0

for i in tree:
  x,y,kyori = i
  if not uf.is_same(x,y):
    uf.union(y,x)
    ans += kyori
print(ans)


Measured memory_bytes: 86580
#include <bits/stdc++.h>
 
using namespace std;
 
#define lli long long int
#define REP(i,s,l) for(lli i=s;i<l;i++)
#define DEBUG 0
#define INF (1LL<<50)
#define MOD 1000000007

bool isJudge(string s){
	lli size = s.size();
	REP(i,0,size/2){
		if(s.at(i) != s.at(size-1-i))return false;
	}
	return true;
}

lli calc(vector<lli> *d){
	lli size = d->size();
	lli rtn=0;
	REP(i,0,size/2){
		rtn += abs(d->at(i)-d->at(size-1-i));
	}
	return rtn;
}

signed main(){

	string s;
	cin>>s;

	string y="";
	vector<lli> d;
	lli cnt=0;

	REP(i,0,s.size()){
		if(s.at(i)!='x'){
			y+=s.at(i);
			d.push_back(cnt);
			cnt=0;
		}
		else {
			cnt++;
		}
	}
	d.push_back(cnt);

	if(isJudge(y)==false){
		cout<<-1<<endl;
		return 0;
	}
	cout<<calc(&d)<<endl;

	return 0;
}

Measured memory_bytes: 1656
#include<bits/stdc++.h>
using namespace std;
int main()
{
  long long n; cin>>n;
  long long ans = 0ll;
  long long p = 1ll;
  while(n>0)
  {
    ans += p;
    p *= 2;
     n /= 2;
  }
  cout<<ans<<"\n";
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n, h; cin >> n >> h;
    int a = 0, b[n];
    for(int i=0; i<n; i++){
        int t;
        cin >> t >> b[i];
        a = max(t, a);
    }
    sort(b, b+n);
    
    int p = n-1;
    while(b[p]>a && h>0)
        h -= b[p--];
    cout << n-1-p+(h>0?(h+a-1)/a:0) << endl;
}


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

#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;

template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7ll;
const double PI=3.14159265358979323846;

vector<int> seg;
int s;
void seg_init(int n) {
    s = 1;
    while(s < n) s*= 2;
    seg.resize(2*s-1, INT_MAX);
    rp(i, 0, n) {
        seg.at(s-1+i) = i+1;
    }
    for(int i = s-2; i >= 0; i--) {
        seg.at(i) = min(seg.at(i*2+1), seg.at(i*2+2));
    }
}

void seg_insert(int a, int b) {
    seg.at(s-1+a) = b;
    int p = s-1+a;
    while(p != 0) {
        p = (p+1)/2 -1;
        seg.at(p) = min(seg.at(p*2+1), seg.at(p*2+2));
    }
}

int seg_find(int x, int y, int a=0, int b=s, int k=0) {
    if(b <= x || y <= a) return INT_MAX;
    if(x <= a && b <= y) return seg.at(k);
    else{
        int left = seg_find(x, y, a, (a+b)/2, 2*k+1);
        int right = seg_find(x, y, (a+b)/2, b, 2*k+2);
        return min(left, right);
    }
}

int main() {
    int n, m; cin >> n >> m;
    seg_init(m);
    vector<pair<int, int>> work;
    rp(i, 0, n) {
        int a, b;
        scanf("%d%d", &a, &b);
        work.emplace_back(-b, a);
    }
    // printf("s = %d\n", s);
    sort(work.begin(), work.end());
    int res = 0;
    rp(i, 0, n) {
        int reward = -work.at(i).first;
        int day = work.at(i).second;
        // printf("reward = %d day = %d", reward, day);
        int c = seg_find(day-1, m);
        // printf("c = %d\n", c);
        if(c == INT_MAX) continue;
        res += reward;
        seg_insert(c-1, INT_MAX);
    }
    cout << res << endl; return 0;
}

Measured memory_bytes: 5392
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
    string S;
    cin >> S;
    int n = stoi(S);
    
    int a = n / 100;
    int b = n - (a*100);
    
    bool x=false,y=false;
    if(0<a && a<13)x=true;
    if(0<b && b<13)y=true;
    
    if(x && y)std::cout << "AMBIGUOUS"<< std::endl;
    else if(x)std::cout << "MMYY"<< std::endl;
    else if(y)std::cout << "YYMM"<< std::endl;
    else std::cout << "NA"<< std::endl;
}


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

#define Fast            ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define fWrite          freopen ("out.txt","w",stdout);
#define TC              int t;cin >> t;FOR(tc,1,t)
#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define pii             pair<int,int>
#define all(a)          a.begin(),a.end()
#define MEM(a,x)        memset(a,x,sizeof(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)

#define Make(x,p)       (x | (1<<p))
#define DeMake(x,p)     (x & ~(1<<p))
#define Check(x,p)      (x & (1<<p))
#define popcount(x)     __builtin_popcount(x)

const LL MOD=1e9+7;
int n;
int arr[21][21];
bool vis[21][2097152];
LL dp[21][2097152];

LL call(int i,int mask){
    if(i==n) {
        if(mask==(1<<n)-1) return 1;
        else return 0;
    }
    if(vis[i][mask]==1) return dp[i][mask];
    vis[i][mask]=1;
    LL ans=0;
    for(int j=0;j<n;j++){
        if(!Check(mask,j) and arr[i][j]){
            ans=(ans+call(i+1,Make(mask,j)))%MOD;
        }
    }
    return dp[i][mask]=ans%MOD;
}

int main()
{
    cin >> n;
    REP(i,n) REP(j,n) cin >> arr[i][j];
    cout << call(0,0);
}


Measured memory_bytes: 350464