akhauriyash/Code-Regression huggingface dataset:
#define MOD_TYPE 2

#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
/*
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using multiInt = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
*/
/*
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
*/
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
//constexpr ll MOD = 1;
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-11;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

inline void init_main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(30) << setiosflags(ios::fixed);
}
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

void solve()
{
  int n;
  cin >> n;
  map<string, int> mp;
  rep(i, n)
  {
    string s;
    cin >> s;
    mp[s]++;
  }
  vector<string> v = {"AC", "WA", "TLE", "RE"};
  for (auto s : v)
  {
    cout << s << " x " << mp[s] << "\n";
  }
}

int main()
{
  init_main();
  solve();

  return 0;
}


Measured memory_bytes: 3664
fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    var t = 0
    var x = 0
    var y = 0

    for (i in 0 until n) {
        val tmp = readLine()!!.split(' ').map { it .toInt() }
        val diff = (tmp[0]-t) - Math.abs(tmp[1]-x) - Math.abs(tmp[2]-y)
        t = tmp[0]
        x = tmp[1]
        y = tmp[2]
        if (diff == 0) continue
        if (diff > 0 && diff % 2 == 0) continue
        println("No")
        return
    }
    println("Yes")
}


Measured memory_bytes: 55456
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String output = "";
		while(sc.hasNext()) {
			output = output + sc.next().substring(0, 1);
		}
		System.out.println(output);
		sc.close();
	}
}

Measured memory_bytes: 22736
#include<iostream>
#include<string>
using namespace std;
int main(){
  string f,r;
  int i;
  int ans;
  ans=0;
  cin >> f >> r;
  for(i=0;i<3;i++){
    if(f[i]==r[i]){
      ans++;
    }
  }
  cout << ans;
  return(0);
}

Measured memory_bytes: 256
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double a = sc.nextDouble();
		double b = sc.nextDouble();
		double x = sc.nextDouble();
		sc.close();
		BigDecimal res = new BigDecimal(0.0);

		//桁数指定
		DecimalFormat format =new DecimalFormat("#.#");
		format.setMinimumFractionDigits(10);

		if(a*a*b/2.0 <= x) {
			res = res.add(new BigDecimal(Math.atan(2*(a*a*b-x)/(a*a*a))));
		}else {
			res = res.add(new BigDecimal(Math.PI / 2.0 - Math.atan(2 * x / (a*b*b))));
		}
		System.out.println(format.format
				(res.multiply(new BigDecimal(180.0))
						.divide(new BigDecimal(Math.PI),10,RoundingMode.HALF_UP)));
	}
}

Measured memory_bytes: 25172
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=200005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,M;cin>>N>>M;
    if(N==M) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}




Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n)	for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)

int main() 
{	
	ios_base :: sync_with_stdio(false),cin.tie(NULL),cout.tie(0);
	
	string s;
	cin>>s;int flag=0,flag2=0;
	forr(i,0,s.size())
	{
		if(s[i]=='A')	flag=1;
		if(s[i]=='B')	flag2=1;
	}
	if(flag==1 and flag2==1)	cout<<"Yes";
	else cout<<"No";
	return 0;	
}

Measured memory_bytes: 256
/*
author: diradon
04-Apr-20
17:33:39
*/

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

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

    int n, m, a, tot, ans = 0;
    tot = 0;
    cin >> n >> m;
    vector<int> mp(n);
    for (int i = 0; i < n; i++)
    {
        cin >> mp[i];
        tot += mp[i];
    }
    int cut = ceil(tot/(4.0*m));
    for (int i = 0; i < n; i++)
    {
        if (mp[i] >= cut) ans++;
    }
    if (ans >= m) cout << "Yes\n";
    else cout << "No\n";
}

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

int sumf(int m){
	int r = 0;
	for(int i = 1;i<m+1;i++)
		r += i;
	return r;
}
int main() {
	int p,q;
	cin >> p >> q;
	int m = 0;
	if(p != 1){
	for(int i=1;i <= q/p;i++){
		if(q%i==0)
			m=i;
	}
	}
	else{
		m=q;
	}
	cout << m << endl;
	return 0;
}

Measured memory_bytes: 256
N=int(input())
A=list(map(int,input().split()))
X=0
P=0
Q=0
j=0
for i in range(N):
  Q+=1
  while X^A[i]!=X+A[i]:
    Q-=1
    X^=A[j]
    j+=1
  X^=A[i]
  P+=Q
print(P)

Measured memory_bytes: 99836
// Coins

#include <iostream>

using namespace std;

int main(){
	int A, B, C, X;
	cin >> A >> B >> C >> X;

	int res = 0;

	for (int i = 0; i <= A; ++i)
		for (int j = 0; j <= B; ++j)
			for (int k = 0; k <= C; ++k)
				if (500*i + 100*j + 50*k == X)
					res++;

	cout << res << endl;

	return 0;
}

Measured memory_bytes: 256
class Arrow
  attr_reader :source, :sink, :label

  def initialize(source, sink, label)
    @source = source
    @sink = sink
    @label = label
  end
end

def push(queue, spell)
  size = queue.size
  index = queue.index{|entry| entry[0] >= spell} || size
  return queue[index][1] if index < size && queue[index][0] == spell

  return nil if index > 0 && !spell.start_with?(queue[index - 1][0])
  queue.slice!(index..-1) if index < size && !queue[index][0].start_with?(spell)

  nodes = {}
  queue.insert(index, [spell, nodes])
  nodes
end

def calc(n, arrows, start, goal)
  ins = Array.new(n){[]}
  arrows.each do |arrow|
    ins[arrow.sink] << arrow.source
  end

  reachable_from = {}
  stack = [goal]
  until stack.empty?
    node = stack.pop
    reachable_from[node] = true
    ins[node].each do |source|
      stack << source unless reachable_from[source]
    end
  end

  return nil unless reachable_from[start]
  arrows.select!{|arrow| reachable_from[arrow.sink]}

  outs = Array.new(n){[]}
  arrows.each do |arrow|
    outs[arrow.source] << arrow
  end

  limit = n * arrows.map{|arrow| arrow.label.size}.max

  queue = [['', {start => true}]]
  until queue.empty?
    spell, nodes = queue.shift
    return spell if nodes[goal]
    return nil if spell.size > limit

    nodes.each do |node, _|
      outs[node].each do |arrow|
        new_nodes = push(queue, spell + arrow.label)
        new_nodes[arrow.sink] = true if new_nodes
      end
    end
  end
  nil
end

loop do
  n, a, s, g = gets.split.map(&:to_i)
  break if n == 0

  arrows = (0...a).map do |i|
    x, y, lab = gets.split
    Arrow.new(x.to_i, y.to_i, lab)
  end

  puts calc(n, arrows, s, g) || 'NO'
end

Measured memory_bytes: 10548
#include<stdio.h>
#include<stdlib.h>
#define VMAX 10000

int main(){
    unsigned short *A, *B;
    int C[VMAX + 1];
    int n, i;

    scanf("%d", &n);

    A = malloc(sizeof(short) * n + 1);
    B = malloc(sizeof(short) * n + 1);

    for(i = 0; i <= VMAX; i++) C[i] = 0;
    for(i = 0; i < n; i++){
        scanf("%hu", &A[i + 1]);
        C[A[i + 1]]++;
    }
    for(i = 1; i <= VMAX; i++) C[i] = C[i] + C[i - 1];
    for(i = n; i >= 1; i--){
        B[C[A[i]]] = A[i];
        C[A[i]]--;
    }
    for(i = 1; i <= n; i++){
        if(i > 1) printf(" ");
        printf("%d", B[i]);
    }
    printf("\n");

    free(A);
    free(B);

    return 0;
}


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

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

    vector<tuple<string, int, int>> t(N);
    
    for (int i=0; i< N; i++) {
        string a;
        int b;
        cin >> a >> b;
        t.at(i) = make_tuple(a,-b,i);
    }
    sort(t.begin(), t.end());
    for (int i=0; i< N; i++) {
        cout << get<2>(t.at(i))+1 << endl;
    }
}


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


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

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

  int ans = 0;
  if (N < 3) { cout << 0 << endl; return 0; }

  for (int i = 0; i <= N - 3; i++) {
    for (int j = i+1; j <= N - 2; j++) {
      for (int k = j+1; k < N; k++) {
        if (L[i] != L[j] && L[j] != L[k] && L[k] < L[i] + L[j]) ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


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

const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;


int main(){
  
  ll n;
  cin >>n;
  if(n<10){
    cout<<n<<endl;
    return 0;
  }
  int ans=0;
  rep(i,10){
    ll num=n-i;
    if(num%10==9){
      string t=to_string(num);
      rep(j,t.size()){
        ans+=t[j]-'0';
      }
      break;
    }
  }
  string u=to_string(n);
  ll num1=0;
  ll num2=0;
  if(u[0]==0){
    ll num1=9*(u.size()-1);
    if(num1>ans) ans=num1;
  }
  else{
    ll num2=u[0]-'1';
    num2+=9*(u.size()-1);
    if(num2>ans) ans=num2;
  }
  
  
  cout<<ans<<endl;
}
  



Measured memory_bytes: 256
#include <bits/stdc++.h>
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size)

using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }

int main()
{
    int N, X;
    cin >> N >> X;
    vector<int> L(N+1, 0);
    vector<int> D(N+1, 0);
    int ans = 0;
    for (int i = 0; i < N; i++)
    {
        cin >> L[i];
    }
    for (int i = 1; i < N+1; i++)
    {
        D[i] = D[i - 1] + L[i - 1];
    }
    for (int i = 0; i <= N; i++){
        if (D[i] <= X) ans++;
    }
    cout << ans << endl;

    return 0;
}


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

#include <iostream>
#include <fstream>

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <bitset>
#include <random>
#include <chrono>
#include <complex>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i,1,n)
#define REP(i,n) FOR(i, 0, (int)(n)-1)
#define fst first
#define snd second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;

string to_string(string s) {
    return '"' + s + '"';
}

string to_string(const char* s) {
    return to_string((string) s);
}

string to_string(bool b) {
    return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}



void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

int XMOV[4] = {0, 0, 1, -1};
int YMOV[4] = {1, -1, 0, 0};

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

using namespace std;

#define MAXN  100005

class CLower {
public:
	void solve(std::istream& in, std::ostream& out) {
		int n;
		in >> n;

		int h[MAXN];
		for (int i = 0; i < n; i++)
			in >> h[i];

		int ans = 0;
		int fidx, lidx, fval, lval;
		fidx = lidx = 0;
		lval = 0x3f3f3f3f;
		for (int i = 0; i < n; i++){
		    if (lval >= h[i]){
		        lval = h[i];
		        lidx = i;
		    }
		    else {
		        ans = max(ans, lidx - fidx);
		        lidx = fidx = i;
		        lval = h[i];
		    }
		}
		ans = max(ans, lidx-fidx);
		out << ans;



	}
};

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

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//==================================

using Graph = vector<Vi>;
Graph g;
int N;
Vi C, ans;

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif

    cin >> N;
    g.resize(N);
    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    C.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> C[i];
    }
    sort(C.begin(), C.end(), greater<int>());


    int start = 0;
    for (int i = 0; i < N; i++) {
        if (g[i].size() > g[start].size()) {
            start = i;
        }
    }

    ans.resize(N);
    int now = 0;
    queue<Pii> q;
    q.emplace(start, -1);
    while (!q.empty()) {
        int idx, par;
        tie(idx, par) = q.front();
        q.pop();
        ans[idx] = C[now++];
        for (const auto& to : g[idx]) {
            if (to == par) continue;
            q.emplace(to, idx);
        }
    }

    ll score = 0;
    for (int i = 1; i < N; i++) {
        score += C[i];
    }

    cout << score << '\n';

    for (int i = 0; i < N - 1; i++) {
        cout << ans[i] << ' ';
    }
    cout << ans[N - 1] << endl;

    return 0;
}

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

using namespace std;

#define F first
#define S second
#define int long long
#define mpr make_pair 
typedef long long ll;
#pragma GCC optimize("Ofast")

const int maxn = 310;
const int N = 1e6+5;
const int mod = 998244353;
const int inf = 1e9+10;

int n, a, b, c, d;

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

    cin>> n >> a >> b >> c >> d;

    if(c > d) swap(c,d);
    if(a > b) swap(a,b);
    b -= a; a = 0;

    bool ans = 0;
    for(int m = 0; m < n; m++)
    {
        int l = c*m-d*(n-m-1), r = d*m-c*(n-m-1);
        if(b >= l && b <= r) ans = 1;
    }
    if(ans) cout<<"YES";
    else cout<<"NO";
}





Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL<<(n))
#define bit_exist(x, n) ((x >> n) & 1)
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl;
#define mdebug(m) cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;}
#define pb push_back
#define f first
#define s second
#define int long long
#define INF 1000000000000000000
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; }
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }

using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using bools = vector<bool>;
using pairs = vector<Pii>;

//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
//char d[4] = {'D','R','U','L'};

const int mod = 1000000007;
//const int mod = 998244353;
//#define Add(x, y) x = (x + (y)) % mod
//#define Mult(x, y) x = (x * (y)) % mod



signed main(){

    int N, M; cin >> N >> M;
    vec A(N); cin >> A;
    priority_queue<int> que;
    REP(i, N) que.push(A[i]);
    REP(i, M){
        int t = que.top(); que.pop();
        que.push(t / 2);
    }
    int ans = 0;
    REP(i, N){
        ans += que.top(); que.pop();
    }
    Out(ans);

    return 0;
}


Measured memory_bytes: 2168
#include <cstdio>

int main() {
    int h, w;
    for (;;) {
        scanf("%d %d", &h, &w);
        if (h == 0 && w == 0) {
            return 0;
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                printf("#");
            }
            printf("\n");
        }
        printf("\n");
    }
}


Measured memory_bytes: 2580
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

using vll = vector<ll>;

void solve();

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

  solve();

  return 0;
}

#define ini(...)   \
  int __VA_ARGS__; \
  in(__VA_ARGS__)

#define inl(...)  \
  ll __VA_ARGS__; \
  in(__VA_ARGS__)

#define ins(...)      \
  string __VA_ARGS__; \
  in(__VA_ARGS__);

void in() {}
template <typename T, class... U>
void in(T& t, U&... u) {
  cin >> t;
  in(u...);
}

void out() {
  cout << endl;
}
template <typename T, class... U>
void out(const T& t, const U&... u) {
  cout << t;
  if (sizeof...(u))
    cout << " ";
  out(u...);
}

#define rep(i, n) for (long long i = 0; i < n; i++)

void solve();
#ifndef ONLINE_JUDGE
#include "./lib.hpp"
#endif

void solve() {
    inl(N, T);
    vll t(N);
    rep(i, N) cin >> t[i];

    ll end_time = 0;
    ll ret = 0;
    rep(i, N) {
        if (t[i] >= end_time) {
            ret += T;
        } else {
            ret += T + t[i] - end_time;
        }
        end_time = t[i] + T;
    }
    out(ret);
}


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

int main() {
  int s,w;
  cin>>s>>w;
  if(s>w){
    cout<<"safe"<<endl;
    
  }
  
  else{
    cout<<"unsafe"<<endl;
  }
}

Measured memory_bytes: 3624
n,t=map(int,input().split())
dp=[0]*(t+3005)
ans=chk=0
w=[]
for i in range(n):
    a,b=map(int,input().split())
    w.append((a,b))
w.sort()

for i in w:
    for j in range(t-1,-1,-1):
        dp[j+i[0]]=max(dp[j+i[0]],dp[j]+i[1])
        ans=max(ans,dp[j+i[0]])
print(ans)

Measured memory_bytes: 45032
#pragma region RegionDefs
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include "boost/multi_array.hpp"
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define reps(i,j,n) for(int i=(j),i##_len=(n);i<i##_len;++i)
#define repr(i,n) for(int i=(n);i>=0;--i)
#define all(x) begin(x),end(x)
using namespace std;
const int INF = 1e9;
template<class T=int> using P  = pair<T, T>;
template<class T=int> using V  = vector<T>;
template<class T=int> using PQ = priority_queue<T>;
template<class T=int> using PQG = priority_queue<T, V<T>, greater<T>>;
typedef long long ll;
const ll MOD = 1000000007LL;
void in() {}
template<class Head, class... Tail>
void in(Head&& head, Tail&&... tail)
{
    cin >> head;
    in(move(tail)...);
}
#define IN(...) ll __VA_ARGS__; in(__VA_ARGS__)

//時間が厳しいとき使う
#define INI(...) int __VA_ARGS__; in(__VA_ARGS__)
#define INS(T, ...) T __VA_ARGS__; in(__VA_ARGS__)
#define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem
template<class T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if (a>b) {a=b; return 1;} return 0;}
#define y0 y__0
#define y1 y__1
#define j0 j__0
#define j1 j__1
#pragma endregion RegionDefs

void solve()
{
    IN(a, b);
    rep(i, 100000)
    {
        if ((int)(i * 1.08)-i == a && (int)(i * 1.1)-i == b)
        {
            cout << i << endl;
            return;
        }
    }
    cout << -1 << endl;
}

int main()
{
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(numeric_limits<double>::max_digits10);

    solve();

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define Pi acos (-1)
int main()
{
    double n;
    while(cin>>n)
    {
        double sum,s;
        sum= Pi *(n*n);
        s=sum/ Pi;
       
        cout<<s<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep3(i, l, n) for (int i = l; i < n; ++i)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define chmax(a, b) a = (a >= b ? a : b)
#define chmin(a, b) a = (a <= b ? a : b)
#define out(a) cout << a << endl
#define outa(a, n) { rep(_, n) cout << a[_] << " "; cout << endl; }
#define outp(a, n) { cout << endl; rep(_, n) cout << a[_].F << " " << a[_].S << endl; }
#define SZ(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
#define FIX(a) fixed << setprecision(a)
#define LB(v, n) (int)(lower_bound(all(v), n) - v.begin())
#define UB(v, n) (int)(upper_bound(all(v), n) - v.begin())

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int k, x;
    cin >> k >> x;
    int flg = 0;
    for (int i = max(-1000000, x - k + 1); i <= min(1000000, x + k - 1); ++i) {
        if (flg) cout << " ";
        cout << i << endl;
    }
    
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
using namespace std;

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

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t=1;
    //cin>>t;
    while(t--) {
        ll n;
        cin>>n;
        double probs[n+1];
        for(int i=1;i<=n;i++){
            double x;
            cin>>x;
            probs[i]=x;
        }

        double dp[n+1][n+1]={0};
        dp[1][0] = 1.0 - probs[1];
        for(int i = 2; i <= n; ++i)
            dp[i][0] = dp[i - 1][0] * (1.0 - probs[i]);

        dp[1][1] = probs[1];
        for(int i = 2; i <= n; ++i)
            dp[i][i] = dp[i - 1][i - 1] * probs[i];
        for(int i=1;i<=n;i++){
            for(int j=1;j<i;j++){
                dp[i][j] = dp[i - 1][j] * (1.0 - probs[i]) + dp[i - 1][j - 1] * probs[i];
            }
        }
        double res=0;
        for(int i = n / 2 + 1; i <= n; i++)
            res += dp[n][i];
        cout<<fixed<<setprecision(12)<<res<<endl;
    }
    return 0;
}


Measured memory_bytes: 70528
#include<iostream>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define A 3
#define B 9

struct puzzle{
    int f[B];
    int space;
    string path;
    
    bool operator<(const puzzle &p) const{
        int i;
        for(i=0;i<B;i++){
            if(f[i]==p.f[i]) continue;
            return f[i]>p.f[i];
        }
        return false;
    }
};

static const int dx[4]={-1,0,1,0};
static const int dy[4]={0,-1,0,1};
static const char dir[4]={'u','l','d','r'};

bool Target(puzzle p){
    int i;
    for(i=0;i<B;i++)
        if(p.f[i] != (i+1)) return false;
    return true;
}

string bfs(puzzle s){
    queue<puzzle> Q;
    map<puzzle,bool>V;
    puzzle u,v;
    s.path= "";
    Q.push(s);
    V[s]=true;
    
    while(!Q.empty()){
        u=Q.front();
        Q.pop();
        if(Target(u)) return u.path;
        int sx=u.space/A;
        int sy=u.space%A;
        int r;
        for(r=0;r<4;r++){
            int tx=sx+dx[r];
            int ty=sy+dy[r];
            if(tx<0||ty<0||tx>=A||ty>=A) continue;
            v=u;
            swap(v.f[u.space], v.f[tx*A+ty]);
            v.space=tx*A+ty;
            if(!V[v]){
                V[v]=true;
                v.path += dir[r];
                Q.push(v);
            }
        }
    }
    return "unsolvable";
}

int main(){
    puzzle in;
    int i;
    
    for(i=0;i<B;i++){
        cin >> in.f[i];
        if(in.f[i]==0){
            in.f[i]=B;
            in.space=i;
        }
    }
    string ans=bfs(in);
    cout << ans.size() << endl;
    
    return 0;
}



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

int main(){
  int N, X;
  cin >> N >> X;
  vector<int> L(N);
  for(int i=0; i<N; i++) cin >> L.at(i);
  int D=0;
  int ans;
  for(int i=0; i<N; i++){
    D=D+L.at(i);
    if(D>X){
      ans=i+1;
      break;
    }
  }
  if(D<=X) ans=N+1;
  cout << ans << endl;
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  int a, b;
  cin >> a >> b;
  if (a + b == 15) {
    cout << "+" << endl;
  } else if (a * b == 15) {
    cout << "*" << endl;
  } else {
    cout << "x" << endl;
  }
  return 0;
}


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

void solve() {
  long long x, k, d;
  cin >> x >> k >> d;

  if (x == 0) {
    if (k == 1) {
      cout << d << endl;
    } else {
      cout << 0 << endl;
    }

    return;
  }

  long long turnsNoCross, distNoCross, distCross;

  if (x > 0) {
    turnsNoCross = x / d;
    distNoCross = llabs(x - turnsNoCross * d);
    distCross = llabs(x - (turnsNoCross + 1) * d);
  } else {
    turnsNoCross = -x / d;
    distNoCross = llabs(x + turnsNoCross * d);
    distCross = llabs(x + (turnsNoCross + 1) * d);
  }

  if (turnsNoCross >= k) {
    if (x > 0) {
      cout << x - k * d << endl;
    } else {
      cout << llabs(x + k * d) << endl;
    }

    return;
  }

  long long turnsLeftNoCross = k - turnsNoCross;
  long long turnsLeftCross = k - (turnsNoCross + 1);

  if (distNoCross < distCross) {
    if (turnsLeftNoCross % 2 == 0) {
      cout << distNoCross << endl;
    } else {
      cout << distCross << endl;
    }
  } else {
    if (turnsLeftCross % 2 == 0) {
      cout << distCross << endl;
    } else {
      cout << distNoCross << endl;
    }
  }
}

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


Measured memory_bytes: 3596
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;


int main(){
    int n, h, w;
    cin >> n >> h >> w;
    ll ans = 0;
    rep(i, n){
        ll a, b;
        cin >> a >> b;
        if (a >= h && b >= w) ans++;
    }
    cout << ans << endl;

    return 0;
}

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

int main()
{
  string S, s1, s2;
  int t1, t2;
  cin >> S;

  s1 = S.substr(0, 2);
  s2 = S.substr(2, 2);
  t1 = atoi(s1.c_str());
  t2 = atoi(s2.c_str());

  if((t1 == 0 && t2 > 12 || t1 > 12 && t2 == 0) || t1 == 0 && t2 == 0 || t1 > 12 && t2 > 12)
    cout << "NA" << endl;
  else if(t1 <= 12 && t2 <= 12 && t2 != 0 && t1 != 0)
    cout << "AMBIGUOUS" << endl;
  else if((t1 > 12 && t2 <= 12) || t1 == 0 && t2 <= 12)
    cout << "YYMM" << endl;
  else if((t1 <= 12 && t2 > 12) || t1 <= 12 && t2 == 0)
    cout << "MMYY" << endl;
  return 0;

}


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

string str;
vector< int > stk;

int main() {
  while (getline(cin, str) && str != ".") {
    stk.clear();
    bool is_valid = true;
    int ln = str.length();
    for (int i = 0; i < ln && is_valid; ++i) {
      if (str[i] == '(')
        stk.push_back('(');
      if (str[i] == '[')
        stk.push_back('[');
      if (str[i] == ')') {
        if (stk.size() == 0 || stk.back() != '(')
          is_valid = false;
        else
          stk.pop_back();
      }
      if (str[i] == ']') {
        if (stk.size() == 0 || stk.back() != '[')
          is_valid = false;
        else
          stk.pop_back();
      }
    }
    if (stk.size() > 0) is_valid = false;

    cout << (is_valid ? "yes" : "no") << "\n";
  }

  return 0;
}

Measured memory_bytes: 1196
n = int(input())
s = list(input())

rd = 0
for e in s:
    if e == ".":
        rd += 1

ls = 0
res = rd
for e in s:
    if e == ".":
        rd -= 1
    else:
        ls += 1
    res = min(res, rd + ls)

print(res)


Measured memory_bytes: 4840
#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <complex>
#include <valarray>
#include <fstream>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <numeric>

#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = Y-1;(X) >= 0;--(X))
#define repa(X,A,Y) for (int (X) = A;(X) <= (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define mod(n, m) (((n)%(m)+(m))%m)
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
const int INFINT = 1 << 30;                          // 1.07x10^ 9
const ll INFLL = 1LL << 60;                          // 1.15x10^18
const double EPS = 1e-10;
const int MOD = 1000000007;

ll N, M;

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

  cin >> N >> M;
  vector<int> gcds;
  for (ll i = 1; i*i <= M; ++i) {
    if (M%i == 0) {
      gcds.push_back(i);
      gcds.push_back(M/i);
    }
  }
  sort(all(gcds));
  reverse(all(gcds));


  for (int i = 0; i < gcds.size(); ++i) {
    if (M/gcds[i] >= N) {
      cout << gcds[i] << endl;
      return 0;
    }
  }
}


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()
{
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int N; cin >> N;
    string s;
    map<string, ll> mp;
    rep(i,N) {
        cin >> s;
        sort(s.begin(), s.end());
        mp[s]++;
    }

    ll ans = 0;
    for (auto& m:mp) {
        ans += m.second * (m.second - 1) / 2;
    }

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


Measured memory_bytes: 11264
n = int(input())
btn = []
checked = set()

for i in range(n):
  btn.append(int(input()))

def find(n):
  global btn, checked
  j = 1
  for i in range(n):
    if btn[j - 1] == 2:
      # count += 1
      return len(checked) + 1
    elif j - 1 in checked:
      return -1
    else:
      checked.add(j - 1)
      j = btn[j - 1]
  return -1

print(find(n))

Measured memory_bytes: 15992
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include<time.h>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<string> vstring;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define yes(ans) if(ans)cout<<"yes"<<endl;else cout<<"no"<<endl;
#define Yes(ans) if(ans)cout<<"Yes"<<endl;else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define POSSIBLE(ans) if(ans)cout<<"POSSIBLE"<<endl;else cout<<"IMPOSSIBLE"<<endl;
#define Pi 3.1415926535897932384626
#define mod llint(1e9+7)

class UnionFind {
public:
	//親の番号を格納する。親だった場合は-(その集合のサイズ)
	vector<int> Parent;

	//作るときはParentの値を全て-1にする
	//こうすると全てバラバラになる
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}

	//Aがどのグループに属しているか調べる
	int root(int A) {
		if (Parent[A] < 0) return A;
		return Parent[A] = root(Parent[A]);
	}

	//自分のいるグループの頂点数を調べる
	int size(int A) {
		return -Parent[root(A)];//親をとってきたい
	}

	//AとBをくっ付ける
	bool connect(int A, int B) {
		//AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
		A = root(A);
		B = root(B);
		if (A == B) {
			//すでにくっついてるからくっ付けない
			return false;
		}

		//大きい方(A)に小さいほう(B)をくっ付けたい
		//大小が逆だったらひっくり返しちゃう。
		if (size(A) < size(B)) swap(A, B);

		//Aのサイズを更新する
		Parent[A] += Parent[B];
		//Bの親をAに変更する
		Parent[B] = A;

		return true;
	}
};

//aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long& x, long long& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	long long d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}

bool check(llint a, llint b) {
	return a > b;
}

// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//aCbを1000000007で割った余りを求める
llint convination(llint a, llint b) {
	llint ans = 1;
	for (llint i = 0; i < b; i++) {
		ans *= a - i;
		ans %= 1000000007;
	}
	for (llint i = 1; i <= b; i++) {
		ans *= modinv(i, 1000000007);
		ans %= 1000000007;
	}
	return ans;
}

//aのb乗をmodで割った余りを求める
llint power(llint a, llint b) {
	if (b == 1)return a;
	if (b == 0)return 1;
	llint tmp = power(a, (llint)b / 2);
	tmp *= tmp;
	tmp %= mod;
	if (b % 2 == 1) {
		tmp *= a;
		tmp %= mod;
	}
	return tmp;
}

int main() {
	string s, t, r;
	cin >> s >> t;
	int a, b;
	cin >> a >> b;
	cin >> r;
	if (s == r)cout << a - 1 << " " << b << endl;
	else cout << a << " " << b - 1 << endl;
	return 0;
}

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

namespace D_2 {
    class questionD {
        static void Main(string[] args) {
            int[] NMInput = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();//2つ以上のスペース区切り入力の取得
            int N = NMInput[0];
            int M = NMInput[1];
            bool[] done = new bool[N];
            int[] points = new int[N];
            bool answer = true;
            List<Edge>[] nextNode = new List<Edge>[N];
            for(int i = 0; i < N; i++) {
                nextNode[i] = new List<Edge>();
            }
            for (int i = 0; i < M; i++) {
                int[] input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();//2つ以上のスペース区切り入力の取得
                int L = input[0] - 1;
                int R = input[1] - 1;
                int D = input[2];
                nextNode[L].Add(new Edge { to = R, cost = D });
                nextNode[R].Add(new Edge { to = L, cost = -D });
            }
            Queue<int> queue = new Queue<int>();
            for(int i = 0; i < N; i++) {
                if (done[i]) {
                    continue;
                }
                done[i] = true;
                points[i] = 0;
                queue.Enqueue(i);
                while(queue.Count != 0) {
                    int n = queue.Dequeue();
                    foreach(Edge edge in nextNode[n]) {
                        int t = edge.to;
                        if (done[t]) {
                            if(points[t] - points[n] != edge.cost) {
                                answer = false;
                            }
                        } else {
                            done[t] = true;
                            points[t] = points[n] + edge.cost;
                            queue.Enqueue(t);
                        }
                    }
                }
            }
            // Console.Write("改行なし a = {0} b = {1} c = {2}", a, b, c)//改行なし出力
            // Console.WriteLine("改行あり a = {0} b = {1} c = {2}", a, b, c);//改行付き出力
            if (answer) {
                Console.WriteLine("Yes");
            } else {
                Console.WriteLine("No");
            }
        }

        class Edge {
            public int to;
            public int cost;
        }
    }
}

Measured memory_bytes: 44000
N = int(input())
D1 = [0]*N
D2 = [0]*N

for i in range(N):
  D1[i], D2[i] = map(int, input().split())
  
a = 0
  
for i in range(N-2):
  if D1[i] == D2[i] and D1[i+1] == D2[i+1] and D1[i+2] == D2[i+2]:
    a = 1
    
if a == 1:
  print("Yes")

else:
  print("No")

Measured memory_bytes: 9192
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1000000000;

int N, C;
vector<vector<int>> D, c, cnt;

ll dist(int c0, int c1, int c2) {
  ll sum = 0;
  for (int i = 1; i <= C; i++) {
    sum += D[i][c0] * cnt[0][i];
  }

  for (int i = 1; i <= C; i++) {
    sum += D[i][c1] * cnt[1][i];
  }

  for (int i = 1; i <= C; i++) {
    sum += D[i][c2] * cnt[2][i];
  }

  return sum;
}

void abc099d() {
  if (N == 1) {
    cout << "0\n";
    return;
  }
  else {
    ll ans = INF;
    for (int p = 1; p <= C; p++) {
      for (int q = 1; q <= C; q++) {
        for (int r = 1; r <= C; r++) {
          if (p == q || q == r || r == p) {
            continue;
          }
          else {
            ans = (ans < dist(p, q, r)) ? ans : dist(p, q, r);
          }
        }
      }
    }
    cout << ans << '\n';
    return;
  }
}

int main() {
  cin >> N >> C;
  D.resize(C + 1);
  c.resize(N + 1);
  cnt.resize(3);
  for (int i = 1; i <= C; i++) {
    D[i].resize(C + 1, 0);
  }
  for (int i = 1; i <= N; i++) {
    c[i].resize(N + 1, 0);
  }

  for (int i = 0; i < 3; i++) {
    cnt[i].resize(N + 1, 0);
  }

  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      cin >> D[i][j];
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> c[i][j];
      cnt[(i + j) % 3][c[i][j]]++;
    }
  }
  abc099d();
  return 0;
}

Measured memory_bytes: 1280
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, d: "List[int]"):
    ret = 0
    for i in range(N):
        for j in range(i + 1, N):
            ret += d[i] * d[j]
    print(ret)
    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
    d = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, d)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define BIT(n) (1LL << (n))
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPB(i, n) for (int i = 0; i < BIT(n); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORA(i, n) for (auto &&i : n)
#define FORS(i, m, n) for (int i = m; i <= n; i++)
#define DEBUG(x) cerr << #x << ": " << x << "\n"
#define DEBUG2(x, y) cerr << #x << ": " << x << " " << #y << ": " << y << "\n"
#define DEBUG3(x, y, z) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << "\n"
#define PRINTALL(V)    \
  for (auto v : (V)) { \
    cerr << v << " ";  \
  }                    \
  cerr << "\n";
#define ALL(v) v.begin(), v.end()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define epb emplace_back
#define int long long
#define pint pair<int, int>
#define ld long double
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> using vec = std::vector<T>;
template <class T> void print(const T &x) { cout << x << "\n"; }
const int MOD = 1000000007, INF = 1061109567, INF2 = INF * INF;
const double EPS = 1e-10, PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
signed main() {
  cin.tie(0), ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int A, B, X, Y;
  cin >> X >> Y;
  int ans = INF2;
  REP(i, 4) {
    A = X, B = Y;
    int tmp = 0;
    if (i & BIT(0)) {
      if (A != 0) {
        A *= -1;
        tmp++;
      }
    }

    if (i & BIT(1)) {
      if (B != 0) {
        B *= -1;
        tmp++;
      }
    }
    if (B - A >= 0) {
      chmin(ans, tmp + B - A);
    }
  }
  print(ans);
}

Measured memory_bytes: 256
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    H, W = MI()
    D = Init(H, W, -1)
    L = [input().rstrip() for i in range(H)]
    move = [[1,0],[0,1],[-1,0],[0,-1]]
    for i in range(H):
        for j in range(W):
            if L[i][j] == "#" and D[i][j] == -1:
                D[i][j] = 0
                dq = deque([(i, j)])
                while dq:
                    x, y = dq.popleft()
                    for x1, y1 in move:
                        x1+=x
                        y1+=y
                        if x1 >= 0 and x1 <= H-1 and y1 >= 0 and y1 <= W-1 and D[x1][y1] == -1:
                            if L[x1][y1] == "#":
                                D[x1][y1] = D[x][y]+1
                                dq.append((x1, y1))
                if D[x][y] == 0:
                    print("No")
                    exit()
    print("Yes")

if __name__ == "__main__":
    main()

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

using namespace std;

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

  char b[] = ".,!? ";
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;

    int cnt = 0, ssz = s.size();
    if (s[0] != '0') cnt++;

    for (int j = 1; j < ssz; j++) {
      if (s[j] != '0') cnt++;
      else if (s[j - 1] == '0') cnt = 0;
      else {
        if (s[j - 1] == '1') {
          printf("%c", b[(cnt - 1 + 5) % 5]);
        } else if (s[j - 1] == '7') {
          printf("%c", 'p' + (cnt - 1 + 4) % 4);
        } else if (s[j - 1] == '9') {
          printf("%c", 'w' + (cnt - 1 + 4) % 4);
        } else {
          if (s[j - 1] < '7') {
            printf("%c", 'a' + 3 * (s[j - 1] - '2') + (cnt - 1 + 3) % 3);
          } else {
            printf("%c", 't' + 3 * (s[j - 1] - '8') + (cnt - 1 + 3) % 3);
          }
        }
        cnt = 0;
      }
    }
    cout << endl;
  }

  return 0;
}


Measured memory_bytes: 3108