akhauriyash/Code-Regression huggingface dataset:
using System;

class Program{
    static void Swap(int []A, int a, int b){
        var tmp=A[a];
        A[a]=A[b];
        A[b]=tmp;
    }

    static int Partition(int[] A, int p, int r){
        var x=A[r-1];
        var i=p-1;
        for(var j=p; j<r-1; j++){
            if(A[j]<=x){
                i++;
                Swap(A, i, j);
            }
        }
        Swap(A, i+1, r-1);
        return i+1;
    }

    public static void Main(){
        var n=int.Parse(Console.ReadLine());
        var s=Console.ReadLine().Split(' ');
        var A=new int[n];
        for(var i=0; i<n; i++){
            A[i]=int.Parse(s[i]);
        }

        var q=Partition(A, 0, n);

        var sout=new System.Text.StringBuilder();
        for(var i=0; i<q; i++){
            sout.Append(A[i]+" ");
        }
        sout.Append("["+A[q]+"]");
        for(var i=q+1; i<n; i++){
            sout.Append(" "+A[i]);
        }
        Console.WriteLine(sout);
    }
}

Measured memory_bytes: 18776
#include <stdio.h>
#include <stdlib.h>

int compare_int(const void *a, const void *b)
{
  return *(int*)a - *(int*)b;
}

int main(void)
{
  int n, k;
  scanf("%d %d", &n, &k);
  int i, h[n];
  i = 0;
  while (i < n)
  {
    scanf("%d", &h[i]);
    i++;
  }
  
  qsort(h, n, sizeof(int), compare_int);
  
  if (k >= n)
  {
    printf("0");
    return 0;
  }
  
  long sum;
  sum = 0;
  i = 0;
  while (i < n - k)
  {
    sum += h[i];
    i++;
  }
  printf("%ld", sum);
  return 0;
}


Measured memory_bytes: 1788
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
ll computeXOR(ll n) 
{ 
    // Modulus operator are expensive on most of the  
    // computers. n & 3 will be equivalent to n % 4.    
  
    switch(n & 3) // n % 4  
    { 
    case 0: return n;     // if n is multiple of 4 
    case 1: return 1;     // If n % 4 gives remainder 1   
    case 2: return n + 1; // If n % 4 gives remainder 2     
    case 3: return 0;     // If n % 4 gives remainder 3   
    } 
} 
signed main()
{
  ll n,m,x,y,ans=0;
 
  cin>>n>>m;
  x = computeXOR(m)^computeXOR(n-1);
 cout<<x<<"\n";
}

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

public class Cell
{
    public int X { get; set; }
    public int Y { get; set; }
    public string CellType { get; set; }
    public int Step { get; set; }

    public static Cell[] GetCells(char[] row, int y)
    {
        var cells = Enumerable.Range(0, row.Length).Select(x => new Cell
        {
            X = x + 1,
            Y = y,
            CellType = Convert.ToString(row[x]),
            Step = -1,
        });
        return cells.ToArray();
    }

}


class Program
{
    public static long Read()
    {
        return long.Parse(Console.ReadLine());
    }

    public static long[] Reads()
    {
        return Console.ReadLine().Split().Select(long.Parse).ToArray();
    }

    public static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);

        var n = Read();
        var input = Reads();
        var d = new long[n];
        for(int i = 0; i < n; i++)
        {
            d[i] = input[i];
        }
        long sum = 0;
        for(int i = 0; i < n-1; i++)
        {
            for(int j = i+1; j < n; j++)
            {
                sum += d[j]*d[i];
            }
        }

        Console.WriteLine(sum);




        Console.Out.Flush();
    }
}

Measured memory_bytes: 13396
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <vector>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int main()
{
	int N, K;
	cin >> N >> K;
	vector<pair<int, int>> c;
	for (auto i = 0; i < N; ++i)
	{
		int x, y;
		cin >> x >> y;
		c.push_back({ x, y });
	}

	auto ans = LLONG_MAX;
	sort(c.begin(), c.end());
	for (auto i = 0; i <= N - K; ++i)
	{
		for (auto j = i + K - 1; j < N; ++j)
		{
			vector<int> y;
			for (auto k = i; k <= j; ++k)
			{
				y.push_back(c[k].second);
			}

			sort(y.begin(), y.end());
			for (auto k = 0; k <= (int)y.size() - K; ++k)
			{
				ll diffX = c[j].first - c[i].first;
				ll diffY = y[k + K - 1] - y[k];
				ans = min(ans, diffX * diffY);
			}

		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
MOD = 10**9 + 7

N = int(input())
s = input()

# dp[i][j] := i 番目まで決めて i 番目の数より大きいものが j 個のときの場合の数
dp = [[0]*N for _ in range(N)]
dp[0] = [1]*(N+1)
for i in range(N-1):
    # 累積和をとっておく
    for j in range(N-1):
        dp[i][j+1] += dp[i][j]
    for j in range(N-i-1):
        if s[i] == '<':
            dp[i+1][j] += dp[i][N-i-1]-dp[i][j]
        else:
            dp[i+1][j] += dp[i][j]
        dp[i+1][j] %= MOD

print(dp[N-1][0])

Measured memory_bytes: 113244
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int INF = 1e+9;

int DP[1000];

int main() {
	int n, m, h, r;
	while(cin >> n && n){
		memset(DP, 0, sizeof(DP));
		vector<P> vec;
		for(int i = 0; i < n; ++i){
			cin >> h >> r;
			vec.push_back(P(h, r));
		}
		cin >> m;
		for(int i = 0; i < m; ++i){
			cin >> h >> r;
			vec.push_back(P(h, r));
		}
		sort(vec.begin(), vec.end());
		int ans = 0;
		for(int i = 0; i < (n + m); ++i){
			DP[i] = 1;
			for(int j = 0; j < i; ++j){
				if(vec[j].first < vec[i].first && vec[j].second < vec[i].second)
					DP[i] = max(DP[i], DP[j] + 1);
			}
			ans = max(ans, DP[i]);
		}
		cout << ans << endl;
	}
	
	return 0;
}

Measured memory_bytes: 1208
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
int n;
int t = 0;
vector<int> assigned(102, 0);
vector<int> vis(102);
vector<vector<int>> adj(102);
 
int visit(int u) {
	if (vis[u] != t) {vis[u] = t;}
	else return 0;
	
	for (int i = 0; i < adj[u].size(); i++) {
		if (!assigned[adj[u][i]] || visit(assigned[adj[u][i]])) { 
	      	assigned[adj[u][i]] = u; 
	      	return 1; 
		}
	}
	return 0;
}
 
int main() {
	ios::sync_with_stdio(0);
	
	cin >> n;
	vector<pair<int, int>> a(n), b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i].first >> a[i].second;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i].first >> b[i].second;
	}
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (a[i].first < b[j].first && a[i].second < b[j].second) {
				adj[i+1].push_back(j+1);
			//	adj[j+1].push_back(i+1);
			}
		}
	}
	
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		t++;
		ans += visit(i); 
	}
	
	cout << ans;
	
	return 0;
}

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



int main() {
    int n;
    cin >> n;
    vector< int> v(n);
    for (int &x : v) cin >> x;
    sort(v.rbegin(), v.rend());
    int a = 0, b = 0;
    for(int i = 0; i < n; ++i) {
        if(i & 1) b += v[i];
        else a += v[i];
    }
    printf("%d\n", a - b);
}

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

int main() {
	int n,x,y,b,p;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x>>y>>b>>p;
		cout<<min(x*b+y*p,(max(b,5)*x+max(p,2)*y)*4/5)<<endl;
	}
	return 0;
}

Measured memory_bytes: 1164
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define repi(i,a,b) for(int i=a; i<=(b); ++i)
#define rrep(i,n) for(int i=(n-1); i>=0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define mp make_pair
#define sz(v) (int)v.size()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
using ll = long long;
using namespace std;
typedef pair<int,int> P;
struct aaa{aaa(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);};}aaaaaaa;
int MOD = 1e9 + 7;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return (a*b)/gcd(a,b);}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int x,a,b;
int main(){
    cin >> x >> a >> b;
    cout << ((abs(x-a)>abs(x-b))? "B" : "A") << '\n';
}

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N - 1);
  copy_n(istream_iterator<int>(cin), N - 1, A.begin());
  vector<int> result(N);
  for (int i = 0; i < N - 1; ++i) {
    ++(result[A[i] - 1]);
  }
  copy(result.begin(), result.end(), ostream_iterator<int>(cout, "\n"));
  cout << flush;
  return 0;
}


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

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

signed main() {
  ll N;
  cin >> N;
  vector<ull> a(N);

  ull Z = 0;
  rep(i,N){
    cin >> a[i];
    Z ^= a[i];
  }
  rep(i,N){
    a[i] &= ~Z;
  }

  //printf("Z = 0x%x\n", Z);

  // ここで、青色のxorをX、赤色のxorをY、その他をZとする。
  // Zはどのような分割をしても、かならず答えに現れるもの。
  // X+Y+Z = a[0] ^ a[1] ^ ... ^ a[n-1]
  // X ^ Y = 0 --> X = Y
  // なのでa[i]から幾つか選んで、そのxorの合計Xを最大化したい。
  // これを吐き出し法でやっていく。

  ll pivot = 0;
  for (ull bit=(((ull)1) << 63); bit > 0; bit = bit>>1) {
    //printf("bit=0x%lx\n", bit);
    ll t = -1;

    // pivot行を探す
    for (ll i=pivot; i<N; i++) {
      if ((a[i] & bit) != 0) {
        t = i;
        break;
      }
    }

    if (t == -1)
      continue; // そのbitはスキップ

    swap(a[pivot], a[t]);
    rep(i,N) {
      if (i == pivot)
        continue;
      if ((a[i] & bit) != 0) {
        a[i] ^= a[pivot];
      }
    }

    pivot++;
  }

  //rep(i,N){
  //  printf("a[%d]=0x%lx\n", i, a[i]);
  //}

  ull X = 0;
  rep(i,N) {
    X ^= a[i];
  }
  cout << 2*X + Z << endl;
  return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()

using namespace std;

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

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

int main()
{
    int n;
    cin >> n;
    ll a[n+1];
    a[0] = 2;
    a[1] = 1;
    for(int i=2;i<=n;++i){
        a[i] = a[i - 1] + a[i - 2];
    }
    cout << a[n] << endl;
    return 0;
}



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

const int MAXN = 1e5 + 10;
long long x, ans, n, a[MAXN];

int main() {
	cin >> n >> x;
	for (int i = 0; i < n; i++)
		cin >> a[i], ans += a[i];
	for (int k = 1; k < n; k++) {
		multiset<int> r;
		for (int i = 0; i < k; i++)
			r.insert(a[i]);
		long long res = 0;
		for (int i = k, z = 0; z < n; z++, i++) {
			i %= n;
			r.insert(a[i]);
			res += *r.begin();
			r.erase(r.find(a[(i - k + n) % n]));
		}
		ans = min(ans, res + k * x);
	}
	cout << ans;
}

Measured memory_bytes: 384
n = int(input())
p = [ int(_) for _ in input().split() ]
ls = [ _ for _ in range(1, n+1) ]
cnt = int()

for i in range(n):
    if p[i] != ls[i]:
        cnt += 1
    
if cnt == 2 or cnt == 0:
    print('YES')
else:
    print('NO')

Measured memory_bytes: 3060
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int main(){
	int N,M;

	scanf("%d",&N);

	int T[N];


	int sum[100] ={0};

	for(int i = 0; i < N; i++){
		scanf("%d",&T[i]);
	}

	scanf("%d",&M);

	int P[M];
	int X[M];

	for(int i = 0; i < M; i++){
		scanf("%d",&P[i]);
		scanf("%d",&X[i]);
		// printf("P[%d] = %d\nX[%d] = %d\n",i,P[i],i,X[i]);
	}

	for(int k = 0; k < M; k++){
		for(int i = 0; i < N; i++){
			if(i == P[k]-1){
				sum[k] += X[k];
			}
			else{
			sum[k] += T[i];
			}
			// printf("T[%d] = %d, sum[%d] = %d\n",i,T[i],k,sum[k]);
		}
		printf("%d\n",sum[k]);
	}	
	return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef long long ll;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  FOR(i,0,n)
const double EPS = 1e-10;
const double PI  = acos(-1.0);
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
bool compare_by_b(pair<LL, LL> a, pair<LL, LL> b) {
    if(a.second != b.second)return a.second < b.second;
    else return a.first < b.first;
}


void solve(long long A, long long B, long long K){
    A-=K;
    if(A<=0LL)B+=A;
    cout<<max(0LL,A)<<" "<<max(0LL,B)<<endl;
}

int main(){
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    long long K;
    scanf("%lld",&K);
    solve(A, B, K);
    return 0;
}


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

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9
#define LLINF 1LL<<60
const double PI = 3.14159265358979323846;

bool check[200009];

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

	int X; cin >> X;
	for (int i = 1; i <= 120007; i++) check[i] = true;
	for (int i = 2; i <= X; i++) {
		if (check[i]) {
			for (int j = 2; i * j <= 120008; j++) {
				check[i * j] = false;
			}
		}
	}

	int ans = -1;
	for (int i = X; i <= 120008 && ans == -1; i++) {
		if (check[i]) ans = i;
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 384
//const int K = 20;
//int ask(int u, int v){
//    cout << "? " << u << ' ' << v << endl;
//    char ans;
//    cin >> ans;
//    return ans == 'Y';
//}
//void answer(int n, int t){
//    string ans(n,' ');
//    rep(i,n)
//        ans[i] = ask(t,i) + '0';
//    cout << "! " << ans << endl;
//}
//void __(){
//    int a,b;
//    cin >> a >> b;
//    if(b >= a){
//        cout << "Impossible" << endl;
//        return;
//    }
//    vvi by_size(K);
//    int n = a+b;
//    rep(i,n)
//        by_size[0].pb(i);
//    rep(s,K){
//        while(sz(by_size[s]) >= 2){
//            int u = by_size[s].back();
//            by_size[s].pop_back();
//            int v = by_size[s].back();
//            by_size[s].pop_back();
//            if(ask(u,v))
//                by_size[s+1].pb(v);
//        }
//    }
//    per(s,K)
//        if(!by_size[s].empty()){
//            assert(by_size[s].size() == 1);
//            answer(n,by_size[s][0]);
//            return;
//        }
//}
//
#include <iomanip>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define pb push_back
#define REP_ZERO_INT(i,r) for(int i = 0; i < (r); ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = (r)-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
const int K = 20;
int ask(int u, int v){
    cout << "? " << u << ' ' << v << endl;
    char ans;
    cin >> ans;
    return ans == 'Y';
}
void answer(int n, int t){
    string ans(n,' ');
    rep(i,n)
        ans[i] = ask(t,i) + '0';
    cout << "! " << ans << endl;
}
void __(){
    int a,b;
    cin >> a >> b;
    if(b >= a){
        cout << "Impossible" << endl;
        return;
    }
    vvi by_size(K);
    int n = a+b;
    rep(i,n)
        by_size[0].pb(i);
    rep(s,K){
        while(sz(by_size[s]) >= 2){
            int u = by_size[s].back();
            by_size[s].pop_back();
            int v = by_size[s].back();
            by_size[s].pop_back();
            if(ask(u,v))
                by_size[s+1].pb(v);
        }
    }
    per(s,K)
        if(!by_size[s].empty()){
            assert(by_size[s].size() == 1);
            answer(n,by_size[s][0]);
            return;
        }
}

int main(){
    cout << fixed << setprecision(15);
        __();
}



Measured memory_bytes: 844
#include <iostream>
#include <ios>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <utility>
#include <typeinfo>
#include <tuple>
using namespace std;

#define MOD 1000000007
#define INF 1e9+7
#define INF_LONG 1e18
#define rep(i, n) for(int i=0; i<n; ++i)
#define rep1(i, n) for(int i=1; i<=n; ++i)
#define rrep(i, m, n) for(int i=m; i<n; ++i)
#define countof(array) (sizeof(array) / sizeof(array[0]))
typedef long long ll;

#define MAX_N 200000
#define MAX_M 100000
#define MAX_V 100
#define MAX_E 100
#define MAX_Q 100000

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

int ddx[] = {-1, 0, 1, 0, -1, 1, -1, 1};
int ddy[] = {0, -1, 0, 1, 1, 1, -1, -1};

string ab = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string as = {"abcdefghijklmnopqrstuvwxyz"};

ll gcd(ll x, ll y) {
  if(y==0) return x;
  else return gcd(y, x%y);
}

ll lcm(ll x, ll y) {
  return (x*y)/gcd(x, y);
}

void yes() {
  cout << "Yes" << endl;
}

void no() {
  cout << "No" << endl;
}

void print_array(int array[], int len) {

  for(int i=0; i<len; ++i) {
    cout << array[i] << endl;
  }

}

ll absolute(ll x) {
  return (x<0)? (-1)*x : x;
}

//g++ AtCoder.cpp -o AtCoder
//AtCoder

//--------------------------
int main() {

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

  if(a>=k) {
    cout << a-k << " " << b << endl;
    return 0;
  }else  {
    k -= a;
    a = 0;
  }

  if(b>=k)
    cout << a << " " << b-k << endl;
  else
    cout << 0 << " " << 0 << endl;

  return 0;
}


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

typedef long long int ll;

using namespace std;

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

    cout << 1000 * ((n + 999) / 1000) - n << endl;

    return 0;
}

Measured memory_bytes: 3580
#include <bits/stdc++.h>
#define debug(x) std::cerr<<"L: "<<__LINE__<<" f("<<__FUNCTION__<<") Var: "<<#x<<" Val: "<<x<<"\n";
typedef long long ll;
const int inf = 0x7f7f7f7f;

inline void gettime() {
	debug(clock());
}
inline ll read() {
	static char c;
	static ll x;
	while (c = getchar(), !isdigit(c));
	x = c - '0';
	while (c = getchar(), isdigit(c))
		x = (x << 1) + (x << 3) + c - '0';
	return x;
}
inline char achar() {
	static char c;
	while (c = getchar(), !isalpha(c));
	return c;
}

ll n, ns[200200], sum[200200];

int main() {
#ifndef ONLINE_JUDGE
	std::atexit(gettime);
#endif

	n = read();
	for (int i = 1; i <= n; ++i)
		ns[i] = read(), sum[i] = sum[i - 1] + ns[i];

//枚举中间切哪里
//k是第二部分最右边的元素位置,l&r同理
	ll ans = 1e18;
	for (int k = 2, l = 1, r = 3; k <= n - 2; ++k) {
		r = std::max(r, k + 1);
		for (; l + 1 < k; ++l)
			if (std::abs((sum[k] - sum[l + 1]) - sum[l + 1]) <
			        std::abs((sum[k] - sum[l]) - sum[l]));
			else break;

		for (; r + 1 < n; ++r)
			if (std::abs((sum[n] - sum[r + 1]) - (sum[r + 1] - sum[k])) <
			        std::abs((sum[n] - sum[r]) - (sum[r] - sum[k])));
			else break;

		// printf("%d %d %d\n", l, k, r);

		ll one = sum[l], two = sum[k] - sum[l], three = sum[r] - sum[k], four = sum[n] - sum[r];
		ans = std::min(ans,
		               std::max({one, two, three, four}) - std::min({one, two, three, four}));
	}

	printf("%lld\n", ans);
}

Measured memory_bytes: 3328
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int ANS[4] = {};
  int n;
  string str;
  scanf("%d", &n);
  for(int i = 0; i < n; i++){
    cin >> str;
    if(str == "AC") ANS[0]++;
    else if(str == "WA") ANS[1]++;
    else if(str == "TLE") ANS[2]++;
    else if(str == "RE") ANS[3]++;
  }
  cout << "AC x " << ANS[0] << endl;
  cout << "WA x " << ANS[1] << endl;
  cout << "TLE x " << ANS[2] << endl;
  cout << "RE x " << ANS[3] << endl;
}

Measured memory_bytes: 3296
#include <bits/stdc++.h>
using namespace std;
#define INF int(1e9)
int main()
{
	string s;
	cin >> s;
	int ans = s.size();
	for(int i = 0 ;i < s.size() - 1; i++)
	{
		if(s[i] != s[i + 1])
			ans = min(ans, max(i + 1, int(s.size()) - i - 1));
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 512
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] id = new String[n];
		int open = 0;
		for(int i=0;i<n;i++) {
			id[i] = sc.next();
		}
		int m = sc.nextInt();
		for(int i=0;i<m;i++) {
			String q = sc.next();
			boolean flag = false;
			for(int j=0;j<n;j++) {
				if (q.equals(id[j])) {
					if (open==0) {
						System.out.println("Opened by " + q);
					}else{
						System.out.println("Closed by " + q);
					}
					open = 1 - open;
					flag = true;
					break;
				}
			}
			if (!flag) {
				System.out.println("Unknown " + q);
			}
		}
	}

}

Measured memory_bytes: 21344
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <algorithm>
#include <iomanip>
#include <functional>
#include <numeric>
#include <sstream>

#define REP(i, n) for(int i = 0;i < (n); i++)
#define REP2(i, x, n) for(int i = (x); i < (n); i++)
#define RREP(i, n) for (int i = (n) - 1;i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

#define LL long long int
#define LD long double

#define PI 3.14159265358979

using namespace std;

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

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

	int N;
	cin >> N;
	cin.ignore();

	string from = "Hoshino";
	REP(i, N) {
		string S;
		getline(cin, S);

		while(true) { 
			int res = S.find("Hoshino");
			if (res == -1) break;

			S.replace(res, 7, "Hoshina");
		}
		cout << S << "\n";
	}
	return 0;
}

Measured memory_bytes: 3176
import itertools as it
import collections as col
import bisect

import sys
sys.setrecursionlimit(10000)

def main():
    N = int(input())
    if N == 1:
        return "Hello World"
    
    A = int(input())
    B = int(input())

    return A + B
    
    
if __name__ == '__main__':
    print(main())

Measured memory_bytes: 38384
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define endl '\n'
ll inf = LLONG_MAX;
long double pi = M_PI;
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

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

  ll weird_fish = 1e9;
  ll x1 = 0, y1 = 0;
  ll x2 = weird_fish, y2 = 1;
  ll x3, y3;

  if (s <= weird_fish) {
    x3 = s;
    y3 = 0;
  }
  else if (s % weird_fish == 0) {
    x3 = 0;
    y3 = s / weird_fish;
  }
  else {
    x3 = - (s % weird_fish - weird_fish);
    y3 = s / weird_fish + 1;
  }

  cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << x3 << " " << y3 << endl;
}

Measured memory_bytes: 256
n = int(input())

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

a = factorization(n)
b = [1,3,6,10,15,21,28,36,43]
ans = 0
if len(a) == 1 and a[0][0] == 1:
    print(0)
    exit()
for i in a:
    for j in range(len(b)):
        if i[1] <b[j]:
            ans += j
            break
print(ans)
    

Measured memory_bytes: 9492
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int a,b,c,k;
  cin>>a>>b>>c>>k;
  int ans=0;
  if(k<=a)
  {
  	ans=k;
  }
  else if(k<=a+b)
  {
  	ans=a;
  }
  else if(k<=a+b+c)
  {
  	ans=a;
  	ans=ans+((k-a-b)*-1);
  }
  cout<<ans;
}


Measured memory_bytes: 3640
import heapq
N, M = map(int, input().split())
edge_list = [[] for i in range(N+1)]
for i in range(2, N + 1):
    edge_list[i].append((0, i-1))
for i in range(M):
    L, R, C = map(int, input().split())
    edge_list[L].append((C, R))
def solve(s, t):
    fixed = {}
    q = []
    heapq.heappush(q, (0, s))
    while q:
        w, x = heapq.heappop(q)
        if x == t:
            return w
        if x in fixed:
            continue
        fixed[x] = w
        for c, y in edge_list[x]:
            if y not in fixed:
                heapq.heappush(q, (w + c, y))
    return None

answer = solve(1, N)
if answer is None:
    print(-1)
else:
    print(answer)

Measured memory_bytes: 86688
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define per(i, n) for(ll i=((ll)(n))-1; i>=0; i--)
#define perf(i, n) for(ll i=((ll)(n)); i>0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;

int main(){
    Init();
    string o, e; cin >> o >> e;
    rep(i, o.size()){
        if(i>e.size()-1) cout << o[i];
        else cout << o[i] << e[i];
    }
    cout << endl;
}

Measured memory_bytes: 256
#![allow(unused_imports)]

use proconio::{input, fastout};
use proconio::marker::*;
use nalgebra::{min, max};

#[fastout]
fn main() {
    input! {
    a: usize,
    b: usize,
    c: usize
    }
    let ans = min(b / a, c);
    println!("{}", ans)
}


Measured memory_bytes: 2080
import math


def main():
    """
       /|
    b / | h
     /  |
    /??????|???_
    <- a -->

    ??????a, ??????b, a??¨b??????????§?C(???)
    C???????????????radian????±???????

    ??????1????????????: x
    ??????: h (b?????????????????¨????????????sin????????¨???????±???????
    ??¢???: S (??????)

    L (x + a + b): ??????????????????????????¨??????
    x = sqrt(a_ ** 2 + h ** 2)
    ??????a??????????????????h?????§???????????? a_ ??¨????????¨
    a_ = a - b * cos(rad)
    ??? ??????????????¨?????????????????? ??§??°??°??£??????????????????
    """
    a, b, C = map(int, input().split())

    # ????§???¢????????? 0 < C < 180
    rad = math.pi * C / 180
    sin = math.sin(rad)
    h = b * sin
    S = a * h / 2

    a_ = a - b * math.cos(rad)
    len_d = math.sqrt(a_ ** 2 + h ** 2)

    print("{:.8f}".format(S))
    print("{:.8f}".format(a + b + len_d))
    print("{:.8f}".format(h))

if __name__ == "__main__":
    main()

Measured memory_bytes: 5728
#include<bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int,int>
const int mod=1e9+7;

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

bool check(string &s)
{
  for(int i=0;i<s.size()/2;i++)
  {
    if(s[i]!=s[s.size()-1-i])return 0;
  }
  return 1;
}

int32_t  main()
{ 
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
 
 
 cout<<setprecision(15)<<fixed;
 int l;
 cin>>l;

 double ans=l*l*l;
 ans=ans/27.0;

 cout<<ans<<"\n";



}




Measured memory_bytes: 256
#include<bits/stdc++.h>
typedef  long long int ll;
typedef  long double ld;
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL) 
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define mod 1000000007
#define F first
#define S second 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
//recursions\
#pragma comment(linker, "/stack:200000000")
//loops\
#pragma GCC optimize("unroll-loops")
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>//s.order_of_key(val) *s.find_by_order(ind)
using namespace std;
const int N = 1e6+5;
int main()
{
	sync;
	ll n, ans = 0;
	cin >> n;
	for(ll i = 1; i <= n; i++)
	{
		ll x; cin >> x;
		if(i & 1)
			if(x & 1)
				ans++;
	}
	cout << ans;
}

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

int main(){
	while(1){
		int n, m;
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;

		int g[n][n];
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				g[i][j] = 0;
			}
		}
		for(int i=0; i<m; i++){
			int a, b;
			scanf("%d %d", &a, &b);
			g[a-1][b-1] = 1;
			g[b-1][a-1] = 1;
		}

		vector<int> ans(n, 0);
		for(int i=0; i<n; i++){
			if(g[0][i] == 1){
				ans[i] = 1;
			}
		}
		for(int i=1; i<n; i++){
			if(g[0][i] == 1){
				for(int j=1; j<n; j++){
					if(g[i][j] == 1){
						ans[j] = 1;
					}
				}
			}
		}
		int answer = 0;
		for(int i=1; i<n; i++){
			answer += ans[i];
		}
		printf("%d\n", answer);
	}

	return 0;
}

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

const int N = 1e5 + 9;
bool vis[N][60];
int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	int sum = 0;
	int k = n;
	while (k) {
		sum += k % 10;
		k /= 10;
	}
	queue<pair<int, int>> q;
	q.push({1, 1});
	vis[1][1] = 1;
	while (!q.empty()) {
		auto x = q.front();
		q.pop();
		int k = x.first, d = x.second;
		for (int i = 0; i <= 9; i++) {
			int nk = (k * 10 + i) % n, nd = d + i;
			if (nd <= sum && !vis[nk][nd]) {
				vis[nk][nd] = 1;
				q.push({nk, nd});
			}
		}
	}
	for (int i = 0; i <= sum; i++) {
		if (vis[0][i]) {
			cout << i << '\n';
			return 0;
		}
	}
	assert(0);
    return 0;
}

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

struct UnionFind
{
    vector<int> par, rank;
    
    UnionFind(int n){
        par.resize(n);
        rank.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;
            rank[i] = 0;
        }
    }
    
    int find(int i){
        if(i == par[i]) return i;
        return par[i] = find(par[i]);
    }
    
    bool same(int i, int j){
        return find(i) == find(j);
    }
    
    void unite(int i, int j){
        if(same(i, j)) return;
        i = find(i), j = find(j);
        if(rank[i] < rank[j]) par[i] = j;
        else{
            par[j] = i;
            if(rank[i] == rank[j]) rank[i]++;
        }
    }
};

int main()
{
    int n, q;
    cin >> n >> q;
    UnionFind uf(n);
    while(q--){
        int t, u, v;
        cin >> t >> u >> v;
        if(t) cout << uf.same(u, v) << endl;
        else uf.unite(u, v);
    }
}


Measured memory_bytes: 4820
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;


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

    long long X,Y;
    cin >> X >> Y;

    long long ans=0;
    while(X<=Y){
        X*=2;
        ans++;
    }
    cout << ans << endl;


}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main(){
    int H,W;
    cin>>H>>W;
    string A[H];
    for(int i=0;i<H;i++)cin>>A[i];
    bool row[H]={false};
    bool col[W]={false};
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if(A[i][j]=='#'){
                row[i]=true;
                col[j]=true;
            }
        }
    }
    for(int i=0;i<H;i++){
        if(row[i]){
            for(int j=0;j<W;j++){
                if(col[j])cout<<A[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}

Measured memory_bytes: 3564
fun main(args: Array<String>) {
    val c= readLine()!!.toInt()
    val array= mutableListOf<List<Int>>()
    for(i in 0 until c){
        val array2= readLine()!!.split(" ").map { it.toInt() }
        array.add(array2)
    }
    val pl=Array(100001,{true})
    pl[0]=false
    pl[1]=false
    (2..100000)
            .filter { pl[it] }
            .flatMap { it + it..100000 step it }
            .forEach { pl[it]=false }
    val pl2=Array(100001,{0})
    var count=0
    for(i in 3..100000){
        if(pl[i]&&pl[(i+1)/2])count++
        pl2[i]=count
    }
    for(i in array){
        var k= pl2[i[1]]-pl2[i[0]]
        if(pl2[i[0]]>pl2[i[0]-1])k++
        println(k)
    }
}


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

 int main() {
   int N;
   cin >> N;
   vector<int> A(N);
   map<ll, ll> count;
   ll sum = 0;

   rep(i, N) {
     cin >> A.at(i);
     count[A.at(i)]++;
   }

   for (auto p: count) {
     if (p.second >= 2) sum += p.second * (p.second - 1) / 2;
   }

   rep(i, N) {
     if (count[A.at(i)] <= 1) cout << sum << endl;
     else cout << sum - (count[A.at(i)] - 1) << endl;
   }
 }


Measured memory_bytes: 10240
#include<bits/stdc++.h>
using namespace std;
int main(){
	int d,g,ans=100000000;
	cin>>d>>g;
	vector<int>p(d),c(d);
	for(int i=0;i<d;i++)cin>>p.at(i)>>c.at(i);
	for(int i=0;i<(1<<d);i++){
		vector<int>a(p);
		int s=0,t=0;
		for(int j=0;j<d;j++){
			if(i&(1<<j)){
				s+=(j+1)*a.at(j)*100;
				s=s+c.at(j);
				t+=a.at(j);
				a.at(j)=0;
			}
		}
		int k=d-1;
		while(k>=0&&a.at(k)==0)k--;
		while(k>=0&&a.at(k)>0&&s<g){
			s+=(k+1)*100;
			a.at(k)--;
			t++;
		}
		if(s>=g) ans=min(ans,t);
	}
	cout<<ans;
}

Measured memory_bytes: 3580
#include <iostream>

using namespace std;

int main(void){
  int p[8], p_1[8] = {0}, q[8], q_1[8] = {0}, n, i, cc[8], j, sum1 = 0, sum2 = 0, sum = 0;
  int c = 0;
  cin >> n;
  cc[0] = 1;
  
  for(i = 1; i < n; i++){
    cc[i] = cc[i - 1] * (i + 1);
  }
  for(i = 0; i < n; i++){
    cin >> p[i];
  }
  for(i = 0; i < n; i++){
    cin >> q[i];
  }
  for(i = 0; i < n - 1; i++){
    p_1[p[i] - 1] = 1;
    //cout << p_1[p[i]] << endl;
    for(j = 0; j < p[i]; j++){
      if(p_1[j] == 0){
        c++;
      }
    }
    sum1 = sum1 + c * cc[n - i - 2];
    c = 0;
  }
  //cout << sum1 + 1 << endl;
  
  for(i = 0; i < n - 1; i++){
    q_1[q[i] - 1] = 1;
    //cout << p_1[p[i]] << endl;
    for(j = 0; j < q[i]; j++){
      if(q_1[j] == 0){
        c++;
      }
    }
    sum2 = sum2 + c * cc[n - i - 2];
    c = 0;
  }
  //cout << sum2 + 1 << endl;
  
  cout << abs(sum2 - sum1) << endl;
  return 0;
}

Measured memory_bytes: 384
#include<iostream>
#include<string>
using namespace std;
int main() {
  string s,t;
  cin >> s >> t;
  bool matched;
  int length = s.length();
  for(int i=0;i<length;i++){
    matched = true;
    for(int j=0;j<length;j++){
      if(s[(i+j)%length] != t[j]) {
        matched = false;
        break;
      }
    }
    if(matched){
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x = 0;
		while(true){
			int a = sc.nextInt();
			int b = sc.nextInt();
			if(b == 0 && a == 0) break;
			int count = 0;
			if(x != 0){
				System.out.println();
			}
			for(int i = a ; i <= b ; i++){
				if(i%400==0 || (i%4==0 && i%100!=0)){
					System.out.println(i);
					count++;
				}
			}
			if(count == 0){
				System.out.println("NA");
			}
			x++;
		}
		sc.close();
	}

}

Measured memory_bytes: 20692
#include <bits/stdc++.h>
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk
#define prev fhsgfhjf
#define plus fsghsf
#define minus ytryr
  
using namespace std;
  
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }
  
const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const ld eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
 
deque<int> next_good(deque<int> s) {
    int p = sz(s) - 1;
    while (p >= 0 && s[p] == 9) {
        s[p] = 0;
        p--;
    }
    if (p == -1) {
        s.push_front(1);
    } else {
        s[p]++;
    }
    while (true) {
        int b = -1;
        rep(i, 0, sz(s) - 1) {
            if (abs(s[i] - s[i + 1]) > 1) {
                b = i;
                break;
            }
        }
        if (b == -1) {
            break;
        }
        if (s[b + 1] > s[b]) {
            s[b]++;
        }
        int lll = max(s[b] - 1, 0);
        rep(i, b + 1, sz(s)) {
            s[i] = lll;
            lll = max(lll - 1, 0);
        }
    }
    return s;
}
 
int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;

    int k;
    cin >> k;
    deque<int> ans;
    rep(i, 0, k) {
        ans = next_good(ans);
    }
    for (int i : ans) {
        cout << i;
    }
    cout << "\n";
    return 0;
}
 

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

    
    int main(){
       
        int n,k,x,y,a;
        cin >>n>>k>>x>>y;
        
        if(n<=k){
            a=x*n;
            cout << a << endl;
        }
        
        else{
            a=k*x+(n-k)*y;
            cout << a << endl;
        }
    
    }

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (ll i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (ll i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

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

    ll H, W, Ch, Dh, Cw, Dw;
    cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
    Ch--;
    Cw--;
    Dh--;
    Dw--;
    vector<string> board(H);
    REP(i, 0, H)
    cin >> board[i];
    int inf = 1e9;
    vector<vector<int>> dp(H, vector<int>(W, inf));
    dp[Ch][Cw] = 0;
    int delta[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    deque<pair<ll, ll>> dq;
    dq.emplace_back(Ch, Cw);

    while (!dq.empty())
    {
        auto [y, x] = dq.back();
        if (y == Dh && x == Dw)
            break;
        dq.pop_back();
        for (auto &[dy, dx] : delta)
            if (0 <= y + dy && y + dy < H && 0 <= x + dx && x + dx < W && board[y + dy][x + dx] == '.' && dp[y + dy][x + dx] > dp[y][x])
            {
                dp[y + dy][x + dx] = dp[y][x];
                dq.emplace_back(y + dy, x + dx);
            }

        REP(ty, max(0ll, y - 2), min(H, y + 3))
        REP(tx, max(0ll, x - 2), min(W, x + 3))
        if (board[ty][tx] == '.' && dp[ty][tx] > dp[y][x] + 1)
        {
            dp[ty][tx] = dp[y][x] + 1;
            dq.emplace_front(ty, tx);
        }
    }

    cout << (dp[Dh][Dw] == inf ? -1 : dp[Dh][Dw]) << endl;
}

Measured memory_bytes: 8744
#include <stdio.h>

#define INF 1000000000

#define min(a, b) (((a) < (b)) ? (a) : (b))

const int di[2][9] = {{-2, -1, -1, 0, 0, 0, 1, 1, 2}, {-2, -1, -1, 0, 0, 0, 1, 1, 2}};
const int dj[2][9] = {{1, 1, 2, 1, 2, 3, 1, 2, 1}, {-1, -2, -1, -3, -2, -1, -2, -1, -1}};

typedef struct {
	int i, j, f, d;
} PairCoordDis;

PairCoordDis heap[32400];
int hsize;

inline void insert(int i, int j, int f, int d) {
	int ii;
	for (ii = hsize++; ii > 0 && d < heap[ii - 1 >> 1].d; ii = ii - 1 >> 1)
		heap[ii] = heap[ii - 1 >> 1];
	heap[ii] = (PairCoordDis){i, j, f, d};
}

inline int calcNextIdx(int i) {
	return i * 2 + 1 + (i * 2 + 2 < hsize && heap[i * 2 + 1].d > heap[i * 2 + 2].d);
}

inline PairCoordDis delete(void) {
	int i;
	int nextIdx;
	const PairCoordDis root = heap[0], last = heap[--hsize];

	for (i = 0; i * 2 + 1 < hsize && last.d > heap[nextIdx = calcNextIdx(i)].d; i = nextIdx)
		heap[i] = heap[nextIdx];
	heap[i] = last;

	return root;
}

char field[60][30];
int cost[60][30];
int d[60][30][2];

void dijkstra(int h, int w, int si, int sj, int sf) {
	int i, j;

	for (i = 0; i < h; i++)
		for (j = 0; j < w; j++)
			d[i][j][0] = d[i][j][1] = INF;

	d[si][sj][sf] = 0;
	insert(si, sj, sf, 0);
	while (hsize > 0) {
		const PairCoordDis fromP = delete();
		const int nf = fromP.f ^ 1;

		if (fromP.d > d[fromP.i][fromP.j][fromP.f])
			continue;

		for (i = 0; i < 9; i++) {
			const int ni = fromP.i + di[fromP.f][i], nj = fromP.j + dj[fromP.f][i];
			if (ni >= 0 && ni < h && nj >= 0 && nj < w)
				if (fromP.d + cost[ni][nj] < d[ni][nj][nf]) {
					d[ni][nj][nf] = fromP.d + cost[ni][nj];
					insert(ni, nj, nf, d[ni][nj][nf]);
				}
		}
	}
}

int main(void) {
	int i, j, k;
	int w, h;

	while (scanf("%d %d", &w, &h), w) {
		int min;

		for (i = 0; i < h; i++)
			for (j = 0; j < w; j++) {
				scanf(" %c", field[i] + j);
				cost[i][j] = field[i][j] == 'X' ? INF : field[i][j] <= '9' ? field[i][j] - '0' : 0;
			}

		min = INF;
		for (i = 0; i < w; i++)
			if (field[h - 1][i] == 'S')
				for (j = 0; j < 2; j++) {
					dijkstra(h, w, h - 1, i, j);
					for (k = 0; k < w; k++)
						if (field[0][k] == 'T') {
							min = min(min, d[0][k][0]);
							min = min(min, d[0][k][1]);
						}
				}

		printf("%d\n", min == INF ? -1 : min);
	}

	return 0;
}

Measured memory_bytes: 632
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define per(i, n) for(ll i=((ll)(n))-1; i>=0; i--)
#define perf(i, n) for(ll i=((ll)(n)); i>0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;

int main(){
    Init();
    ll n, x; cin >> n >> x;
    vi a(n);
    ll ans = 0;
    rep(i, n){
        ll in; cin >> in;
        a[i] += in;
        if(a[i] > x){
            ans += (a[i]-x);
            a[i] = a[i]-(a[i]-x);
        }
        if(i>0 && a[i]+a[i-1] > x){
            ll df = a[i]+a[i-1]-x;
            ans += df;
            a[i] -= df;
        }       
    }
    cout << ans << endl;
}

Measured memory_bytes: 1024
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();

		System.out.println("ABC" + n);
	}

	//***********************以下共通関数************************************************
	/**
	 * 文字列を反転して返す
	 * @param pStr 文字列
	 * @return 反転した文字列
	 */
	public static String myReverse(String pStr) {
		if (pStr.length() < 1) {
			return "";
		}

		StringBuffer sb = new StringBuffer(pStr);
		return sb.reverse().toString();
	}

	/**
	 * 余りを求める。
	 * @param a
	 * @param b
	 * @return 余り。余りがマイナスの時はプラスで返す。
	 */
	public static long myMod(long a, long b) {
		long ret = 0;

		ret = a % b;

		if (ret < 0) {
			ret += b;
		}

		return ret;
	}

	/**
	 * 乗数を求める ※mod対応
	 * @param kisu 基数
	 * @param josu 乗数
	 * @param primeNumMod_0 法 mod値 使わない場合は「0」
	 * @return
	 */
	public static long myPowWithMod(long kisu, long josu, long primeNumMod_0) {

		long ans = 1;

		//乗数が0の場合→1
		if (josu == 0)
			return 1;

		if (primeNumMod_0 == 0) {
			//modを使わないとき

			while (josu > 0) {

				if (josu % 2 == 0) {
					//累乗が偶数の時

					kisu *= kisu;
					josu = josu / 2;
				} else {
					//累乗が奇数の時
					ans *= kisu;
					josu--;
				}
			}

			return ans;

		} else {
			//modを使うとき

			//初めに桁を落とす
			kisu = myMod(kisu, primeNumMod_0);

			while (josu > 0) {

				if (josu % 2 == 0) {
					//累乗が偶数の時

					kisu *= myMod(kisu, primeNumMod_0);
					josu = josu / 2;

				} else {
					//累乗が奇数の時
					ans *= myMod(kisu, primeNumMod_0);
					josu--;

				}
			}

			return myMod(ans, primeNumMod_0);

		}

	}

}


Measured memory_bytes: 27112
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let a = read::<usize>();
    let b = read::<usize>();
    let c = read::<usize>();
    let mut open = vec![false; b];
    let mut t = 0;
    loop {
        t = (t + a) % b;
        if t == c || open[t] {
            break;
        }
        open[t] = true;
    }
    println!("{}", if t == c { "YES" } else { "NO" });
}


Measured memory_bytes: 4352
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
 
public class Main{
	public static void main(String[] args){
		FastScanner sc = new FastScanner();
		long MOD = 1000000007;
		int n = sc.nextInt();
		int m = sc.nextInt();
		long ans1 = 0;
		long ans2 = 0;
		for(int i=0; i<n; i++){
			ans1 += sc.nextLong()*(2*i-n+1);
			ans1 %= MOD;
		}
		for(int i=0; i<m; i++){
			ans2 += sc.nextLong()*(2*i-m+1);
			ans2 %= MOD;
		}
		
		System.out.println(ans1*ans2%MOD);
		return;
	}
}
 
 
 
 
class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}

Measured memory_bytes: 22824