akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>

using namespace std;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int H,W;
	cin >> H >> W;
	string S[H];
	int R[H][W];
	for(int i = 0;i < H;i++){
		cin >> S[i];
	}
	int count;
	for(int i = 0;i < H;i++){
		for(int j = 0;j < W;j++){
			count = 0;
			if(S[i][j] == '.'){
				if(i-1 >= 0 and j-1 >= 0){
					if(S[i-1][j-1] == '#') count++;
				}
				if(j-1 >= 0){
					if(S[i][j-1] == '#') count++;
				}
				if(i+1 < H and j-1 >= 0){
					if(S[i+1][j-1] == '#') count++;
				}
				if(i-1 >= 0){
					if(S[i-1][j] == '#') count++;
				}
				if(i+1 < H){
					if(S[i+1][j] == '#') count++;
				}
				if(i-1 >= 0 and j+1 < W){
					if(S[i-1][j+1] == '#') count++;
				}
				if(j+1 < W){
					if(S[i][j+1] == '#') count++;
				}
				if(i+1 < H and j+1 < W){
					if(S[i+1][j+1] == '#') count++;
				}
				R[i][j] = count;
			}else{
				R[i][j] = -1;
			}
		}
	}
	for(int i = 0;i < H;i++){
		for(int j = 0;j < W;j++){
			if(R[i][j] == -1) cout << '#';
			else cout << R[i][j];
		}
		cout << endl;
	}

}

Measured memory_bytes: 256
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sum_hunt = 0
old_hunt=0

for i in range(N+1):
    if i == N:
        hunt = A[i] - old_hunt
        if hunt <= 0:
            sum_hunt += A[i]
        else:
            sum_hunt += old_hunt
    else:
        hunt = A[i] - old_hunt
        if hunt <= 0:
            sum_hunt += A[i]
            old_hunt = B[i]
        else:
            hunt_ex = hunt - B[i]
            if hunt_ex <= 0:
                sum_hunt += A[i]
                old_hunt = -(hunt - B[i])
            else:
                sum_hunt += (B[i]+ old_hunt)
                old_hunt = 0

print(sum_hunt)

Measured memory_bytes: 18752
//In the name of ALLAH the most merciful the most beneficial

#include<cstdio>
#include<cstring>
#include<bits/stdc++.h>
#include<iostream>
#include<math.h>
#include<fstream>
#define sc(x) scanf("%d",&x)
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORd(i,a,b) for(int i=a;i>b;i--)
#define ff first
#define ss second
#define sz(x) x.size()
#define pb push_back

using namespace std;

typedef pair<int,int> pii;
typedef vector<pii> vii;
const int con = 1e5+7;
static const int INF = 0x3f3f3f3f;
static const int MOD = 1e9+7;


int main(){
/*
    clock_t start,end;
    start = clock();*/
    
    
    string s;cin >> s;
    string ans = "";
    FOR(i,0,sz(s)){
        
        if(s[i] == '0')
            ans += s[i];
        else if(s[i] == '1')
            ans += s[i];
        else{
            
            if(sz(ans) > 0){
                ans = ans.substr(0,sz(ans)-1);
            }
        }
    }
    
        cout << ans;
/*    end = clock();
    double time = double(end-start)/(double)CLOCKS_PER_SEC;
    cout <<fixed<< time/1000 << "sec";*/
}

Measured memory_bytes: 256
// IN THE NAME OF ALLAH
#include <math.h>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <stack>
#include<cstring>

#define F first
#define S second
#define mp make_pair

using namespace std;
typedef long long ll;

const int N = 5e3 + 10 , M = 2e5+10 , MOD = 1e9 + 7 , MAXN = 1e5 + 10;
const ll inf = 4e18 + 10;

int main()
{
      int a,b,c,x;
      cin>>a>>b>>c>>x;
      int ans = 0;
      for( int i = 0 ; i <= 40 ; i++ )
      {
            for( int j = 0 ; j <= 200 ; j++ )
            {
                  if( i*500 + j*100 <= x && ( x - ( i*500 + j*100 ) ) % 50 == 0 )
                  {
                        if( i <= a && j <= b && ( x - ( i*500 + j*100 ) ) / 50 <= c )
                        {
                              ans++;
                        }
                  }
            }
      }
      cout<<ans<<endl;
      return 0;
}


Measured memory_bytes: 256
/*      ░░█ ▄▀█ █   █▀ █░█ █▀█ █▀▀ █▀▀   █▀█ ▄▀█ █▀▄▀█
        █▄█ █▀█ █   ▄█ █▀█ █▀▄ ██▄ ██▄   █▀▄ █▀█ █░▀░█     */

// Author : Udit "luctivud" Gupta @ (https://www.linkedin.com/in/udit-gupta-1b7863135/)


#include <bits/stdc++.h>
#pragma GCC optimize "trapv"

using namespace std;

typedef long long int lld;
typedef unsigned long long int llu;


#define       TESTCASES()  cin >> (T3X0); T353 = T3X0; while(T3X0--)
#define       _input(V3C)  for(auto &V3C_I7 : (V3C)) cin >> (V3C_I7)
#define mems(A77AY, V4LU)  memset((A77AY), (V4LU), sizeof((A77AY)))
#define  CH3K(I7, E4, S7)  (((S7)<0) ? (I7)>(E4) : (I7)<(E4))
#define for4(I7,S4,E4,S7)  for(auto I7=(S4); CH3K(I7,E4,S7); (I7)+=(S7))




void solveEachTest(lld T35TC453N = 1) {

	lld n; cin >> n;
	lld ans = 1ll;
	lld wrong = 1ll;
	for4(i, 0ll, n, 1ll) {
		ans *= 3ll;
		lld temp;
		cin >> temp;
		if (!(temp & 1)) wrong *= 2ll;
	}

	ans -= wrong;
	cout << ans;
	return;
}


signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0);

	lld T3X0 = 0, T353 = 1;

	// TESTCASES()
	solveEachTest(T353 - T3X0);
	return 0;
}
// Random Thought :  null

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

map<ll, ll> prime_factorize(ll N) {
    map<ll, ll> res;
    for (ll i = 2; i * i <= N; ++i) {
        while (N % i == 0) {
            res[i]++;
            N /= i;
        }
    }
    if (N != 1) res[N] = 1;
    return res;
}


void Main() {
    ll N;
    cin >> N;
    auto primes = prime_factorize(N);
    ll ans = 0;
    for (P prime : primes) {
        ll cnt = 0;
        ll se = prime.second;
        while (se >= (cnt + 1)) {
            cnt++;
            se -= cnt;
        }
        ans += cnt;
    }
    cout << ans << '\n';
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}


Measured memory_bytes: 3668
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end() 
using namespace std;

typedef long long ll;

int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ll n;
   cin >> n;
   if(n&1) {
       cout << 0 << "\n";
   } else {
       ll res = 0ll;
       while(n >= 10) {
            ll p = n/5;
            p /= 2;
            res += p;
            n /= 5;

       }
       cout << res << "\n";
   }
   return 0;
} 


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back

int main() {
    
    // 1. 入力情報.
    char c[3][22];
    rep(i, 3) scanf("%s", c[i]);
    
    // 2. 大文字に変換.
    string ans = "";
    rep(i, 3) ans.pb(toupper(c[i][0]));
    
    // 3. 出力.
    printf("%s\n", ans.c_str());
    return 0;
    
}

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

int main(){

	string S ;
	cin >> S ;
	
	if(S == "Sunny"){
		cout << "Cloudy" << endl;
	}else if(S == "Cloudy"){
		cout << "Rainy" << endl;
	}else{
		cout << "Sunny" << endl;
	}
}

Measured memory_bytes: 256
import collections
N, M, P = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(M)]
cd = collections.defaultdict
Ga = cd(set)
Gb = cd(set)
G = []
ok = cd(int)
for a, b, c in ABC:
    Ga[a].add(b)
    Gb[b].add(a)
#dfs
for pair in [[1, Ga], [N, Gb]]:
    S = pair
    Gn = pair.pop()
    visited = [0] * (N + 1)
    while S:
        s = S.pop()
        if not visited[s]:
            visited[s] = 1
            ok[s] += 1
            for ns in Gn[s]:
                S += [ns]
for a, b, c in ABC:
    if ok[a] == ok[b] == 2:
        G += [(a, b, P - c)]


def bellman_ford(V, G, i):
    INF = float('inf')
    D = [INF] * V
    D[i] = 0
    for _ in range(V):
        update = False
        for s, t, d in G:
            if D[s] != INF and D[t] > D[s] + d:
                D[t] = D[s] + d
                update = True
        if not update:
            return D
        if _ == V:
            return False


a = bellman_ford(N + 1, G, 1)
try:
    print(max(-a[-1], 0))
except:
    print(-1)


Measured memory_bytes: 54232
import numpy as np

N, S = map(int, input().split())
A = list(map(int, input().split()))
MOD = 998244353

ans = 0
dp = np.zeros(S + 1, dtype=np.int64)

for a in A:
    dp[0] += 1
    dp[a:] += dp[: -a].copy()
    dp %= MOD
    ans += dp[S]

print(ans % MOD)


Measured memory_bytes: 12504
#include<bits/stdc++.h>
using namespace std;
int a[1001],i,j,k,n,m;
int main()
{
	cin>>n;
	for(i=1;i<=n*2;i++)scanf("%d",&a[i]);
	sort(a+1,a+n*2+1);
	for(i=1;i<=n;i++)k+=a[i*2-1];
	cout<<k<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
using ll=long long;
int main(){
    int n; cin>>n;
    vector<int>d(n);
    map<int,int>mp;
    rep(i,n){
        cin>>d[i];
        mp[d[i]]++;
    }
    int m; cin>>m;
    vector<int>t(m);
    bool ok=true;
    rep(i,m){
        cin>>t[i];
        mp[t[i]]--;
        if(mp[t[i]]<0){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
}


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

int main(){
  int n;
  cin >> n;
  float x[n];
  float y[n];
  for(int i = 0; i < n; i++){
    cin >> x[i] >> y[i];
  }
  float ans = 0;
  for(int i = 0; i < n-1; i++){
    for(int j = i+1; j < n; j++){
      ans += 2.0/n*(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
    }
  }
  printf("%.10f\n",ans);
}

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
static const int N=1000;
int lcs(string X,string Y)
{
    int c[N+1][N+1];
    int m=X.size();
    int n=Y.size();
    int maxl=0;
    X=" "+X;
    Y=" "+Y;
    for(int i=0;i<=m;i++)c[i][0]=0;
    for(int j=0;j<=n;j++)c[0][j]=0;

      for(int i=1;i<=m;i++)
      {
          for(int j=1;j<=n;j++)
          {
              if(X[i]==Y[j])
                 {
                     c[i][j]=c[i-1][j-1]+1;
                 }
                    else{
                        c[i][j]=max(c[i-1][j],c[i][j-1]);
                }
                    maxl=max(maxl,c[i][j]);
          }
      }
      return maxl;
}
int main()
{
    string s1,s2;
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
    return 0;
}

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

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP (i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back

using namespace std;

typedef long long ll;

// const int INF = 1 << 30;
// const ll INF = 1LL << 60;
// const int MOD = 1000000007;

int main() {
  int N, K;
  string S;
  cin >> N >> K;
  cin >> S;

  vector<pair<char, int>> vp;
  int cnt = 0;
  rep (i, N) {
    cnt++;
    if (i + 1 == N || S[i] != S[i + 1]) {
      vp.eb(mp(S[i], cnt));
      cnt = 0;
    }
  }

  int ans = 0;
  int sum = 0;
  int kCnt = 0;
  int pos = 0;
  rep (i, sz(vp)) {
    sum += vp[i].S;
    if (vp[i].F == '0') kCnt++;
    while (kCnt > K && pos <= i) {
      sum -= vp[pos].S;
      if (vp[pos].F == '0') kCnt--;
      pos++;
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;
}


Measured memory_bytes: 1528
#include <iostream>
#include <queue>
#include <string>
using namespace std;
  
class Task {
public:
  void input() {
    cin >> name >> time;
  }
  string name;
  int time;
};
  
int main() {
  int n, q;
  queue<Task> tasks;
  
  cin >> n >> q;
  for (int i=0; i<n; i++) {
    Task task = Task();
    task.input();
    tasks.push(task);
  }
  
  int time = 0;
  
  while (!tasks.empty()) {
    Task current = tasks.front(); tasks.pop();
    if (current.time > q) {
      time += q;
      current.time -= q;
      tasks.push(current);
    } else {
      time += current.time;
      cout << current.name << ' ' << time << endl;
    }
  }
}

Measured memory_bytes: 4404
#include<bits/stdc++.h>
#define cs const
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pi;
cs int N = 2e5 + 50;
cs int INF = 1e9 + 7;
int n, a[N], c[N], bck[N][2];
struct bit{
	int c[N]; pi st[N<<5]; int top;
	void init(){ memset(c,-0x3f,sizeof(c)); }
	void mdf(int x, int v){ ++x; for(;x;x-=x&-x) st[++top]=mp(x,c[x]),c[x]=max(c[x],v); }
	int qry(int x){ int as=-INF; ++x; for(;x<=n+1;x+=x&-x) as=max(as,c[x]); return as; }
	void back(int bot){ while(top>bot) c[st[top].fi]=st[top].se, --top; }
}odd, even;
bool chk(int x, int v){
	if(v<0) return false;
	if(v&1) return odd.qry(x)>=v;
	return even.qry(x)>=v;
}
int main(){
	#ifdef FSYolanda
	freopen("1.in","r",stdin);
	#endif
	scanf("%d",&n);
	for(int i=1,mx=0; i<=n; i++){
		scanf("%d",&a[i]);
		if(a[i]>mx) c[i]=2; else c[i]=1;
		mx=max(mx,a[i]); 
	} odd.init(); for(int i=n; i>=1; i--){
		bck[i][0]=odd.top, bck[i][1]=even.top;
		int A=odd.qry(a[i]), B=even.qry(a[i]);
		if(c[i]&1) even.mdf(a[i],A+c[i]), odd.mdf(a[i],B+c[i]);
		else odd.mdf(a[i],A+c[i]), even.mdf(a[i],B+c[i]);
	} for(int i=n; i>=1; i--) c[i]+=c[i+1]-1;
	int cx=0, cy=0, mx=0, my=0;
	static bool ans[N];
	for(int i=1; i<=n; i++){
		odd.back(bck[i][0]); 
		even.back(bck[i][1]);
		if(chk(my,cx-cy+c[i+1]+(a[i]>mx))) 
		cx+=a[i]>mx, mx=max(mx,a[i]), ans[i]=0;
		else if(chk(max(mx,a[i]),cy+c[i+1]-cx-(a[i]>mx)))
		cx+=a[i]>mx, mx=max(mx,a[i]), ans[i]=0;
		else ans[i]=1, cy+=a[i]>my, my=max(my,a[i]);
	} if(cx^cy) return puts("-1"),0;
	for(int i=1; i<=n; i++) cout<<ans[i];
	return 0;
}

Measured memory_bytes: 105344
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();

		System.out.println(C - A + B >= 0 ? C - A + B : 0);
	}
}


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

int main() {
	int N, A, B;
	cin >> N >> A >> B;
	int total = 0, digits_sum;
	string s;
	for (int i = 1; i <= N; i++) {
		s = to_string(i);
		digits_sum = 0;
		for (int j = 0; j < s.size(); j++) 
			digits_sum += int(s[j] - '0');
		if (A <= digits_sum && digits_sum <= B)
			total += i;
	}
	cout << total << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
signed main(){
    int n=0,x[10000]={};
    int i=0,j=0,temp=0,m=0,sum=0;
    scanf("%d %d",&n,&m);
    
    for(i=0;i<n;i++){
        scanf("%d",&x[i]);
    }
    for(i=0;i<n;i++){
    for(j=0;j<n-i-1;j++){
    if(x[j]>x[j+1]){
        temp=x[j];
        x[j]=x[j+1];
        x[j+1]=temp;
    }
}
}
for(i=0;i<m;i++){
sum+=x[i];

}


printf("%d\n",sum);
return(0);
}


Measured memory_bytes: 1736
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
using namespace std;

#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 998244353;
const ll MAX = 1000000;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

//gcd,lcm,べき,二進展開
//組み合わせ、mod計算
//最短路問題、bit、union find

///////////////////////////

ll bin(ll n, ll i) {
	if (i == 0) { return n % 2; }
	else { return bin(n / 2, i - 1); }
}

	
int main() {

	ll a, b;
	cin >> a>> b;

	cout << max(a + b, max(a - b, a*b));

	system("PAUSE");
}

Measured memory_bytes: 504
#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>;
template<typename T> auto compare = [](T x, T y) -> bool{return (x < y);};
const int MOD = 1000000007;

signed main(){
    string S;
    string T[7] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
    cin >> S;
    REP(i, 7){
        if(S == T[i]){
            PRINT(7 - i);
            return 0;
        }
    }
    return 0;
}

Measured memory_bytes: 3616
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << '\n';
typedef int64_t Int;
Int mod = 1e9+7;
Int INF = 1e9+18;

vector<Int> G[200005];
bool used[200005];

void dfs(Int x) {
    if (used[x]) return;
    used[x] = true;
    for (Int i = 0; i < (Int)G[x].size(); i++) {
        Int to = G[x][i];
        if (used[to]) continue;
        dfs(to);
    }
}

int main() {
    ios::sync_with_stdio(false);
    Int n, m;
    cin >> n >> m;
    for (Int i = 0; i < n; i++) {
        Int k;
        cin >> k;
        for (Int j = 0; j < k; j++) {
            Int l;
            cin >> l;
            l--;
            G[i].emplace_back(n + l);
            G[n + l].emplace_back(i);
        }
    }
    dfs(0);
    bool res = true;
    for (Int i = 0; i < n; i++) {
        if (!used[i]) res = false;
    }
    dump(res ? "YES" : "NO");
    return 0;
}

Measured memory_bytes: 9216
/*
 * main.cpp
 *
 *  Created on: 2014/03/16
 *      Author: uchiya
 */
#include <iostream>
//#include <string>
//#include <iomanip>

using namespace std;
//#define

int main() {
	int n, x;

	while(1){
		cin >> n >> x;
		if( n == 0 && x == 0 ) break;

		int count = 0;
		for( int i = 1 ; i <= n - 2 ; ++i){
			for( int j = i + 1 ; j <= n - 1 ; ++j){
				for( int k = j + 1 ; k <= n ; ++k){
					if( i + j + k == x){
						count++;
					}
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
result = []
while 1:
    n = int(input())
    if n == 0:
        break
    ans = 0
    for i in range(1,1001):
        ans += n // pow(10,1000-i)
        n = n % pow(10,1000-i)
    result.append(ans)

for i in range(len(result)):
    print(result[i])



Measured memory_bytes: 5616
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <random>
#include <chrono>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001

int main(){
    int h, w; cin >> h >> w;
    int a, b; cin >> a >> b;
    cout << h*w - h/a*a*(w/b)*b << endl;
    return 0;
}



Measured memory_bytes: 3076
//Author: AnandRaj uux
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll; 

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test() int t;cin>>t;while(t--)
#define forn(i,f,n) for(int i=f;i<f+n;i++)
#define rforn(i,l,n) for(int i=l;i>l-n;i--)
#define all(v) v.begin(),v.end()
#define prin(V) for(auto v:V) cout<<v<<" ";cout<<endl
#define take(V,f,n) for(int i=f;i<f+n;i++) cin>>V[i]
#define what(x) cerr<<#x<<" = "<<x<<endl
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "

const int MOD = 1e9+7,MAX = 1e5+5;
/////////////////FastExp///////////////////
ll powN(ll a,ll p)
{
	if(p==0) return 1;
	ll z=powN(a,p/2);
	z=(z*z)%MOD;
	if(p%2) z=(z*a)%MOD;
	return z;
}
/////////////////FastExp///////////////////
//////////////////Sieve////////////////////
vector<bool> is_prime(MAX+1, true);
void Sieve()
{
	is_prime[0] = is_prime[1] = false;
	int i,j;
	for (i = 2; i*i <= MAX; i++) 
	{
    	if (is_prime[i]) 
    	{
        	for (j = i * i; j <= MAX; j += i)
            	is_prime[j] = false;
    	}
	}
}
//////////////////Sieve////////////////////

int main()
{
	ll n,k;
	cin>>n>>k;
	n%=k;
	cout<<min(abs(n-k),n)<<endl;

}		

Measured memory_bytes: 256
from math import gcd
from collections import Counter

mod = 10 ** 9 + 7

N, *AB = map(int, open(0).read().split())

def std(a, b):
    if a == 0:
        return (0, int(b != 0))

    g = gcd(a, b)
    a, b = a // g, b // g
    return (a, b) if a > 0 else (-a, -b)

C = Counter(std(a, b) for a, b in zip(*[iter(AB)] * 2))

ans = 1
cnt = 0
for (a, b), v in C.items():
    if b > 0:
        if (b, -a) in C:
            ans *= -1 + pow(2, v, mod) + pow(2, C[(b, -a)], mod)
            ans %= mod
        else:
            cnt += v
    elif (-b, a) not in C:
        cnt += v

ans *= pow(2, cnt, mod)
ans += C[(0, 0)] - 1

print(ans % mod)

Measured memory_bytes: 73036
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;

int l_to_r(const char exp[])
{
  int sum;
  sum=(exp[0]-'0');
  for(int i=1;i+1<strlen(exp);i+=2){
    if(exp[i]=='*')
      sum*=(exp[i+1]-'0');
    else sum+=(exp[i+1]-'0');
  }
   
  return sum;
}

int mul_fir(char exp[])
{
  int mul,sum=0;
  vector<int> plus;
  mul=(exp[0]-'0');
  for(int i=1;i<strlen(exp);i+=2){
    if(exp[i]=='*')
      mul*=(exp[i+1]-'0');
    else {
      plus.push_back(mul);
      mul=(exp[i+1]-'0');
    }
  }
  for(int i=0;i<plus.size();i++)
    sum+=plus[i];
  
  return sum+mul;
}

int main()
{
  long long int n,ans1,ans2;
  char exp[18];

  scanf("%s", exp);
  cin>>n;
  ans1=l_to_r(exp);
  ans2=mul_fir(exp);
  if(ans1==n&&ans2!=n) cout<<"L"<<endl;
  else if(ans1!=n&&ans2==n) cout<<"M"<<endl;
  else if(ans1==n&&ans2==n) cout<<"U"<<endl;
  else cout<<"I"<<endl;
}


Measured memory_bytes: 1240
q,b,c,d,r;main(y){for(;scanf("%d%d%d%d%d",&q,&b,&c,&d,&r),q;puts("NA"))for(;r;r--)y=b-c*r,y<0|r+y/d<q||main(printf("%d %d\n",r,y/d));exit(0);}

Measured memory_bytes: 0
import math

def resolve():
  N = int(input())

  result = [0] * (N + 1)
  max_range = int(math.sqrt(N)) + 1
  for x in range(1, max_range):
    for y in range(1, max_range):
      for z in range(1, max_range):
        index = x**2 + y**2 + z**2 + x*y + y*z + z*x
        if index <= N:
          # if index == 8:
          #   print("x: {0}, y: {1},z: {2},".format(x, y, z))
          result[index] += 1

  for i in result[1:]:
    print(i)

if __name__ == "__main__":
  resolve()

Measured memory_bytes: 68940
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main() {
  int N, A, B;
  cin >> N >> A >> B;
  vector<int> a(N), b(N), c(N);
  rep(i, N) cin >> a[i] >> b[i] >> c[i];
  vector<vector<vector<int>>> dp(N+1, vector<vector<int>>(401, vector<int>(401, 1e9)));
  dp[0][0][0] = 0;
  for(int i=1; i<=N; i++) {
    rep(j, 401) {
      rep(k, 401) {
        chmin(dp[i][j][k], dp[i-1][j][k]);
        if(j-a[i-1]>=0 && k-b[i-1]>=0)
          chmin(dp[i][j][k], dp[i-1][j-a[i-1]][k-b[i-1]]+c[i-1]);
      }
    }
  }
  int ans = 1e9;
  for(int i=1; i*A<=400&&i*B<=400; i++) {
      if(dp[N][i*A][i*B] != 1e9)
        chmin(ans, dp[N][i*A][i*B]);
  }
  if(ans == 1e9)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Measured memory_bytes: 27264
#include<bits/stdc++.h>
using namespace std;
#define  ll  long long
#define  pi  pair<ll,ll>
#define  F   first
#define  S   second
const int N=2e3+3;
pi a[N];
ll n,d[N][N];
ll go(ll l,ll r,ll i)
{
    if(l>r) return 0ll;
    else if(d[l][r]!=-1) return d[l][r];
    ll lv=abs(a[i].S-l)*a[i].F+go(l+1ll,r,i+1);
    ll rv=abs(a[i].S-r)*a[i].F+go(l,r-1ll,i+1);
    return d[l][r]=max(lv,rv);
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    for(ll m,i=0ll;i<n;i++){
        cin>>m;
        a[i]={m,i};
    }
    sort(a,a+n,greater<pi>());
    memset(d,-1,sizeof(d));
    cout<<go(0ll,n-1ll,0ll)<<"\n";
    return 0;
}

Measured memory_bytes: 35084
import std.algorithm;
import std.container;
import std.conv;
import std.range;
import std.stdio;
import std.string;

long [] a;
long [] b;
long [] c;

struct Summand
{
	int u;
	int v;
	int w;

	@property auto value () const
	{
		return a[u] + b[v] + c[w];
	}

	@property auto id () const
	{
		return (u << 20) | (v << 10) | w;
	}

	int opCmp () (const auto ref Summand that) const
	{
		if (this.value != that.value)
		{
			return (this.value < that.value) -
			    (this.value > that.value);
		}
		return (this.id > that.id) - (this.id < that.id);
	}
}

void main ()
{
	int x, y, z, k;
	while (readf (" %s %s %s %s", &x, &y, &z, &k) > 0)
	{
		readln;
		a = readln.splitter.map !(to !(long)).array;
		b = readln.splitter.map !(to !(long)).array;
		c = readln.splitter.map !(to !(long)).array;
		sort !(q{a > b}) (a);
		sort !(q{a > b}) (b);
		sort !(q{a > b}) (c);

		auto t = redBlackTree !(Summand) ();
		t.insert (Summand (0, 0, 0));
		foreach (p; 0..k)
		{
			auto cur = t.front;
			writeln (cur.value);
			t.removeFront ();
			if (cur.u + 1 < x)
			{
				t.insert (Summand (cur.u + 1, cur.v, cur.w));
			}
			if (cur.v + 1 < y)
			{
				t.insert (Summand (cur.u, cur.v + 1, cur.w));
			}
			if (cur.w + 1 < z)
			{
				t.insert (Summand (cur.u, cur.v, cur.w + 1));
			}
		}
	}
}


Measured memory_bytes: 892
n = int(input())
a = list(map(int,input().split()))
q = int(input())
count = 0
s = sum(a)

L = [0] * (10**5+1)
for aa in a:
    L[aa] += 1
for _ in range(q):
    b, c = list(map(int, input().split()))
    L[c] += L[b]
    s += L[b] * (c-b)
    L[b] = 0
    print(s)

Measured memory_bytes: 20856
#include<cstdio>
#include<iostream>
#define RI register int
#define CI const int&
using namespace std;
const int N=505;
const long long INF=1e18;
int n,a[N][N]; long long s1[N][N],s2[N][N],f[N][N],ans=INF;
int main()
{
	RI i,j,k; for (scanf("%d",&n),i=1;i<=n;++i)
	for (j=1;j<=n;++j) if (i!=j) scanf("%d",&a[i][j]);
	for (i=1;i<=n;++i) for (j=i+1;j<=n+1;++j)
	for (s1[i][j]=s1[i][j-1],k=i;k<j;++k) s1[i][j]+=a[k][j];
	for (i=1;i<=n;++i) for (j=n;j>i;--j)
	for (s2[i][j]=s2[i][j+1],k=1;k<=i;++k) s2[i][j]+=a[j][k];
	for (i=0;i<=n+1;++i) for (j=i;j<=n+1;++j) f[i][j]=INF;
	for (f[0][0]=i=0;i<=n;++i) for (j=i;j<=n;++j) if (f[i][j]!=INF)
	for (k=j+1;k<=n+1;++k) f[j][k]=min(f[j][k],f[i][j]+s1[j+1][k]+s2[j][k+1]-s2[i][k+1]);
	for (i=0;i<=n;++i) ans=min(ans,f[i][n+1]); return printf("%lld",ans),0;
}

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

const int N = 1e5 + 5;

int arr[N][3];
int n;
long long dp[N][3];

void read() {
  scanf("%d", &n);
  for (int i = 1 ; i <= n ; i++) {
    scanf("%d %d %d", &arr[i][0], &arr[i][1], &arr[i][2]);
  }
}

long long work() {
  for (int i = n ; i >= 1 ; i--) {
    for (int j = 0 ; j < 3 ; j++) {
      dp[i][j] = 0;
      for (int k = 0 ; k < 3 ; k++) {
        if (j != k) dp[i][j] = max(dp[i][j], dp[i+1][k] + arr[i][j]);
      }
    }
  }

  return max(dp[1][0], max(dp[1][1], dp[1][2]));
}

int main() {
  read();
  printf("%lld\n", work());
  return 0;
}

Measured memory_bytes: 3712
i=int(input())
d=i%10
c=(i//10)%10
b=(i//100)%10
a=i//1000
if a+b+c+d==7:
    print(a,"+",b,"+",c,"+",d,"=7",sep="")
elif a+b+c-d==7:
    print(a,"+",b,"+",c,"-",d,"=7",sep="")
elif a+b-c+d==7:
    print(a,"+",b,"-",c,"+",d,"=7",sep="")
elif a+b-c-d==7:
    print(a,"+",b,"-",c,"-",d,"=7",sep="")
elif a-b+c+d==7:
    print(a,"-",b,"+",c,"+",d,"=7",sep="")
elif a-b+c-d==7:
    print(a,"-",b,"+",c,"-",d,"=7",sep="")
elif a-b-c+d==7:
    print(a,"-",b,"-",c,"+",d,"=7",sep="")
else:
    print(a,"-",b,"-",c,"-",d,"=7",sep="")



Measured memory_bytes: 3064
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef int32_t i32;
typedef int64_t i64;

const i32 mod = 1000000007;

i32 mod_pow (i32 r, i32 n) {
  i32 t = 1;
  i32 s = r;
  while (n > 0) {
    if (n & 1) t = (i64) t * s % mod;
    s = (i64) s * s % mod;
    n >>= 1;
  }
  return t;
}

i32 inv (i32 a) {
  return mod_pow (a, mod - 2);
}

i32 *fact = NULL;
i32 *iFact = NULL;
void init_fact (const i32 n) {
  fact = (i32 *) calloc (n + 1, sizeof (i32));
  fact[0] = 1;
  for (i32 i = 1; i <= n; ++i) {
    fact[i] = (i64) i * fact[i - 1] % mod;
  }
  iFact = (i32 *) calloc (n + 1, sizeof (i32));
  iFact[n] = inv (fact[n]);
  for (i32 i = n - 1; i >= 0; --i) {
    iFact[i] = (i64) (i + 1) * iFact[i + 1] % mod;
  }
}

i32 comb (i32 n, i32 k) {
  if (!(0 <= k && k <= n)) return 0;
  return (i64) fact[n] * iFact[k] % mod * iFact[n - k] % mod;
}

void run (void) {
  i32 n, a, b, c;
  scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32 "%" SCNi32, &n, &a, &b, &c);
  i32 p = (i64) a * inv (a + b) % mod;
  i32 q = (1 + mod - p) % mod;
  init_fact (2 * n);
  i64 ans = 0;
  for (i32 i = 0, s = mod_pow (p, n); i < n; ++i, s = (i64) s * q % mod) {
    i64 prob = (i64) comb (n + i - 1, i) * s % mod;
    ans = (ans + prob * (n + i)) % mod;
  }
  for (i32 i = 0, s = mod_pow (q, n); i < n; ++i, s = (i64) s * p % mod) {
    i64 prob = (i64) comb (n + i - 1, i) * s % mod;
    ans = (ans + prob * (n + i)) % mod;
  }
  ans = ans * 100 % mod * inv (100 - c) % mod;
  printf ("%" PRIi64 "\n", ans);
}

int main (void) {
  run();
  return 0;
}


Measured memory_bytes: 1664
/*
[制約]
2≤N≤100
1≤mi≤1000
m1+m2+...+mN≤X≤10^5
入力中のすべての値は整数である。
[入力]
N
X
m1
m2
:
mN
*/
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, X, count=0;
  cin >> N >> X;
  int m[N], min = 1000;
  for(int i=0; i<N; i++) {
    cin >> m[i];
    if(m[i] < min) min = m[i];
    // make one donuts
    count++;
    X -= m[i];
  }
  count += X / min;
  cout << count << endl;
}

Measured memory_bytes: 256
using System.Linq;
using System.Collections.Generic;
using System;

public class Hello
{
    public static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var n = int.Parse(line[0]);
        var m = int.Parse(line[1]);
        line = Console.ReadLine().Trim().Split(' ');
        var a = Array.ConvertAll(line, int.Parse);
        var d = new Dictionary<long, long>();
        d[0] = 1L;
        var pre = 0L;
        for (int i = 0; i < n; i++)
        {
            var t = pre + a[i];
            var k = t % m;
            if (d.ContainsKey(k)) d[k]++;
            else d[k] = 1L;
            pre = t;
        }
        var res = getAns(d);
        Console.WriteLine(res);
    }
    public static long getAns(Dictionary<long, long> d)
    {
        var ret = 0L;
        foreach (var x in d.Where(y => y.Value >= 2))
            ret += (x.Value * (x.Value - 1)) / 2L;
        return ret;
    }
}


Measured memory_bytes: 22880
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF=1001001001;

int main() {
  int N;
  cin>>N;
  string s,t;
  cin>>s>>t;
  int count=0;
  for(int i=0;i<N;i++){
    string h=s.substr(i);string k;
   for(int j=0;j<N;j++){
     k=t.substr(0,j+1);
     int a=h.size();
     if(h==k){count=max(count,a);}
   }
  }
  string f=s+t;
  cout<<f.size()-count<<endl;
  return 0;}

Measured memory_bytes: 256
from collections import Counter

def main():
    N = int(input())
    A = []

    for _ in range(N):
        A.append(int(input()))

    ans = 0
    counter = Counter(A)
    for _, cnt in counter.most_common():
        if cnt % 2 == 1:
            ans += 1

    print(ans)
    
main()

Measured memory_bytes: 21344
#include <iostream>
#define llint long long int
#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;

int N;
int k;

int main(void)
{
	cin >> N >> k;
	
	llint x = 0;
	rep(i, N-1){
		x += x/(k-1) + 1;
	}
	
	cout << x << endl;
	return 0;
}

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

int main() {
    long long int x, y;
    cin >> x  >> y;
    if (abs(x- y) <= 1) {
        cout << "Brown" << endl;
    } else {
        cout << "Alice" << endl;
    }
}


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

int H,W;

/*
bool check(vector<vector<char>> &field, int x, int y){
    
    
}
*/

int main(){
    
    cin >> H >> W;
    H=H+2;
    W=W+2;
    //cout << H;
    //データを受け取る
    vector<vector<char>> field(H,vector<char>(W));
    
    rep(i,H){
        rep(j,W){
            if(i==0 || j==0 || i==H-1 || j==W-1){
                field[i][j]='.';
            }else{
                cin >> field[i][j];
            }
        }
    }
    
    bool ans_flg=true;
    rep(i,H){
        rep(j,W){
            if(field[i][j]=='#'){
                if(field[i-1][j]=='.' && field[i+1][j]=='.' && field[i][j-1]=='.' && field[i][j+1]=='.'){
                    ans_flg=false;
                }
            }
            //cout << field[i][j];
        }
        //cout << endl;
    }
    
    if(ans_flg){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}


Measured memory_bytes: 256
while true do
    i = gets
    h = i.split[0].to_i
    w = i.split[1].to_i
    break if h == 0 && w == 0
    w.times {print "#"}
    print "\n"
    (h-2).times do
        print "#"
        (w-2).times {print "."}
        print "#"
        print "\n"
    end
    w.times {print "#"}
    print "\n\n"
end


Measured memory_bytes: 8860
n = int(input())
h = list(map(int, input().split()))
INF = float('inf')
dp = [INF] * (1 + 10 ** 5)
dp[0] = 0
for i in range(n):
    dp[i] = min(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]))
    if i < 2:
        continue
    dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]))
print(dp[n-1])

Measured memory_bytes: 13904
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fr(i,j,n)   for(int i=j;i<(n);++i)
#define bk(i,j,n)   for(int i=j;i>n;--i)
#define mset(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define mod 1000000007
#define inf 1e18
#define iosbase ios_base::sync_with_stdio(false); cin.tie(NULL);
#define bye return

int main()
{
   ll h,ans=1,c,s;
   cin>>h;
   c=1;
   while(h!=1)
   {
        h=floor(h/2);
        s=pow(2,c);
        c++;
        ans+=s;
   }
   cout<<ans<<endl;

}

Measured memory_bytes: 256
#pragma warning(disable: 4996)
#include <string>
#include <vector>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <map>
#include <bitset>
#include <queue>

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

int main() {
    int n;
    cin >> n;
    int a, b;
    int cnt = 0;
    rep(i, n) {
        cin >> a >> b;
        if (a == b)cnt++;
        else cnt = 0;
        if (cnt == 3) {
            cout << "Yes" << endl;
            return 0;
        }

    }
    cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 3124
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c;
    cin >>  a >> b >> c;
    int count = 3;
    if(a == b) count--;
    if(b == c) count--;
    if(c == a) count--;
    cout << ((count == 0) ? 1 : count) << endl;
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <stdio.h>
#include <string.h>
using ll = long long;

//LLONG_MAX
#define int long long
#define CONTAINS(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v,a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v,a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

char S[10];
char T[10];

signed main()
{
	cin >> S;
	cin >> T;
	int ans = 0;
	REP(i, 3)
	{
		if (S[i] == T[i]) ans++;
	}
	cout << ans << endl;
}




Measured memory_bytes: 256
// https://atcoder.jp/contests/practice2/tasks/practice2_e

#include <atcoder/all>

#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
template <typename T>
using VV = V<V<T>>;
using VVL = VV<ll>;
template <typename T>
using VVV = VV<V<T>>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
using asc_pq = priority_queue<T, V<T>, greater<T>>;

template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline ll len(V<T> arr) {
  return arr.size();
}

struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};

template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}

const ll INF = 1e18;

void solve();

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

  try {
    solve();
  } catch (exit_exception& e) {
  }

  return 0;
}
#endif

void solve() {
  ll N, K;
  cin >> N >> K;

  ll BIG = 1e10;

  mcf_graph<ll, ll> g(2 * N + 2);

  ll S = 2 * N;
  ll T = 2 * N + 1;

  g.add_edge(S, T, N * K, BIG);

  rep(i, N) {
    g.add_edge(S, i, K, 0);
    g.add_edge(N + i, T, K, 0);
  }

  rep(h, N) rep(w, N) {
    ll a;
    cin >> a;
    g.add_edge(h, N + w, 1, BIG - a);
  }

  auto flow_res = g.flow(S, T, N * K);
  ll res = N * K * BIG - flow_res.se;
  cout << res << "\n";

  V<string> mat(N, string(N, '.'));

  auto edges = g.edges();

  for (auto e : edges) {
    if (e.from == S || e.to == T || e.flow == 0) continue;
    mat[e.from][e.to - N] = 'X';
  }

  rep(i, N) cout << mat[i] << "\n";
}


Measured memory_bytes: 3964
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#include<algorithm>//next_permutation
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

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

ll mod1e7(ll a){
  return (a % 1000000007);
}

ll mod1e9(ll a){
  return (a % 1000000009);
}

ll mod998(ll a){
  return (a % 998244353);
}

ll lcm(ll a,ll b){
  return (a*b)/gcd(a,b);
}

vector<ll> fac(200001);
void factorial(){
  fac[0] = 1; 
  for(ll i=1; i < 200001; i++){
    fac[i] = mod998(fac[i-1]*i);
  }
}
    
ll nCr(ll n,ll r){
  if(r == 1) return n;
  else if(r == 0) return 1;
  else return mod998(fac[n]/fac[r]/fac[n-r]);
}

int main(){
  ll a,b; cin >> a >> b; ll ans = 0;
  
  for(ll i=a; i <= b; i++){
    ll id = i; vector<ll> num(5,0); 
    
    rep(j,5){
      num[j] = id % 10;
      id /= 10;
    }
    
    if(num[0] == num[4] and num[1] == num[3]) ans++;
    
    
  }
  
  cout << ans << endl;
  
}


Measured memory_bytes: 1792
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int main() {
    vector<int> lis;
    deque<int> node;
    int n;
    cin >> n;
    for (int i=0;i<n;i++) {
        int a;
        cin >> a;
        node.push_front(a);
    }
    for (auto a: node) {
        auto it = upper_bound(lis.begin(), lis.end(), a);
        if (it == lis.end()) {
            lis.push_back(a);
        } else {
            *it = a;
        }
    }
    cout << lis.size() << endl;
    return 0;
}


Measured memory_bytes: 1284
const main = (input) => {
  input = input.split(' ');
  const n = parseInt(input[0], 10);
  const k = parseInt(input[1], 10);
  
  console.log(k*Math.pow(k-1, n-1));
}
 
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;

#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e16+18;
const ll MAX=100005;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#define sum(v) accumulate(ALL(v),0)
#define fi first
#define se second

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }

// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
    int len=v.size();
    for(int i=0; i<len; ++i) {
        s<<v[i];
        if(i<len-1) s<<"	";
    }
    return s;
}

// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
    int len=vv.size();
    for(int i=0; i<len; ++i) {
        s<<vv[i]<<endl;
    }
    return s;
}

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

    ll n;
    cin>>n;
    map<ll,ll> a;
    map<ll,ll> b;
    Vec va(1e5+1);
    Vec vb(1e5+1);
    rep(i,n){
        ll x;
        cin>>x;
        if(i%2==0){
            a[x]++;
            va[x]++;
        }else{
            b[x]++;
            vb[x]++;
        }
    }

    sort(rALL(va));
    sort(rALL(vb));

    ll maxa=0;
    ll maxb=0;
    ll tmpa=0;
    ll tmpb=0;
    Each(el,a){
        if(chmax(tmpa,el.se)){
             maxa = el.fi;
        }
    }
    Each(el,b){
        if(chmax(tmpb,el.se)){
            maxb = el.fi;
        }
    }
    ll ans = 0;
    if(maxa!=maxb){
        ans= n-va[0]-vb[0];
    }else{
        if(n>2){
            ans = min(n-va[0]-vb[1],n-va[1]-vb[0]);
        }else{
            ans = 1;
        }
    }
    cout<<ans<<endl;
    return  0;
}


Measured memory_bytes: 8064
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#define int long
#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define IOS cin.tie(0); ios::sync_with_stdio(false)
const int INF = 1e9;
using namespace std;

signed main() {
    int N; cin >> N;
    int A[N]; rep(i,N) cin >> A[i];
    int cnt=0;
    rep(i,N-1){
        if(A[i]==i+1){
            cnt++;
            swap(A[i],A[i+1]);
        }
    }
    if(A[N-1]==N) cnt++;
    cout << cnt << endl;
}

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

using namespace std;

int main() {
	int n;cin>>n;
	int A[n];
	for(int i = 0;i < n;i++) cin>>A[i];
	
	for(int j = n-1;j >= 0;j-=2) cout<<A[j]<<" ";
	int s = 0;
	if(n%2 != 0) s = 1;
	while(s < n) {
		cout<<A[s]<<" ";
		s += 2;
	}

	return 0;
}

Measured memory_bytes: 2944
#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<utility>
#include<functional>
#include<queue>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;


int main(){
    

    int A,N;
    cin>>N>>A;
    cout<<max(N-A,0);
    return 0;
}


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

char a[101],b[101];

int len(char*);

int main(void){
    scanf("%s%s", a, b);
    if(a[0]=='0' || b[0]=='0'){
        printf("Error\n");
        return 0;
    }

    if(len(a)>len(b)){
        printf("GREATER\n");
    }else if(len(a)<len(b)){
        printf("LESS\n");
    }else{
        int i=0;
        while(a[i]!='\0'){
           if((int)a[i]>(int)b[i]){
               printf("GREATER\n");
               return 0;
           }else if((int)a[i]<(int)b[i]){
               printf("LESS\n");
               return 0;
           }else{
               i++;
           }
       }
       printf("EQUAL\n");
    }
    return 0;
}


int len(char arr[]){
    char* p = arr;
    int count=0;
    char end = '\0';
    while(strcmp(p,&end)){
        count++;
        p++;
    }
    return count;
}


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

using namespace std;

int main (){
    int x , y;
    cin >> x >> y;
    int a = (y - 2 * x);
    if(a < 0) {cout << "No" << endl; return 0;}
    if(a % 2 != 0) cout << "No" << endl;
    else {
        a /= 2;
        if(a > x) cout << "No" << endl;
        //int b = x - a;
        //if(b >= 0) cout << "Yes" << endl;
        else cout << "Yes" << endl;
    }

}


Measured memory_bytes: 3596
using System;

public class Test
{
	public static void Main()
	{
            string[] data;

            int max = -1000000,min = 1000000,x=0;
            long sum=0;
            x = int.Parse(Console.ReadLine());
            data = Console.ReadLine().Split();
            for (int num = x-1; num >=0 ; num--)
            {
                if (max < int.Parse(data[num]))
                {
                    max = int.Parse(data[num]);
                }

                if (min > int.Parse(data[num]))
                {
                    min = int.Parse(data[num]);
                }

                sum += long.Parse(data[num]);
            }
            Console.WriteLine("{0} {1} {2}",min,max,sum);

	}
}

Measured memory_bytes: 9876