akhauriyash/Code-Regression huggingface dataset:
//author: zekigurbuz (Zeki Gurbuz) 2019

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef queue<int> qi;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef map<int, vi> mvi;
typedef map<int, vpii> mvpii;
typedef priority_queue<int> pqi;
typedef set<int> si;

#define int ll
#define double ld
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define rsz resize
#define forn(a,b) for(int a=0;a<b;++a)
#define rep(a,b,c) for(int a=b;a<c;++a)
#define all(x) begin(x), end(x)

const ld PI = 4*atan((ld)1);
const int MOD = 1000000007; //998244353
const ll INF = 1e18;
const int N = 200005;

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

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }



signed main() {ios_base::sync_with_stdio(0);cin.tie(0);
	
	int n; cin >> n;
	vpii v(n);
	forn(i, n) {
		cin >> v[i].fi;
		v[i].se = i + 1;
	}
	sort(all(v));
	for (pii p : v) cout << p.se << " ";
	
return 0;}


Measured memory_bytes: 1664
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

void ABC168D()
{
	int N, M;
	cin >> N >> M;

	vector<set<int> > graph(N+1);
	for (int i = 0; i < M; ++i)
	{
		int A, B;
		cin >> A >> B;
		graph[A].insert(B);
		graph[B].insert(A);
	}

	vi from(N + 1);
	queue<int> qu;
	qu.push(1);
	while (!qu.empty())
	{
		int i = qu.front();
		qu.pop();
		for (auto& x : graph[i])
		{
			if (from[x] != 0)
				continue;
			from[x] = i;
			qu.push(x);
		}
	}
	int z = *min_element(from.begin() + 1, from.end());
	if (z == 0)
	{
		cout << "No" << endl;
		return;
	}
	cout << "Yes" << endl;
	for (int i = 2; i <= N; ++i)
	{
		cout << from[i] << endl;
	}
}

int main()
{
	ABC168D();
	return 0;
}


Measured memory_bytes: 27508
#include<bits/stdc++.h>
// #define int long long
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define repa(i, s, n) for(int i = s; i < (int)n; i++)
#define MOD 1000000007
using namespace std;
using ll = long long;
typedef vector<int> vi;

signed main() {
    int n;
    cin >> n;
    vi a(n);
    rep(i, n) cin >> a[i];

    int cnt = 0;
    rep(i, n){
        if(a[a[i]-1] == i+1) cnt++;
    }
    cout << cnt / 2 << endl;
}

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

int main() {
  string S;
  cin >> S;
  int N = S.size();
  priority_queue<int, vector<int>, greater<int>> x;
  for (int i = 0; i <= N-3; i++) {
    string s;
    for (int j = i; j <= i+2; j++) {
      char c = S.at(j);
      s += c;
    }
    int X = stoi(s);
    //cout << X << endl;
    x.push(abs(X-753));
  }
  cout << x.top() << endl;
}

Measured memory_bytes: 256
n,m=[int(x) for x in input().split()]
student=[]
checkpoint=[]
for i in [0]*n:
    student.append([int(x) for x in input().split()])
for i in [0]*m:
    checkpoint.append([int(x) for x in input().split()])
from math import fabs
for a,b in student:
    l=float("inf")
    for i,(c,d) in enumerate(checkpoint):
        kyori=fabs(a-c)+fabs(b-d)
        if l>kyori:
            ans=i+1
            l=kyori
    print(ans)

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

#define Max 1000002
bool prime[Max];

int main()
{
	for (int i = 2; i < Max; i++)
	{
		prime[i] = true;
	}
	for (int i = 2; i < Max; i++)
	{
		if (!prime[i]) continue;
		for (int j = i * 2; j < Max; j += i)
		{
			prime[j] = false;
		}
	}

	int a, d, n;
	while (cin >> a >> d >> n, a + d + n)
	{
		int m = a;
		while (true)
		{
			if (prime[m]) n--;
			if (n == 0) break;
			m += d;
		}
		cout << m << endl;
	}

	return 0;
}

Measured memory_bytes: 2136
#include<stdio.h>

int main()
{
	int i;
	char blood[3];
	int num[4]={0,0,0,0};
	
	while(scanf("%d,%s",&i,&blood)!=EOF)
	{
		switch(blood[0])
		{
			case 'A':
				if(blood[1]=='B'){num[2]++;}
				else{num[0]++;}
				break;
			case 'B':
				num[1]++;
				break;
			case 'O':
				num[3]++;
				break;
		}
	}
	for(i=0;i<4;i++)
	{
		printf("%d\n",num[i]);
	}
	return 0;
}

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

bool isPrime(int x)
{
	for (int i=2; i<=sqrt(x); ++i)
		if (x%i==0)
			return false;
	return true;
}

int main()
{
	int n;
	cin>>n;
	for (int i=n; ; i++)
		if (isPrime(i))
		{
			cout<<i;
			break;
		}
	return 0;
}

Measured memory_bytes: 256
n,k=map(int,input().split())
S=list(map(int,input().split()))
s=input()
T=[s[i::k] for i in range(k)]
a=0
for t in T:
    b='x'
    for c in t:
        if c!=b:
            a+=S['spr'.index(c)]
            b=c
        else:
            b='x'
print(a)

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

int main(){ 
  ll n;
  cin >> n;
  ll a, b, c, d, e;
  cin >> a >> b >> c >> d >> e;
  ll res = min({a, b, c, d, e});
  if (n % res == 0) cout << n / res + 4 << endl;
  else cout << n  / res + 5 << endl;
  return 0;  
} 

Measured memory_bytes: 256
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		long sum = 1;
		int a = 1000000007;
		long[] yaku = new long[n + 1];
		Arrays.fill(yaku, 1);
		int t;
		for (int i = 2; i <= n; i++) {
			t = i;
			int idx = 2;
			while (t >= idx) {
				if (t % idx == 0) {
					yaku[idx]++;
					t = t / idx;
				} else {
					idx++;
				}
			}
		}

		for (int i = 2; i <= n; i++) {
			sum =sum * yaku[i] % a;
		}

		System.out.println(sum);
	}
}


Measured memory_bytes: 21712
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MOD = 998244353;

template<typename T>
class SegTree {
public:
    explicit SegTree(int n, T def) : N(calcN_(n)), def(def), mVal(2*calcN_(n)-1, def) {}
    void update(int idx, T value){
        int i = N + idx - 1;
        update_(mVal[i], value);
        while(i > 0){
            i = (i-1)/2;
            mVal[i] = operate(mVal[2*i+1], mVal[2*i+2]);
        }
    }
    T get(int l, int r){
        return getImpl_(l, r, 0, 0, N);
    }
private:
    int calcN_(int n){
        int res = 1;
        while(res < n) res *= 2;
        return res;
    }
    T getImpl_(int l, int r, int idx, int rangeL, int rangeR){
        if(r <= rangeL || rangeR <= l) return def;
        if(l <= rangeL && rangeR <= r) return mVal[idx];
        int rangeM = (rangeL+rangeR)/2;
        T a = getImpl_(l, r, 2*idx+1, rangeL, rangeM);
        T b = getImpl_(l, r, 2*idx+2, rangeM, rangeR);
        return operate(a, b);
    }
    void update_(T& data, T val) { data = val; }
    T operate(T a, T b) { return max(a, b); }
    const int N;
    const T def;
    vector<T> mVal; 
};

int main(){
    int N;
    while(cin >> N){
        vector<pair<int, int>> r(N);
        for(auto& p : r) cin >> p.first >> p.second;
        sort(r.begin(), r.end());
        SegTree<int> seg(N, 0);
        for(int i=0;i<N;i++) seg.update(i, i+1);
        int idx = N-1;
        for(auto it=r.end();it!=r.begin();){
            --it;
            auto v = distance(r.begin(), lower_bound(it, r.end(), make_pair(it->first + it->second, 0)));
            int next = seg.get(idx, v);
            seg.update(idx, next);
            --idx;
        }
        vector<int> dp(N+1, 0);
        dp[0] = 1;
        for(int i=0;i<N;i++){
            dp[i+1] = (dp[i+1] + dp[i]) % MOD;
            int next = seg.get(i, i+1);
            dp[next] = (dp[next] + dp[i]) % MOD;
        }
        cout << dp.back() << endl;
    }
}

Measured memory_bytes: 4608
/*
    ALWAYS REMMEMBER:
        NEVER GIVE UP !
        DON'T MESS WITH ME !!!
        "" THE EXPERT IN ANYTHING WAS ONCE A BEGINNER ""
*/

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define f(i,a,b) for(ll i=a; i<=b; i++)
#define fi(i,a,b) for(ll i=a; i>=b; i--)
#define fa(x,b) for(auto x:b)
#define TC ll test_case; cin>>test_case; for(ll number=1 ; number <= test_case ; number ++ )
#define vll vector<ll>
#define pb push_back
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a).size()
#define sp(x) cout << fixed << setprecision(x);
#define ReadFile freopen("input.txt","r",stdin)
#define WriteFile freopen("output.txt","w",stdout)
#define endl "\n"

const ld pi = acos(-1);
const ll M=1e9+7,N=1e7+5,INF=1e18;

void solve()
{
    ll n,c=0;
    cin >> n;
    map<string,ll> mp;
    string s;

    f(i,1,n)
    {
        cin >> s;
        mp[s]++;
        c=max(c,mp[s]);
    }

    fa(it,mp)
        if(it.ss==c)
            cout << it.ff << endl;
}

int32_t main()
{
    FIO
    solve();
    return 0;
}

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

using namespace std;

int main()
{
    int n,x[100],co=0;
    scanf("%d",&n);

    for(int i=0; i<n; i++)
        scanf("%d",&x[i]);

    for(int i=0; i<n-1; i++)
    {
        for(int j=0; j<n-1-i; j++)
        {
            if(x[j]>x[j+1])
            {
                swap(x[j],x[j+1]);
                co++;
            }
        }
    }

 for(int i = 0;i < n;i++){
    cout <<x[i];
    if(i !=n-1)
      cout <<" ";
  }
  cout <<endl;
  cout <<co<<endl;

    return 0;
}



Measured memory_bytes: 3204
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=100000;
ll a[MAXN+5];
ll n,ans;
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)
	{
		int x=min(a[i-1],a[i]);
		a[i-1]-=x;
		a[i]-=x;
		ans+=x;
		int y=a[i]/2; 
		a[i]-=y*2;
		ans+=y;
	}
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 1024
n=int(input())
a=list(map(int,input().split()))
c=0
for i in range(n):
    x=a[i]
    if x%2==0:
        f=True
    else:
        f=False
    while f:
        c+=1
        x=x//2
        if x%2==0:
            f=True
        else:
            f=False
print(c)


Measured memory_bytes: 40564
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int res = 0;
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			if (i % 2 == 1 && tmp % 2 == 1) {
				res++;
			}

		}
		System.out.println(res);
	}
}

Measured memory_bytes: 35688
'use strict'
function Main(input) {
	// 1行目がinput[0], 2行目がinput[1], …に入る
	//input = input.split("\n");
	const tmp = input.split(" ");
	//文字列から10進数に変換するときはparseIntを使います
	const a = parseInt(tmp[0]);
	const b = parseInt(tmp[1]);
	let output_string= null;
	output_string = a * b % 2 == 0 ? "Even" : "Odd" ;
	//出力
	console.log(output_string);
}

//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

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

int A[2000000],h;

void maxheapify(int i){
  int l,r,largest;
  l=2*i;
  r=2*i+1;
  if(l<=h && A[l]>A[i]) largest=l;
  else largest=i;
  if(r<=h && A[r]>A[largest]) largest=r;
  if(largest!=i){
    swap(A[i],A[largest]);
    maxheapify(largest);
  }
}

int extractmax(){
  int v;
  if(h<1) return 0;
  v=A[1];
  A[1]=A[h--];
  maxheapify(1);
  return v;
}

void buildmaxheap(){
  for(int i=h/2;i>0;i--){
    maxheapify(i);
  }
}

void insert(int k){
  h++;
  int i=h;
  A[h]=k;
  while(i>1 && A[i/2]<A[i]){
    swap(A[i], A[i/2]);
    i/=2;
  } 
  
}

int main(){
  string s;
  int a;
  while(1){
    cin>>s;
    if(s[0]=='i'){
      cin>>a;
      insert(a);
    }
    else if(s[1]=='x'){
      cout<<extractmax()<<endl;
    }
    else return 0;
  }
}



Measured memory_bytes: 7056
import java.util.*

val sc = Scanner(System.`in`)

fun main(args : Array<String>) {
    val (n, k) = listOfInt(2)
    val hh = listOfInt(n).sorted().toIntArray()

    var min = Int.MAX_VALUE

    for (i in 0..n-k) {
        val diff = hh[i+k-1] - hh[i]
        if (diff < min) min = diff
    }

    println(min)
}

fun listOfInt(n: Int) = (1..n).map { sc.nextInt() }.toList()

Measured memory_bytes: 56672
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
#define INF 1000000000;
#define MOD 1000000007;
typedef long long int ll;
int main(void){
    int n,m;
    cin>>n>>m;
    unordered_map<int,int>mp;
    for(int i=0;i<m;i++){
        int a;
        cin>>a;
        mp[a]=1;
    }   
    vector<ll>ans(n+1,0);
    ans[0]=1;
    if(mp.find(1)==mp.end()){
        ans[1]=1;
    }else{
        ans[1]=0;
    }
    for(int i=2;i<=n;i++){
        if(mp.find(i)==mp.end()){
            ans[i]=(ans[i-1]+ans[i-2])%MOD;
        }else{
            ans[i]=0;
        }
        //cout<<ans[i]<<endl;
    }
    cout<<ans[n]<<endl;
}

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

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n){
        cin >> a[i];
    }
    vector<int> color(9);
    rep(i,n){
        int ai = a[i];
        int j = min(ai/400,8);
        color[j]++;
    }

    int cl_u3200 = 0;
    rep(i,8){
        if(color[i] > 0){
            cl_u3200++;
        }
    }

    int ansmax = cl_u3200+color[8];
    int ansmin;
    if(cl_u3200 < 1) ansmin = 1;
    else ansmin = cl_u3200;
    
    cout << ansmin << " " << ansmax << endl;
}


Measured memory_bytes: 256
#include <stdio.h>

#define MIN(A,B) ((A)<(B)?(A):(B))

int B[1000];

#define Inf 100000

int main(){
  int n, a, i;
  scanf("%d", &n);
  B[n]=Inf;
  for (i=1; i<=n-1; i++){
    scanf("%d", &B[i]);
  }
  B[0]=B[1];
  a=0;
  for (i=1; i<=n; i++){
    a+=MIN(B[i], B[i-1]);
  }
  printf("%d\n", a);

}


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

void Main() {
  int h, w;
  cin >> h >> w;
  int a[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  vector<vector<int>> ans;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      vector<int> tmp;
      if (a[i][j] % 2 == 0) continue;
      if (i > 0 && a[i - 1][j] % 2 == 1) {
        a[i][j]--;
        a[i - 1][j]++;
        tmp.push_back(i + 1);
        tmp.push_back(j + 1);
        tmp.push_back(i);
        tmp.push_back(j + 1);
        ans.push_back(tmp);
      } else if (j > 0 && a[i][j - 1] % 2 == 1) {
        a[i][j]--;
        a[i][j - 1]++;
        tmp.push_back(i + 1);
        tmp.push_back(j + 1);
        tmp.push_back(i + 1);
        tmp.push_back(j);
        ans.push_back(tmp);
      } else if (j < w - 1 && a[i][j + 1] % 2 == 1) {
        a[i][j]--;
        a[i][j + 1]++;
        tmp.push_back(i + 1);
        tmp.push_back(j + 1);
        tmp.push_back(i + 1);
        tmp.push_back(j + 2);
        ans.push_back(tmp);
      } else if (i < h - 1 && a[i + 1][j] % 2 == 1) {
        a[i][j]--;
        a[i + 1][j]++;
        tmp.push_back(i + 1);
        tmp.push_back(j + 1);
        tmp.push_back(i + 2);
        tmp.push_back(j + 1);
        ans.push_back(tmp);
      } else {
        if (j < w - 1) {
          a[i][j]--;
          a[i][j + 1]++;
          tmp.push_back(i + 1);
          tmp.push_back(j + 1);
          tmp.push_back(i + 1);
          tmp.push_back(j + 2);
          ans.push_back(tmp);
        } else if (i < h - 1) {
          a[i][j]--;
          a[i + 1][j]++;
          tmp.push_back(i + 1);
          tmp.push_back(j + 1);
          tmp.push_back(i + 2);
          tmp.push_back(j + 1);
          ans.push_back(tmp);
        }
      }
    }
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << " " << ans[i][3] << endl;
  }
}

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



Measured memory_bytes: 9904
use std::cmp;
use std::fmt::Debug;
use std::str::FromStr;
#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where
    T: FromStr,
    <T as FromStr>::Err: Debug,
{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim()
        .split_whitespace()
        .map(|c| T::from_str(c).unwrap())
        .collect()
}

#[allow(dead_code)]
fn read_as_string() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn main() {
    let line = read_as_vec::<usize>();
    let (n, k) = (line[0], line[1]);
    let mut p = read_as_vec::<u64>();
    p.sort();
    println!("{}", p.iter().take(k).sum::<u64>());
}


Measured memory_bytes: 2156
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f(i,x,n) for(int i=x;i<n;++i)
#define ar array
void func(int n){
    for(int i=0;i<=n;++i)
        cout<<i<<" ";
    for(int i=n-1;i>=0;--i)
        cout<<i<<" ";
    cout<<endl;
}
int32_t main()
{
    string s;
    cin>>s;
    int a=0,b=0;
    for(int i=0;i<s.size();++i){
      if(s[i]=='A')
        a++;
      else ++b;
    }
    if(a>0 && b>0)
      cout<<"Yes\n";
    else cout<<"No\n";
    return 0;
}


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

int main() {
  string S,T;
  cin >> S >> T;

  int count = 0;
  for (int i = 0; i < 3; i++) {
    if (S.at(i) == T.at(i)) {
      count++;
    }
  }
  cout << count <<endl;
}


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

#define int long long

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int A, P;
  cin >> A >> P;

  cout << (A*3+P)/2 << endl;
  
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using  namespace std;
int main()
{
    int n;
    cin>>n;
    int c1=0,c2=0,c3=0,c4=0;
    for(int i=0;i<n;i++){
    string s;
    cin>>s;
    if(s=="AC")
        c1++;
    else if(s=="WA")
        c2++;
    else if(s=="TLE")
        c3++;
    else if(s=="RE")
        c4++;
    }
    cout<<"AC x "<<c1<<endl;
    cout<<"WA x "<<c2<<endl;
    cout<<"TLE x "<<c3<<endl;
    cout<<"RE x "<<c4<<endl;

}


Measured memory_bytes: 3628
N, Ma, Mb = [int(_) for _ in input().split()]
T = [[int(_) for _ in input().split()] for i in range(N)]

dp = [[float('inf') for i in range(401)] for i in range(401)]
dp[0][0] = 0

sum_a = 0
sum_b = 0
for x in range(N):
    a, b, c = T[x]
    sum_a += a
    sum_b += b
    for i in list(range(a, sum_a+1))[::-1]:
        for j in list(range(b, sum_b+1))[::-1]:
            dp[i][j] = min(dp[i][j], dp[i-a][j-b]+c)

ans = float('inf')
for i in range(1, sum_a+1):
    for j in range(1, sum_b+1):
        if i * Mb == j * Ma:
            ans = min(ans, dp[i][j])

print(ans if ans != float('inf') else -1)

Measured memory_bytes: 8692
import sys
sys.setrecursionlimit(10**9)
INF = 10**18

def input():
    return sys.stdin.readline().rstrip()

def main():
    H, W = map(int, input().split())
    S = [list(input()) for i in range(H)]
    ans = 0
    memo_y = [[-INF]*W for i in range(H)]
    memo_x = [[-INF]*W for i in range(H)]
    for y in range(H):
        for x in range(W):
            c = 0
            if S[y][x] == '.':
                c += 1
                c_x = 0
                c_y = 0
                tmp_x = [x]
                tmp_y = [y]
                if memo_x[y][x] == -INF:
                    for i in range(1, x+1):
                        if S[y][x-i] == '.':
                            c_x += 1
                            tmp_x.append(i)
                        else:
                            break
                    for i in range(x+1, W):
                        if S[y][i] == '.':
                            c_x += 1
                            tmp_x.append(i)
                        else:
                            break
                    for i in tmp_x:
                        memo_x[y][i] = c_x
                else:
                    c_x += memo_x[y][x]
                if memo_y[y][x] == -INF:
                    for i in range(1, y+1):
                        if S[y-i][x] == '.':
                            c_y += 1
                            tmp_y.append(i)
                        else:
                            break
                    for i in range(y+1, H):
                        if S[i][x] == '.':
                            c_y += 1
                            tmp_y.append(i)
                        else:
                            break
                    for i in tmp_y:
                        memo_y[i][x] = c_y
                else:
                    c_y += memo_y[y][x]
                c += c_x + c_y
            ans = max(ans, c)
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 248712
using System;

public class Hello
{
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        for (int i = 0; i < n; i++)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            var a = long.Parse(line[0]);
            var b = long.Parse(line[1]);
            var w = (long)Math.Sqrt(a * b);
            long ret;
            if (w * (w + 1) < a * b) ret = 2 * w - 1;
            else if (w * (w + 1) == a * b) ret = 2 * w - 2;
            else if (w * w == a * b && w != a) ret = 2 * w - 3;
            else if (w * w == a * b && w == a) ret = 2 * w - 2;
            else ret = 2 * w - 2;
            Console.WriteLine(ret);
        }
    }
}

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

#define int long long

const int N = 1e5 + 10 ;

int dp[N][4] ;

int a[N][3] ;
int n ;

int solve(int i = 0 , int j = 0){
	if(i == n){
		return 0 ;
	}
	if(~dp[i][j]) return dp[i][j] ;
	int ans = 0 ;
	for(int x = 1 ; x <= 3 ; x++){
		if(x == j) continue ;
		ans = max(ans , solve(i + 1 , x) + a[i][x-1]) ;
	}
	return dp[i][j] = ans ;
}

int32_t main(){
	cin >> n ;
	memset(dp , -1 , sizeof(dp)) ;
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < 3 ; j++) cin >> a[i][j] ;
	}
	cout << solve() ;
	return 0 ;
}



Measured memory_bytes: 15300
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
ans = 0
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if L[i] == L[j] or L[i] == L[k] or L[j] == L[k]:
                continue
            if L[i] >= L[j]+L[k]: continue
            ans += 1
print(ans)


Measured memory_bytes: 9188
// Author :: <Hitesh_Saini>
#include<bits/stdc++.h>

#define __speed() ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define dbg(x) cout << "--- " << #x << " = " << x << " ---\n";
#define findmax(v) *max_element(v.begin(), v.end())
#define _sum(a) accumulate(a.begin(), a.end(), 0)
#define tt int t;for(cin>>t; t--; )
#define f0(i, n) for(i = 0; i < int(n); i++)
#define f1(i, n) for(i = 1; i<= int(n); i++)
#define all(x) x.begin(), x.end()
#define EB emplace_back
#define PB push_back
#define F first
#define S second
#define endl "\n"

using namespace std;
using mii = map<int, int>;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
const int mod = 1e9+7,mxN = 1e6+5;

template<typename... Args>void print(Args... args){((cout << args << " "), ...);cout << endl;}
ll mod_pow(ll x, ll y){ll res = 1;x = x % mod;while(y>0){if(y&1)res = (res*x) % mod; y = y>>1;x = (x*x) % mod; }return res; }ll mod_inv ( ll n ){ll ans = mod_pow ( n , mod - 2 ) ;return ans ;}ll mul(ll a, ll b){a%=mod, b%=mod;return (a*b)%mod;}ll sub(ll a,ll b){a%=mod, b%=mod;return (a-b+mod)%mod;}ll add(ll a, ll b){a%=mod, b%=mod;return (a+b)%mod;}
bool _prime(ll n){if(n<2)return 0;if(n<4)return 1;if(n%2==0||n%3==0)return 0;for(ll i=5;i*i<=n;i+=6)if(n%i==0||n%(i+2)==0)return 0;return 1;}

/* #include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define iset tree<ll , null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define iof order_of_key
#define findat find_by_order */

int n, cache[mxN], A[mxN];
int f(int pos){
	if(pos<=0)
		return 0;
	if(pos==1)
		return abs(A[0]-A[1]);
	if(cache[pos]!=-1)
		return cache[pos];
	return cache[pos]=min(abs(A[pos]-A[pos-2])+f(pos-2), abs(A[pos]-A[pos-1])+f(pos-1));
}

void solve(){//D
	cin >> n;
	int i;
	memset(cache, -1, sizeof(cache));
	f0(i, n)
		cin >> A[i];
	print(f(n-1));
}

int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin); 
		freopen("output.txt", "w", stdout);
	#endif
    __speed();
    solve();
    return 0;
}

Measured memory_bytes: 12640
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>

using namespace std;
typedef pair<int, int> pii;
static const int INF = 1000000000;
static int g_dist[256][102][100];

int main(){
	ios_base::sync_with_stdio(false);
	int n, m, e, s, t, r;
	cin >> n >> m >> e >> s >> t >> r;
	vector<int> a(m), b(m);
	for(int i = 0; i < m; ++i){ cin >> a[i] >> b[i]; }
	vector<int> x(e), y(e), z(e);
	for(int i = 0; i < e; ++i){ cin >> x[i] >> y[i] >> z[i]; }
	vector<int> has_event(n, -1);
	for(int i = 0; i < e; ++i){ has_event[z[i]] = i; }
	for(int i = 0; i < (1 << e); ++i){
		for(int j = 0; j <= r; ++j){
			for(int k = 0; k < n; ++k){ g_dist[i][j][k] = INF; }
		}
	}
	const int init = (has_event[s] >= 0 ? (1 << has_event[s]) : 0);
	g_dist[init][0][s] = 0;
	int answer = INF;
	for(int i = 0; i < (1 << e); ++i){
		vector< vector<int> > conn(n);
		for(int j = 0; j < m; ++j){
			conn[a[j]].push_back(b[j]);
			conn[b[j]].push_back(a[j]);
		}
		for(int j = 0; j < e; ++j){
			if(!(i & (1 << j))){ continue; }
			conn[x[j]].push_back(y[j]);
			conn[y[j]].push_back(x[j]);
		}
		for(int j = 0; j <= r; ++j){
			for(int u = 0; u < n; ++u){
				if(g_dist[i][j][u] >= INF){ continue; }
				g_dist[i][0][s] =
					min(g_dist[i][0][s], g_dist[i][j][u] + 1);
			}
		}
		for(int j = 0; j <= r; ++j){
			for(int u = 0; u < n; ++u){
				if(g_dist[i][j][u] >= INF){ continue; }
				if(has_event[u] >= 0){
					const int mask = i | (1 << has_event[u]);
					g_dist[mask][j][u] =
						min(g_dist[mask][j][u], g_dist[i][j][u]);
				}
				for(int t = 0; t < conn[u].size(); ++t){
					const int v = conn[u][t];
					g_dist[i][j + 1][v] =
						min(g_dist[i][j + 1][v], g_dist[i][j][u] + 1);
				}
			}
		}
		for(int j = 0; j <= r; ++j){
			answer = min(answer, g_dist[i][j][t]);
		}
	}
	if(answer >= INF){ answer = -1; }
	cout << answer << endl;
	return 0;
}

Measured memory_bytes: 11076
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define revrep(i, s) for (int i = s-1; i > 0; i--)
#define revrrep(i, s) for (int i = s-1; i >= 0; i--)
#define all(a)  begin((a)),end((a))
#define uniq(c) (c).erase(unique(all((c))), end((c)))

long long distance(pair<long long, long long> student, pair<long long, long long> check_point)
{
    return abs(student.first - check_point.first) + abs(student.second - check_point.second);
}

int main()
{
    int N, M; cin >> N >> M;
    vector<pair<long long, long long>> AB, CD;
    rep (i, 0, N) {
        long long a, b; cin >> a >> b;
        AB.push_back(make_pair(a, b));
    }
    rep (i, 0, M) {
        long long c, d; cin >> c >> d;
        CD.push_back(make_pair(c, d));
    }

    rep (i, 0, N) {
        long long min_distance = distance(AB[i], CD[0]);
        int index = 1;
        rep (j, 1, M) {
            long long current_distance = distance(AB[i], CD[j]);
            if (min_distance > current_distance) {
                index = j+1;
                min_distance = current_distance;
            }
        }
        cout << index << endl;
    }
}

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

int main(void){
    int n;
    cin>>n;
    vector<int> d(n);
    for(int i=0;i<n;i++){
        cin>>d[i];
    }
    sort(d.begin(),d.end());
    int ans=d[n/2]-d[n/2-1];
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 768
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(H, W, S):
    g = defaultdict(dict)
    for i, r in enumerate(S):
        for j, c in enumerate(r):
            if i > 0 and S[i-1][j] != c:
                g[(i, j)][(i-1, j)] = 1
                g[(i-1, j)][(i, j)] = 1
            if i < H-1 and S[i+1][j] != c:
                g[(i, j)][(i+1, j)] = 1
                g[(i+1, j)][(i, j)] = 1
            if j > 0 and S[i][j-1] != c:
                g[(i, j)][(i, j-1)] = 1
                g[(i, j-1)][(i, j)] = 1
            if j < W-1 and S[i][j+1] != c:
                g[(i, j)][(i, j+1)] = 1
                g[(i, j+1)][(i, j)] = 1
    
    done = set([])
    ans = 0
    ac = {}
    for k in g:
        
        i, j = k
        if S[i][j] != '#':
            continue
        
        if k in ac:
            ans += ac[k]
            continue
        s = [k]
        done = set([k])
        w = set([])
        count = 0
        while s:
            # print(s)
            k = s.pop()
            for n in g[k]:
                if n not in done:
                    s.append(n)
                    done.add(n)
            i, j = k
            if S[i][j] == '.':
                count += 1
            else:
                w.add(k)
        for l in w:
            ac[l] = count
        ans += count
        

    return ans


def main():
    H, W = read_int_n()
    S = []
    for _ in range(H):
        S.append(read_str())
    # S = []
    # H = 400
    # W = 400
    # for i in range(H):
    #     if i%2 == 0:
    #         S.append('#.'*200)
    #     else:
    #         S.append('.#'*200)

    print(slv(H, W, S))


if __name__ == '__main__':
    main()


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

int main() {
    int N;
    cin >> N;
    long A, ans = 0, preA = 0;
    for (int i = 0; i < N; i++)
    {
        cin >> A;
        if (A < preA) {
            ans += preA - A;
        }
        else {
            preA = A;
        }
    }
    
    cout << ans << endl;
    return 0;
}


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

int main() {
    string s, ans = "yes";
    cin >> s;
    map<char, int> mp;
    for(int i = 0; i < s.size(); ++i) {
        if(mp[s[i]] == 0) mp[s[i]]++;
        else {
            ans = "no";
            break;
        }
    }
    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(void){
  long long a, b, k;
  long long ans_a, ans_b;
  cin >> a >> b >> k;
  
  if (k < a){
    ans_a = a - k;
    ans_b = b;
  } else if (k - a < b){
    ans_a = 0;
    ans_b = b - (k - a);
  } else{
    ans_a = 0;
    ans_b = 0;
  }
  
  cout << ans_a << " " << ans_b;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	long long h=0,cnt=0,sum=1,i=0;
	cin >>h;
	while (h!=1)
	{
		h/=2;
		cnt++;
	}
	for (i=0;i<cnt;i++)
		sum=sum*2+1;
	cout <<sum<<endl;
	return 0;
}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10 ** 7)

def resolve():
    def dfs1(idx, par):
        ret = 0
        child = 0
        for to in G[idx]:
            if to == par:
                continue
            dfs1(to, idx)
            ret += EV[to] + 1.0
            child += 1
        EV[idx] = 0
        if child >= 1:
            EV[idx] += ret / child

    def dfs2(idx, d_par, par):
        ret = 0
        for to in G[idx]:
            if to == par:
                ret += d_par + 1.0
            else:
                ret += EV[to] + 1.0
        ans[idx] = ret / len(G[idx])
        for to in G[idx]:
            if to == par:
                continue
            max_l = max(1, len(G[idx]) - 1)
            nx_d_par = (ret - EV[to] - 1.0) / max_l
            dfs2(to, nx_d_par, idx)

    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)

    EV = [0] * N
    ans = [0] * N
    dfs1(0, -1)
    dfs2(0, 0, -1)
    for i in range(N):
        print(ans[i])


if __name__ == '__main__':
    resolve()

Measured memory_bytes: 158480
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<limits>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
using namespace std;

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

    return 0;
}

Measured memory_bytes: 3576
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;

class UnionFind{
public:
  vector<int> par;
  vector<int> rank;
  vector<int> sz;
  int N;

  UnionFind(int n){
      N = n;
      init();
  }

  //初期化
  void init(){
      for(int i = 0; i < N; i++){
          par.push_back(i);
          rank.push_back(0);
          sz.push_back(1);
      }
  }

  //木の根を求める
  int find(int x){
    if(par[x] == x) return x;
    else {
      par[x] = find(par[x]);
      return par[x];
    }
  }

  //xとyの集合の併合
  void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

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

  //x,yが同じ集合にあるか
  bool same(int x, int y){
      return find(x) == find(y);
  }

  //xの木のサイズ
  int size(int x){
      return sz[find(x)];
  }

  //ufの連結成分の数
  int cnt_connection(){
      int res = 0;
      bool ex[N];
      fill(ex, ex + N, false);
      for(int i = 0; i < N; i++){
          ex[find(i)] = true;
      }

      for(int i = 0; i < N; i++){
          if(ex[i]) res++;
      }

      return res;
  }
};

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

int main(){
    int N, K;
    cin >> N >> K;
    int p[N];
    for(int i = 0; i < N; i++) cin >> p[i];

    UnionFind uf = UnionFind(N - K + 1);

    set<int> st;
    for(int i = 0; i < K - 1; i++) st.insert(p[i]);

    for(int i = 0; i < N - K; i++){
        st.erase(p[i]);
        st.insert(p[i + K - 1]);

        if(p[i] < *st.begin() && *st.rbegin() < p[i + K]){
            uf.unite(i, i + 1);
        }
    }

    int x[N];
    x[0] = 1;
    for(int i = 1; i < N; i++){
        if(p[i - 1] < p[i]) x[i] = x[i - 1] + 1;
        else x[i] = 1;
    }

    int id = -1;
    for(int i = 0; i < N; i++){
        if(x[i] == K){
            if(id == -1) id = i - K + 1;
            else {
                uf.unite(id, i - K + 1);
                id = i - K + 1;
            }
        }
    }

    /*for(int i = 0; i < N; i++){
        cout << x[i] << " ";
    }
    cout << endl;
    */


    cout << uf.cnt_connection() << endl;
    return 0;
}

Measured memory_bytes: 11136
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template<typename T>
void print(const vector<T> &x) {
    int n = x.size();
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x) {
    int n = x.size();
    rep(i,n) {
        rep(j,x[i].size()) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<typename T>
void print(const vector<T> &x, int n) {
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x, int n, int m) {
    rep(i,n) {
        rep(j,m) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

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

void input_init() {
    cin.tie(0); ios::sync_with_stdio(false);
}

int main(int argc, char const *argv[]) {
    int n; cin>>n;
    vector<vector<ll>> cnt(10, vector<ll>(10, 0));
    for (int i = 1; i <= n; ++i) {
        string s(to_string(i));
        cnt[s[0]-'0'][s.back()-'0']++;
    }
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
        string s(to_string(i));
        ans += cnt[s.back()-'0'][s[0]-'0'];
    }
    cout << ans << '\n';
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	vector<vector<long long int> > ans(2000);
	for(int i=0;i<2000;i++) ans[i]=vector<long long int>(2000,0); 
	long long int h,w;
	
	cin>>h>>w;
	char a[h+10][w+10];
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=0;i<h;i++)
	{
		if(a[i][0]=='#')
		break;
		ans[i][0]=1;
	}
	for(int i=0;i<w;i++)
	{
		if(a[0][i]=='#')
		break;
		ans[0][i]=1;
	}
	long long int p=1000000007;
	for(int i=1;i<h;i++)
	{
		for(int j=1;j<w;j++)
		{
			if(a[i][j]=='#')
			continue;
			ans[i][j]=(ans[i-1][j]+ans[i][j-1])%p;
		}
	}
	cout<<ans[h-1][w-1]<<endl;
}

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

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

  set<int> S;
  for (int i = 0; i < N; i++) {
    int d;
    cin >> d;
    S.insert(d);
  }

  cout << S.size() << "\n";
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

std::vector<int> divs(int x) {
	std::vector<int> ans;
	for(int i = 1; i * i <= x; i++) {
		if(x % i == 0) {
			ans.push_back(i);
			if(x / i != i) {
				ans.push_back(x / i);
			}
		}
	}
	std::sort(ans.begin(), ans.end());
	return ans;
}

bool test(std::vector<int> a, int x, int k) {
	int wtf = 0;
	for(auto &v : a) {
		v %= x;
		wtf = (wtf + v) % x;
	}
	if(wtf != 0) return false;
	std::sort(a.begin(), a.end());
	for(int l = 0, r = (int) a.size() - 1; l < r; ) {
		if(a[l] == 0) {
			l++;
			continue;
		}
		if(a[r] == x) {
			r--;
			continue;
		}
		int use = std::min(a[l], x - a[r]);
		a[l] -= use;
		a[r] += use;
		k -= use;
	}
	return k >= 0;
}

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n, k;
	std::cin >> n >> k;
	std::vector<int> a(n);
	int sum = 0;
	for(auto &v : a) {
		std::cin >> v;
		sum += v;
	}
	auto ha = divs(sum);
	int ans = 1;
	for(auto d : ha) {
		if(test(a, d, k)) {
			ans = d;
		}
	}
	std::cout << ans << '\n';
}

Measured memory_bytes: 256
import java.util.*;

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

        int n = sc.nextInt();
        int ma = sc.nextInt();
        int mb = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        int[] c = new int[n];
        int a_sum = 0;
        int b_sum = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            b[i] = sc.nextInt();
            c[i] = sc.nextInt();
            a_sum += a[i];
            b_sum += b[i];
        }

        int[][] dp = new int[a_sum+1][b_sum+1];
        for (int i = 0; i < n; i++) {

            for (int j = a_sum; j >= 0; j--) {
                for (int k = b_sum; k >= 0; k--) {
                    if (dp[j][k] != 0 && j+a[i] <= a_sum && k+b[i] <= b_sum) {
                        if (dp[j+a[i]][k+b[i]] == 0) {
                            dp[j+a[i]][k+b[i]] = dp[j][k] + c[i];
                        } else {
                            dp[j+a[i]][k+b[i]] = Math.min(dp[j+a[i]][k+b[i]], dp[j][k] + c[i]);
                        }
                    }
                }
            }

            if (dp[a[i]][b[i]] == 0) {
                dp[a[i]][b[i]] = c[i];
            } else {
                dp[a[i]][b[i]] = Math.min(dp[a[i]][b[i]], c[i]);
            }
        }


//        for (int i = 0; i < a_sum; i++) {
//            for (int j = 0; j < b_sum; j++) {
//                System.out.print(dp[i][j]);
//                System.out.print(" ");
//            }
//            System.out.println("\n");
//        }

        int ans = -1;
        int tmp_ma = ma;
        int tmp_mb = mb;
        while (tmp_ma <= a_sum && tmp_mb <= b_sum) {
            if (dp[tmp_ma][tmp_mb] != 0) {
                if (ans == -1) {
                    ans = dp[tmp_ma][tmp_mb];
                } else {
                    ans = Math.min(ans, dp[tmp_ma][tmp_mb]);
                }
            }
            tmp_ma += ma;
            tmp_mb += mb;
        }
        System.out.println(ans);
    }
}


Measured memory_bytes: 23124
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cassert>
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#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;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

int N, M, Q;
vl a, b, c, d;
ll ans;
void func(vl A) {
	int n = A.size();
	if (n == N + 1) {
		ll anstmp = 0;
		rep(i, Q) {
			if (A[b[i]] - A[a[i]] == c[i])
				anstmp += d[i];
		}
		ans = max(ans, anstmp);
		return;
	}
	int Aend = A[n - 1];
	for (int i = Aend; i <= M; i++) {
		vl Atmp = A;
		Atmp.push_back(i);
		func(Atmp);
	}
	return;
}

int main() {
	ans = -InfLL;
	cin >> N >> M >> Q;
	a.resize(Q);
	b.resize(Q);
	c.resize(Q);
	d.resize(Q);
	rep(i, Q) {
		cin >> a[i] >> b[i] >> c[i] >> d[i];
	}
	vl Aini(1, 1);
	func(Aini);
	cout << ans << endl;
	return 0;
}

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

typedef long long LL;
typedef pair<int , int> P2;
typedef pair<pair<int , int> , int> P3;
typedef pair<pair<int , int> , pair<int , int> > P4;
#define PB(a) push_back(a)
#define MP(a , b) make_pair((a) , (b))
#define M3P(a , b , c) make_pair(make_pair((a) , (b)) , (c))
#define M4P(a , b , c , d) make_pair(make_pair((a) , (b)) , make_pair((c) , (d)))
#define repp(i,a,b) for(int i = (int)(a) ; i < (int)(b) ; ++i)
#define repm(i,a,b) for(int i = (int)(a) ; i > (int)(b) ; --i)

int main () {
int k; cin >> k;
cout << (k / 2) * (k - k / 2) << endl;
return 0;

}



Measured memory_bytes: 256
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,flag=0;
    cin>>t;
bool ok,take;
    vector<int>v;
    for(int i=0;i<t;i++){
    cin>>n;
    v.push_back(n);
    }

ok=is_sorted(v.begin(),v.end());
if(ok){
    cout<<"YES"<<endl;
}
if(!ok){
for(int i=0;i<t;i++){
    for(int j=i+1;j<=t-1;j++){
        if(v[i]>v[j]){
            swap(v[i],v[j]);
     take=is_sorted(v.begin(),v.end());
    if(take){
        cout<<"YES"<<endl;
    flag++;
    break;
    }
        else{
            swap(v[i],v[j]);
        }
}

    }
}
}
if(flag==0&&!ok){
    cout<<"NO"<<endl;
}

}










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

int main() {

	int n,p;
	int i;
	int cup,*member;
	int Answer[1000],a=0;

	for(;;) {
		scanf("%d %d",&n,&p);
		if(n==0&&p==0) break;
		member=(int *)malloc(n * sizeof(int));
		for(i=0;i<n;i++)
			member[i]=0;

		i=0,cup=p;
		while(1) {
			if(cup>0) {
				cup--;
				member[i]++;
			}
			else if(cup==0&&member[i]>0){
				cup=member[i];
				member[i]=0;
			}
			if(member[i]==p) {
				Answer[a]=i;
				a++;
				break;
			}
			if(i==n-1) i=0;
			else i++;
		}

	}
	for(i=0;i<a;i++)
		printf("%d\n",Answer[i]);
	return 0;
}

Measured memory_bytes: 608
#include<cstdio>
#include<cstring>
#include<cmath>
#include<bitset>
#include<iostream>
#include<algorithm>
#include<string>
#include<utility>
#include<tuple>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<numeric>
#include<functional>
#include<iomanip>
#include<random>

#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define REP(i,n) for(int i = 0;i < n;i++)
#define FOR(i,a,b) for(int i = a;i < b;i++)
#define MEM(a,b) memset(a,b,sizeof(a))
#define ALL(x) (x).begin(),(x).end()
#define LMAX 9223372036854775807
#define LMIN -9223372036854775808

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ull, ull> UP;
typedef tuple<int, int, int> T;

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) {}
	void unionSet(int x, int y) {
		x = root(x), y = root(y);
		if (x != y) {
			if (data[y] < data[x])swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};

ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	int k = a % b;
	if (k == 1)return 1;
	if (k == 0)return b;
	int ans = gcd(b, k);
	return ans;
}
ll digisum(ll a) {
	int sum = 0;
	while (a) {
		sum += a % 10;
		a /= 10;
	}
	return sum;
}
ll countpow(ll a, ll b) {
	int ans = 0;
	if (b == 0)return -1;
	while (1) {
		if (a % b == 0) {
			a /= b;
			ans++;
		}
		else break;
	}
	return ans;
}


int main() {
	IOS;
	int a, b;
	cin >> a >> b;
	cout << (a + b) % 24 << endl;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    long n,k;
    cin >> n >> k;
    vector <long> h(n);
    for(long i = 0; i < h.size(); i++){
        cin >> h[i];
    }
    if(n<=k){
        cout << 0 <<endl;
        return 0;
    }
    sort(h.begin(),h.end());
    while(k>0){
        h.pop_back();
        if(h.size()==0){
            break;
        }
        k--;
    }
    long long ans = 0;
    long long div = 0;
    long long mod = pow(10,6);
    for(long i = 0; i < h.size(); i++){
        ans += h[i];
        div += (ans/mod);
        ans %= mod;
        // cout << div << endl;
    }
    if(div>0){
        cout << div;
        cout << setw(6) << setfill('0') << ans;
        cout << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 1792