akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <algorithm>

using namespace std;

#define MAX_N 101
#define INF (1<<28)

int n, m, s, g1, g2;
int graph[MAX_N][MAX_N];
int main() {
    while (scanf("%d %d %d %d %d", &n, &m, &s, &g1, &g2), n || m || s || g1 || g2) {
        for (int i = 0; i <= n; i++) 
            for (int j = 0; j <= n; j++) {
                if (i == j) graph[i][j] = 0;
                else graph[i][j] = INF;
            }
        for (int i = 0; i < m; i++) {
            int b1, b2, c;
            scanf("%d %d %d", &b1, &b2, &c);
            graph[b1][b2] = c;
        }
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
                }
            }
        }

        int min_cost = graph[s][g1] + graph[s][g2];
        for (int i = 1; i <= n; i++) { // 分岐する点
            min_cost = min(min_cost, graph[s][i] + graph[i][g1] + graph[i][g2]);
        }
        printf("%d\n", min_cost);
    }
    return 0;
}

Measured memory_bytes: 1068
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
typedef long long ll;

using namespace std;

// init {{{
struct ProconInit {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;


const int NM = 101001;//ここを問題の制約を見ながらいじる．配列の確保に使う．
int A[NM];//あまり褒められたことではないが関数の外側に配列を確保すると怒られにくい．

// Main program
void solve(){
  int A,B;
    cin >> A ;
      cin >> B ;

    if((A == 1 && B== 2) || (A == 2 && B== 1)){
      cout << 3;
      return;
    }
    if((A == 2 && B== 3) || (A == 3 && B== 2)){
      cout << 1;
      return;
    }

    if((A == 3 && B== 1) || (A == 1 && B== 3)){
      cout << 2;
      return;
    }


}

int main() {
    solve();
    return 0;
}




Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <utility>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <functional>
#include <stdlib.h>
using namespace std;
signed main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	long long k;
	cin >> k;
	long long t = 0;
	while (k % 50 != 0) {
		t += 1; k -= 1;
	}
	long long a, b, c, d, e;
	if (t < 10) { a = 49 + k / 50 + t * 50;
	b = c = d = e = 49 + k / 50 - t;
	}
	else {
		if (t < 20) {
			a = 49 + k / 50 + 500 - (t-10);
			b = 49 + k / 50 + (t - 10) * 50 -10;
			c=d=e= 49 + k / 50 - t;
		}
		else {
			if (t < 30) {
				a=b= 49 + k / 50 + 500 - (t - 10);
				c = 49 + k / 50 + (t - 20) * 50 - 20;
				d=e= 49 + k / 50 - t;
			}
			else {
				if (t < 40) {
					a=b=c= 49 + k / 50 + 500 - (t - 10);
					d = 49 + k / 50 + (t - 30) * 50 - 30;
					e= 49 + k / 50 - t;
				}
				else {
					a=b=c=d= 49 + k / 50 + 500 - (t - 10);
					e = 49 + k / 50 + (t - 40) * 50 - 40;
				}
			}
		}
	}
	cout << 50<<endl;
	cout << a << " " << b << " " << c << " " << d << " " << e << " ";
	for (int i = 5; i < 50; i++) {
		cout << 49 + k / 50 - t<<" ";
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
using P = pair<int,int>;
int main()
{
    string s;
    cin >> s;
    string ans = "";
    int i = (int)s.size() -1;
    int dontuse = 0;
    while (i >= 0){
        if(s[i] == 'B')dontuse++;
        
        else{
            if(dontuse > 0){
                dontuse--;
                
            }
            else ans  = s[i]+ans;

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


Measured memory_bytes: 256
#include <bits/stdc++.h> 
#define ll long long 
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,mod(1e9+7);cin>>n;
        long long tot(1),nin(1),bo(1);
        for(int i=0;i<n;i++){
            tot*=10;tot%=mod;
            nin*=9;nin%=mod;
            bo*=8;bo%=mod;
        }
        cout<<(tot-(2*nin)%mod+bo+mod)%mod;
    }
}

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

using namespace std;

using ll = long long;
constexpr int mo = 1e9+7;
constexpr int inf = 1<<30;
constexpr ll infl = 1ll<<60;
constexpr ll mo2 = 998244353;
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int n;
    cin>>n;
    vector<int> a(n);
    for(auto & t : a) cin>>t;
    ll ret = 0;
    map<int,int> m;
    
    for(int i=0;i<n;++i){
        if(i - a[i] >=0){
            ret += m[i-a[i]];
        }
        m[a[i]+i]++;
    }
    cout << ret << endl;
    return 0;
}

Measured memory_bytes: 14292
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <utility>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <functional>
#include <stdlib.h>
#include <climits>
#include <iomanip>
using namespace std;

signed main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,m;
	int d[200005];
	int t[200006];
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> d[i];
	}
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> t[i];
	}if (m > n)cout << "NO";
	else {
		int judge=0;
		int num = 0;
		sort(d, d + n);
		sort(t, t + m);
		for (int i = 0; i < m; i++) {
			while (d[num] < t[i]&&num<n)num += 1;
			if (d[num] == t[i]&&num<n)num += 1;
			else { judge = 1; break; }
		}
		if (judge == 0)cout << "YES";
		else cout << "NO";
	}

	return 0;
}

Measured memory_bytes: 1792
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
	//min(a,b)...a,bの小さいほう
	//max(a,b)...     大きいほう
	int a;
	int b;
	int c;
	int d;
	int e;
	std::cin >> a >> b;
	if (a < b) {
		cout << a << "\n";
	}
	else if (a > b) {
		cout << a - 1 << "\n";
	}
	else if (a == b) {
		cout << a << "\n";
	}
	std::cin >> e;
}

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

using ll = long long;

int main() {
    ll x, k, d;
    cin >> x >> k >> d;

    if (abs(x) / d >= k) {
        cout << abs(x) - k * d << endl;
    }
    else {
        if (abs(x) / d % 2 == k % 2) {
            cout << abs(x) % d << endl;
        }
        else {
            cout << d - abs(x) % d << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 3648
#include<iostream>
#include<bitset>
#include<string>
#include<cmath>

using namespace std;
bool iseven(int x){ return x % 2 == 0; }
bool isodd(int x){ return !iseven(x); }
int dist(int x, int y) { return abs(x - y); }

int main(){
    int x;
    cin >> x;
    int max_exp = 0;
    if (x == 1) {
        cout << x;
        return 0;
    }
    for (int b = 2; b <= x; b++){
        int r = 2;
         while(true){
             int y = pow(b, r);
             if (y > x) break;
             if (y > max_exp) max_exp = y;
             r++;
         }
    }
    cout << max_exp;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
/*  struct fruit {
  int name;  // 名前
  int price; // 価格
};
*/
int main() {
    int H,W,h,w;
    std::cin >> H>>W;
    std::cin >> h>>w;
    std::cout << H*W-h*W-H*w+h*w << std::endl;
    /*int i,j,k=0;
    int n,m;
    int b,c;
    int flag=0;
    std::cin >> n>> m;
    
    vector<int> a(n);
    vector<fruit> fruits(m);
    for (i=0;i<n;i++){
        std::cin >> a.at(i);
    }
    sort(a.begin(), a.end());
    
    for (i=0;i<m;i++){
        std::cin >> fruits.at(i).name>>fruits.at(i).price;
    }
    sort(fruits.begin(), fruits.end(), 
    [](const fruit& x, const fruit& y) { return x.price > y.price;});
    
    for(i=0;i<m;i++){
        if (flag==-1)break;
        b=fruits.at(i).name;
        c=fruits.at(i).price;
        for(j=0;j<b;j++){
            if(k==n){flag=-1;break;}
            else if(a[k]<c){a[k]=c;k++;}
            else {flag=-1;break;}
        }
    }
    
    
    long long int ans=0;
     for (i=0;i<n;i++){
        ans+=a.at(i);
    }
    std::cout << ans << std::endl;
*/
}


Measured memory_bytes: 256
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 07/15/20 19:44:33
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n,t; cin>>n>>t;
  vector<int> A(n); for (auto& a:A) cin>>a;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == n-1 or A[i+1]-A[i] >= t) {
      ans += t;
    } else {
      ans += A[i+1]-A[i];
    }
  }
  cout << ans << endl;

  return 0;
}


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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ll long long
using namespace std;


int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  if (a < k) {
    if (b < k - a) {
      cout << a + (k - a - b) * (-1) << endl;
    }
    else {
      cout << a << endl;
    }
  }
  else {
    cout << k << endl;
  }
}



Measured memory_bytes: 3572
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;



int main() {
  int a, b;
  char op;

  cin >> a >> op >> b;

  printf("%d\n", op == '+' ? a + b : a - b);
  return 0;
}




Measured memory_bytes: 256
#include<cstdio>
#include<vector>
using namespace std;

int main()
{
	int N,count=0;
	vector<int> A;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int a;
		scanf("%d", &a);
		A.push_back(a);
	}
	bool flag=true;
	while(flag){
		flag = 0;
		for (int j = N - 1; j > 0; j--) {
			if (A[j] < A[j - 1]) {
				swap(A[j], A[j - 1]);
				flag = 1;
				count++;
			}
		}
	}
	for (int i = 0; i < N-1; i++) {
		printf("%d ", A[i]);
	}
	printf("%d\n%d\n", A[N - 1], count);
	return 0;
}

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

#define INF 1234567890
#define ll long long

int N;
string s, t;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> s >> t;
	t.pop_back();
	if (s == t) cout << "Yes\n";
	else cout << "No\n";
	return 0;
}

Measured memory_bytes: 3644
#pragma GCC optimize("03")
#include <bits/stdc++.h>

using namespace std;

int n, k, a[100005], dp[200005];

int main()
{
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	
	cin >> n >> k;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
		
	for (int i = 0; i <= k; ++i)
	{
		if (dp[i] == 0)
			for (int j = 1; j <= n; ++j)
				dp[i + a[j]] = 1; 
	}
	
	cout << (dp[k] ? "First\n" : "Second\n");
	
	return 0;
}


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

int main(void)
{
    int h1, m1, h2, m2, k;
    cin >> h1 >> m1 >> h2 >> m2 >> k;
    cout << (60 * h2 + m2) - (60 * h1 + m1) - k << endl;
    return 0;
}


Measured memory_bytes: 3608
/* Great things never come from comfort zones,
"whatever the mind of a man can conceive and believe,it can achieve." */
 
#include <bits/stdc++.h>
#define ll long long
#define scf(n) scanf("%d",&n)
#define lscf(n) scanf("%lld",&n)
#define lpri(n) printf("%lld ",n)
#define pri(n) printf("%d ",(int)n)
#define prin(n) printf("%d\n",(int)n)
#define lprin(n) printf("%lld\n",n)
#define rep(i,ini,n) for(int i=ini;i<(int)n;i++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define F first
#define S second
#define all(x)     x.begin(),x.end()
#define tc   int tt; scf(tt); while(tt--)
#define inf INT_MAX
#define ninf INT_MIN
#define gcd __gcd
#define bitcount(n) __builtin_popcount(n)
typedef double dd;
using namespace std;
const ll mod =1e9+7;
const int N = 1e6+7;

void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B) 
{ cout << ' ' << A; my_dbg(B...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)


int main() 
{ 
    ll k;
    int n;
    scf(n),lscf(k);

    vector<pair<int,int>>v;

    int a,b;

    rep(i,0,n)
    {
        scf(a),scf(b);
        v.pb({a,b});
    }

    sort(all(v));

    for(auto it: v)
    {
      

        if(k<=it.S)
        {
            cout<<it.F;
        break;}

        else
        k-=it.S;
    }




}


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

long long H, W, K, mod = 1000000007;

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 32; i++) {
		if ((b / (1LL << i)) % 2 == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}
long long Div(long long a, long long b, long long m) {
	a %= mod; b %= mod;
	return (a * modpow(b, m - 2, m)) % m;
}
long long solve1() {
	long long S = 0;
	for (int i = 1; i <= K; i++) { S += i; S %= mod; }
	S = Div(S, H + W, mod);
	return S;
}
long long solve2() {
	long long S = 0;
	for (int i = 1; i <= K; i++) { S += 1LL * i * (i - 1) / 2; S %= mod; }
	S = Div(S, (H + W) * (H + W - 1) / 2, mod);
	return S;
}

int main() {
	cin >> H >> W >> K;

	long long A1 = K;
	long long A2 = solve1(); A2 *= (H + W); A2 %= mod;
	long long A3 = solve2(); A3 *= H; A3 %= mod; A3 *= W; A3 %= mod;

	long long DEG = A1 + A2 + A3;
	for (int i = 1; i <= K; i++) { DEG *= (H + W + 1LL - i); DEG %= mod; }
	cout << DEG << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <list>
#include <string>
#include <numeric>
#include <math.h>
#include <set>
#include <map>
#include <fstream>
#include <complex>
using namespace std;
const int inf = 2000000;
int f_arg[4][2] =
{
	{1,0},
	{-1,0},
	{0,1},
	{0,-1}
};
typedef struct Node
{
	int x;
	int y;
	int count;
	int score;

	bool operator<(const Node &another) const
	{
		return score>another.score;
	}
}Node;

int MD(int x,int y,int t_x,int t_y)
{
	return abs(t_x - x) + abs(t_y-y);
}

int main()
{
	int H,W;
	cin>>H>>W;

	vector<vector<int>> p(H+2);
	int s_count=0;
	for(int i=0; i<H+2; i++)
	{
		p[i].resize(W+2);
		for(int j=0; j<=W+1; j++)
		{
			if(i==0 || i==H+1 || j==0 || j==W+1)
			{
				p[i][j] = -1;
				continue;
			}
			char t;
			cin>>t;
			if(t=='.')
			{
				p[i][j] = inf;
			}
			else
			{
				p[i][j] = -1;
				s_count++;
			}
		}
	}
	
	priority_queue<Node> q;

	Node initial;
	initial.x = 1;
	initial.y = 1;
	initial.count = 1;
	initial.score = MD(1,1,H,W);
	q.push(initial);

	while(!q.empty())
	{
		Node n = q.top();
		//map update
		p[n.x][n.y] = n.count;
		//cout<<n.x<<","<<n.y<<endl;
		if(n.x==H && n.y==W)
		{
			break;
		}
		q.pop();
		for(int i=0; i<4; i++)
		{
			int next_x = n.x + f_arg[i][0];
			int next_y = n.y + f_arg[i][1];
			
			if(p[next_x][next_y] != -1 && p[next_x][next_y]-1 > p[n.x][n.y])
			{
				Node next;
				next.x = next_x;
				next.y = next_y;
				next.count = n.count+1;
				next.score = next.count + MD(next.x,next.y,H,W);
				q.push(next);
			}
			
		}
		
	}
	if(q.empty())
	{
		cout<<-1<<endl;
	}
	else
	{
		cout<<H*W-q.top().count-s_count<<endl;
	}
	return 0;
}

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

using namespace std;

#define FOR(i, n) for(int i = 0; i < (int) n; i++)
#define all(v) v.begin(), v.end()
#define PB push_back
#define F first
#define S second

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

const ll MOD = 1e9+7;

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

	string s; cin >> s;
	int n; cin >> n;
	bool flip = false;
	string a = "", b = "";
	FOR(i, n){
		int op; cin >> op;
		if(op == 1){
			flip = !flip;
		}else{
			int x; cin >> x;
			char c; cin >> c;
			if(x == 1){
				if(flip) b += c;
				else a = c + a;
			}else{
				if(flip) a = c + a;
				else b += c;
			}
		}
	}

	s = a + s + b;
	if(flip) reverse(all(s));

	cout << s << '\n';

	return 0;
}

Measured memory_bytes: 1316
import sys
input = sys.stdin.readline
k, q = map(int, input().split())
d = list(map(int, input().split()))
query = [None]*q
for i in range(q):
    query[i] = list(map(int, input().split()))

def answer_query(n, x, m):
    l = [m if i%m == 0 else i%m for i in d]
    count = n // k
    rem = n % k
    s = sum(l)
    total = s%m * count + x%m
    for i in range(rem):
        total += l[i]

    total //= m
    total += s//m * count

    print(n-total)


for n, x, m in query:
    answer_query(n-1, x, m)


Measured memory_bytes: 55388
x, y = map(int, input().split())

prize = 0
if x==1:
    prize += 300000
elif x==2:
    prize += 200000
elif x==3:
    prize += 100000

if y==1:
    prize += 300000
elif y==2:
    prize += 200000
elif y==3:
    prize += 100000

if x==1 and y==1:
    prize += 400000

print(prize)

Measured memory_bytes: 38384
S = input()
s_1 = int(S[:2])
s_2 = int(S[2:])

if 0 < s_1 < 13 and 0 < s_2 < 13:
  print("AMBIGUOUS")
elif 0 < s_1 < 13:
  print("MMYY")
elif 0 < s_2 < 13:
  print("YYMM")
else:
  print("NA")

Measured memory_bytes: 3060
N, M = map(int, input().split())
H = list(map(int, input().split()))
hlist = [0] * N
clist = [0] * N
cnt = 0

for i in range(M):
  A, B = map(int, input().split())
  clist[A-1] += 1
  clist[B-1] += 1
  if H[A-1] > H[B-1]:
    hlist[A-1] += 1
  elif H[A-1] < H[B-1]:
    hlist[B-1] += 1

for i in range(N):
  if hlist[i] == clist[i]:
    cnt += 1

print(cnt)

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

#include <queue>
#include <vector>
using namespace std;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

#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 dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

typedef long long ll;

const int D = 60;
const int MAX_N = 200005;

vector<vector<int>> link;
vector<int> ans;
int M;
int N;

void bfs() {
    queue<int> que;
    que.push(1);
    while (que.size()) {
        int q = que.front();
        que.pop();
        for (int i : link[q]) {
            // まだ来たこと無いところだったら
            if (ans[i] == INF) {
                que.push(i);
                ans[i] = q;
            }
        }
    }
    cout << "Yes" << endl;
    for (int i = 2; i < N + 1; i++) {
        cout << ans[i] << endl;
    }
}

int main() {
    cin >> N >> M;
    link = vector<vector<int>>(N + 1);
    ans = vector<int>(N + 1);
    rep(i, N + 1) {
        ans[i] = INF;
    }
    rep(i, M) {
        int a, b;
        cin >> a >> b;
        link[a].push_back(b);
        link[b].push_back(a);
    }
    bfs();
    return 0;
}

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

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


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

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

vector<int> numbers;


int yGame(int x, int y, int start);

int xGame(int x, int y, int start) {
    int last = abs(numbers[numbers.size() - 1] - y);
    int last2 = abs(numbers[numbers.size() - 2] - numbers[numbers.size() - 1]);

    if (numbers.size() - start == 3) {
        return max(last, last2);
    }

    int y_game = yGame(start + 1, numbers[start], start + 1);

    return max(max(last, last2), y_game);
}

int yGame(int x, int y, int start) {
    int last = abs(numbers[numbers.size() - 1] - x);
    int last2 = abs(numbers[numbers.size() - 2] - numbers[numbers.size() - 1]);

    if (numbers.size() - start == 3) {
        return min(last, last2);
    }

    int x_game = xGame(x, numbers[start], start + 1);
    return min(min(last, last2), x_game);

}

int main() {

    int n, z, w;
    cin >> n >> z >> w;

    if (n == 1) {
        int a;
        cin >> a;
        cout << abs(a - w) << endl;
        return 0;
    }

    numbers.resize(n);
    rep(i, n) {
        cin >> numbers[i];
    }

    int ans = xGame(z, w, 0);
    cout << ans << endl;
}

Measured memory_bytes: 384
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,k,x,y;

int main(){
  ll ans=1;
  cin >> n;
  vi a(n);
  REP(i,n) cin>>a[i];

  vector<int> col(3,0);


  REP(i,n){
    
    int flag=0;
    if (a[i]==col[0])
      flag++;
    if (a[i]==col[1])
      flag++;
    if (a[i]==col[2])
      flag++;
    ans*=flag;
    ans%=MOD;
    if (flag!=0)
    {  
      if (a[i]==col[0]){
        col[0]++;
      }else{
        if (a[i]==col[1]){
          col[1]++;
        }else{
          col[2]++;
        }
      }
    }
  }

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







Measured memory_bytes: 1024
n = int(input())
a = list(map(int, input().split()))
def doit(a):
    c = {}
    for ai in a:
        c[ai] = c.get(ai, 0) + 1
    if len(c) > 3:
        return "No"
    k = list(sorted(c.keys()))
    if len(k) == 1:
        if k[0] == 0:
            return "Yes"
    if len(k) == 2:
        if k[0] == 0 and c[k[0]] * 2 == c[k[1]]:
            return "Yes"
    if len(k) == 3:
        if c[k[0]] == c[k[1]] == c[k[2]] and k[0] ^ k[1] == k[2]:
            return "Yes"
    return "No"

print(doit(sorted(a)))

Measured memory_bytes: 60784
/////////////////////////   _LeMur_
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <list>
#include <map>
#include <set>

using namespace std;

const int N = 100005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int n;
int a[N] , b[N];

vector <int> id1 , id2;
bool used[N];

bool cmp1(int i,int j){
    return a[i] - b[j] > a[j] - b[i];
}

bool cmp2(int i,int j){
    return b[i] - a[j] > b[j] - a[i];
}

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i],&b[i]);
        id1.push_back(i);
        id2.push_back(i);
    }
    sort(id1.begin() , id1.end() , &cmp1);
    sort(id2.begin() , id2.end() , &cmp2);
    int it1 = 0 , it2 = 0;
    long long answ1 = 0 , answ2 = 0;
    for(int i=0;i<n;i++){
        if(i % 2 == 0){
            while(used[id1[it1]] == true){
                ++it1;
            }
            used[id1[it1]] = true;
            answ1 += a[id1[it1]];
        }
        else{
            while(used[id2[it2]] == true){
                ++it2;
            }
            used[id2[it2]] = true;
            answ2 += b[id2[it2]];
        }
    }
    cout<<answ1 - answ2<<endl;
    return 0;
}


Measured memory_bytes: 2164
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int INF=99999999;
int N;
int a[45],b[45],c[45];
int d[10][10];
int m;
void print(){
  for(int i=0;i<=m;i++){
    for(int j=0;j<=m;j++){
      printf("%d ",d[i][j]);
    }
    printf("\n");
  }
  printf("\n");
}
int main(){
  while(1){
    m=0;
    scanf("%d",&N);
    if(N==0)break;
    for(int i=0;i<N;i++){
      scanf("%d %d %d",&a[i],&b[i],&c[i]);
      a[i];b[i];
      m=max(m,b[i]);
    }
    for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
	if(i==j)d[i][j]=0;
	else d[i][j]=INF;
      }
    }
    for(int i=0;i<N;i++){
      d[a[i]][b[i]]=min(d[a[i]][b[i]],c[i]);
      d[b[i]][a[i]]=min(d[b[i]][a[i]],c[i]);
    }
    for(int k=0;k<=m;k++){
      for(int j=0;j<=m;j++){
	for(int i=0;i<=m;i++){
	  d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
	}
      }
    }
    int x,ans=INF;
    for(int i=0;i<=m;i++){
      int sum=0;
      for(int j=0;j<=m;j++){
	if(d[i][j]!=INF && d[i][j]!=0)sum+=d[i][j];
      }
      if(ans>sum){ans=sum;x=i;}
    }
    printf("%d %d\n",x,ans);
  }
  return 0;
}

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

int main() {

    int n;
    cin >> n;

    vector<int> v(n);
    vector<int> p(n);

    for(int i = 0; i < n; ++i)
        cin >> v[i];

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

    double value = v[0];

    for(int i = 1; i < n; ++i) {
        value += v[i];
        value /= 2.0;
    }

    printf("%.10f\n", value);
    return 0;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  
  bool p = false, w = false, g = false, y = false;
  for (int i = 0; i < N; i++) {
    string str;
    cin >> str;
    if (str == "P")
      p = true;
    else if (str == "W")
      w = true;
    else if (str == "G")
      g = true;
    else
      y = true;
  }
  
  if (p + w + g + y == 3)
    cout << "Three" << endl;
  else
    cout << "Four" << endl;
}

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

int N, M;
std::vector<int> WA(105000,0);
std::vector<bool> AC(105000,false);

void solve() {
    cin >> N >> M;
    int WAnum = 0;
    int ACnum = 0;

    while(M--) {
        int p;
        string S;
        cin >> p >> S;
        if(AC[p]) continue;
        if(S == "AC") {
            AC[p] = true;
            ACnum++;
            WAnum += WA[p];
        } else if(S == "WA") {
            WA[p]++;
        }
    }
    cout << ACnum << " " << WAnum << endl;
    return;
}

int main() {
    solve();
    return 0;
}


Measured memory_bytes: 3584
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for(int i=0;i<n;i++)
#define why(n,x) int n;while(cin >>n,n!=x)
#define iFOR(i,x,n)for(int i=x;i<n;i++)
#define fin <<'\n'
#define __ <<" "<<
#define ___ <<" "
#define bash push_back
#define ALL(x) x.begin(),x.end()
#define SWAP(a,b) ((a != b) && (a += b,b = a - b,a -= b))
//#define int long long
typedef long long ll;
typedef pair < int , int >  pii;
typedef vector < int > vit;
typedef map < string , int > mstit;
typedef vector < pii > vpi ;
typedef greater < pii > gpi;
typedef priority_queue < pii , vpi , gpi > dijk ;
static const signed int INF = 0x3f3f3f3f;
static const signed long long LINF = 0x3f3f3f3f3f3f3f3fLL;
static const signed int SMOD = 1000000007;
static const signed int NMOD = 1000000009;
static const signed int dx[]={1,0,-1,0,1,1,-1,-1};
static const signed int dy[]={0,-1,0,1,-1,1,-1,1};

bool inside(int x,int y,int w,int h){return (x>=0 && y>=0 && x<w && y<h);}
template<class T>T abs(T &x){return x<0 ? -x : x;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}


inline void solve(){

  int n, m;

  int a[100005];
  int b[100005];
  int c[100005];
  priority_queue<pii> que;
  cin >> n >> m;

  FOR(i,n)
    cin >> a[i];
  FOR(i,m){
    cin >> b[i] >> c[i] ;
    que.push(pii(c[i],b[i]));
  }

  sort(a,a+n);
  int i = 0;
  while(!que.empty()){
    pii now = que.top();
    que.pop();
    int cos = now.first;
    int val = now.second;
    FOR(j,val){
      if(a[i] < cos){
        a[i] = cos;
        i++;
      }else
        break;
    }
  }
  ll sum = 0;
  FOR(i,n){
    sum += a[i] ;
  }

  cout << sum fin;
  
}
struct xyz{ xyz(){ cin.tie(0), ios::sync_with_stdio(false); cout <<fixed<<setprecision(12);};}xyzzy;signed main(){solve();return 0;}


Measured memory_bytes: 2296
#include<iostream>
	#include<vector>
	#include<algorithm>
	#include<string>
	#include<cmath>
	#include<map>
	#include<cstdio>
	#include<stack>
	#include<queue>
	#include<fstream>
	#include<cstdio>
	#include<deque>
	#include<iomanip>
	#include <numeric>
	//#include<bits/stdc++.h>

	using namespace std;
	#define ok1 printf("ok1\n");
	#define ok2 printf("ok2\n");
	#define M 1000000000000000000LL
	#define rep(i,n) for(int i=0;i<n;i++)
	#define REP(i,s,n) for(int i=(s);i<(n);i++)
	#define repr(i,n) for(int i=n-1;i>=0;i--)
	#define REPR(i,s,n) for(int i=(s);i>=(n);(i)--)
	#define all(a) (a).begin(),(a).end()
	#define reall(a) (a).rbegin(),(a).rend()
	#define pb push_back
	#define pf push_front
	#define MIN(a,b) a=min((a),(b))
	#define MAX(a,b) a=max((a),(b))
	#define SIZE(v) (int)v.size()
	const double pi = acos(-1.0);

	typedef vector<int> vi;
	typedef vector<string> vs;
	typedef long long ll;
	typedef vector<ll> vll;
	typedef vector<vi> vvi;
	typedef vector<vll> vvll;
	typedef deque<ll> dll;
	typedef pair<ll, ll> P;
	const ll mod = 1e9 + 7;
	ll dx[4] = {1,0,-1,0};
	ll dy[4] = {0,1,0,-1};


	int main() {
		cin.tie(0);
		ios::sync_with_stdio(false);
		ll n, ans = 0;
		cin >> n;
		vs s(3);
		rep(i, 3) cin >> s[i];
		rep(i, n) {
			//cout << s[0][i] << s[1][i] << s[2][i] << endl;
			if (s[0][i] != s[1][i]) {
				if (s[1][i] != s[2][i]) {
					if (s[0][i] == s[2][i]) ans++;
					else ans += 2;
				}
				else ans++;
			}
			else {
				if (s[1][i] != s[2][i]) ans += 1;
			}
			//cout << ans << endl;
		}
		cout << ans << endl;

		
		/*ll n,a,b;
		cin >> n >> a >> b;
		ll x=0;
		if (min(a, b) - (n - max(a, b)) > 0) x = min(a, b) - (n - max(a, b));
		cout << min(a, b) << " " << x << endl;*/
		/*ll n, ans = 0;
		cin >> n;
		vll a(n), b(n);
		vector<P> v(n), w(n);
		rep(i, n) {
			cin >> a[i] >> b[i];
			v[i] = P(a[i], i);
			w[i] = P(b[i], i);
		}
		sort(reall(v));
		sort(reall(w));
		//rep(i, n) cout << w[i].first << " " << w[i].second << endl;
		ll x = 0, y = 0;
		map<ll, bool> m;
		rep(i, n) {
			if (i % 2 == 1) {
				while (m[v[x].second]) x++;
				ans -= b[v[x].second];
				m[v[x].second] = true;
				x++;
			}
			else {
				//cout << m[w[y].second] << endl;
				while (m[w[y].second]) y++;
				ans += a[w[y].second];
				m[w[y].second] = true;
				y++;
			}
			//cout << ans << endl;
		}
		cout << ans << endl;*/
		return 0;
	}	

Measured memory_bytes: 256
#include <stdio.h>
int main(void){
    int a,b,c,d;
    scanf("%d %d %d\n",&a,&b,&c);
    if(a>b){
        d=a;
        a=b;
        b=d;
    }
    if(b>c){
        d=b;
        b=c;
        c=d;
    }
    if(a>b){
        d=a;
        a=b;
        b=d;
    }
    printf("%d %d %d\n",a,b,c);
    
    return 0;
    
}



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

#define MOD 1000000007
#define MOD99 998244353
 
int main(){
  int k;
  cin >> k;
  string s;
  for(int i = 0;i < k;i++){
    s += "ACL";
  }
  cout << s << endl;
}

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

int main() {
	int n,c=0;

	cin >> n;
	while (true) {
		if (n < pow(2, c)) {
			break;
		}
		c++;
	}

	cout << pow(2, c - 1) << endl;
}


Measured memory_bytes: 384
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdint.h>
#include<vector>
#include<deque>
#include<stack>
#include<string>
#include<cstring>
#include<time.h>
#include<iomanip>
#include<list>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<bitset>
#include <queue>
#include <map>


using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

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



//素因数分解
vector<pair<long long, int>> factorize(long long n) {
    vector<pair<long long, int>> res;
    for (long long i = 2; i * i <= n; ++i) {
        if (n % i != 0) continue;
        res.emplace_back(i, 0);
        while (n % i == 0) {
            n /= i;
            res.back().second++;
        }
    }
    if (n != 1) res.emplace_back(n, 1);
    return res;
}

ll n;
ll cnt = 0;
vector<pair<long long, int>> fc;

void f(ll v, int r) {
    if (r == fc.size()) {
        if (v == 1) return;
        ll bf = n;
        while (bf % v == 0) bf /= v;
        if (bf % v == 1) cnt++;
    }
    else {
        ll a = 1;
        for (int i = 0; i <= fc[r].second; ++i) {
            f(v * a, r + 1);
            a *= fc[r].first;
        }
    }
}

int main() {
    //1のばあい！！！！！！！！！！
    cin >> n;
    fc = factorize(n);
    auto fc2 = factorize(n - 1);
    ll stori = 1;
    rep(i, fc2.size()) stori *= (fc2[i].second + 1);//1ふくむ
    stori--;//1 hiita
    f(1, 0);
    cout << cnt + stori << endl;
    return 0;
}



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

using namespace std;

int main() {
  int n;
  cin>>n;
  vector<string> a(n);
  for (int i = 0; i < n; i++) cin>>a[i];
  map<string, int> m;
  long ans = 0;
  for (int i = 0; i < n; i++) {
      string s = a[i];
      sort(s.begin(), s.end());
      if (m.count(s)) {
        ans += m[s];
        m[s]++;
      } else m.insert({s, 1});
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 18176
#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 all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define bit(k) (1LL<<(k))
typedef long long ll;

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 int INF = 1e9;
const ll INFLL = 1e18;
const ll MOD = 1e9+7;
const double PI = acos(-1.0);

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

/*------------------------------------/
for library*/

/*------------------------------------*/

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    char a,b;
    cin>>a>>b;
    string A = "";
    string B = "";
    rep(i,a - '0')B.push_back(b);
    rep(i,b - '0')A.push_back(a);
    cout<<min(A,B)<<endl;
}

Measured memory_bytes: 256
# 二分探索を利用した
N, M = map(int, input().split())
tmp = input().split()

A = [int(tmp[i]) for i in range(N)]
A.sort()
cum_A = [None]*N
cum_A[N-1] = A[N-1]
for i in range(1,N):
    cum_A[N-1-i] = cum_A[N-i] + A[N-1-i]
cum_A.append(0)


def geq(A, cum_A, X, N):  # ある定数X以上になる握手の方法が何通りあるか,Aは昇順sort済み
    sum = 0
    val = 0
    index = 0
    for i in range(N):
        while True:
            if index != N and A[i] + A[N - 1 - index] >= X:
                index += 1
            else:
                sum += index
                val += A[i] * index + cum_A[N-index]
                break
    return sum, val

# 2<=X<=2*Nで二分探索をする
low = 2
high = 2*A[N-1]
while low <= high:
    X = (low + high)//2
    sum, val = geq(A, cum_A, X, N)
    if sum == M:
        break
    elif sum > M:
        low = X+1
    else:
        high = X-1
if sum < M:
    X -= 1
sum, val = geq(A, cum_A, X, N)
ans = val - X*(sum-M)
print(ans)





Measured memory_bytes: 20584
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

int main () {
    int n, T;
    std::cin >> n >> T;
    std::vector<int> c(n);
    std::vector<int> t(n);
    std::vector<int> cs;
    for (size_t i = 0; i < n; i++)
    {
        std::cin >> c[i];
        std::cin >> t[i];
        if (t[i] <= T)
        {
            cs.push_back(c[i]);
        }
    }
    
    if(cs.size() != 0) {
       std::cout << *std::min_element(cs.begin(), cs.end()) << "\n";
    } else {
        std::cout << "TLE" << std::endl;
    }
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <set>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>

using namespace std;

constexpr long long mod = 1000000007;

int main() {
    /*
    int n,x[100000],l[100000];
    cin >> n;
    for(int i = 0;i < n;i++) cin >> x[i] >> l[i];
    */
    int n,k,s;
    cin >> n >> k >> s;
    if(k==n) {
        for (int i = 0; i < n; i++) cout << s << " ";
    } else {
        for(int i = 0;i < k;i++) cout << s << " ";
        for(int i = 0;i < (n-k);i++) cout << s/2+400000000 << " ";
    }
}

Measured memory_bytes: 1280
def actual(C):
    return C[0][0] + C[1][1] + C[2][2]
    # return ''.join((C[i][j] for i, j in zip(range(len(C)), range(len(C)))))

C = []
C.append(input())
C.append(input())
C.append(input())

print(actual(C))


Measured memory_bytes: 2940
N = int(input())
A = list(map(int, input().split()))
x = 1
if 0 in A:
    print(0)
else:
    for i in A:
        x *= i
        if x > 10**18:
            break
    if x > 10**18:
        print(-1)
    else:
        print(x)

Measured memory_bytes: 21644
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//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; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

int main(){
    int N; cin >> N;
    string pre;
    vector<string> w(N);
    for(int i=0; i<N; i++) cin >> w[i];

    map<string, int> mp;

    for(int i=0; i<N; i++){
        string s; s= w[i];
        if(i && pre[pre.size()-1] != s[0]){
            cout << "No" << endl;
            return 0;
        }
        if(mp[s]){
            cout << "No" << endl;
            return 0;
        }
        mp[s]++;
        pre = s;
    }

    cout << "Yes" << endl;
}

Measured memory_bytes: 256
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Vishal Burman
 */
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);
        ANextAlphabet solver = new ANextAlphabet();
        solver.solve(1, in, out);
        out.close();
    }

    static class ANextAlphabet {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            String str = "abcdefghijklmnopqrstuvwxyz";
            char ch = in.readLine().charAt(0);
            out.println(str.charAt(str.indexOf(ch) + 1));
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

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

        private String readLine0() {
            StringBuilder buf = new StringBuilder();
            int c = read();
            while (c != '\n' && c != -1) {
                if (c != '\r') {
                    buf.appendCodePoint(c);
                }
                c = read();
            }
            return buf.toString();
        }

        public String readLine() {
            String s = readLine0();
            while (s.trim().length() == 0) {
                s = readLine0();
            }
            return s;
        }

    }
}



Measured memory_bytes: 21332
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define endl "\n"
#define ff first
#define ss second
#define inp(a) int a; cin>>a;
#define inpa(a,n) vector<int>a(n); FOR(i,0,n-1) cin>>a[i];
#define PI 3.141592653589793238
#define QUICK ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vec(x) vector<x>
#define pii pair<int,int>
#define mem(a,b) memset(a, b, sizeof a)
#define FOR(i,j,n) for(int i= j; i<=n; i++)
#define srt(a); sort(a.begin(), a.end())
#define CASE int t; cin>>t; while(t--)
#define setBits(n) __builtin_popcountll(n)
#define prec(n) fixed<<setprecision(n)
#define print(x) for(auto c : x) cout << c << " ";
#define dbg(x) cerr << #x << " :: "<< x << endl;
#define dbg2(x,y) cerr << #x << " :: "<<x <<"\t" << #y << " :: "<<y <<endl;
#define upcase(s)  transform(s.begin(), s.end(), s.begin(), ::toupper)
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define int ll
const int INF = 1e9 + 7;
const int SIZE = 150007;
const int MOD = 1e9 + 7; 
//////////// template ends here //////////////////////////



void solve(){	
	
	inp(n);
	vec(int)a(n+2, 0),b(n+2, 0),c(n+2, 0);
	vec(vec(int))dp(n+2, vec(int)(4,0));

	FOR(i,1,n)
		cin>>a[i]>>b[i] >> c[i];
	
	dp[1][1] = a[1]; dp[1][2] = b[1]; dp[1][3] = c[1];
	
	for(int i = 2; i<= n; i++){
		dp[i][1] = max(dp[i-1][2], dp[i-1][3]) + a[i];
		dp[i][2] = max(dp[i-1][1], dp[i-1][3]) + b[i];
		dp[i][3] = max(dp[i-1][2], dp[i-1][1]) + c[i];
		//dbg2(dp[i][1], dp[i][2])
	}

	cout <<  max ({dp[n][1], dp[n][2], dp[n][3]});
	
}



int32_t main(){
	#ifndef ONLINE_JUDGE 
		freopen ("input.txt","r",stdin);
		freopen ("output.txt","w",stdout);
	#endif	
	QUICK;
	
	solve();
	printclock;
}

Measured memory_bytes: 12672
#!/usr/bin/env python3
#ABC61 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def Bellman_Ford(edges,start):
    dist = [-inf for _ in range(n)]
    dist[start] = 0
    for cnt in range(n+1):
        for edge in edges:
            e_from, e_to, e_cost = edge
            if dist[e_to] < dist[e_from] + e_cost:
                dist[e_to] = dist[e_from] + e_cost
        if cnt == n - 1:
            d = dist[:]
    if d[-1] < dist[-1]:
        print('inf')
        quit()
    else:
        return d[-1]

n,m = LI()
edges = []
for _ in range(m):
    a,b,c = LI()
    edges.append([a-1,b-1,c])

ans = Bellman_Ford(edges,0)
print(ans)


Measured memory_bytes: 42220
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
int[] dx = [-2,-2,-2,-1,-1,0,0,1,1,2,2,2];
int[] dy = [1,0,-1,2,-2,2,-2,2,-2,1,0,-1];
int[] spx,spy;
int tl;

bool saiki(int x,int y,int n){
    if(tl==n) return true;
    bool ans = false;
    for(int i=0;i<12;i++){
	int nx,ny;
	nx = x + dx[i];
	ny = y + dy[i];
	if(0<=nx&&nx<=9&&0<=ny&&ny<=9){
	    if(abs(nx-spx[n])<=1 && abs(ny-spy[n])<=1){
		ans = ans || saiki(nx,ny,n+1);
	    }
	}
    }
    return ans;
}

void main(){
    while(true){
	int x,y;
	auto s = readln().split().to!(int[]);
	x = s[0];
	y = s[1];
	if(x==0&&y==0) break;
	spx = new int[10];
	spy = new int[10];
	int n = readln().chomp().to!int;
	tl = n;
	auto s1 = readln().split().to!(int[]);
	for(int i=0;i<n;i++){
	    spx[i] = s1[2*i];
	    spy[i] = s1[2*i+1]; 
	}
	writeln(saiki(x,y,0) ? "OK" : "NA");
    }
}

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

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define MOD 1000000007

typedef long long ll;

long long mod_div(long long a, long long b) {
	if (a % b == 0)return a / b;
	long long tmp = MOD - 2, c = b, ret = 1;
	while (tmp > 0) {
		if (tmp & 1) {
			ret *= c; ret %= MOD;
		}
		c *= c; c %= MOD; tmp >>= 1;
	}
	return a*ret%MOD;
}


ll dp[100011][4];

int main() {

	string str; cin >> str;

	ll tmp = 1;
	ll cou = 0;
	REP(i, str.size()) {
		if (str[i] == '?')cou++;
	}
	REP(i, cou) {
		tmp *= 3;
		tmp %= MOD;
	}

	dp[0][0] = tmp;
	REP(i, str.size()) {
		REP(j, 4) {
			if (dp[i][j]) {
				if (j < 3) {
					if (str[i] == 'A' + j) {
						dp[i + 1][j + 1] += dp[i][j];
						dp[i + 1][j + 1] %= MOD;
					}
					else if (str[i] == '?') {
						dp[i + 1][j + 1] += mod_div(dp[i][j], 3);
						dp[i + 1][j + 1] %= MOD;
					}
				}
				dp[i+1][j] += dp[i][j];
				dp[i+1][j] %= MOD;
			}
		}
	}

	cout << dp[str.size()][3] << endl;

	return 0;
}


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

int main(int argc, char const *argv[]) {
  int A, B;

  cin >> A >> B;
  int max = A > B ? A : B;
  int min = A > B ? B : A;

  int ans = 0;
  if (max - min > 0) {
    ans = 2 * max - 1;
  } else {
    ans = max + min;
  }

  cout << ans << "\n";

  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;

typedef long long ll;
typedef vector<int> VI;

#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR1(i,n) for(int (i)=1;(i)<(n);(i)++)
#define eFOR(i,n) for(int (i)=0;(i)<=(n);(i)++)
#define eFOR1(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define SORT(i) sort((i).begin(),(i).end())
constexpr auto INF = 1000000000;
constexpr auto LLINF = 9223372036854775807;
constexpr auto mod = 1000000007;

int main() {
	
	int n;
	ll k;
	cin >> n >> k;
	vector<pair<int, int>> ab(n);
	FOR(i, n)cin >> ab[i].first >> ab[i].second;
	SORT(ab);
	FOR(i, n) {
		if (ab[i].second >= k) {
			cout << ab[i].first << endl;
			return 0;
		}
		k -= ab[i].second;
	}

	return 0;
}

Measured memory_bytes: 1024
import copy

def same(items):
    return all(x == items[0] for x in items)

s = list(raw_input())
d = dict()
for c in s:
    if c in d:
        d[c] += 1
    else:
        d[c] = 1
if max(d.values()) == 1:
    print len(s)/2
else:
    steps = []
    for c in d.keys():
        ss = copy.copy(s)
        cnt = 0
        while not same(ss):
            cnt += 1
            for i in range(len(ss) - 1):
                if ss[i+1] == c:
                    ss[i] = c
            ss.pop()
        steps.append(cnt)
    print min(steps)

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

using namespace std;

int main()
{
    int N;
    scanf("%d", &N);
    vector<int> A(2*N);
    for(int i=0; i<2*N; i++)
        scanf("%d", &A[i]);
    sort(A.rbegin(), A.rend());
    int ans=0;
    for(int i=1; i<2*N; i+=2)
        ans+=A[i];
    printf("%d\n", ans);
    return 0;
}


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

using namespace std;

typedef long long ll;

int main()
{
    int X, Y, Z;
    cin >> X >> Y >> Z;
    int tmp = Y;
    Y = X;
    X = tmp;
    tmp = Z;
    Z = X;
    X = tmp;
    cout << X << " " << Y << " " << Z << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <array>
#include <vector>
#include <functional>
#include <unordered_map>
#include <map>
#include <numeric>
#include <limits>
#include <utility>
#include <queue>
#include <random>
using namespace std;

int main(){
	int n;

	cin >> n;

	cout << "ABC" << n;

	return 0;
}

Measured memory_bytes: 256