akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{

#if !ONLINE_JUDGE

	freopen("in1.txt", "r", stdin);
	freopen("out1.txt", "w" , stdout);

#endif

	int sx, sy, tx, ty;
	cin >> sx >> sy >> tx >> ty;


	int v = ty - sy;
	int h = tx - sx;

	map<char, char>op;


	op['U'] = 'D';
	op['D'] = 'U';
	op['R'] = 'L';
	op['L'] = 'R';



	string s1u = "";
	for (int i = 0; i < v; i++)
	{
		s1u = s1u + "U";
	}
	for (int i = 0; i < h; i++)
	{
		s1u = s1u + "R";
	}

	string s1d = "";

	for (int i = 0; i < s1u.size(); ++i)
	{
		if (s1u[i] == 'U')
		{
			s1d = s1d + 'D';
		}
		else if (s1u[i] == 'R')
		{
			s1d = s1d + 'L';
		}
	}


	v++;
	h++;

	string s2u = "L";
	for (int i = 0; i < v; i++)
	{
		s2u = s2u + "U";
	}
	for (int i = 0; i < h; i++)
	{
		s2u = s2u + "R";
	}
	s2u += 'D';

	string s2d = "";

	for (int i = 0; i < s2u.size(); ++i)
	{
		if (s2u[i] == 'U')
		{
			s2d = s2d + 'D';
		}
		else if (s2u[i] == 'R')
		{
			s2d = s2d + 'L';
		}
		else if (s2u[i] == 'L')
		{
			s2d = s2d + 'R';
		}
		else if (s2u[i] == 'D')
		{
			s2d = s2d + 'U';
		}

	}


	cout << s1u + s1d + s2u + s2d << endl;




	return 0;
}

Measured memory_bytes: 3676
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
using namespace std;

int main(void) {
    int N;
    string s, t;
    cin >> N >> s >> t;

    int mx = 0;
    for(int i=0;i<s.size();i++) {
        int cnt = 0;
        for(int j=0;j<s.size() - i;j++) {
            if(s[i+j] == t[j]) cnt++;
        }
        mx = max(mx, cnt);
    }

    if(N > s.size() + t.size() - mx)cout << N;
    else cout << s.size() + t.size() - mx;

    return 0;
}

Measured memory_bytes: 256
n=int(input())
d=[input().split() for _ in range(n)]
tmp=0
judge=False
for i in range(n):
    if d[i][0]==d[i][1]:
        tmp+=1
    else:
        tmp=0
    if tmp==3:
        print("Yes")
        judge=True
if judge==False:
    print("No")

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef priority_queue <int> pq;
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
#define o_set tree<ii, null_type,less<ii>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<vi>
#define Mod 1000000007
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main()
{
    fastio;
//	#ifndef ONLINE_JUDGE
//    freopen("input.txt", "r" , stdin);
//    freopen("output.txt", "w" , stdout);
//    #endif
    int k,a, b;
    cin >> k >> a >> b;
    
    if (a % k == 0 || b % k == 0 || b/k - a/k > 0)
    	cout << "OK\n";
    else
    	cout << "NG\n";
	//code
	return 0;
}


Measured memory_bytes: 3652
/*+lmake
 * STD = c++14
 * DEFINE += MDEBUG
 */
#include <bits/stdc++.h>
using namespace std;
using LL=long long;
#ifdef DEBUG
     #define debug(args...)            {dbg,args; cerr<<endl;}
#else
    #define debug(args...)              // Just strip off all debug tokens
#endif

struct debugger
{
    template<typename T> debugger& operator , (const T& v)
    {    
        cerr<<v<<" ";    
        return *this;    
    }
} dbg;

void solve(long long N, vector<long long> A, vector<long long> B){
	LL Min=INT_MAX;
	LL sum=0;
	for(int i=0; i<N; ++i) {
		if( A[i]>B[i] ) {
			Min=min(Min,B[i]);
		}
		sum+=A[i];
	}	
	if (Min==INT_MAX) {
		puts("0");
		return;
	}
	cout<<sum-Min<<endl;
}

int main(){	
	long long N;
	scanf("%lld",&N);
	vector<long long> A(N-1+1);
	vector<long long> B(N-1+1);
	for(int i = 0 ; i <= N-1 ; i++){
		scanf("%lld",&A[i]);
		scanf("%lld",&B[i]);
	}
	solve(N, A, B);
	return 0;
}



Measured memory_bytes: 896
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, *abc = LI()
    s = SR(n)
    d = {"A": 0, "B": 1, "C": 2}
    ans = [None] * n
    for i in range(n - 1):
        S1, S2 = s[i][0], s[i][1]
        d1, d2 = d[S1], d[S2]
        if abc[d1] == abc[d2] == 0:
            print("No")
            return
        else:
            if abc[d1] == abc[d2] == 1:
                S11, S12 = s[i+1][0], s[i+1][1]
                d11, d12 = d[S11], d[S12]
                if d1 in [d11, d12]:
                    abc[d1] += 1
                    abc[d2] -= 1
                    ans[i] = S1
                else:
                    abc[d1] -= 1
                    abc[d2] += 1
                    ans[i] = S2
            else:
                if abc[d1] <= abc[d2]:
                    abc[d1] += 1
                    abc[d2] -= 1
                    ans[i] = S1
                else:
                    abc[d2] += 1
                    abc[d1] -= 1
                    ans[i] = S2

    S1, S2 = s[-1][0], s[-1][1]
    d1, d2 = d[S1], d[S2]
    if abc[d1] == abc[d2] == 0:
        print("No")
        return
    if abc[d1] <= abc[d2]:
        abc[d1] += 1
        abc[d2] -= 1
        ans[-1] = S1
    else:
        abc[d2] += 1
        abc[d1] -= 1
        ans[-1] = S2
    print("Yes")
    print(*ans, sep="\n")
    return


#main
if __name__ <= '__main__':
    solve()


Measured memory_bytes: 89924
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>
#include <iomanip>

using namespace std;

int main(){
    int n,L;
    cin >> n >> L;
    vector<int> a(n);

    for(int i = 0; i < n; i++){
        cin >> a[i];
    }

    int kijyun = -1;
    for(int i = 0; i < n-1; i++){
        int tmp = a[i];
        int tmp2 = a[i+1];
        if(abs(tmp + tmp2) >= L){
            kijyun = i+1;
        }
    }

    if(kijyun == -1){
        cout << "Impossible" << endl;
        return 0;
    }
    else{
        cout << "Possible" << endl;
        for(int i = 1; i < kijyun; i++){
            cout << i << endl;
        }
        for(int i = n-1; i > kijyun; i--){
            cout << i << endl;
        }
        cout << kijyun << endl;
    return 0;
    }

}


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

const int NMAX = 26;
int pa[NMAX], lch[NMAX], rch[NMAX];
int depth[NMAX], height[NMAX];

int dfs(int id, int d) {
    if (depth[id] != -1 ) {
        return height[id];
    }
    depth[id] = d;
    int h_max = -1, h;
    if ( lch[id] != -1 ) {
        h_max = max(h_max, dfs(lch[id], d+1 ) );
    }
    if ( rch[id] != -1 ) {
        h_max = max(h_max, dfs(rch[id], d+1 ) );
    }
    height[id] = h_max + 1;
    return height[id];
}

int main() {
    int N;
    cin >> N;
    fill_n(pa, N, -1);
    fill_n(lch, N, -1);
    fill_n(rch, N, -1);
    fill_n(depth, N, -1);
    fill_n(height, N, -1);

    int id, l, r;
    for ( int i = 0; i < N; i++ ) {
        cin >> id >> l >> r;
        lch[id] = l;
        rch[id] = r;
        if ( l != -1 ) pa[l] = id;
        if ( r != -1 ) pa[r] = id;
    }
    
    for ( int i = 0; i < N; i++ ) {
        if ( pa[i] == -1 ) dfs(i, 0);
    }

    for ( int i = 0; i < N; i++ ) {
        string type;
        if ( pa[i] == -1 ) type = "root";
        else if ( lch[i] == -1 && rch[i] == -1 ) type = "leaf";
        else type = "internal node";

        int sib = -1;
        if ( pa[i] != -1 && lch[pa[i]]==i ) sib = rch[pa[i]];
        else if ( pa[i] != -1 && rch[pa[i]]==i ) sib = lch[pa[i]];

        int deg = 0;
        if ( lch[i] != -1 ) deg++;
        if ( rch[i] != -1 ) deg++;

        cout << "node " << i << ": ";
        cout << "parent = " << pa[i] << ", ";
        cout << "sibling = " << sib << ", ";
        cout << "degree = " << deg << ", ";
        cout << "depth = " << depth[i] << ", ";
        cout << "height = " << height[i] << ", ";
        cout << type << "\n";
    }
    return 0;
}


Measured memory_bytes: 3212
#include <iostream>
#include <algorithm>
#include <complex>
#include <string>
#define pb push_back
#define pll pair <ll, ll>
#define MOMI using namespace std;
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#pragma optimize("TKACHENKO-GORYACHENKO")
#pragma GCC optimize("O3")
 
//#pragma GCC optimize("unroll-loops")
 
typedef long long ll;
 
typedef long double ld;
 
using namespace std;
 
const ll maxn = 1e4;

ll A[maxn];

ll S[maxn];
 
ll n, m, k;

int main() {
    ll q, w, e, a, b, c;
    cin >> n;
    for (q = 0; q < n; q++) {
        cin >> A[q];
        S[A[q]]++;
    }
    for (q = 1; q < maxn; q++) {
        S[q] += S[q - 1];
    }
    ll ans = 0;
    for (q = 0; q < n; q++) {
        for (w = q + 1; w < n; w++) {
            ll mx = A[q] + A[w] - 1;
            ll mn = max(A[q], A[w]) - min(A[q], A[w]) + 1;
            // cout << A[q] << " " << A[w] << " " << mn << " " << mx << endl;
            ans += (S[mx] - S[mn - 1]);
            if (A[q] >= mn && A[q] <= mx) {
                ans--;
            }
            if (A[w] >= mn && A[w] <= mx) {
                ans--;
            }
        }
    }
    cout << ans / 3;
}

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

int main(){
  int N, M;
  cin >> N >> M;
  vector<int> p(M);
  vector<string> S(M);
  for(int i = 0; i < M; i++){
    cin >> p.at(i) >> S.at(i);
  }
  
  vector<int> check(N);
  vector<int> count(N);
  int AC = 0;
  int WA = 0;
  
  for(int i = 0; i < M; i++){
    if(check.at(p.at(i) - 1) == 0){
      if(S.at(i) == "AC"){
        check.at(p.at(i) - 1)++;
      }
      else{
        count.at(p.at(i) - 1)++;
      }
    }
  }
  
  for(int i = 0; i < N; i++){
    if(check.at(i)){
      AC++;
      WA += count.at(i);
    }
  }
  
  cout << AC << endl << WA <<endl;
}

Measured memory_bytes: 6912
import sys

input = sys.stdin.readline
x, n = map(int, input().split())
p = list(map(int, input().split()))
for i in range(100):
    s = x - i
    e = x + i
    if p.count(s) == 0:
        print(s)
        break
    elif p.count(e) == 0:
        print(e)
        break

Measured memory_bytes: 9188
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i,a) for(int i = 0;i < (a); i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define repd(i,a,b) for(int i = (a); i >= (b); i--)
#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)
#define all(u) (u).begin(),(u).end()
#define rall(u) (u).rbegin(),(u).rend()
#define UNIQUE(u) (u).erase(unique(all(u)),(u).end())
#define pb push_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)

using namespace std;

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

int n;
vector<ll> a;

ll attempt() {
    ll ret = 0;
    int cnt = 0;
    for (int j = 0; j < n - 2; ++j) {
	if (a[j] < a[j + 1] + a[j + 2]) {
	    ret += a[j] + a[j + 1] + a[j + 2];
	    if (++cnt == 2)
		return ret;
	    j += 2;
	}
    }
    return 0;
}

inline int next(int s) { int foo = s & -s, bar = s + foo; return bar | (s & ~bar) / foo >> 1; }

bool chk(int i, int s)
{
    ll tmp = 0;
    int cnt = 0;
    rep(j,6) {
	if (s >> j & 1) {
	    if (cnt++)
		tmp += a[i + j];
	    else
		tmp -= a[i + j];
	}
    }
    return tmp > 0;
}

ll attempt1(int i) {
    for (int s = 7; s < 1<<6; s = next(s)) {
	if (chk(i, s) && chk(i, 63 ^ s)) {
	    ll ret = 0;
	    rep(j,6)
		ret += a[i + j];
	    return ret;
	}
    }
    return 0;
}

int main() {
    cin >> n;
    rep(i,n){
	ll t;
	cin >> t;
	a.pb(t);
    }
    sort(rall(a));

    ll ans = 0;
    ans = max(ans, attempt());
    rep(i,n-5)
	ans = max(ans, attempt1(i));
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 2076
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

LL N;
vector<LL>A;
vector<LL>xmos;
const LL MOD = 1000000007;

LL solvedp(vector<pair<LL, int>>& vec, LL lasval) {
	LL zero = 1, notz = 0;
	LL zpar = 1, npar = 0;
	for (auto p : vec) {
		if (p.first == 0) {
			zpar = (notz * p.second) % MOD;
			zero += zpar;
			zero %= MOD;
		}
		else {
			npar = (zero * p.second) % MOD;
			notz += npar;
			notz %= MOD;
		}
	}
	if (lasval == 0) {
		return zpar;
	}
	return npar;
}

int main() {
	cin >> N;
	LL xsum = 0;
	xmos.push_back(0);
	for (int i = 0; i < N; ++i) {
		LL tmp;
		cin >> tmp;
		A.push_back(tmp);
		xsum ^= tmp;
		xmos.push_back(xsum);
	}
	/*vector<LL>dp(N + 1);
	dp[1] = 1;
	LL odd = 1, eve = 0;
	for (int i = 2; i <= N; ++i) {
		if (i % 2) {
			dp[i]++;
			dp[i] += eve;
			dp[i] %= MOD;
			odd += dp[i];
			odd %= MOD;
		}
		else {
			dp[i] += odd;
			eve += dp[i];
			eve %= MOD;
		}
	}*/
	if (xsum != 0) {
		vector<pair<LL, int>>nowarr;
		for (int i = 1; i <= N; ++i) {
			if (xmos[i] == 0) {
				nowarr.push_back({ 0,1 });
			}
			if (xmos[i] == xsum) {
				nowarr.push_back({ xsum,1 });
			}
		}
		LL ans = solvedp(nowarr, xsum);
		cout << ans << endl;
	}
	else {
		map<LL, int>zerolas;
		map<LL, vector<pair<LL, int>>>arrs;
		int zerocnt = 0;
		LL zerotwo = 1;
		for (int i = 1; i <= N; ++i) {
			if (xmos[i] == 0) {
				zerocnt++;
				if (i < N) {
					zerotwo *= 2;
					zerotwo %= MOD;
				}
			}
			else {
				if (zerocnt != zerolas[xmos[i]]) {
					arrs[xmos[i]].push_back({ 0,zerocnt - zerolas[xmos[i]] });
					zerolas[xmos[i]] = zerocnt;
				}
				arrs[xmos[i]].push_back({ xmos[i],1 });
			}
		}
		LL ans = zerotwo;
		for (auto& elm : arrs) {
			if (elm.second.back().second != 0) {
				elm.second.push_back({ 0,1 });
			}
			ans += solvedp(elm.second, 0);
			ans %= MOD;
		}
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 85084
# coding: utf-8
# 81

while True:
    n=int(input())
    c=0
    if n==0:
        break
    while True:
        if n==1:
            break
    
        if n%2==0:
            n = n/2
            c += 1
        
        else:
            n = n*3+1
            c += 1
    print(c)
    




Measured memory_bytes: 5604
const stdin = require('fs').readFileSync('/dev/stdin', 'utf8')

const [a, b] = stdin
  .split('\n')[0]
  .split(' ')
  .map(Number)

let res = 'No'
for (let i = 1; i < 4; i++) {
  const product = a * b * i
  if (product % 2 !== 0) res = 'Yes'
}

console.log(res)


Measured memory_bytes: 7372
s = input()
cnt = 0
for i in range(len(s)):
    if i == 0:
        if s[i] != "A":
            print("WA")
            exit()
    else:
        if s[i] == "C" and 2 <= i <= len(s) - 2:
            cnt += 1
        else:
            if s[i] != s[i].lower():
                print("WA")
                exit()
if cnt == 1:
    print("AC")
else:
    print("WA")


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

int main(){
	string s;cin >> s;
  	int zero = 0, ichi = 0;
  	for(auto c:s){
    	if(c == '0')++zero;
      	else ++ichi;
    }
    cout << min(zero,ichi)*2<<endl;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#define F first
#define S second
#define INF 1<<26
using namespace std;
typedef pair<int,int>P;
typedef pair<int,P>PP;
typedef struct{
  int to,cost;
}Edge;
int main(){
  int c,n,m,st,go;
  while(1){
    int d[11111][111];
    cin >> c >> n >> m >> st >> go;
    if(c==0 && n==0 && m==0 && st==0 && go==0)break;
    for(int i=0;i<=m;i++){
      for(int j=0;j<=c;j++){
	d[i][j]=INF;
      }
    }
    st--;go--;
    
    int a,b,f;
    vector<Edge> G[1111];
    for(int i=0;i<m;i++){
      cin >> a >> b >> f;
      a--;b--;
      G[a].push_back((Edge){b,f});
      G[b].push_back((Edge){a,f});
    }
    priority_queue<PP,vector<PP>,greater<PP> >pq;
    pq.push(PP(0,P(st,c)));
    d[st][c]=0;
    while(!pq.empty()){
      PP p=pq.top();//左から、コスト、今いる場所、残りのチケット
      pq.pop();
      int now=p.S.F;//今いる場所
      int ti=p.S.S;//チケット
      for(int i=0;i<G[now].size();i++){
	Edge e=G[now][i];
	if(ti){//チケットがあったら
	  if(d[e.to][ti-1] > d[now][ti]+(e.cost/2)){
	    d[e.to][ti-1]=d[now][ti]+(e.cost/2);
	    pq.push(PP(d[e.to][ti-1],P(e.to,ti-1)));
	  }
	}
	if(d[e.to][ti] > d[now][ti]+e.cost){
	  d[e.to][ti]=d[now][ti]+e.cost;
	  // cout << d[e.to][ti] << endl;
	  pq.push(PP(d[e.to][ti],P(e.to,ti)));
	}
      } 
    }
    
    int ans=INF;
    for(int i=0;i<=c;i++){
      ans=min(ans,d[go][i]);
    }
    cout << ans << endl;
  }
}

Measured memory_bytes: 3332
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  int a[n];
  int b[n];
  int c[n];
  int dpa[n];//n日目にaをした時のそれまでの最大満足度
  int dpb[n];
  int dpc[n];
  for(int i = 0;i < n;i++){
    cin >> a[i] >> b[i] >> c[i];
  }
  dpa[0] = a[0];
  dpb[0] = b[0];
  dpc[0] = c[0];
  for(int i = 1;i < n;i++){
    dpa[i] = max(dpb[i-1],dpc[i-1]) + a[i];
    dpb[i] = max(dpa[i-1],dpc[i-1]) + b[i];
    dpc[i] = max(dpb[i-1],dpa[i-1]) + c[i];
  }
  int MAX = max(dpa[n-1],dpb[n-1]);
  MAX = max(MAX,dpc[n-1]);
  cout << MAX << endl;
}

Measured memory_bytes: 2560
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long int
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define INF 0x3f3f3f3f

using namespace std;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int,null_type, less<int>,rb_tree_tag,tree_order_statistics_node_update>   ordered_set;
const int N=1e5+10, mod=1e9+7;

int main(){
	ios_base::sync_with_stdio(0);	cin.tie(0);
	int n,res=0;
	cin>>n;
	
	int a[n+1],b[n+1],c[n+1];
	
	for(int i=1;i<=n;i++)cin>>a[i];
	
	for(int i=1;i<=n;i++){
		cin>>b[i];
		res+=b[i];
	}
	
	for(int i=1;i<=n-1;i++)cin>>c[i];
	
	for(int i=1;i<n;i++){
		if( a[i]+1==a[i+1] ){
			res+=c[ a[i] ];
		}
	}
	cout<<res;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  string a,b,c;
  cin>>a>>b>>c;
  char next='a';
  while(true){
    if(next=='a'){
      if(a.size()==0){
        cout<<'A'<<endl;
        return 0;
      }
      next=a.at(0);
      a=a.erase(0,1);
    }
    if(next=='b'){
      if(b.size()==0){
        cout<<'B'<<endl;
        return 0;
      }
      next=b.at(0);
      b=b.erase(0,1);
    }
    if(next=='c'){
      if(c.size()==0){
        cout<<'C'<<endl;
        return 0;
      }
      next=c.at(0);
      c=c.erase(0,1);
    }
  }
  return 0;
}

Measured memory_bytes: 256
n = int(input())
now = n // 1000
n %= 1000
flag = 1
for i in range(3):
  if now == n // 10**(2-i) and flag == 1:
    print('Bad')
    flag = 0
  now = n // (10**(2-i))
  n %= 10**(2-i)
if flag == 1:
  print('Good')

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,a,b,c,d,e,flag=0,cnt=0,flag1=0;
    vector<int>vec;
    map<int,int>mp;

     cin>>n;
     cout<<(double)2*3.14159265359*n<<endl;
}


Measured memory_bytes: 3756
import java.util.*;
import java.io.*;

class EratosSieve {
	private int sieve[];
	private Set<Integer> primeSet = new HashSet<>();

	EratosSieve(int x) {
		//篩の作成
		sieve = new int[x + 1];
		sieve[0] = sieve[1] = -1;
		for (int i = 2; i <= x; i++) {
			if (sieve[i] > 0)
				continue;
			for (int j = i + i; j <= x; j += i) {
				sieve[j] = i;
			}
		}
		//素数Setの作成
		Set<Integer> primeSet = new HashSet<>();
		for (int i = 2; i < sieve.length; i++) {
			if (sieve[i] == 0)
				primeSet.add(sieve[i]);
		}
	}

	//0~xまでの篩の配列を返す。配列値は素数なら0。素数でなければ、最大の素因数。(0,1は素数でないので-1)
	int[] getSieve() {
		return sieve;
	}

	//0~xまでの、素数のSetを返す
	Set<Integer> getPrimeSet() {
		return primeSet;
	}

	//素数判定
	boolean isPrime(int x) {
		return sieve[x] == 0;
	}

	//xの素因数分解結果をmapで返す(1は含まない)
	Map<Integer, Integer> primeFactrization(int x) {
		Map<Integer, Integer> map = new HashMap<>();
		while (sieve[x] > 0) {
			map.put(sieve[x], map.getOrDefault(sieve[x], 0) + 1);
			x /= sieve[x];
		}
		map.put(x, map.getOrDefault(x, 0) + 1);
		return map;
	}
}

public class Main {
	static int mod = (int) 1e9 + 7;
	static int DX[] = { -1, 0, 1, 0 }, DY[] = { 0, -1, 0, 1 };
	static final int INF = Integer.MAX_VALUE / 3;
	static final long LINF = Long.MAX_VALUE / 3;

	public static void main(String[] args) {
		FastScanner fs = new FastScanner(System.in);
		int n = fs.nextInt();
		EratosSieve es = new EratosSieve(10000);
		Map<Integer,Long> map = new HashMap<>();
		for(int i=2;i<=n;i++) {
			Map<Integer,Integer> tmap = es.primeFactrization(i);
			for(int key:tmap.keySet()) {
				int v = tmap.get(key);
				map.put(key, map.getOrDefault(key, 0L)+v);
			}
		}
		long ans = 1;
		for(int key:map.keySet()) {
			long v = map.get(key);
			ans = ans * (v+1) % mod;
		}
		System.out.println(ans);
		
	}
}

//高速なScanner
class FastScanner {
	private BufferedReader reader = null;
	private StringTokenizer tokenizer = null;

	public FastScanner(InputStream in) {
		reader = new BufferedReader(new InputStreamReader(in));
		tokenizer = null;
	}

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

	public String nextLine() {
		if (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				return reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken("\n");
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

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

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public int[] nextIntArray(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		return a;
	}

	public long[] nextLongArray(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nextLong();
		return a;
	}
}

Measured memory_bytes: 23508
import java.io.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    new Main();
  }
  Scanner in = new Scanner(System.in);
  PrintWriter out = new PrintWriter(System.out);
  
  int len;
  char[] s;
  long[] cmp;

  Main() {
    s = in.next().toCharArray();
    cmp = new long[len = s.length];
    int at = 0;
    for (int i = 0; i < len; i++) {
      int j = i;
      while (j < len && s[i] == s[j])
        j++;
      cmp[at++] = j - i;
      i = j - 1;
    }
	// out.println(Arrays.toString(cmp));
	// out.println(at);
    
    long ans = 0;
    if (s[0] == '>') {
      for (int i = 1; i < at - 1; i += 2) {
		  ans += tri(max(cmp[i], cmp[i + 1]));
		ans += tri(min(cmp[i], cmp[i + 1]) - 1);
	  }
	  if (at %2 == 0)
	  	ans += tri(cmp[at - 1]);
	  ans += tri(cmp[0]);
    }
    else {
      for (int i = 0; i <= at - 2; i += 2) {
		ans += tri(max(cmp[i], cmp[i + 1]));
		ans += tri(min(cmp[i], cmp[i + 1]) - 1);
      }
	  if (at %2 == 1)
	  	ans += tri(cmp[at - 1]);
    }

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

long tri(long a) {
	return a*(a + 1L)/2L;
}

long min(long a, long b) {
	if (a < b)
		return a;
	return b;
}
  long max(long a, long b) {
	  if (a > b)
	  	return a;
	return b;
  }
}

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

int n, a[10000], ans = 0;

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	for (int i = 0; i < n; i++) {
		for (int j = a[i]; j % 2 == 0 && j > 0; j /= 2) {
			ans++;
		}
	}

	cout << ans << endl;
}

Measured memory_bytes: 256
//Bokan ga bokka--nn!!
//Daily Lunch Special Tanoshii !!
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d",&x)
#define rep(i,x) for(int i=0;i<x;i++)
void rec(int rem,vector<int>num)
{
	if(rem==0)
	{
		printf("%d",num[0]);
		for(int i=1;i<num.size();i++) printf(" %d",num[i]);
		puts("");
		return ;
	}
	for(int i=min(rem,num[num.size()-1]);i>=1;i--)
	{
		num.pb(i);
		rec(rem-i,num);
		num.pop_back();
	}
}
int main()
{
	int n;
	while(1)
	{
		scanf("%d",&n);
		if(!n) return 0;
		for(int i=n;i>=1;i--)
		{
			vector<int>vec(0); vec.pb(i);
			rec(n-i,vec);
		}
	}
}

Measured memory_bytes: 1220
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
	n= int(input())
	l = []
	for i in range(n):
		s, p = input().split()
		l.append([s, int(p), i+1])
	l = (sorted(l, key=lambda x: x[1], reverse=True))
	key_sort = (sorted(l, key=lambda x: x[0]))
	for ans in key_sort:
		print(ans[2])

if __name__ == '__main__':
	import sys
	sys.exit(main())


Measured memory_bytes: 3064
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <iomanip>	
#include <deque>
#include <set>
#include <climits>
#include <memory>
#include <numeric>
#include <utility>


#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
#define fore(i,a) for(auto &i:a)
#define INF INT_MAX/2;
#define PI 3.14159265358979323846264338327950L;
const int INF2 = 1001001001;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
//ll llmax = 10000000000000000000ll;
const ll LINF = 1e18;
using namespace std;
using Graph = vector<vector<int>>;



int cnt_digit(ll N)
{
	int digit = 0;

	while (N > 0)
	{
		N /= 10;
		digit++;
	}
	return digit;
}


 // 最大公約数計算
 
ll gcd(ll a, ll b)
{
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
// 最小公倍数の計算

ll lcm(ll a, ll b)
{
	ll g = gcd(a, b);

	return a / g * b;
}

struct union_find
{
	vector<int> par, r;
	union_find(int n)
	{
		par.resize(n);
		r.resize(n);
		init(n);
	}


	void init(int n)
	{
		for (int i = 0; i < n; i++) par[i] = i;
		for (int i = 0; i < n; i++) r[i] = 0;
	}

	int find(int x)
	{
		if (par[x] == x)return x;
		else return find(par[x]);
	}

	void unite(int x, int y)
	{
		x = find(x);
		y = find(y);

		if (r[x] < r[y])
		{
			par[x] = y;
		}
		else
		{
			par[y] = x;
			if (r[x] == r[y])
			{
				r[x]++;
			}
		}
	}
};

const int MAX_N = 110000;

int memo[MAX_N];

int rec(int n) {
	if (n == 0)return 0;
	if (memo[n] != -1)return memo[n];

	int res = n;

	for (int pow6 = 1; pow6 <= n; pow6 *= 6)res = min(res, rec(n - pow6) + 1);

	for (int pow9 = 1; pow9 <= n; pow9 *= 9)res = min(res, rec(n - pow9) + 1);

	return memo[n] = res;
}

int calc(int x) {
	int ret = 0;
	while (x % 2 == 0) {
		x /= 2;
		ret++;
	}

	return ret;
}

int a[12][12];
int D;
ll G;
vector<ll> P, C;

//aのp乗を求めるアルゴリズム
long modpow(long a, int p) {
	if (p == 0)return 1;
	if (p % 2 == 0) {
		//pが偶数のとき
		int halfP = p / 2;
		long half = modpow(a, halfP);
		return half * half % mod;
	}
	else {
		//pが奇数の時は、
		//pを偶数にするために1減らす
		return a * modpow(a, p - 1) % mod;
	}

}


//(10*9*8)/(3*2*1);
//10*9*8 -> ansMul
//3*2*1 -> ansDiv;
long calcComb(int a, int b) {
	if (b > a - b) return calcComb(a, a - b);
	long ansMul = 1;
	long ansDiv = 1;
	rep(i, 0, b) {
		ansMul *= (a - i);
		ansDiv *= (i + 1);
		ansMul %= mod;
		ansDiv %= mod;
	}
	//ansMul /ansDivをやりたい
	//ansDivの逆元を使って求めよう。

	long ans = ansMul * modpow(ansDiv, mod - 2) % mod;
	return ans;

}


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

	int n, m,l;
	cin >> n >> m >> l;

	int ans = max<int>({ n * 10 + m + l,n + 10 * m + l,n + m + 10 * l });

	cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <numeric>
#include <regex>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define modulo 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9



const int MAX_V = 100 + 1;
int d[MAX_V][MAX_V]; // 重み付き隣接行列
					 // ワーシャルフロイドではこれを更新していく
					 // 最初、辺がない場合はINFで初期化
					 // d[i][j] : iからjまでの最短距離
int N, M; // N : 頂点数(Vertex)
		  // M : 辺数(Edge)



void warshall_floyd() {
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			if (i != j && d[i][j] == 0) d[i][j] = INF; // この初期化を忘れてはいけない
													   // i == jのときは d[i][i] = 0
		}
	}

	for (int k = 1; k <= N; k++) {
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}
}


int main() {
	cin >> N >> M;
	vector<P> E(M);
	int W[1001];
	for (int i = 0; i < M; i++) {
		cin >> E[i].first >> E[i].second >> W[i];
		d[E[i].second][E[i].first] = d[E[i].first][E[i].second] = W[i];
	}

	warshall_floyd();

	int ans = 0;

	for (int e = 0; e < E.size(); e++) {
		bool flag = false;

		for (int i = 1; i <= N && !flag; i++) {
			if (d[i][E[e].second] == d[i][E[e].first] + W[e]) {
				flag = true; // 最短経路に使われていた
			}
		}

		if (!flag) ans++;
	}

	cout << ans << endl;

	return 0;
}

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

int main() {
  int P = 0;
  int W = 0;
  int G = 0;
  int Y = 0;
  int N;
  cin >> N;
  
  for ( int i = 0; i< N; i++) {
    string S;
    cin >> S;
    if ( S == "P") P++;
    if ( S== "W") W++;
    if ( S == "G") G++;
    if (S=="Y") Y++;
  }
  if (Y > 0 ) {
    cout << "Four" << endl;
  } else {
    cout << "Three" << endl;
  }
}

Measured memory_bytes: 256
#include<stdio.h>
int main()
{
    int x,a,b,d1,d2;
    scanf("%d%d%d",&x,&a,&b);
    d1=abs(x-a);
    d2=abs(x-b);
    if(d1<d2)
        printf("A\n");
    if(d1>d2)
        printf("B\n");
    return 0;
}


Measured memory_bytes: 128
x,y = map(int, input().split())
if x == y :
    print(0)
elif (-x) == y:
    print(1)
elif x >= 0 and x < y: # 0 1
    print(y-x)
elif x >= 0 and x > y: # 5 4 5 -7
    if x <= (-y): #10 -11
        print(abs(x-(-y))+1)
    else: # 10 -9
        print(min(abs(y-(-x))+1,abs(x-y)+2))
elif x < 0 and x > y: # -1 -3
    print(min(abs(x-y)+2, -y -x+1))
elif x < 0 and x < y: # -3 -1
    if y > 0: # -5 6
        print(min(abs(y-x),abs((-y)-x)+1))
    else:
        print(abs(y - x))



Measured memory_bytes: 61956
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout<<(x)<<endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,vector<ll>,greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define mp make_pair
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define sz(x) (int)(x).size()
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
using vc = vector<char>;
using vvc = vector<vc>;
ll dx[4]={0,1,0,-1};
ll dy[4]={1,0,-1,0};
int main(){
  ll n,a,b;
  cin>>n>>a>>b;
  vll h(n);
  rep(i,n) cin>>h[i];
  ll l=0;
  ll r=MOD;
  while(l+1<r){
    ll mid=(l+r)/2;
    ll cu=0;
    rep(i,n){
      if(h[i]-b*mid<=0)continue;
      else{
        cu+=(h[i]-b*mid+a-1-b)/(a-b);
      }
    }
    if(cu<=mid){
      r=mid;
    }
    else l=mid;
  }
  COUT(r);
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c,k;
    cin >> a>>b>>c>>k;
    int h=0;
    if(h<a)h=a;
    if(h<b)h=b;
    if(h<c)h=c;
    //cout << h << endl;
    
    if(h==a){
        for(int i=0;i<k;i++){
            a=a*2;
        }
    }
    else if(h==b){
        for(int i=0;i<k;i++){
            b=b*2;
        }
    }
    else if(h==c){
        for(int i=0;i<k;i++){
            c=c*2;
        }
    }
    cout << a+b+c << endl;
    return 0;

    
}


Measured memory_bytes: 256
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<ll, ll> P;
const ll inf = 1e15;
struct edge{
  ll to, cost;
  edge(ll t, ll c) {
    to = t, cost = c;
  }
};
// Kからの距離を求めておく
int N;
vector<edge> G[100005];
ll d[100005];
void dijkstra(int s){
  priority_queue<P, vector<P>, greater<P> >que;
  FOR(i,1,N+1) d[i] = inf;
  d[s] = 0;
  que.push(P(0, s));
  while(!que.empty()){
    P p = que.top(); que.pop();
    int v = p.second;
    if(d[v] < p.first) continue;
    for(auto &e : G[v]){
      if(d[e.to] > d[v] + e.cost){
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main()
{
  cin >> N;
  FOR(i,0,N-1) {
    ll a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }
  int Q, K; cin >> Q >> K;
  // Kからの距離
  dijkstra(K);
  FOR(i,0,Q) {
    int x, y; cin >> x >> y;
    cout << d[x] + d[y] << endl;
  }
  return 0;
}

Measured memory_bytes: 11120
s = input()
t = input()
m = []

for i in range(len(s)):
    a = s[i:len(t)+i]
    if  len(a) < len(t):
        break
    else:
        cnt = 0
        for x,y in zip(a,t):
            if x != y:
                cnt += 1
        m.append(cnt)
print(min(m))


Measured memory_bytes: 9116
/*16D8103005J 相良和希 ayachi0117 C++*/
#include<iostream>
#include<cmath>
bool Pnumber(int x)
{
	for(int i=2;i<=std::sqrt(x);i++){
		if(i/10 != 0){
			if(i%10==1 || i%10==3 || i%10==7 || i%10==9){
				if(x%i == 0){
					return false;
				}
			}
		}else{
			if(x%i == 0){
				return false;
			}
		}
	}
	return true;
}

int main()
{
	int n;
	std::cin >> n;
	int x[n];
	int cou=0;
	for(int i=0;i<=n-1;i++){
		std::cin >> x[i];
	}
	for(int i=0;i<=n-1;i++){
		if(Pnumber(x[i])){
			cou++;
		}
	}
	std::cout << cou << "\n" ;
	return 0;
}

/*
11
7
8
9
10
11
12
13
14
15
16
17
4

*/



Measured memory_bytes: 3076
#include <bits/stdc++.h>
#define int long long
#define P pair<int,int>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
signed main(){
    int n; cin>>n;
    vector<pair<int, bool>> a(n);
    int ans=0;
    rep(i,n){
        cin>>a.at(i).first; ans+=a.at(i).first;
        if(a.at(i).first%10==0) a.at(i).second=false;
        else a.at(i).second=true;
    }
    if(ans%10!=0){
        cout<<ans<<endl;
        return 0;
    }
    sort(a.begin(),a.end());
    rep(i,n){
        if(a.at(i).second){
            ans-=a.at(i).first;
            cout<<ans<<endl;
            return 0;
        }
    }
    cout<<0<<endl;
}

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

using namespace std;
using ll =long long;
typedef pair<int,int> P;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//Write From this Line

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

	sort(a.begin(),a.end());
	sort(f.rbegin(),f.rend());

	ll ng = -1, ok = 1e13;
	while(ok - ng > 1){
		ll mid = ok + ng; mid /= 2;
		ll count = 0;
		rep(i,n){
			// midを下回るまで修行する
			ll ob = mid / f[i];
			count += max(0ll, a[i] - ob);
		}
		if(count <= k) ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
}


Measured memory_bytes: 6408
#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define IOS cin.tie(0),ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
template<class Type>struct is_vector : std::false_type {};
template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T> inline ostream& operator << (ostream& out, const vector<T>& v) {
	if (v.empty())return out;
	constexpr bool is_vector_v = is_vector<T>::value;
	if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
	else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
	return out;
}
inline void put() {}
template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
inline void putn() {}
template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

struct edge {
	ll to, cost, index;
	edge(ll to, ll cost,ll index) :to(to), cost(cost), index(index) {}
};

void dfs(vector<vector<edge>>& path, vector<int>& st,int now) {
	REP(i, path[now].size()) {
		if (st[path[now][i].index] == 0) {
			st[path[now][i].index] = 1;
			dfs(path, st, path[now][i].to);
		}
	}
}

int main() {
	IOS;
	int n, m, p;
	cin >> n >> m >> p;
	vector<ll> a(m), b(m), c(m);
	vector<vector<edge>> path(n);
	vector<vector<edge>> path2(n);
	vector<vector<edge>> path3(n);
	vector<int> st(m, 0), dt(m, 0);
	REP(i, m) {
		cin >> a[i] >> b[i] >> c[i];
		a[i]--;
		b[i]--;
		path[a[i]].push_back(edge(b[i], c[i] - p,i));
		path2[b[i]].push_back(edge(a[i], c[i] - p,i));
	}
	vector<ll> dist(n, -1e15);
	int src = 0, dest = n - 1;
	dfs(path, st, src);
	dfs(path2, dt, dest);
	dist[src] = 0;
	REP(i, m) {
		if (st[i] && dt[i]) {
			path3[a[i]].push_back(edge(b[i], c[i] - p, i));
		}
	}
	REP(i, n * 2) {
		REP(j, n) {
			REP(k, path3[j].size()) {
				if (chmax(dist[path3[j][k].to], dist[j] + path3[j][k].cost)) {
						
				}
			}
		}
	}
	REP(i, n) {
		REP(j, path3[i].size()) {
			if (dist[i] + path3[i][j].cost > dist[path3[i][j].to]) {
				put(-1);
				return 0;
			}
		}
	}
	put(max(dist[dest], 0ll));
	return 0;
}


Measured memory_bytes: 1280
#include <bits/stdc++.h>
using namespace std;
 
#define vi vector<int>
#define ll long long
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define MOD 998244353
#define endL << '\n'
#define ii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define endL << '\n'
#define vii vector<ii>
#define pb push_back

/*
ll powmod(ll x, ll y, ll mod){
	ll res = 1;
	while (y > 0){
		if (y%2) res = (res*x) % mod;
		x = (x*x) % mod;
		y /= 2;
	}
	return res;
}
*/

/*const int N = 1000000;
int lp[N+1];
vector<ll> pr;

void sieve(){
	for (int i=2; i<=N; ++i) {
	    if (lp[i] == 0) {
	        lp[i] = i;
	        pr.push_back (i);
	    }
	    for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
	        lp[i * pr[j]] = pr[j];
	}
}*/

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t=1; //cin >> t;
	while(t--){
		int n, k; cin >> n >> k;
		vll h(n); for (int i = 0; i < n; i++) cin >> h[i];
		vll dp(n, -1); dp[0] = 0;
		for (int i = 1; i < n; i++){
			dp[i] = dp[i-1] + abs(h[i] - h[i-1]);
			for (int j = 2; j <= k; j++){
				if (i-j < 0) break;
				dp[i] = min(dp[i], dp[i-j] + abs(h[i-j] - h[i]));
			}
		}
		cout << dp[n-1];
	}
}

Measured memory_bytes: 1792
#include <iostream>
#include <vector>
#define int long long
using namespace std;
const int NM = 100000+1;
const int MOD = 1e9+7;
int n;
bool visited[NM];
vector<int> graph[NM];
pair<int,int> colors[NM];

pair<int,int> getColor(int i) {
    if(colors[i].first and colors[i].second)
        return colors[i];
    colors[i].first = colors[i].second = 1;
    visited[i] = true;
    for(int j = 0;j<graph[i].size();j++) {
        if(not visited[graph[i][j]]) {
            auto c = getColor(graph[i][j]);
            colors[i].first = (colors[i].first * c.second)%MOD;
            colors[i].second = (colors[i].second * (c.first + c.second))%MOD;
        }
    }
    return colors[i];
}
void solve() {
    auto ans = getColor(1);
    cout << (ans.first+ans.second)%MOD<<'\n';
}
signed main() {
    cin>>n;
    for(int i=0;i<NM;i++) visited[i] = false;
    for(int i=0;i<NM;i++) colors[i].first = colors[i].second = 0;

    for(int i = 0;i<n-1;i++) {
        int x,y;
        cin>>x>>y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    solve();
//    for(int i =0;i<5;i++)
//        cout << colors[i].first << ' ' << colors[i].second << '\n';
}



Measured memory_bytes: 12928
N = int(input())
S = input()
lis = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
for i in range(len(S)):
    id = lis.index(S[i])
    id += N
    id = id%26
    print(lis[id],end="")

Measured memory_bytes: 4468
#include <stdio.h>

int main(void) {
  int S;

  scanf("%d", &S);

  int t[3];

  t[0] = S/(60*60);
  t[1] = S%(60*60) / 60;
  t[2] = S%(60*60) % 60;

  printf("%d:%d:%d\n", t[0], t[1], t[2]);
  return (0);
}



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

// types
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;

// macros
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#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 FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

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

  ll n,m,R;
  cin >> n >> m >> R;
  vll r(R);
  REP(i, R){
    cin >> r[i];
    r[i]--;
  }

  vector<vll> edge(n, vll(n, LLINF));
  ll a,b,c;
  REP(i, m){
    cin >> a >> b >> c;
    a--;
    b--;
    edge[a][b] = edge[b][a] = c;
  }

  REP(i, n)
    edge[i][i] = 0;


  REP(k, n){
    REP(i, n){
      REP(j, n)
        edge[i][j] = min(edge[i][j], edge[i][k]+edge[k][j]);
    }
  }

  sort(r.begin(), r.end());
  ll ans = LLINF;
  do{
    ll sum = 0;
    REP1(i, R)
      sum += edge[r[i-1]][r[i]];
    ans = min(ans, sum);
  }
  while(std::next_permutation(r.begin(), r.end()));


  cout << ans << endl;

}


Measured memory_bytes: 640
n, k = gets.split.map(&:to_i)

hash = {}
n.times { 
	price, kind = gets.split.map(&:to_i)
	hash[kind] ||= []
	hash[kind] << price
}

accum = []
hash.each{|kind, prices|
	accum << [0]
	prices.sort.reverse.each_with_index{|p, i|
		a = accum[-1]
		a << a[-1] + p + 2 * i
	}
}

table = (0..1).map{ Array.new(k+1, 0) }
for i in 0..accum.size-1
	for j in 0..k
		table[1][j] = 
			(0..[accum[i].size-1, j].min)
			.map{|l| table[0][j-l] + accum[i][l]}
			.max
	end
	table << table.shift
end
p table[0][k]

Measured memory_bytes: 8692
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int,int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i,n) for (int i = 1;i <= (int)(n); ++i)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repv(i,a,b) for(int i=b-1;i>=a;i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int ,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set ;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const int N=200005;
vi v[N];
bool vis[N];
int val[N],p[N],n;
ii e;
int finda(int x)
{
  if(p[x]==x) return x;
  return p[x]=finda(p[x]);
}
void dfs(int u)
{
  set<int> s;
  for(int v1:v[u])
    {
      dfs(v1);
      s.insert(val[v1]);
    }
  int cnt=0;
  for(int i=0;;i++)
    if(s.count(i)) cnt++;
    else break;
  if(val[u]==-1) val[u]=cnt;
}
void fun()
{
  v[e.fi].pb(e.se);
  bool ok=true;
  rep(i,1,n+1)
    {
      set<int> s;
      for(int v1:v[i])
	s.insert(val[v1]);
      int cnt=0;
      for(int j=0;;j++)
	if(s.count(j)) cnt++;
	else break;
      if(val[i]!=cnt) ok=false;
    }
  if(ok)
    {
      cout<<"POSSIBLE"<<endl;
      exit(0);
    }
}
int main()
{
  std::ios::sync_with_stdio(false);
  cin.tie(NULL) ; cout.tie(NULL) ;
  int x;
  cin>>n;
  rep(i,1,n+1) p[i]=i,val[i]=-1;
  rep(i,1,n+1)
    {
      cin>>x;
      int x1=finda(x),y1=finda(i);
      if(x1!=y1)
	{
	  v[x].pb(i);
	  p[y1]=x1;
	}
      else
	e=mp(x,i);
    }
  dfs(e.se);
  fun();
  int cnt=0;
  set<int> s;
  for(int v1:v[e.fi])
    s.insert(val[v1]);
  v[e.fi].pop_back();
  for(int i=0;;i++)
    if(s.count(i)) cnt++;
    else break;
  rep(i,1,n+1) val[i]=-1;
  val[e.fi]=cnt;
  dfs(e.se);
  fun();
  cout<<"IMPOSSIBLE"<<endl;
  return 0 ;
}


Measured memory_bytes: 28032