akhauriyash/Code-Regression huggingface dataset:
#include  <bits/stdc++.h>
#define ll long long
using namespace std;

int  main(){
    //freopen("inp.txt","r",stdin);
    ll n,res=0;
    cin>>n;
    while (n!=0){
        if (n%10==2) res++;
        n/=10;
    }
    cout<<res;
}


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

int main(){
  int a, b, c;
  cin>>a>>b>>c;

  if(a > b) swap(a, b);
  if(b > c) swap(b, c);
  if(a > b) swap(a, b);

  cout<<a<<" "<<b<<" "<<c<<endl;
  return 0;
}



Measured memory_bytes: 3092
#include <stdio.h>

int main(void) {

  int N, M, C;
  do {
    scanf("%d %d %d", &N, &M, &C);
  } while (N < 1 || N > 20 || M < 1 || M > 20 || C < -100 || C > 100);

  int B[M];
  for (int i = 0; i < M; i++) {
    do {
      scanf("%d", &B[i]);
    } while (B[i] < -100 || B[i] > 100);
  }

  int A[N][M];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      do {
	scanf("%d", &A[i][j]);
      } while (A[i][j] < -100 || A[i][j] > 100);
    }
  }

  int m[N];
  int n = 0;
  for (int i = 0; i < N; i++) {
    m[i] = 0;
    for (int j = 0; j < M; j++) {
      m[i] += A[i][j] * B[j];
    }
    m[i] += C;
    if (m[i] > 0) {
      n++;
    }
  }

  printf("%d\n", n);

  return 0;
}

Measured memory_bytes: 128
package main
import . "fmt"
func main() {
  var n,m,x,a,c int
  Scan(&n,&m,&x)
  c = 0
  for i:=0;i<m;i++ {
    Scanf("%d",&a)
    if x < a { break }
    c++
  }
  Println(min(c,m-c))
}
func min(x int,y int) int {
  if x < y {
    return x
  } else {
    return y
  }
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin>>n;
vector<ll> a(n);
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	int ans = 0;
	sort(a.begin(),a.end());
	for(int i=0;i<n;i++)
	{
		for(int j = i+1;j<n;j++)
		{
			ll l = 0,r =  n;
			while(l+1<r)
			{
				ll mid = (l+r)/2;
				;if(a[i]+a[j]>a[mid])
					l = mid;
				else
					r = mid;
			}
			ans+=r-j-1;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits>
#include <sstream>
#include <fstream>
#include <climits>
#include <map>
#include <numeric>
#include <functional>
#include <utility>

using namespace std;

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

P f(int x) {
  P ret;
  ret.first = x % 10;
  while (x >= 10) {
    x /= 10;
  }
  ret.second = x;
  return ret;
}

int main(void) {
  int n;
  cin >> n;
  map<P, int> mp;
  for (int i = 1; i <= n; i++) {
    P p = f(i);
    mp[p]++;
  }
  int ret = 0;
  for (int i = 1; i <= n; i++) {
    P p = f(i);
    ret += mp[P(p.second, p.first)];
  }
  std::cout << ret << std::endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        List<Integer> list = getDivisor(getGcd(a, b));
        Collections.sort(list);
        System.out.println(list.get(list.size() - k));
    }

    // 公約数
    private static List<Integer> getDivisor(int n) {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                list.add(i);
                if (n / i != i) {
                    list.add(n / i);
                }
            }
        }
        return list;
    }

    // 最大公約数
    private static int getGcd(int x, int y) {
        return y == 0 ? x : getGcd(y, x % y);
    }
}

Measured memory_bytes: 22996
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

using namespace std;

class BPowerSocket {
public:
void solve(std::istream& cin, std::ostream& cout) {
    int a, b;
    cin >> a >> b;
    cout << (a + b - 3) / (a - 1) << endl;
}
};

int main() {
	BPowerSocket solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
 
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
 
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> P;
 
constexpr int INF = 1e9;
constexpr int MOD = 1e9+7;

ll x10[1000010] = {1};

int main() {
    ll k;
    cin >> k;
    for(int i = 1; i < 1000010; i++){
        (x10[i] = x10[i-1]*10)%=k;
    }
    ll sum = 0;
    for(int i = 0; i < 1000010; i++){
        (sum += 7*x10[i]) %= k;
        if(sum%k == 0){
            cout << i+1 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
	return 0;
}

Measured memory_bytes: 11444
#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[] = {1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};

signed main() {
  int K;
  cin >> K;
  cout << a[K-1] << endl;
}


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

int main() {
    string s;
    cin >> s;

    int begin = 0;
    int end = 0;
    for (int i=0; i<s.size(); i++) {
        if (s[i] == 'A') {
            begin = i;
            break;
        }
    }
    for (int i=s.size()-1; i>=0; i--) {
        if (s[i] == 'Z') {
            end = i;
            break;
        }
    }

    cout << end - begin + 1 << endl;

    return 0;
}



Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; ++i)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100000000;

int main()
{
   int a,b,c,d;
   cin >> a >> b >> c >> d;
   cout<<max(a*b,c*d)<<endl;
   return 0;
}

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

int main() {
  int n,k;
  scanf("%d",&n);
  int cnt=0,ans=1000001;
  
  for(int i=0;i<=n;i++){
    int t=i;
    int cnt6=0,cnt9=0;
    cnt=0;
    while(t){
      for(int j=1;pow(6,j)<=t;j++)
        cnt6++;
      t-=pow(6,cnt6);
      cnt++;
      
      cnt6=0;
      
    }
    t=n-i;
    while(t){
      for(int j=1;pow(9,j)<=t;j++)
        cnt9++;
      t-=pow(9,cnt9);
      cnt++;
      
      cnt9=0;
    }
    
    ans=min(ans,cnt);
  }
  
  printf("%d",ans);
  
  
  
}

Measured memory_bytes: 512
#include <stdio.h>

#define INF (10000000)

int min(int a, int b)
{
    return (a > b ? b : a);
}

int max(int a, int b)
{
    return (b > a ? b : a);
}

int main(void)
{
    static char map[500][501];
    static int cnt[500][500];
    int n;
    int x, y;
    int i, j, k;
    int ans, height;
    
    while (1){
        scanf("%d%d", &y, &x);
        
        if (x + y == 0){
            break;
        }
        
        for (i = 0; i < y; i++){
            scanf("%s", map[i]);
        }
        
        for (i = 0; i < y; i++){
            for (j = 0; j < x; j++){
                if (i == 0){
                    cnt[i][j] = map[i][j] == '*' ? 0 : 1;
                }
                else {
                    if (map[i][j] == '.'){
                        cnt[i][j] = cnt[i - 1][j] + 1;
                    }
                    else {
                        cnt[i][j] = 0;
                    }
                }
            }
        }
        
        ans = 0;
        for (i = 0; i < y; i++){
            for (j = 0; j < x; j++){
                height = INF;
                for (k = j; k < x; k++){
                    height = min(cnt[i][k], height);
                    ans = max(ans, (k - j + 1) * height);
                }
            }
        }
        printf("%d\n", ans);
    }
    
    return (0);
}

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

using namespace std;


#define uniq(x)  x.erase(unique(x.begin(),x.end()), x.end()) //Unique value find from vector
#define upper(arr,n,fixed) upper_bound(arr,arr+n,fixed)-arr  //Upper value search  >
#define lower(arr,n,fixed) lower_bound(arr,arr+n,fixed)-arr  //Lower value search  >=
#define FOR(i,a,n) for(int i=a; i<(int)n; i++)
#define FORI(i,a,n) for(int i=a; i>=(int)n; i--)
#define pii pair<int,int>
#define vpii vector<pii>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define sz(a) int((a).size())
#define fastIO() ios_base::sync_with_stdio(0); cin.tie(0)
#define endl "\n"
#define all(a) a.begin(), a.end()
#define MEMSET(p,i) memset(p,i,sizeof(p))

/* a=target variable, b=bit number to act upon 0-n */
#define BIT_SET(a,b) ((a) |= (1<<(b)))
#define BIT_CLEAR(a,b) ((a) &= ~(1<<(b)))
#define BIT_FLIP(a,b) ((a) ^= (1<<(b)))
#define BIT_CHECK(a,b) ((a) & (1<<(b)))
#define BIT_COUNT(a) (__builtin_popcount(a))

 
/* x=target variable, y=mask */
#define BITMASK_SET(x,y) ((x) |= (y))
#define BITMASK_CLEAR(x,y) ((x) &= (~(y)))
#define BITMASK_FLIP(x,y) ((x) ^= (y))
#define BITMASK_CHECK(x,y) (((x) & (y)) == (y))

typedef long long LL;
typedef unsigned long long ULL;


LL dp[21][2100000] = {0}; // > 2^21
vector<int> M[21];

int main(void)
{
	fastIO();
	
	int n;
	cin >> n;

	
	FOR(i,0,n){
		FOR(j,0,n){
			int x;
			cin >> x;
			int b = 0;
			if(x == 1){
				BIT_SET(b,j);
				M[i].pb(b);
			}
		}
	}

	for(auto x : M[0]){
		dp[0][x] = 1;
	}

	int m = 2100000;
	LL MOD = 1e9 + 7;
	FOR(i,0,n-1){
		
		FOR(j,0,m){
			if(dp[i][j] == 0) continue;
			for(auto k : M[i+1]){
				int nxt = j^k; 
				dp[i+1][nxt] += dp[i][j];
				if(dp[i+1][nxt] >= MOD)
					dp[i+1][nxt] -= MOD;
			}
		}
	}
	// FOR(i,0,n){
	// 	cout << i << ":\n";
	// 	FOR(j,0,m){
	// 		if(dp[i][j] != 0)
	// 			cout << j << " " << dp[i][j] << endl;
	// 	}
	// 	cout << endl;
	// }
	int last = 0;
	FOR(i,0,n)
		last += pow(2,i);
	// cout << last << endl;
	cout << dp[n-1][last] << endl;




	
}




Measured memory_bytes: 332544
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
        return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main() {
        int n;
        cin >> n;

        vector<string> c(n);
        set<string> s;

        rep(i, n) {
                cin >> c[i];
                s.insert(c[i]);
        }

        cout << s.size() << endl;

        return 0;
}


Measured memory_bytes: 25096
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

#define int long long

using namespace std;


class Solver {
};

signed main() {
	while (true) {
		int n;
		cin >> n;
		if (n == 0)break;

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

		int change = 50000;
		int cnt = 100;
		vector <tuple<int, int>> dp(change + 500);
		for (int i = 0; i < change + 500; i++)dp[i] = make_tuple(0, 1e9);
		get<1>(dp[0]) = 0;
		for (int i = 0; i < n; i++) {
			vector <tuple<int, int>> next(change + 500);
			for (int j = 0; j < change + 500; j++)next[j] = make_tuple(0, 1e9);
			for (int j = 0; j < change; j++) {
				int cnt_tmp = get<0>(dp[j]);
				int price_tmp = get<1>(dp[j]);
				if (price_tmp == 1e9)continue;

				if(get<0>(next[j])<cnt_tmp
					|| (get<0>(next[j]) == cnt_tmp 
						&& get<1>(next[j]) > price_tmp)) {
					next[j] = make_tuple(cnt_tmp, price_tmp);
				}

				int next_price = price_tmp + prices[i];

				if (prices[i] % 1000 == 0) {
					if (j >= 500) {
						int next_change = j - 500;
						if (get<0>(next[next_change]) < cnt_tmp + 1
							|| (get<0>(next[next_change]) == cnt_tmp + 1
								&& get<1>(next[next_change]) > next_price)) {
							next[next_change] = make_tuple(cnt_tmp + 1, next_price);
						}
					}
					continue;
				}

				if (prices[i] % 1000 <= 500) {
					int next_change = j + 500 - (prices[i] % 1000);
					if (get<0>(next[next_change]) < cnt_tmp + 1
						|| (get<0>(next[next_change]) == cnt_tmp + 1
							&& get<1>(next[next_change]) > next_price)) {
						next[next_change] = make_tuple(cnt_tmp + 1, next_price);
					}
				}
				else {
					int next_change = j + 1000 - (prices[i] % 1000);
					if (get<0>(next[next_change]) < cnt_tmp
						|| (get<0>(next[next_change]) == cnt_tmp
							&& get<1>(next[next_change]) > next_price)) {
						next[next_change] = make_tuple(cnt_tmp, next_price);
					}
					if (j >= prices[i] % 500) {
						next_change = j - (prices[i] % 500);
						if (get<0>(next[next_change]) < cnt_tmp + 1
							|| (get<0>(next[next_change]) == cnt_tmp + 1
								&& get<1>(next[next_change]) > next_price)) {
							next[next_change] = make_tuple(cnt_tmp + 1, next_price);
						}
					}
				}
			}
			dp = next;
		}

		int res_cnt = 0;
		int res_price = 1e9;
		for (int i = 0; i < change;i++) {
			int cnt_tmp = get<0>(dp[i]);
			int price_tmp = get<1>(dp[i]);
			if (res_cnt < cnt_tmp
				|| (res_cnt == cnt_tmp && price_tmp < res_price)) {
				res_cnt = cnt_tmp;
				res_price = price_tmp;
			}
		}

		cout << res_cnt << " " << res_price << endl;
	}
}


Measured memory_bytes: 4408
import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int No=1;
		while(true) {
			int W=in.nextInt();
			if(W==0)break;
			
			int N=in.nextInt();
			List<Integer> va=new ArrayList<>(),wa=new ArrayList<>();
			for(int i=0;i<N;i++) {
				String str=in.next();
				String[] datas=str.split(",");
				va.add(Integer.parseInt(datas[0]));
				wa.add(Integer.parseInt(datas[1]));
			}
			
			int dp[][]=new int[N+1][W+1];
			
			for(int i=1;i<=N;i++) {
				int v=va.get(i-1),w=wa.get(i-1);
				for(int j=0;j<=W;j++) {
					dp[i][j]=dp[i-1][j];
					if(j-w>=0) {
						dp[i][j]=Math.max(dp[i-1][j], dp[i-1][j-w]+v);
					}
				}
			}
			
			int max=0,p=W+1;
			for(int i=W;i>=0;i--) {
				if(dp[N][i]>=max) {
					p=i;
					if(dp[N][i]>max)max=dp[N][i];
				}
			}
			
			System.out.printf("Case %d:\n%d\n%d\n",No,max,p);
			No++;
		}
	}

}



Measured memory_bytes: 36576
import fractions
from functools import reduce

n = int(input())
a = list(map(int,input().split()))

l = [0]
r = [0]
for i in range(n):
    l.append(fractions.gcd(l[i], a[i]))
    r.append(fractions.gcd(r[i], a[-(i + 1)]))

m = [fractions.gcd(l[i], r[-(i + 2)]) for i in range(n)]
print(max(m))

Measured memory_bytes: 16152
n = int(input())
arr = []
for i in range(n):
    arr.append(list(map(int,input().split())))

dp = [list([0,0,0]) for i in range(n)]

dp[0][0] = arr[0][0]
dp[0][1] = arr[0][1]
dp[0][2] = arr[0][2]
for i in range(1,n):
    dp[i][0] = arr[i][0] + max(dp[i-1][1],dp[i-1][2])
    dp[i][1] = arr[i][1] + max(dp[i-1][0],dp[i-1][2])
    dp[i][2] = arr[i][2] + max(dp[i-1][1],dp[i-1][0])
print(max(dp[-1]))
                  

Measured memory_bytes: 100700
import java.util.Scanner;

public class Main {
    
  public static void main(String[] args) {
    int result = 0;
    String s, c;
    Scanner scanner = new Scanner(System.in);
    s = scanner.nextLine();
    c = "CODEFESTIVAL2016";
    for (int i = 0; i < 16; i++) {
        if (s.charAt(i) != c.charAt(i)) {
            result++;
        }
    }
    scanner.close();

    System.out.println(result);
  }
}

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

typedef long long int ll;

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

    ll dp[n][3];

    dp[0][0] = a[0];
    dp[0][1] = b[0];
    dp[0][2] = c[0];

    for(ll i=1; i<n; i++){
        dp[i][0] = a[i] + max(dp[i-1][1] , dp[i-1][2]);
        dp[i][1] = b[i] + max(dp[i-1][0] , dp[i-1][2]);
        dp[i][2] = c[i] + max(dp[i-1][0] , dp[i-1][1]);
    }
    
    ll ans = max(dp[n-1][0], dp[n-1][1]);
    ans = max(ans ,  dp[n-1][2]);

    cout << ans<<endl;

    return 0;
}

Measured memory_bytes: 4992
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int> > matrix_pow(vector<vector<int> > a, vector<vector<int> > b, int m) {
    int len = a[0].size();
    vector<vector<int> > c(len, vector<int>(len, 0));
    
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < len; j++) {
            for (int k = 0; k < len; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    } 
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < len; j++) {
            c[i][j] %= m;
        }
    }
    return c;
}

int main(void){
    int n,m,a,b,c,t;
    while (cin>>n>>m>>a>>b>>c>>t && n) {
        vector<vector<int> > s(n, vector<int>(n, 0));
        vector<vector<int> > d = s, e = s;
        for (int i = 0; i < n; i++) cin >> s[i][0];
        
        for (int i = 0; i < n; i++) {
            if (i != 0) d[i][i-1] = a;
            d[i][i] = b;
            if (i != n-1) d[i][i+1] = c;
        }
        
        for (int i = 0; i < n; i++) e[i][i] = 1;
        while (t) {
            if (t & 1) e = matrix_pow(e, d, m);
            d = matrix_pow(d, d, m);
            t >>= 1;
        }
        s = matrix_pow(e, s, m);
        for (int i = 0; i < n; i++) cout << s[i][0] << (i==n-1 ? "\n" : " ");
    }
}

Measured memory_bytes: 3140
n,m=map(int,input().split())
def BellmanFord(edges,num_v,s):
  #グラフの初期化
  inf=float("inf")
  dist=[inf]*num_v
  dist[s]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges: #edges[頂点][頂点][重み]
      if dist[edge[0]] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1 and edge[1]==num_v-1: return -1
  
  return dist[num_v-1]
edges=[]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  c*=-1
  edges.append((a,b,c))
if BellmanFord(edges,n,0)==-1:
  print('inf')
else:
  print(-BellmanFord(edges,n,0))

Measured memory_bytes: 3388
N=int(input())
A=list(map(int, input().split()))

mon=1000
kabu=0

for i in range(N-1):
    if A[i] < A[i+1]:
        mon += kabu*A[i]
        kabu = mon//A[i]
        mon = mon - (kabu*A[i])
    else:
        mon += kabu*A[i]
        kabu = 0
    #print(mon)
    #print(kabu)
    #print("----")

mon += kabu*A[N-1]

print(mon)




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

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i=0; i<n; i++) cin >> a.at(i);
    reverse(a.begin(), a.end());
    for(int i=0; i<n; i++){
        cout << a.at(i);
        if (i != a.size() - 1) cout << " ";
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 3064
function main(input) {
  input = input.trim();
  const a = input.split(" ");
  const ind = "abcdefghijklmnopqrstuvwxyz";
  const n = ind.indexOf(a[0]);
	console.log(ind.charAt(n+1));
  
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

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

using namespace std;

int main()
{
  string S, T;
  cin >> S >> T;
  bool a = false;
  for(int i = 0; i < S.size(); i++){
    if(S == T){
      a = true;
      break;
    }
    T = T.back() + T.substr(0, T.size() - 1);
  }
  if(a){
    cout << "Yes";
  }else{
    cout << "No";
  }
  return 0;
}

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

using namespace std;

#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 ALL(v) v.begin(),v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define Decimal fixed << setpresicion(20)
#define SHOWP(x) cerr<<"("<<(x).FI<<", "<<(x).SE<<")"<<endl;
#define SHOWX(x) cerr<<#x<<": "<<x<<endl;
#define SHOWVEC(v, e) REP(i, e) cerr << (v[i]) << ' '; cerr << endl;
#define SHOW2D(a, h, w) REP(i, h){REP(j, w)cerr<<setw(3)<<(a[i][j])<<' ';cerr<<endl;}

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

vector<P> vv[110000];
ll v[110000];
bool used[110000];

bool dfs(int from, int x)
{
  if (used[from]) return v[from] == x;
  v[from] = x;
  used[from] = true;
  for (auto to : vv[from]) {
    if (!dfs(to.FI, x + to.SE)) return false;
  }
  return true;
}

int main()
{
  ll N, M;
  cin >> N >> M;
  REP(i, M) {
    ll L, R, D;
    cin >> L >> R >> D;
    L--; R--;
    vv[L].PB(MP(R, D));
    vv[R].PB(MP(L, -D));
  }

  bool flag = true;
  REP(i, N) {
    if (!used[i] && !dfs(i, 0)) flag = false;
  }
  if (flag) cout << "Yes" << endl;
  else cout << "No" << endl;

  return 0;
}

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



const double PI = asinl(1) * 2;
struct Point{
  double x,y;
  Point(){}
  Point(double x,double y) :x(x),y(y){}
  Point operator + (Point p) {return Point(x+p.x,y+p.y);}
  Point operator - (Point p) {return Point(x-p.x,y-p.y);}
  Point operator * (double k) {return Point(x*k,y*k);}
  Point operator / (double k) {return Point(x/k,y/k);}
  double norm(){return x*x+y*y;}
  double abs(){return sqrt(norm());}

  bool operator < (const Point &p) const{
    return x!=p.x ? x < p.x : y < p.y;
  }

  bool operator == (const Point &p) const{
    return fabs(x-p.x) < (1e-10) && fabs(y-p.y) < (1e-10);
  }
};
typedef Point Vector;
typedef vector<Point> Polygon;

struct Segment{
  Point p1,p2;
  Segment(){}
  Segment(Point p1, Point p2):p1(p1),p2(p2){}
};
typedef Segment Line;

struct Circle{
  Point c;
  double r;
  Circle(Point c,double r):c(c),r(r){}
};

double norm(Vector a){
  return a.x*a.x+a.y*a.y;
}
double abs(Vector a){
  return sqrt(norm(a));
}
double dot(Vector a,Vector b){
  return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
  return a.x*b.y-a.y*b.x;
}

bool isOrthogonal(Vector a,Vector b){
  return (fabs((dot(a,b))-(0.0)) < (1e-10));
}

bool isOrthogonal(Point a1,Point a2,Point b1,Point b2){
  return isOrthogonal(a1-a2,b1-b2);
}

bool isOrthogonal(Segment s1,Segment s2){
  return (fabs((dot(s1.p2-s1.p1,s2.p2-s2.p1))-(0.0)) < (1e-10));
}

bool isParallel(Vector a,Vector b){
  return (fabs((cross(a,b))-(0.0)) < (1e-10));
}

bool isParallel(Point a1,Point a2,Point b1,Point b2){
  return isParallel(a1-a2,b1-b2);
}

bool isParallel(Segment s1,Segment s2){
  return (fabs((cross(s1.p2-s1.p1,s2.p2-s2.p1))-(0.0)) < (1e-10));
}

int main(){
    int n;cin >> n;
    Point p0,p1,p2,p3;
    for(int i=0;i<n;i++){
        cin >> p0.x >> p0.y;
        cin >> p1.x >> p1.y;
        cin >> p2.x >> p2.y;
        cin >> p3.x >> p3.y;
        Segment s1,s2;
        s1 = Segment(p0,p1);
        s2 = Segment(p2,p3);
        if(isParallel(s1,s2)){
            cout << 2 << endl;
        }else if(isOrthogonal(s1,s2)){
            cout << 1 << endl;
        }else {
            cout << 0 << endl;
        }
    }

}


Measured memory_bytes: 3212
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,a[200005],sum;
signed main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	sort(a+1,a+n+1);
	for(int i=n;i>=max(n-m+1,1ll);i--)
		sum-=a[i];
	cout<<sum;
	return 0;
}

Measured memory_bytes: 1792
import bisect
from collections import deque

N = int(input())

# c = [int(input())]
c = deque()
c.appendleft(int(input()))


for ai in range(1, N):
    a = int(input())
    if a <= c[0]:
        c.appendleft(a)
    else:
        i = bisect.bisect_left(c, a) - 1
        c[i] = a

print(len(c))


Measured memory_bytes: 47616
while gets
  a = $_.split.map(&:to_i)
  b = gets.split.map(&:to_i)
  ht,bw = 0,0
  4.times do |i|
    if a[i] == b[i] then ht += 1
    elsif a.include? b[i] then bw += 1 end
  end
  puts "#{ht} #{bw}"
end

Measured memory_bytes: 8580
import * as fs from 'fs';

// parameter "input" gets all data
function Main(input: string) {
    const lines = input.split('\n');
    const [n, m] = lines[0].split(' ').map(int => parseInt(int));
    const a = lines[1].split(' ').map(int => parseInt(int));

    let left = n;
    for (let i = 0; i < m; i++) {
        left -= a[i];
        if (left < 0) {
            left = -1;
            break;
        }
    }

    //output

    let result = left;
    console.log(result);
    return result;
}

// Don't edit this line!
Main(fs.readFileSync('/dev/stdin', 'utf8'));


Measured memory_bytes: 30760
#include "bits/stdc++.h"
using namespace std;
#define fst first
#define scd second
int main()
{
	int a = 0;
	string s;
	cin >> s;
	for(int i = 0; i < s.length(); ++i)
	{
		a += s[i] == 'o';
		if(14 - i + a < 8)
		{
			printf("NO\n");
			return 0;
		}
	}
	printf("YES\n");
}

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

// Shrotening
#define fst first
#define snd second
#define pb push_back

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

typedef std::pair<int,int> PII;

int X;
int main() {
    std::cin >> X;


    if(X == 1) {
        std::cout << 1 << std::endl;
        return 0;
    }


    int i = 2, j = 2;
    int ans = 0;
    while(i <= X) {
        j = 2;
        while(std::pow(i, j) <= X) {
            ans = std::max(ans, (int)std::pow(i, j));
            ++j;
        }
        ++i;
    }
    std::cout << ans << std::endl;
}

Measured memory_bytes: 640
N=int(input())
ans=0
for i in range((3**10-1)//2):
 Sui=[0]*9
 for j in range(9):
  if i==0:
   Sui[j]=0
  elif i%3==0:
   Sui[j]=3
   i-=3
  elif i%3==1:
   Sui[j]=5
   i-=1
  else:
   Sui[j]=7
   i-=2
  i=i//3
 if 3 in Sui and 5 in Sui and 7 in Sui:
  b=0
  for j in range(9):
   b+=Sui[j]*(10**j)
  if b<=N:
   ans+=1
print(ans)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
int main(){
 int k,a,b;
  cin >> k >> a >> b;
  for(int i=a;i<=b;i++){
    if(i%k == 0){cout << "OK" << endl;
                 return 0;
                }
  }
  cout << "NG" << endl;
}

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

int main(){
  string s;
  cin >> s;
  int ans = s.size();
  for (int i = 0;i < 2;i++){
    int cnt = 0;
    for (int j = 0;j < s.size();j++){
      if (j % 2 == 0 && s[j] != (char)(i + '0')) cnt++;
      if (j % 2 == 1 && s[j] == (char)(i + '0')) cnt++;
    }
    ans = min(ans,cnt);
  }
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> ps(n), qs(n);
  for (auto &&p : ps) {
    cin >> p;
  }
  for (auto &&q : qs) {
    cin >> q;
  }
  
  int a = 1, b = 1;
  {
    vector<int> xs(n);
    for (int i = 0; i < n; i++) {
      xs[i] = i + 1;
    }
    
    do {
      bool f = true;
      for (int i = 0; i < n; i++) {
        f &= (ps[i] == xs[i]);
      }
      if (f) {
        break;
      }
      a++;
    } while (next_permutation(xs.begin(), xs.end()));
  }
  {
    vector<int> xs(n);
    for (int i = 0; i < n; i++) {
      xs[i] = i + 1;
    }
    
    do {
      bool f = true;
      for (int i = 0; i < n; i++) {
        f &= (qs[i] == xs[i]);
      }
      if (f) {
        break;
      }
      b++;
    } while (next_permutation(xs.begin(), xs.end()));
  }
  
  cout << abs(a - b) << endl;
  
  return 0;
}


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


def solve(N: int, A: int):
    print(N*N - A)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    solve(N, A)


if __name__ == '__main__':
    main()


Measured memory_bytes: 2940
#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;





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


Measured memory_bytes: 640
/*
La soluzione si basa sull'uso di un Order Statistic Tree ( https://en.wikipedia.org/wiki/Order_statistic_tree )
gcc ha gia' implementato questo tipo di albero al suo interno, quindi il grosso e' gia' stato fatto.
*/

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<unordered_map>

namespace std {
    class NODO {
        public:
            NODO(int _index, bool _teamA) : index(_index), teamA(_teamA) {};
            int index;
            bool teamA;
    };
    template<> struct less<std::NODO> {
        bool operator() (const NODO& lhs, const NODO& rhs) const
        {
            return lhs.index < rhs.index;
        }
    };

}

using namespace std;
using namespace __gnu_pbds;
typedef tree<
            NODO,
            null_type,
            less<NODO>,
            rb_tree_tag,
            tree_order_statistics_node_update > STATISTIC_TREE;


int N, best = 0;


int solve(STATISTIC_TREE& tree, int teamCount, int circularIndex = 0, bool used = false) {
    if(teamCount <= 0) return 0;
    if(teamCount < best) return 0;

    if(tree.size() == teamCount) {
        if(teamCount > best) best = teamCount;
        return teamCount;
    }
    
    /* DIAGNOSTIC
    cout << tree.size() << " " << teamCount << " " << circularIndex << " " << used << endl;
    */

    int action1 = 0, action2;

    int newIndex = (circularIndex + N - 1) % tree.size();
    auto toDel = tree.find_by_order(newIndex);


    if(!used && (*toDel).teamA) {
        STATISTIC_TREE copy = tree;
        circularIndex = (circularIndex + N) % copy.size();
        auto toDel2 = copy.find_by_order(circularIndex);
        if(!(*toDel2).teamA) {
            copy.erase(toDel2);
            action1 = solve(copy, teamCount, circularIndex, true);
        }
    }

    if((*toDel).teamA) {
        tree.erase(toDel);
        action2 = solve(tree, teamCount - 1, newIndex, used);
    } else {
        tree.erase(toDel);
        action2 = solve(tree, teamCount, newIndex, used);
    }
    action1 = max(action1,action2);
    if(action1 > best) best = action1;
    return action1;
}

int main(int argc, char** argv)
{


    int team_A_count = 0;

    int dataset;
    cin >> dataset;
    STATISTIC_TREE original;
    for(int j = 0; j < dataset; j++) {
        string s;
        cin >> N;
        cin >> s;
        int i = 1;
        for(auto c : s) {
            if(c == 'B') {
                team_A_count++;
                original.insert(NODO(i,true));
            } else {
                original.insert(NODO(i,false));
            }
            ++i;
        }

        cout << solve(original, team_A_count) << endl;

        original = STATISTIC_TREE();
        team_A_count = 0;
        best = 0;
    }

    return 0;
}


Measured memory_bytes: 3224
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define endl "\n"
#define pb push_back
#define f(i,n) for(i=0;i<n;i++)
#define F(i,a,b) for(i=a;a<=b;i++)
#define arr(a,n) for( i=0;i<n;i++)cin>>a[i];
#define fi first
#define se second
#define mp make_pair
#define mod 1000000007
#define YES cout<<"YES"<<endl;
#define Yes cout<<"Yes"<<endl;
#define NO cout<<"NO"<<endl;
#define No cout<<"No"<<endl;
#define yes cout<<"yes"<<endl;
#define no cout<<"no"<<endl;
#define vi vector<ll>
#define ed end()
#define bg begin()
#define sz size()
#define ln length()
#define s() sort(a,a+n);
#define sr() sort(a,a+n,greater<ll>());
#define v()  sort(v.begin(),v.end());
#define vr() sort(v.begin(),v.end(),greater<ll>());
#define mod 1000000007
#define fast() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int main() {
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
        

fast();
//ll t;cin>>t;while(t--)
{
    ll n,c=0;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    s()
    //for(auto t:a)cerr<<t<<" ";cerr<<endl;
      for(int i=0;i<n;i++)
      {
        for(int j=i+1;j<n;j++)
        {
          for(int k=j+1;k<n;k++)
          {
            if((a[i]<a[j]+a[k])&&(a[j]<a[k]+a[i])&&(a[k]<a[i]+a[j])&&a[i]!=a[j]&&a[i]!=a[k]&&a[j]!=a[k])
            {
              //cerr<<a[i]<<" "<<a[j]<<" "<<a[k]<<endl;
              c++;
            }
          }
        }
      }
      cout<<c<<endl;

}
return 0;
}

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define loop(i,a,b) for(ull i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define iter(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define riter(i,c) for(auto i=(c).rbegin(); i!=(c).rend(); ++i)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

#define clr(a,i) memset((a), (i) ,sizeof(a))

int main(){
  string s,ans;
  std::cin >> s;
  rep(i,s.size()){
    if(s[i]=='0') ans.pb('0');
    else if(s[i]=='1') ans.pb('1');
    else if(ans.size()>0) ans.pop_back();
  }
  std::cout << ans << std::endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
main(){
  int n,c,k;cin >> n >> c >> k;
  vector<int> t(n);for(int i=0;i<n;i++) cin >> t[i];
  sort(t.begin(),t.end());
  int ans=0;
  for(int i = 0; i<n;){
    ans++;
    int j = i;
    while(i-j<c && t[i]-t[j]<=k && i<n){
      i++;
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3800
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                           \
  cerr << __LINE__ << ": " << #x << " = {";        \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) \
    cerr << ((hoge) ? "," : "") << x[hoge];        \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

template <int mod>
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) { return *this *= p.inverse(); }
  ModInt operator-() const { return ModInt() - *this; }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
    return ModInt(u);
  }
  ModInt pow(int64_t e) const {
    ModInt ret(1);
    for (ModInt b = *this; e; e >>= 1, b *= b)
      if (e & 1) ret *= b;
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int modulo() { return mod; }
  int get() const { return x; }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  using Mint = ModInt<int(1e9 + 7)>;
  long long N;
  cin >> N;
  cout << Mint(10).pow(N) - Mint(9).pow(N) * 2 + Mint(8).pow(N) << '\n';
  return 0;
}


Measured memory_bytes: 3648
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <bitset>
#include <queue>
#include <iomanip>
using namespace std;
using ll = long long int;
using i64 = int64_t;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
const ll INF = 1LL << 60;
int mod = (int)1e9 + 7;

int main() {
    i64 n;
    cin >> n;
    i64 l[86 + 1];
    l[0] = 2; l[1] = 1;
    for(int i = 2;i <= n;i++) l[i] = l[i-1] + l[i-2];
    cout << l[n] << endl;
}


Measured memory_bytes: 3640
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
  static BigInteger[] facts;

  public static void main(String[] args) throws IOException {
    facts = new BigInteger[27];
    facts[0] = BigInteger.ONE;
    for (int i = 1; i <= 26; i++) {
      facts[i] = facts[i-1].multiply(BigInteger.valueOf(i));
    }

    char[] s = new BufferedReader(new InputStreamReader(System.in)).readLine().toCharArray();
    int[] a = new int[26];
    for (char c : s) {
      ++a[c-'a'];
    }
    System.out.println(s.length % 2 == 0 ? evenCase(a) : oddCase(a));
  }

  static BigInteger evenCase(int[] a) {
    BigInteger y = BigInteger.ONE;
    int n = 0;
    for (int x : a) {
      if (x % 2 != 0) {
        return BigInteger.ZERO;
      }
      n += x/2;
      y = y.multiply(facts[x/2]);
    }
    return facts[n].divide(y);
  }

  static BigInteger oddCase(int[] a) {
    for (int i = 0; i < 26; i++) {
      if (a[i] % 2 != 1) {
        continue;
      }
      --a[i];
      return evenCase(a);
    }
    return BigInteger.ZERO;
  }
}

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

using ll = long long;
#define int long long
using P = pair<int, int>;

#define LOG(variable) cerr << #variable":\t" << (variable) << endl
#define LOGCON(i, container) for(int (i) = 0; (i) < (container).size(); ++(i)) cerr << (i) << ":\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for(int i = (n); i >= 0; --i) // from n to 0
#define REPRS(i, n, r) for(int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj) for(auto itr = (obj).begin(); itr != (obj).end() ; ++itr)
#define REPROBJ(itr, obj) for(auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
template<typename T = int>
T in() {T a; cin >> a; return a;}
/***** MAIN *****/
signed main() {
  int x, k, d; cin >> x >> k >> d;
  x = abs(x);
  if(k <= x/d) {
    cout << x - (k * d) << endl;
    return 0;
  }
  if(d > x) {
    if(k % 2) {
      cout << d - x << endl;
    } else {
      cout << x << endl;
    }
    return 0;
  }
  int neccesarry_k = x / d;
  if(neccesarry_k % 2 == k % 2) {
    cout << x % d;
  } else {
    cout << d - (x % d);
  }
  cout << "\n";
  return 0;
}
/***** MAIN *****/

Measured memory_bytes: 3624
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int W, H;
int dxy[4] = {1,-1,0,0};
vector<string> len, rin;
/*
bool solve(vector<vector<vector<vector<int> > > > &used, int ly, int lx, int ry, int rx){
  if(used[ly][lx][ry][rx]) return false;
  used[ly][lx][ry][rx] = true;

  // cout<<ly<<" "<<lx<<" "<<ry<<" "<<rx<<endl;
  for(int i = 0; i < 4; i++){
      int nly = ly + dxy[i], nlx = lx + dxy[3-i];
      int nry = ry + dxy[i], nrx = rx + dxy[3-i]*-1;
      if(nly < 0 || H <= nly || nlx < 0 || W <= nlx || len[nly][nlx] == '#'){
	nly -= dxy[i], nlx -= dxy[3-i];	
      }
      
      if(nry < 0 || H <= nry || nrx < 0 || W <= nrx || rin[nry][nrx] == '#'){
	nry -= dxy[i], nrx -= dxy[3-i]*-1;
      }
      
      if(len[nly][nlx] == '%' &&  rin[nry][nrx] == '%') return true;

      if(solve(used,nly,nlx,nry,nrx)) return true;
  }

  return false;
}
*/

bool solve(vector<vector<vector<vector<int> > > > &used, int ly, int lx, int ry, int rx){
  queue<pair<pair<int,int>,pair<int,int> > > Q;

  Q.push(make_pair(make_pair(ly,lx),make_pair(ry,rx)));
  
  while(!Q.empty()){
    pair<pair<int,int>,pair<int,int> > P = Q.front(); Q.pop();
    int ly = P.first.first, lx = P.first.second;
    int ry = P.second.first, rx = P.second.second;
    used[ly][lx][ry][rx] = true;
  
    for(int i = 0; i < 4; i++){
      int nly = ly + dxy[i], nlx = lx + dxy[3-i];
      int nry = ry + dxy[i], nrx = rx + dxy[3-i]*-1;
      if(nly < 0 || H <= nly || nlx < 0 || W <= nlx || len[nly][nlx] == '#'){
	nly -= dxy[i], nlx -= dxy[3-i];	
      }
      
      if(nry < 0 || H <= nry || nrx < 0 || W <= nrx || rin[nry][nrx] == '#'){
	nry -= dxy[i], nrx -= dxy[3-i]*-1;
      }
      
      if(len[nly][nlx] == '%' &&  rin[nry][nrx] == '%') return true;
      if(len[nly][nlx] == '%' ||  rin[nry][nrx] == '%') continue;
       
      if(used[nly][nlx][nry][nrx]) continue;
      
      used[nly][nlx][nry][nrx] = true;
      
      Q.push(make_pair(make_pair(nly,nlx),make_pair(nry,nrx)));
    }
    
  }

  return false;
}
signed main(){

  while(true){
    
    vector<vector<vector<vector<int> > > > used;
    int lsx,lsy, rsx, rsy;
    cin>>W>>H;

    if(!W && !H) break;

    len.clear();
    rin.clear();
    len.resize(H);
    rin.resize(H);
    used.resize(H,vector<vector<vector<int> > >(W,vector<vector<int> >(H,vector<int>(W,0))));
     
    for(int i = 0; i < H; i++){
      cin>>len[i];
      cin>>rin[i];
      for(int j = 0; j < W; j++){
	if(len[i][j] == 'L'){
	  lsx = j;
	  lsy = i;
	}
	if(rin[i][j] == 'R'){
	  rsx = j;
	  rsy = i;
	}
      }
    }
     
     cout<<(solve(used,lsy,lsx,rsy,rsx)?"Yes":"No")<<endl;
  }
  return 0;
}



Measured memory_bytes: 31964
//雪花飄飄北風嘯嘯
//天地一片蒼茫

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

double rnd(){
	const int INF=1e9;
	return (double)(rng()%INF)/INF;
}

int n;
ll c[26];
ll d[400][26];

ll calc(vector<int> &contest){
	ll last[26];
	
	memset(last,-1,sizeof(last));
	
	ll res=0;
	rep(x,0,n){
		res+=d[x][contest[x]];
		last[contest[x]]=x;
		
		rep(y,0,26) res-=c[y]*(x-last[y]);
	}
	
	return res;
}

//store curr best
ll best;
vector<int> ans;

ll test(double COOLING_RATE,double SCALE_FACTOR){
	vector<int> curr;
	rep(x,0,n){
		curr.push_back(0);
	}
	
	ll val=calc(curr);
	
	best=val;
	ans=curr;
	
	double tempreture=1;
	
	auto begin_time=chrono::steady_clock::now().time_since_epoch().count();
	while (chrono::steady_clock::now().time_since_epoch().count()-begin_time<1950000000){
		int idx=rng()%n;
		int type=curr[idx];
		
		curr[idx]=rng()%26;
	
		ll temp=calc(curr);
		if (temp>val || rnd()<exp((temp-val)*SCALE_FACTOR/tempreture)){
			val=temp;
			
			if (best<val){
				best=val;
				ans=curr;
			}
		}
		else{
			curr[idx]=type;
		}
		
		tempreture*=COOLING_RATE;
	} 
	
	//pls uncomment later
	rep(x,0,n) cout<<ans[x]+1<<endl;
	
	return best;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	//freopen("sample2.in","r",stdin);
	
	cin>>n;
	rep(x,0,26) cin>>c[x];
	
	rep(x,0,n){
		rep(y,0,26) cin>>d[x][y];
	}
	
	test(0.99998,0.000005);
}


Measured memory_bytes: 4268
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <tuple>
#include <iomanip>


using namespace std;

typedef long long ll;

const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
	return f.second > s.second;
}

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


int main(void){
	vector<pair<int, int>> d(5);

	for (int i = 0; i < 5; i++){
		int tmp;
		cin >> tmp;

		d[i].first = tmp;
		d[i].second = tmp % 10;

	}

	int last = 1e9;
	int lastindex = 0;
	for (int i = 0; i < 5; i++){
		if (d[i].second == 0)continue;
		if (last > d[i].second){
			last = d[i].second;
			lastindex = i;
		}
	}

	int ans = 0;
	for (int i = 0; i < 5; i++){
		if (i == lastindex)continue;
		if (d[i].second == 0){
			ans += d[i].first;
		}
		else {
			ans += d[i].first + (10 - d[i].second);
		}
	}

	ans += d[lastindex].first;
	cout << ans << endl;
}


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

const int MAX_N=8000;

bool dp[MAX_N+3][MAX_N+3][2];
int x[MAX_N+3];
int y[MAX_N+3];

int main(){
    string s;
    int dx, dy, dir=1, sx;
    cin >>s >> dx >> dy;
    
    for(int i =0; i < s.size(); i++){
        if(s[i]=='F')  dx--;
        else{
            sx = i ;
            break;
        }
    }
    
    int ix=0, iy=0;
    for(int i =sx; i < s.size(); i++){
        if(s[i]=='T' && dir==1){
            dir *=-1;
            iy++;
        }
        else if(s[i]=='T' && dir== -1){
            dir *=-1;
            ix++;
        }
        else if(s[i]=='F' && dir==-1){
            y[iy]++;
        }
        else if(s[i]=='F' && dir==1){
            x[ix]++;
        }
        //else cout << << " " << endl;
       
    }
    dp[0][0][0] = 1;
    dp[0][0][1] = 1;

   for(int i =1; i <= ix; i++){
        for(int j =0; j <= MAX_N; j++){
            if(j+x[i] <= MAX_N) dp[i][j][0] = dp[i-1][abs(j-x[i])][0] || dp[i-1][abs(j+x[i])][0];
            else dp[i][j][0] |= dp[i-1][abs(j-x[i])][0];
        }
    }
    for(int i =1; i <= iy; i++){
        for(int j =0; j <= MAX_N; j++){
            if(j+y[i] <= MAX_N) dp[i][j][1] = dp[i-1][abs(j-y[i])][1] || dp[i-1][abs(j+y[i])][1];
            else dp[i][j][1] |= dp[i-1][abs(j-y[i])][1];
        }
    }

   if(dp[ix][abs(dx)][0] && dp[iy][abs(dy)][1]) cout << "Yes" << endl;
   else cout << "No" << endl;

return 0;
}
    

Measured memory_bytes: 63616