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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
Int mod = 1e9+7;
//Int mod = 998244353;

typedef struct {
    Int y, x;
} p_t;

int main() {
    Int n, m;
    cin >> n >> m;
    vector<pair<Int, bool>> v(n);
    for (Int i = 0; i < n; i++) {
        v[i].fi = 0;
        v[i].se = false;
    }
    for (Int i = 0; i < m; i++) {
        Int p;
        string s;
        cin >> p >> s;
        p--;
        if ((not v[p].se) and s == "WA") {
            v[p].fi++;
        }
        if ((not v[p].se) and s == "AC") {
            v[p].se = true;
        }
    }
    Int ac = 0;
    Int wa = 0;
    for (Int i = 0; i < n; i++) {
        if (v[i].se) {
            ac++;
            wa += v[i].fi;
        }
    }
    cout << ac << " " << wa << endl;
    return 0;
}


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

int main(void){
    int n;
    cin >> n;
    int p[n],min = 100000;
    for(int i = 0;i < n;i++)cin >> p[i];
    int sum[n];
    sum[0] = p[0];
    for(int i = 1;i < n;i++)sum[i] = sum[i-1] + p[i];
    for(int i = 1;i < n-1;i++){
        if(abs((sum[n-1] - sum[i]) - sum[i] )< min)min = abs((sum[n-1] - sum[i]) - sum[i]);
    }
    cout << min;
}

Measured memory_bytes: 256
for i in range(50):
  m,f,r=(int(x) for x in input().split())

  sum=m+f
  
  if m==-1 and f==-1 and r==-1:
    break

  if m==-1 or f==-1:
    print('F')
  elif sum>=80:
    print('A')
  elif sum>=65:
    print('B')
  elif sum>=50:
    print('C')
  elif sum>=30:
    if r>=50:
      print('C')
    else:
      print('D')
  else:
    print('F')


Measured memory_bytes: 5600
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        // int n = Integer.parseInt(bf.readLine());
        StringTokenizer st = new StringTokenizer(bf.readLine());
        // int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
        int a = Integer.parseInt(st.nextToken());
          int b = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());

       int transfer = Math.min(a-b, c);
       c -= transfer;
       out.println(c);

        out.close(); System.exit(0);
    }
}


Measured memory_bytes: 20692
#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod=1e9+7;
int fact_arr[100001];
int fact(int n){
    if(fact_arr[n]!=0)
        return fact_arr[n];
    if(n==0)
        return fact_arr[n]=1;
    return fact_arr[n]=fact(n-1)*n%mod;
}
int pow(int x,int n){
    int ret=1;
    while(n){
        if(n&1)
            (ret*=x)%=mod;
        (x*=x)%=mod;
        n>>=1;
    }
    return ret;
}
int inv(int n){
    return pow(n,mod-2);
}
int comb(int n,int k){
    if(k>n)
        return 0;
    return fact(n)*inv(fact(k)*fact(n-k)%mod)%mod;
}
signed main(){
    int n,k,a[100000];
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    int ans=0;
    for(int i=0;i<n;i++)
        (ans+=comb(i,k-1)*(a[i]-a[n-1-i]))%=mod;
    cout<<ans<<endl;
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

int main() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    
    int cnt = 0;
    rep(i, n-1) if (s[i] != s[i+1]) cnt++;

    int res = n-1-max(0, cnt - 2 * k);

    cout << res << endl;
    return 0;
}

Measured memory_bytes: 512
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
typedef long long ll;

const ll LMT = pow(10,6) + 1;
ll mod[LMT];

void solver() {
	ll k;
	cin >> k;
	
	mod[0]=7%k;
	for (ll i=1; i<k; i++) {
		mod[i]=(mod[i-1]*10+7)%k;
	}
	
	ll ans = -1;
	for (ll i=0; i<k; i++) {
		if (mod[i]==0) {
			ans=i+1;
			break;
		}
	}
	cout << ans << endl;
}

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


Measured memory_bytes: 11420
#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 pb push_back
#define int long long
using namespace std;
typedef long long ll;
ll a[100005],b[100005],n;
signed main()
{

    //ios_base::sync_with_stdio(0),cin.tie(0);
    while(cin>>n)
    {
        rep(i,n+1) cin>>a[i];
        rep(i,n) cin>>b[i];
        ll sum=0;
        rep(i,n)
        {
            if(a[i]>0)
            {
                if(b[i]>a[i])
                {
                    sum+=a[i];
                    b[i]-=a[i];
                    a[i]=0;
                }
                else
                {
                    sum+=b[i];
                    a[i]-=b[i];
                    b[i]=0;
                }
            }
            if(a[i+1]>0)
            {
                if(b[i]>a[i+1])
                {
                    sum+=a[i+1];
                    b[i]-=a[i+1];
                    a[i+1]=0;
                }
                else
                {
                    sum+=b[i];
                    a[i+1]-=b[i];
                    b[i]=0;
                }
            }
        }
        cout<<sum<<"\n";
    }

    return 0;
}


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

int n,m,q,T;
int i,j,k,flag,counter;
int a,b,c,t;
// long long int sum;
// char c;
// string s;

// vector <int> v;
// set <int> st;
// multiset <int> ms;
// map <int,int> mp;
int main(){
    cin>>a>>b>>c;
    cout<<max(0,b+c-a)<<endl;
    return 0;
}

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

int main(){
  int n;
  cin >> n;
  string a,b,c;
  cin >> a >> b >> c;
  int count=0,count1=0;
  r(i,n){
    if(a.at(i)==b.at(i)&&b.at(i)==c.at(i))count1=0;
    else if(a.at(i)!=b.at(i)&&b.at(i)!=c.at(i)&&a.at(i)!=c.at(i))count+=2;
    else count++;
  }
  cout << count << endl;
}

Measured memory_bytes: 256
#include<cstdio>
const int MaxN=100010;
int N;
struct edge{int to;edge*next;}E[MaxN*2],*ne=E,*fir[MaxN];
void link(int u,int v){*ne=(edge){v,fir[u]};fir[u]=ne++;}
int dfs(int i,int f=0){
	int g=0;
	for(edge*e=fir[i];e;e=e->next)
		if(e->to!=f)g^=dfs(e->to,i)+1;
	return g;
}
int main(){
	scanf("%d",&N);
	for(int i=1,u,v;i<N;i++)scanf("%d%d",&u,&v),link(u,v),link(v,u);
	puts(dfs(1)?"Alice":"Bob");
}

Measured memory_bytes: 8704
// C - Takahashi's Information
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define rp(i,s,e) for(int i=(s);i<(e);++i)
#define rpz(i,e) rp(i,0,e)

int main(){
	vvi C(3,vi(3)); rpz(i,3) rpz(j,3) cin>>C[i][j];
	vi A(3,INT_MAX), B(3,INT_MAX);
	A[0] = 0;
	rpz(j,3) B[j] = C[0][j] - A[0];
	rpz(i,3) A[i] = C[i][0] - B[0];
	bool ans = true;
	rpz(i,3) rpz(j,3) if(C[i][j] != A[i]+B[j]) ans = false;
	puts(ans? "Yes":"No");
}

Measured memory_bytes: 256
N = $stdin.gets.to_i
A = $stdin.gets.split.map {|i| i.to_i }
$a_map = {}
$sum = 0
A.each_with_index do |a, i|
  $a_map[a] ||= 0
  $a_map[a] += 1
  $sum += a
end
Q = $stdin.gets.to_i
B = []
C = []
0.upto(Q - 1) do |i|
  b, c = $stdin.gets.split.map {|j| j.to_i }
  B[i] = b
  C[i] = c
end

def repl(i)
  b = B[i]
  c = C[i]
  b_count = $a_map.delete(b)
  if !b_count
    return
  end
  $sum -= b * b_count
  $a_map[c] ||= 0
  $a_map[c] += b_count
  $sum += c * b_count
end

#$stderr.puts "#{$sum}, #{$a_map.to_s}"
0.upto(Q - 1) do |i|
  repl(i)
  #$stderr.puts "#{$sum}, #{$a_map.to_s}"
  puts $sum
end


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


int main(void){
    // Your code here!
    int A,B,C,X;
    int num50,num100;
    int count=0;
    
    cin >> A >> B >> C >> X;
    
    //50円で何枚分か
   for(int i=0;i<A+1;i++){
       
       for(int j=0;j<B+1;j++){
           
           for(int k=0;k<C+1;k++){
               
               if((500*i+100*j+50*k)==X){
                   count++;
               }
               
               
           }
           
           
       }
       
       
   }
   
    
      
  cout << count<< endl;
  
   
   
   
   
}


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

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < n; i++) {
			list.add(scan.nextInt());
		}
		Collections.reverse(list);
		
		for(int i = 0; i < n; i++) {
			if(i == 0) System.out.print(list.get(i));
			else {
				System.out.print(" " + list.get(i));
			}
		}
		System.out.println();
	}

}



Measured memory_bytes: 26280
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n) ;i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll=long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD  1000000007
const ll INF=1e18;
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; }

ll inv[10000100];
ll FactorialInv[10000100];
ll Factorial[10000100];
ll beki(ll a, ll b){
    a %= MOD;
    if(b == 0){
        return 1;
    }
    ll ans = beki(a, b / 2);
    ans = ans * ans % MOD;
    if(b % 2 == 1){
        ans = ans * a % MOD;
    }
    return ans;
}
void init_combination(){
    const int MAX = 10000002;
    Factorial[0] = 1;
    inv[0] = 1;
    for(int i = 1; i <= MAX; i++){
        Factorial[i] = Factorial[i - 1] * i % MOD;
    }
    FactorialInv[MAX] = beki(Factorial[MAX], MOD - 2);
    for(ll i = MAX - 1; i >= 0; i--) {
        FactorialInv[i] = FactorialInv[i+1] * (i+1) % MOD;
    }
    for(int i = 1; i <= MAX; i++) {
        inv[i] = FactorialInv[i] * Factorial[i-1] % MOD;
    }
}
ll combination(ll a, ll b){
    if((a == b) || (b == 0)){
        return 1;
    }
    if(a < b) return 0;
    ll ret = 1;
    for(ll i = 0; i < b; i++) {
        ret *= (a - i);
        ret %= MOD;
        ret *= beki(i + 1, MOD - 2);
        ret %= MOD;
    }
    return ret;
}

int main(int argc, char const *argv[]) {
  init_combination();
  ll n,a,b;
  cin >> n >> a >> b;
  ll ans=beki(2,n)-1;
  cout << (2*MOD+ans-combination(n,a)-combination(n,b))%MOD << endl;

  return 0;
}


Measured memory_bytes: 234624
#include <bits/stdc++.h>
using namespace std;
int main(){
    int R; 
    cin >> R;
    if(R < 1200)
    cout << "ABC" << endl;
    else if(R >= 1200 && R < 2800)
    cout << "ARC" << endl;
    else if(R >= 2800)
    cout << "AGC" << endl;
}

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

#define ll long long
#define ull unsigned ll

#define vi vector <int>
#define ii pair <int, int>
#define vii vector <ii>
#define INFLL 9e18
#define INF 2e9
#define EPS 1e-9
#define pb push_back
#define emp emplace_back
#define PI acos(-1.0)
#define endl "\n"
#define mp make_pair
#define DEBUG 0

int isPresent (string real, string wanted){
    for (int i=0, j=0; i<real.size(); i++){
        if (real[i] == wanted[j]){
            j++;
            if (j == wanted.size()) return 1;
        }
    }
    return 0;
}


int main ()

{
    #define int ll
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.tie(NULL);

    //freopen("input.txt", "r", stdin); 
    //freopen("output.txt", "w", stdout);
    
    int n;
    cin >> n;
    string s;
    cin >> s;

     string now = "000";
     int ans = 0;
     for (int i=0; i<=9; i++){
         for (int j=0; j<=9; j++){
             for (int k=0; k<=9; k++){
                 now[0] = '0' + i;
                 now[1] = '0' + j;
                 now[2] = '0' + k;
                 ans += isPresent(s, now);
             }
         }
     }   

     cout << ans << endl;

    return 0;
}

Measured memory_bytes: 384
// ProblemURL : https://atcoder.jp/contests/abc080/tasks/abc080_b
// ---------------------------------------------
package main

import "fmt"

func sumDigits(n int) int {
	res := 0
	for n != 0 {
		res += n % 10
		n /= 10
	}
	return res
}

func main() {
	var n int
	fmt.Scan(&n)

	if n%sumDigits(n) == 0 { // n is HarshadNumber?
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


Measured memory_bytes: 512
import java.util.*;
public class Main {
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        boolean check = false;
        for(int i=1;i<=1009;i++){
            if(Math.floor(i*0.08)==A&&Math.floor(i*0.1)==B){
                System.out.println(i);
                check = true;
                break;
            }
        }
        if(!check) System.out.println(-1);
    }
}

Measured memory_bytes: 21972
#include<iostream>
#include <stdio.h>
#include<string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdint>
#include <queue> 
#include <map>
#define MOD 1000000007
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n)  FOR(i,0,n)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
typedef long long ll;
struct Point{
  int x,y;
};

  queue<Point> p;
  char b[55][55];              //サイズは適宜変更する
  int  dist[55][55];           //!! distは-1で初期化 !!
  
// !! 不定長の配列は1次元のみ．　多次元では最上位以外に定数をいれる  !!//
// 訪問地点のキュー, スタートのx,y座標, 最短距離, 迷路 //
void BFS(queue<Point> p,int sx, int sy, int dist[][55], char b[][55]){

  int dx[4] = { 1,0,-1,0 };
  int dy[4] = { 0,1,0,-1 };
  
  p.push({sx,sy});                // スタート地点
  dist[sy][sx] = 0;               // スタートの距離は０
  
    while (!p.empty()) {               // 訪問可能な点がなくなるまで
		Point now = p.front();       //　ポップされる点を保持して４近傍の未訪問の点を探す
		p.pop();
 
		for (int i = 0; i < 4; i++) {
			Point next = { now.x + dx[i],now.y + dy[i] };
			if (b[next.y][next.x] == '#')continue;           //壁には進めない．
			if (dist[next.y][next.x] != -1)continue;         //"-1でない = 既訪問" ならすでに最短距離であることは保証されている．
			dist[next.y][next.x] = dist[now.y][now.x] + 1;   //距離を更新，既訪問になる．
			p.push({ next.x,next.y });                       //既訪問地点を追加．
		}
	}
}



int main(){

int h,w;
cin >> h >> w;

int white = 0;

for(int i = 0; i < h; i++){
 for(int j = 0; j< w; j++){
char c;
   cin >> c;
b[i][j] = c;
if(c == '.') white++;
   dist[i][j] = -1;
  }
}
  
BFS(p,0,0,dist,b);
 
  //debug(white);
  //debug(dist[h-1][w-1]);
  
  if(dist[h-1][w-1] == -1)  cout << -1 << endl;
  else						cout << white - dist[h-1][w-1] - 1<< endl;

return 0;
}

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

class D3
{
	static void Main()
	{
		var n = int.Parse(Console.ReadLine());
		var s = Console.ReadLine();

		var d = s.ToLookup(c => c);
		var r = d.Count < 3 ? 0 : d.Select(g => g.LongCount()).Aggregate((x, y) => x * y);

		var q = from i in Enumerable.Range(0, n)
			from j in Enumerable.Range(i + 1, n - i - 1)
			where 2 * j - i < n && s[i] + s[j] + s[2 * j - i] == 'R' + 'G' + 'B'
			select 0;
		Console.WriteLine(r - q.Count());
	}
}


Measured memory_bytes: 41220
using System;

public class Hello
{
    public static void Main()
    {
        var s = Console.ReadLine().Trim();
        var ret = int.Parse(s[0].ToString()) + 9 * (s.Length - 1);
        if (s.Substring(1).Replace("9", "").Length != 0) ret -= 1;
        Console.WriteLine(ret);
    }
}


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

using namespace std ;

int s ;

signed main() {
	cin >> s ;
	s = s % 10 ;
	if( s == 0 ) printf( "pon\n" ) ;
	else if( s == 1 ) printf( "pon\n" ) ;
	else if( s == 2 ) printf( "hon\n" ) ;
	else if( s == 3 ) printf( "bon\n" ) ;
	else if( s == 4 ) printf( "hon\n" ) ;
	else if( s == 5 ) printf( "hon\n" ) ;
	else if( s == 6 ) printf( "pon\n" ) ;
	else if( s == 7 ) printf( "hon\n" ) ;
	else if( s == 8 ) printf( "pon\n" ) ;
	else if( s == 9 ) printf( "hon\n" ) ;
	return 0 ;
} 

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


using namespace std;

int main(){

	string S,temp;
	int x[4]={0};
	



	cin >> S;

	for(int i=0 ; i<S.size(); i++){


	temp =	S.substr(i,1);

	if(temp == "S")x[0]=1;
	else if(temp == "W")x[2]=1;
	else if(temp == "N")x[1]=1;
	else if(temp == "E")x[3]=1;
//	cout << x <<" " << y << endl;
	}


	if(x[0]==1 &&x[1]==1&&x[2]==1&&x[3]==1)cout << "Yes" << endl;
	else if(x[0]==1 &&x[1]==1&&x[2]!=1&&x[3]!=1)cout << "Yes" << endl;
	else if(x[0]!=1 &&x[1]!=1 &&x[2]==1 &&x[3]==1)cout << "Yes" << endl;
	else cout << "No" << endl;

}

Measured memory_bytes: 256
#include<cstdio>
#define mod 1000000007
#define int long long
int x,y,fac[20010000];
int quickpow(int x,int y){
	if(y==0)return 1;
	if(y%2==0)return quickpow(x*x%mod,y/2);
	else return quickpow(x*x%mod,y/2)*x%mod;
}
int C(int n,int m){
	if(m==0||n==m)return 1;
	return fac[n]*quickpow(fac[m],mod-2)%mod*quickpow(fac[n-m],mod-2)%mod;
}
signed main(){
	scanf("%lld%lld",&x,&y);
	fac[0]=1;
	for(int i=1;i<=20000000;i++)
		fac[i]=fac[i-1]*i%mod;
	if((x+y)%3!=0)return puts("0"),0;
	if(2*y<x||2*x<y)return puts("0"),0;
	int X=(2*y-x)/3,Y=(2*x-y)/3;
	printf("%lld\n",C(X+Y,X));
}

Measured memory_bytes: 156416
/* {{{ */
use std::io::*;
use std::str::FromStr;

struct Scanner<R: Read> {
    reader: R,
}

impl<R: Read> Scanner<R> {
    fn new(r: R) -> Scanner<R> {
        Scanner { reader: r }
    }
    fn read<T: FromStr>(&mut self) -> T {
        let token = self
            .reader
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok().unwrap()
    }
}
/* }}} */

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);
    let n: usize = sc.read();
    let mut g = vec![vec![0;n];n];
    for _ in 0..n {
        let u: usize = sc.read();
        let m: usize = sc.read();
        for _ in 0..m {
            let v: usize = sc.read();
            g[u-1][v-1] = 1;
        }
    }
    for i in 0..n {
        for j in 0..n {
            print!("{}", g[i][j]);
            if j != n-1 {
                print!(" ");
            }
        }
        println!();
    }
}
/* vim:set foldmethod=marker: */



Measured memory_bytes: 5032
R,G,B = map(int,input().split())
K = int(input())
  
for i in range(K):
  if R > G:
    G*=2
  elif G > B:
    B*=2
  elif R > B:
    B*=2
  elif R == G == B:
    B *= 2
  elif R == G:
    G *= 2
  elif G == B:
    B *= 2
  else:
    pass
if R < G < B:
  print("Yes")
else:
  print("No")


Measured memory_bytes: 9184
#include<bits/stdc++.h>
using namespace std;
int zd(int x,int y)
{
	if (x%y==0) return y;
	return zd(y,x%y);
}
int main()
{
	int n,k,x,y,max1=-1,z;
	scanf("%d%d%d",&n,&k,&x);
	z=x;
	max1=x;
	for (int i=2;i<=n;i++)
	{
		scanf("%d",&y);
		z=zd(y,z);
		max1=max(max1,y);
	}
	if (k%z==0&&k<=max1) printf("POSSIBLE");
	else printf("IMPOSSIBLE");
	return 0;
}

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

ll mod = 998244353, u = 1, d[300001];

ll mypow(ll x, ll y){
  if(x <= 0 || y <= 0) return 1;
  if(y == 1) return x;
  ll z = mypow(x, y / 2) % mod;
  if(y % 2 == 1){
    return ((z * z) % mod) * x % mod;
  }
  else{
    return (z * z) % mod;
  }
}

void pc(ll l){
  for(ll i = l; i >= 1; i--){
    u = (u * i) % mod;
  }
  d[0] = 1;
  for(ll i = 1; i <= l; i++){
    d[i] = (d[i - 1] * i) % mod;
  }
}

ll choose(ll l, ll r){
  return u * mypow((d[l - r] * d[r]) % mod, mod - 2) % mod;
}

void solve(){
  ll n, m, k, x, y, z; cin >> n >> m >> k;
  if(m == 1){
    if(k == n - 1) cout << 1 << endl;
    else cout << 0 << endl;
    return;
  }
  x = (mypow(m - 1, n - 1) * m) % mod;
  ll ans = x;
  //z = mypow(m - 1, n - k - 2);
  pc(n - 1);
  for(ll i = 1; i <= k; i++){
    y = choose(n - 1, i);
    z = mypow(m - 1, n - i - 1);
    ans = (ans + (m * ((y * z) % mod) % mod)) % mod;
  }
  cout << ans << endl;
  return;
}

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


Measured memory_bytes: 5160
#include<stdio.h>

int main(){
  int N,X,n1,n2;
  scanf("%d",&N);
  X = (100*N)/108;
  n1 = X * 1.08;
  n2 = (X+1) * 1.08;
  if(N == n1){
    printf("%d\n",X);
  }else if (N == n2)
  {
    printf("%d\n",++X);
  }else
  {
    printf(":(");
  }

return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
using namespace std;
#define prev prev228
#define all(x) (x).begin(), (x).end()


typedef pair<int, int> pii;

const long long MOD = 1e9 + 7;
long long n, a, b;
long long s[500000];

long long solve(int l, int r)
{
    vector<long long> dp(r - l + 1);
    for (int i = 0; i < r - l + 1; i++)
    {
        dp[i] = 1;
        if (i == r - l || (i >= 1 && s[i + l] - s[i + l - 1] < a))
            break;
    }
    long long ss = 0;
    long long R = -1, L = 0;
    for (int i = 1; i < dp.size(); i++)
    {
        while (s[i + l] - s[l + R + 1] >= b)
        {
            ss = (ss + dp[R + 1]) % MOD;
            R++;
        }
        if (L <= R)
        {
            dp[i] = (dp[i] + ss) % MOD;
        }
        if (s[i + l] - s[i + l - 1] < a)
        {
            while (L < i - 1)
            {
                ss = (ss + MOD - dp[L]) % MOD;
                L++;
            }
        }
    }
    long long ans = 0;
    int t = 1;
    for (int i = r - l; i >= 0; i--)
    {
        ans = (ans + dp[i]) % MOD;
        if (i != r - l && s[i + l + 1] - s[i + l] < a) {t = 0; break;}
    }
    if (t) ans = (ans + 1) % MOD;
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> a >> b;
    for (int i = 0; i < n; i++) cin >> s[i];
    if (a > b) swap(a, b);
    for (int i = 1; i + 1 < n; i++) if (s[i + 1] - s[i - 1] < a) cout << 0, exit(0);
    long long ans = 1;
    int go = 0;
    for (int i = 1; i < n; i++)
    {
        if (s[i] - s[i - 1] >= b)
        {
            ans = ans * solve(go, i - 1) % MOD;
            go = i;
        }
    }
    ans = ans * solve(go, n - 1) % MOD;
    cout << ans;
}


Measured memory_bytes: 1920
while True:
    x,y,s=map(int,input().split())
    if x==y==s==0:
        break
    ans=0
    for p1 in range(1,s+1):
        for p2 in range(p1,s+1):
            if (p1+p2+(p1*x)//100+(p2*x)//100)==s:
                t=p1+p2+(p1*y)//100+(p2*y)//100
                if t>ans:
                    ans=t
    print(ans)


Measured memory_bytes: 5596
#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
using ll = long long;
//#define int ll
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
signed main(){
    int n; cin >> n;
    vector<int>v(n);
    rep(i,n) cin >> v[i];
    sort(all(v));
    int ans = 0;
    int dif = INF;
    int key = v.back()/2+(v.back()%2);
    rep(i,n){
        if(dif>abs(v[i]-key)){
            dif = abs(v[i]-key);
            ans = v[i];
        }
    }
    cout << v.back()<<" " << ans << endl;
}


Measured memory_bytes: 640
import java.util.*;
class Main{
    String[][] puzzle;
    String[][] field; 
    boolean[] used;
    void run(){
	Scanner sc=new Scanner(System.in);
	int n=sc.nextInt();
	for(int attend=0;attend<n;attend++){
	    field =new String[3][3];
	    puzzle=new String[9][4];
	    used=new boolean[9];
	    for(int i=0;i<9;i++){
		setPuzzle(i,sc.next());
	    }
	    System.out.println(dfs(0));
	}
    }
    void setPuzzle(int num,String pu){
	for(int i=0;i<4;i++){
	    puzzle[num][i]=pu.substring(i,pu.length())+pu.substring(0,i);
	}
    }
    int dfs(int step){
	if(step==9) return 1;
	int ans=0;
	for(int i=0;i<9;i++){
	    if(used[i]==true)continue;
	    for(int j=0;j<4;j++){
		if(step>2){
		    if(!puzzleCompare(field[step/3-1][step%3].charAt(2),puzzle[i][j].charAt(0)))continue;
		}
		if(step%3!=0){
		    if(!puzzleCompare(field[step/3][step%3-1].charAt(3),puzzle[i][j].charAt(1)))continue;
		}
		field[step/3][step%3]=puzzle[i][j];
		used[i]=true;
		ans+=dfs(step+1);
		used[i]=false;
	    }
	}
	return ans;
    }
    boolean puzzleCompare(char puz1,char puz2){
	if(puz1-'a'+'A'==puz2)return true;
	else if(puz2-'a'+'A'==puz1)return true;
	else return false;
    }
    public static void main(String[] args){
	new Main().run();
    }
}

Measured memory_bytes: 12000
#include<stdio.h>
int main()
{
    int x,y;
    int turu;
    // 整数の入力
    scanf("%d %d", &x, &y);
    if(x*4 == y)
    {
        //x匹全て亀だったとき
        printf("Yes");
    }
    else
    {
        //鶴が一匹以上いる
        for (turu = 1; turu <= x; turu++)
        {
            if (turu * 2 + (x-turu) * 4 == y)
            {
                printf("Yes");
                break;
            }
        }
        if (turu > x)
        //条件を満たさなかった
        {
            printf("No");
        }
    }
    return 0;
}

Measured memory_bytes: 2152
#include<cmath>
#include<cstdio>
#include<vector>

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

using namespace std;

const double EPS=1e-8;

template<class T>
struct point{
	T x,y;
	point operator+(const point &a)const{ return (point){x+a.x,y+a.y}; }
	point operator-(const point &a)const{ return (point){x-a.x,y-a.y}; }
};

template<class T>
point<T> operator*(T c,const point<T> &a){ return (point<T>){c*a.x,c*a.y}; }

template<class T>
T cross(const point<T> &a,const point<T> &b){ return a.x*b.y-a.y*b.x; }

template<class T>
struct line{ point<T> a,b; };

template<class T>
struct polygon:vector< point<T> >{
	polygon(){}
	polygon(int n):vector< point<T> >(n){}
};

enum {CCW=1,CW=-1,ON=0};
int ccw(const point<double> &a,const point<double> &b,const point<double> &c){
	double rdir=cross(b-a,c-a);
	if(rdir> EPS) return CCW;
	if(rdir<-EPS) return CW;
	return ON;
}

point<double> get_intersect(const line<double> &L1,const line<double> &L2){
	double a1=cross(L1.b-L1.a,L2.b-L2.a);
	double a2=cross(L1.b-L1.a,L1.b-L2.a);
	if(abs(a1)<EPS) return L1.a;
	return L2.a+a2/a1*(L2.b-L2.a);
}

template<class T>
polygon<double> convex_cut(const polygon<T> &G,const line<T> &L){
	int n=G.size();
	polygon<double> H;
	rep(i,n){
		int d1=ccw(L.a,L.b,G[i]);
		int d2=ccw(L.a,L.b,G[(i+1)%n]);
		if(d1!=CW) H.push_back(G[i]);
		if(d1==CCW && d2==CW || d1==CW && d2==CCW){
			H.push_back(get_intersect(L,(line<T>){G[i],G[(i+1)%n]}));
		}
	}
	return H;
}

template<class T>
T area2(const polygon<T> &G){
	int n=G.size();
	T a=0;
	rep(i,n) a+=cross(G[i],G[(i+1)%n]);
	return abs(a);
}

template<class T>
line<double> perp_bisector(const point<T> &a,const point<T> &b){
	return (line<double>){(point<double>){(a.x-a.y+b.x+b.y)/2.0,(a.y+a.x+b.y-b.x)/2.0},
						  (point<double>){(a.x+a.y+b.x-b.y)/2.0,(a.y-a.x+b.y+b.x)/2.0}};
}

int main(){
	for(int T=1;;T++){
		point<int> a,b,c;
		scanf("%d%d%d%d%d%d",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
		if(a.x==0 && a.y==0 && b.x==0 && b.y==0 && c.x==0 && c.y==0) break;

		polygon<double> G(4);
		G[0]=(point<double>){ 0 , 0 };
		G[1]=(point<double>){ 0 ,1e4};
		G[2]=(point<double>){1e4,1e4};
		G[3]=(point<double>){1e4, 0 };

		G=convex_cut(G,perp_bisector(a,b));
		G=convex_cut(G,perp_bisector(a,c));

		printf("%d %.9f\n",T,area2(G)/2/1e8);
	}

	return 0;
}

Measured memory_bytes: 0
n = int(input())
s = input()

sum_e=0
for tmp in s:
    if tmp=="E":
        sum_e+=1

ans=sum_e
for tmp in s:
    if tmp=="E":
        sum_e-=1
    else:
        sum_e+=1
    if ans > sum_e:
        ans=sum_e

print(ans)

Measured memory_bytes: 42080
#include <bits/stdc++.h> 
#define rep(i,n) for(int i=0; i<(int)(n); i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
int main(){
	int N, T;
	cin >> N >> T;
	vector<P> AB;
	rep(i,N){
		int A, B;
		cin >> A >> B;
		AB.push_back(make_pair(A,B));
	}
	sort(AB.begin(),AB.end());
	int Amax=AB[N-1].first;
	int dp[3010][6010];
    rep(i,N){
      rep(j,T+Amax) dp[i][j]=0;
    }
	rep(i,N){
		rep(j,T+Amax){
			if(j<AB[i].first||j-AB[i].first>=T) dp[i+1][j]=dp[i][j];
			else dp[i+1][j]=max(dp[i][j],dp[i][j-AB[i].first]+AB[i].second);
		}
	}
	int ans=0;
	for(int i=T; i<T+Amax; i++){
		ans=max(ans,dp[N][i]);
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 70784
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100000;
int n,q;
ll e;
vector<pair<int,int> > E[2];
int p[N],sz[N];
int find(int u){
	if(p[u]==u)
		return u;
	return p[u]=find(p[u]);
}
int main() {
	cin>>n>>e>>q;
	for(int i=0;i<n;++i){
		p[i]=i;
		sz[i]=1;
	}
	vector<bool> seen(n);
	for(int i=0;i<q;++i){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		E[c].push_back({a,b});
		if(c==0){
			seen[a]=seen[b]=true;
			a=find(a);
			b=find(b);
			if(a!=b){
				p[a]=b;
				sz[b]+=sz[a];
			}
		}
	}
	for(auto &e:E[1])
		if(find(e.first)==find(e.second)){
			puts("No");
			return 0;
		}
	ll need=0;
	int last=-1;
	int comp=0;
	for(int i=0;i<n;++i)
		if(p[i]==i){
			if(sz[i]==1){
				if(last==-1)
					last=i;
				int u=find(last);
				int v=find(i);
				if(u!=v){
					p[u]=v;
					sz[v]+=sz[u];
				}
			}else{
				need+=sz[i]-1;
				++comp;
			}
		}
	int s=0;
	if(last!=-1)
		s=sz[find(last)];
	bool res=false;
	comp+=s;
	if(need+comp-E[1].empty()<=e && e<=need+(ll)comp*(comp-1)/2)
		res=true;
	if(res)
		puts("Yes");
	else
		puts("No");
	return 0;
}

Measured memory_bytes: 2172
n,m=map(int,input().split())
mod=pow(10,9)+7
# n<=m
# コンビネーション、さらに高速。あらかじめO(N)の計算をすることでのちの計算が早くなる
def cmb(n,r,mod):
  if (r<0 or r>n):
    return 0
  r=min(r,n-r)
  return g1[n]*g2[r]*g2[n-r]%mod
g1=[1,1] # g1[i]=i! % mod
g2=[1,1] # g2[i]=(i!)^(-1) % mod
inverse=[0,1]
for i in range(2,m+1):
  g1.append((g1[-1]*i)%mod)
  inverse.append((-inverse[mod%i]*(mod//i))%mod)
  g2.append((g2[-1]*inverse[-1])%mod)

# 数列aの作り方
ans_a=g1[m]*g2[m-n]

# 数列bの作り方
ans_b=0
for i in range(n+1):
  tmp=cmb(n,i,mod)
  tmp*=(g1[m-i]*g2[m-n])%mod
  tmp%=mod
  ans_b+=tmp*(-1)**i
  ans_b%=mod
  #print(i,tmp,ans_b)

print((ans_a*ans_b)%mod)
#print(ans_a,ans_b)



Measured memory_bytes: 68340
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
int INF = (int)1 << 62;
int getLIS(vector<int> &a)
{
	int N = a.size();
	vector<int> dp(N, INF);
	for (int i = 0; i < N; ++i) {
		*upper_bound(dp.begin(), dp.begin() + N, a[i]) = a[i];
	}
	return lower_bound(dp.begin(), dp.begin() + N, INF) - dp.begin();
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> A(N);
	int res = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	reverse(A.begin(), A.end());
	cout << getLIS(A) << endl;
}


Measured memory_bytes: 1792
from math import factorial

n=int(input())
p=[int(i)-1 for i in input().split()]
q=[int(i)-1 for i in input().split()]

plst=[i for i in range(n)]
qlst=[i for i in range(n)]
pls=[]
qls=[]

for i in p:
    a=plst.index(i)
    pls.append(a)
    plst.remove(i)
for i in q:
    a=qlst.index(i)
    qls.append(a)
    qlst.remove(i)

ansp=1
ansq=1

for i in range(n):
    ansp+=pls[i]*(factorial(n-i-1))
for i in range(n):
    ansq+=qls[i]*(factorial(n-i-1))
    
print(abs(ansp-ansq))

Measured memory_bytes: 9096
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x<<1)
#define w2 second
#define rs (x<<1|1)
#define mp make_pair
#define pb push_back
#define mid ((l+r)>>1)
#define SZ(x) (int((x).size()))
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)])
#define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)])
template<class T>void read(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
const int maxn=3e3+5;
int n,tot;
int head[maxn],des[maxn<<1],nxt[maxn<<1];
int win[maxn],a[maxn];
void adde(int x,int y){
	des[++tot]=y;nxt[tot]=head[x];head[x]=tot;
}
void dfs(int x,int fa){
	win[x]=0;
	Rep(p,x)if(des[p]!=fa&&a[des[p]]<a[x]){
		dfs(des[p],x);
		if(!win[des[p]])win[x]=1;
	}
}
int main(){
	read(n);
	rep(i,1,n)read(a[i]);
	rep(i,1,n-1){
		int x,y;read(x);read(y);
		adde(x,y);adde(y,x);
	}
	rep(i,1,n){
		dfs(i,0);
		if(win[i])printf("%d ",i);
	}
	return 0;
}

Measured memory_bytes: 384
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

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

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

/*
 * ref. [Union-Find](https://www.slideshare.net/chokudai/union-find-49066733)
 * ref. [AtCoder ABC 157 - AtCoder Live](https://www.youtube.com/watch?v=TdR816rqc3s)
 */
class UnionFind {
    public:
        UnionFind(int n = 0): d(n, -1) {}

        // 0-index, [0, n) in other words
        // Time complexity: O(a(n)) (a = inverse Achkermann function)
        int find(int x) {
            if (d[x] < 0) return x;
            return d[x] = find(d[x]);
        }

        // Return false if x and y are alreday in the same group, otherwise return true.
        // 0-index, [0, n) in other words
        // Time complexity: O(a(n)) (a = inverse Achkermann function)
        bool unite(int x, int y) {
            x = find(x);
            y = find(y);
            if (x == y) return false;
            if (d[x] > d[y]) swap(x, y);
            d[x] += d[y];
            d[y] = x;
            return true;
        }

        // 0-index, [0, n) in other words
        // Time complexity: O(a(n)) (a = inverse Achkermann function)
        bool same(int x, int y) {
            return find(x) == find(y);
        }

        // 0-index, [0, n) in other words
        // Time complexity: O(1)
        int size(int x) {
            return -d[find(x)];
        }

    private:
        vector<int> d;
};

vector<int> ps;

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

    ps.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> ps[i];
        ps[i]--;
    }

    UnionFind uf(N);

    for (int i = 0; i < M; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        uf.unite(x, y);
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        if (uf.same(i, ps[i])) {
            ans++;
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>
#include <bitset>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

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

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

ll op(ll a, ll b) { return max(a, b);}
ll e() {return 0ll;}

void solve() {
    ll n, k;
    cin >> n >> k;
    const int range = 300000;
    vll ini(range + 5, 0);
    segtree<ll, op, e> seg(ini);
    ll ans = 0;
    rep(i, n) {
        ll a;
        cin >> a;
        ll l = a - k;
        if(l < 0) l = 0;
        ll r = a + k;
        if(r > range) r = range;
        ll _ans = seg.prod(l, r + 1);
        chmax(ans, _ans + 1);
        seg.set(a, _ans + 1);
    }
    cout << ans << endl;
    return;
}

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

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

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

ll N, A[2][100];

int main(){
    cin >> N;
    for(int i=0;i<2;i++){
        for(int j=0;j<N;j++)    cin >> A[i][j];
    }

    ll ans = 0;
    for(int i=0;i<N;i++){
        ll res = 0;
        for(int j=0;j<=i;j++){
            res += A[0][j];
        }
        for(int j=i;j<N;j++){
            res += A[1][j];
        }
        ans = max(ans, res);
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
n=int(input())
a=list(map(int,input().split()))
x=1
flag=0
for i in range(len(a)):
    x*=a[i]
    if x>10**18:
        flag=1
        break
if 0 in a:
    flag=2
if flag==0:
    print(x)
elif flag==1:
    print(-1)
else:
    print(0)

Measured memory_bytes: 21704
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
typedef long long ll;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

ll d[33][33]; //d[i][j]:色iを色jに置き換えたときの違和感
ll grid[510][510];

void solve(){

    ll n,c;
    cin >> n >> c;
    rep(i,c){
        rep(j,c){
            cin >> d[i][j];
        }
    }
    rep(i,n){
        rep(j,n){
            cin >> grid[i][j];
            grid[i][j]--;
        }
    }

    ll x[4][33];

    rep(i,4){
        rep(j,33){
            x[i][j]=0;
        }
    }

    rep(i,c){
        rep(a,n){
            rep(b,n){
                if((a+b)%3==0) x[0][i] += d[grid[a][b]][i];
                else if((a+b)%3==1) x[1][i] += d[grid[a][b]][i];
                else x[2][i] += d[grid[a][b]][i];
            }
        }
    }

    ll ans = INF; 
    rep(i,c){
        rep(j,c){
            rep(k,c){
                //座標の和が0:i, 1:j, 2:k
                if(i==j||j==k||i==k) continue;
                ll tmp = 0;
                tmp += x[0][i];
                tmp += x[1][j];
                tmp += x[2][k];
                ans = min(ans, tmp);
            }
        }
    }

    cout << ans << endl;

}

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

Measured memory_bytes: 2304
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int ans,n,k,r,s,p;
string ss;
int vis[max_n];
int main()
{
	cin>>n>>k;
	cin>>r>>s>>p;
	cin>>ss;
    for(int i=0;i<n;i++)
	{
		if(i-k>=0&&vis[i-k]&&ss[i]==ss[i-k]) continue;
		if(ss[i]=='r')ans+=p;
		if(ss[i]=='s')ans+=r;
		if(ss[i]=='p')ans+=s;
		vis[i]=1;
    }
    cout<<ans;
}

Measured memory_bytes: 896
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
H = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

# BIT で、各高さに対して、（最後がその高さの場合の最大値）を管理
def BIT_update(tree,h,x):
    while h <= N and tree[h] < x:
        tree[h] = x
        h += h&(-h)

def BIT_max(tree,h):
    x = 0
    while h:
        y = tree[h]
        x = x if x > y else y
        h -= h&(-h)
    return x

tree = [0] * (N+1)
for h,a in zip(H,A):
    x = BIT_max(tree,h)
    BIT_update(tree,h,x+a)

answer = max(tree)
print(answer)

Measured memory_bytes: 34308
#include<iostream>
#include<climits>
#include<math.h>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include <string>
#include <complex>
#include <functional>
#include <map>
using namespace std;
using ll = long long;
//typedef pair<string,string> P;
//double dat[100][100];
//int dp[6][1010];//動的計画法
//int prime[100001];
//char str[1010][1010];
//vector<pair<int,int> > pc[100001];
//int ABS(int a){return max(a,-a);}
int main(){
	int n,t,ans=0;
	double a,tmp=700,h;
	cin>>n>>t>>a;
	for(int i=1;i<=n;i++){
		cin>>h;
		if(abs(a-tmp)>abs(a-(t-h*0.006))){
			tmp=(t-h*0.006);
			ans=i;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    print(n*(n-1)>>1)
    return

#Solve
if __name__ == "__main__":
    solve()


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

typedef long long ll;
typedef long double ld;
const ll M=1e9+7;
const ll MAXN=1e6+7;
const ld pi=acos(-1);

#define gi(n) ll n=read()
#define endl '\n'
#define rep(i,a,b) for(ll i=a; i<b; ++i)
#define per(i,a,b) for(ll i=b-1; i>=a; --i)

typedef vector<ll> V;
typedef vector<V> VV;
typedef set<ll> S;
typedef pair<ll,ll> pl;
typedef vector<pl> Vp;

#define all(v) v.begin(),v.end()
#define pb push_back
#define fs first
#define sc second

inline ll read(){
	ll x=0,ff=1;
	char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') ff=-1;ch=getchar();}
	while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*ff;
}
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll pow1(ll p,ll d, ll P){return d?(pow1(p*p%P,d>>1,P)*(d&1?p:1))%P:1;}

V prime;
bool is_composite[MAXN];
void sieve (ll n){
	std::fill (is_composite, is_composite + n, false);
	rep(i,2,n){
		if(!is_composite[i]) prime.pb(i);
		for(ll j = 0; j < prime.size() && i*prime[j] < n; ++j){
			is_composite[i*prime[j]] = true;
			if(i % prime[j] == 0) break;
		}
	}
}
ll modInverse(ll a, ll m){
	ll m0=m,y=0,x=1;
	if(m==1) return 0;
	while(a>1){
		ll q=a/m,t=m;
		m=a%m;
		a=t; 
		t=y;

		y = x-q*y; 
		x = t;
	}
	if(x<0) x += m0;
	return x; 
}

void doit(){
	gi(a);
	string s;
	for(;a;){
		if(a%26) s=(char)(a%26+96)+s, a=a/26;
		else s='z'+s, a=a/26-1;
     // cout<<s<<' ';
    }
	cout<<s;
}
int main(){
//	for(gi(t);t--;cout<<endl)
		doit();
	return 0;
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

int main(){
	int n;
	cin >> n;
	int flag = 0;
	for (int i = 0;i <n;i++){
		char l;
		cin >> l;
		if (l == 'Y'){
			flag = 1;
		}
	}
	if (flag ==1){
		cout << "Four" << endl;
	}
	else{
		cout << "Three" << endl;
	}
	return 0;
}

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

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

  std::vector<std::pair<int, int>> r(n);
  std::vector<std::pair<int, int>> b(n);

  for (auto &i : r) {
    std::cin >> i.second >> i.first;
  }

  for (auto &i : b) {
    std::cin >> i.first >> i.second;
  }

  std::sort(r.begin(), r.end());
  std::sort(b.begin(), b.end());

  int count = 0;

  for (auto &i : b) {
    std::vector<decltype(r)::iterator> itr_v;
    for (auto itr = r.begin(); itr != r.end(); itr++) {
      if (itr->first < i.second && itr->second < i.first) {
        itr_v.push_back(itr);
      }
    }

    if (!itr_v.empty()) {
      count++;
      r.erase(itr_v.back());
    }
  }

  std::cout << count << std::endl;
  return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <memory.h>
#include <iomanip>
#include <bitset>
#include <list>
#include <stack>

using namespace std;

#define mod 1000000007

int main()
{
	int n;
	cin >> n;
	int maxa = 0;
	map<int, int> cnt;
	for(int i = 1; i < n; i++){
		cnt[i] = 0;
	}
	for(int i = 0; i < n; i++){
		int a;
		cin >> a;
		maxa = max(maxa, a);
		cnt[a]++;
	}
	for(int i = 1; i <= maxa; i++){
		if(i < (maxa + 1) / 2){
			if(cnt[i] > 0){
				cout << "Impossible" << endl;
				return 0;
			}
		} else if(i == (maxa + 1) / 2){
			if((maxa % 2 == 0 && cnt[i] != 1) || (maxa % 2 == 1 && cnt[i] != 2)){
				cout << "Impossible" << endl;
				return 0;
			}
		} else {
			if(cnt[i] < 2){
				cout << "Impossible" << endl;
				return 0;
			}
		}
	}
	cout << "Possible" << endl;
	return 0;
}

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

namespace CSharpSample01
{


	class Program
	{
		static void Main(string[] args)
		{
			var inputNum = Console.ReadLine().Split(' ').Select(int.Parse).ToList().Distinct();
			if(inputNum.Count() == 2)
			{
				Console.WriteLine("Yes");
			}
			else
			{
				Console.WriteLine("No");
			}

		}


	}
}

Measured memory_bytes: 13396
import heapq
n=int(input())
inf=10000000000000
ans=[[-1,-1] for i in range(n)]
ans[-1][1],ans[0][0]=0,0
t=list(map(int,input().split()))
v=list(map(int,input().split()))
v2=[]
for i in range(n):
    heapq.heappush(v2,(v[i],i))

def next(y,wh):
    global ans,n
    ansi=inf
    if wh:
        r1,r2=range(y[1]-1,-1,-1),range(y[1],n)
    else:
        r1,r2=range(y[1],-1,-1),range(y[1]+1,n)
    now=0
    for j in r1:
        if ans[j][1]!=-1:
            ansi=min(ansi,ans[j][1]+now)
        now+=t[j]
        if ans[j][0]!=-1:
            ansi=min(ansi,ans[j][0]+now)
    now=0
    for j in r2:
        if ans[j][0]!=-1:
            ansi=min(ansi,ans[j][0]+now)
        now+=t[j]
        if ans[j][1]!=-1:
            ansi=min(ansi,ans[j][1]+now)  
    if wh:
        ans[y[1]][0]=min(ansi,y[0])
        ans[y[1]-1][1]=ans[y[1]][0]
    else:
        ans[y[1]][1]=min(ansi,y[0])
        ans[y[1]+1][0]=ans[y[1]][1]  

for i in range(n):
    y=heapq.heappop(v2)
    if ans[y[1]][0]==-1:next(y,True)
    if ans[y[1]][1]==-1:next(y,False)
answer=0
for i in range(n):
    h=min((t[i]+sum(ans[i]))/2,v[i])
    answer+=((h**2-ans[i][0]**2)/2)
    answer+=((h**2-ans[i][1]**2)/2)
    answer+=(h*(t[i]+sum(ans[i])-2*h))
print(answer)

Measured memory_bytes: 3192