akhauriyash/Code-Regression huggingface dataset:
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
//#include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)

priority_queue<LP,vector<LP>,greater<LP> > q;
set<ll> s;

ll N;
ll A[200000];
ll rec(){
	if(q.empty()) return 0;
	LP p=q.top();
	q.pop();
	ll l,r;
	auto ite=s.upper_bound(p.sc);
	r=*ite;
	ite--;
	l=*ite+1;
	s.insert(p.sc);
	//cout<<p.fr<<" "<<l<<" "<<r<<endl;
	return rec()+p.fr*(p.sc-l+1)*(r-p.sc);
}

int main()
{
	cin>>N;
	rep(i,N){
		cin>>A[i];
		q.push(P(A[i],i));
	}
	s.insert(-1);
	s.insert(N);
	cout<<rec()<<endl;
}

Measured memory_bytes: 24556
n, k = map(int, input().split())
a = list(map(int, input().split()))
sum_a = sum(a)
ans = 0
def try_ans(target):
    remain = [i % target for i in a]
    remain.sort()
    count = 10**10
    #sum_remainは全てをマイナス側に落とし込むときの操作回数
    sum_remain = sum(remain)
    #plus_remainはプラス側に落とし込むときの操作回数
    plus_remain = 0
    for i in range(n-1,-1,-1):
        plus_remain += target - remain[i]
        sum_remain -= remain[i]
        count = min(count, max(sum_remain,plus_remain))
    return count <= k

for i in range(1,int(sum_a**0.5)+1):
    if sum_a % i == 0:
        if try_ans(i):
            ans = max(ans, i)
        if try_ans(sum_a // i):
            ans = max(ans, sum_a//i)
print(ans)


Measured memory_bytes: 42536
import java.util.*;

public class Main {

  public static void main (String[] args) {

    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] C = new int[N];
    int[] S = new int[N];
    int[] F = new int[N];

    for (int i = 0; i < N-1; i++) {
      C[i] = sc.nextInt();
      S[i] = sc.nextInt();
      F[i] = sc.nextInt();
    }

    sc.close();

    for (int i = 0; i < N; i++) {
      
      int t = 0;

      for (int j = i; j < N -1; j++) {
        if (t < S[j]) {
          t = S[j];
        } else if (t % F[j] != 0) {
          t = t + F[j] - t % F[j];
        }

        t += C[j];
      }

      System.out.println(t);
    }
  
  }

}

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


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

	vector<string> str(N);
	for (int i = 0; i < N; i++) {
		cin >> str.at(i);
	}
	string P[4] = {"AC", "WA", "TLE", "RE" };
	int Q[4] = {};
	for (int i = 0; i < N; i++) {
		if (str.at(i) == P[0]) {
			Q[0]++;
		}else 	if (str.at(i) == P[1]) {
			Q[1]++;
		}else 	if (str.at(i) == P[2]) {
			Q[2]++;
		}
		else {
			Q[3]++;
		}
	}
	cout<<"AC x "<<Q[0]<<endl;

	cout << "WA x " << Q[1] << endl;

	cout << "TLE x " << Q[2] << endl;

	cout << "RE x " << Q[3] << endl;
	return 0;
}

Measured memory_bytes: 6424
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func init() {
	const maxWord = (1 << 30)
	buf := []byte{}
	sc.Buffer(buf, maxWord)
	sc.Split(bufio.ScanWords)
}

func input() string {
	sc.Scan()
	return sc.Text()
}

func gcd(x, y int) int {
	if x < y {
		x, y = y, x
	}
	for y > 0 {
		r := x % y
		x = y
		y = r
	}
	return x
}

func lcm(x, y int) int {
	return x * y / gcd(x, y)
}

func countNotMod(a, x, y int) int {
	c1 := a / x
	c2 := a / y
	c3 := a / lcm(x, y)
	return a - (c1 + c2 - c3)
}

func main() {
	a, _ := strconv.Atoi(input())
	b, _ := strconv.Atoi(input())
	c, _ := strconv.Atoi(input())
	d, _ := strconv.Atoi(input())

	ans := countNotMod(b, c, d) - countNotMod(a-1, c, d)
	fmt.Println(ans)
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define M (998244353)
#define N (200010)
struct data{
	int x,y;
}a[N];
int n,ans,pow2[N]={1};
struct tree{
	int a[N];
	void clear(){memset(a,0,sizeof(a));}
	void update(int x){
		for (int i=x;i<=n;i+=i&-i) ++a[i];
	}
	int query(int x){
		int res=0;
		for (int i=x;i;i-=i&-i) res+=a[i];
		return res;
	}
}tr;

bool cmpx(data o1,data o2){return o1.x<o2.x;}

bool cmpy(data o1,data o2){return o1.y<o2.y;}

int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
	for (int i=1;i<=n+1;i++) pow2[i]=pow2[i-1]*2%M;
	ans=1ll*n*(pow2[n]-1)%M;//假设每个点都出现在所有集合中 
	sort(a+1,a+1+n,cmpx);
	for (int i=1;i<=n;i++){//减去所有构成元素的x都比a[i].x小/大的集合 
		ans=(ans-(pow2[i-1]-1)+M)%M;//<x
		ans=(ans-(pow2[n-i]-1)+M)%M;//>x
		a[i].x=i;
	} 
	sort(a+1,a+1+n,cmpy);
	for (int i=1;i<=n;i++){//减去所有构成元素的y都比a[i].y小/大的集合 
		ans=(ans-(pow2[i-1]-1)+M)%M;//<y
		ans=(ans-(pow2[n-i]-1)+M)%M;//>y
		a[i].y=i;
	}
	for (int i=1;i<=n;i++){
		int t=tr.query(a[i].x-1);
		ans=(ans+(pow2[t]-1))%M;//<x <y
		ans=(ans+(pow2[i-1-t]-1))%M;//>x <y
		tr.update(a[i].x);
	} 
	tr.clear();
	for (int i=n;i>=1;i--){
		int t=tr.query(a[i].x);
		ans=(ans+(pow2[n-i-t]-1))%M;//>x >y
		ans=(ans+(pow2[t]-1))%M;//<x >y
		tr.update(a[i].x);
	}
	printf("%d",(ans+M)%M);
}
//容斥 

Measured memory_bytes: 3328
# -*- coding: utf-8 -*-

s = input()

def check(txt):
    if len(txt)%2==1:
        return False
    else:
        half = len(txt)//2
#        print(txt[:half])
#        print(txt[half:])
        
        if txt[:half] == txt[half:]:
            return True
        else:
            return False

c = len(s)
while True:
    c -= 1
    txt = s[:c]
#    print(txt)

    if check(txt):
        print(len(txt))
        break

    


Measured memory_bytes: 2940
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
//deque/functional/iterator/list/map/memory/numeric/queue/set/stack/utility/vector
//#include<bits/stdc++.h>
#define ll long long
using namespace std;
string a,b,c;
int aa=0,bb=0,cc=0;
void va();
void vb();
void vc();

void va(){
	aa++;
	if(aa==a.length()+1){
		cout<<"A"<<endl;
		return;
	}
	else{
		if(a[aa-1]=='a') va();
		else if(a[aa-1]=='b') vb();
		else if(a[aa-1]=='c') vc();
	}
}
void vb(){
	bb++;
	if(bb==b.length()+1){
		cout<<"B"<<endl;
		return;
	}
	else{
		if(b[bb-1]=='a') va();
		else if(b[bb-1]=='b') vb();
		else if(b[bb-1]=='c') vc();
	}
}
void vc(){
	cc++;
	if(cc==c.length()+1){
		cout<<"C"<<endl;
		return;
	}
	else{
		if(c[cc-1]=='a') va();
		else if(c[cc-1]=='b') vb();
		else if(c[cc-1]=='c') vc();
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>a>>b>>c;
	va();
return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;

int main() {
    string s;
    cin >> s;
    set<char> se;
    for (auto c : s) {
        se.insert(c);
    }
    int ans = INF;
    for (auto c : se) {
        string now = s;
        int cnt = 0;
        while (true) {
            bool flag = true;
            for (int i = 0; i < (int)now.size() - 1; i++) {
                if (now[i] != now[i + 1]) flag = false;
            }
            if (flag) {
                break;
            }
            string next;
            for (int i = 0; i < (int)now.size() - 1; i++) {
                if (now[i] == c || now[i + 1] == c) {
                    next.push_back(c);
                } else {
                    next.push_back(now[i]);
                }
            }
            cnt++;
            now = next;
        }
        chmin(ans, cnt);
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template<typename T>
using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define ull unsigned long long
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define inf 1000000000000000000
#define iinf -1000000000000000000
#define __   ios_base::sync_with_stdio(0);cin.tie();cout.tie();
#define mod 1000000007
#define base 1949313259
#define en '\n'

ll Set(ll N,ll pos){ return N=N | (1LL<<pos); }
ll reset(ll N,ll pos){ return N= N & ~(1LL<<pos); }
bool check(ll N,ll pos){ return (bool)(N & (1LL<<pos)); }

ll ar[]={0,0,1,-1};
ll br[]={1,-1,0,0};

ll bigmod(ll n,ll pow){
if(pow==0) return 1;
if(pow==1) return n%mod;

ll ans=bigmod(n,pow/2);
ans=(ans*ans)%mod;

if(pow%2==1){ans=(ans*n)%mod;}
return ans%mod;
}

ll fact[1000005];

ll nCr(ll n,ll r){

ll ans=fact[n];
ans=(ans*bigmod(fact[r],mod-2))%mod;
ans=(ans*bigmod(fact[n-r],mod-2))%mod;
return ans;
}

string s,s1,s2;
ll n,m;
ll arr[500010];
ll brr[500010];
vector<ll>v,v1;

map<ll,ll>mp;
ll vis[500005];

int main()
{__
        ll i,j,a,b,c,d,e,f,g,x,y,z,t,k,l,r;
        fact[0]=1;

      //  for(i=1;i<=1000000;i++) fact[i]=(fact[i-1]*i)%mod;
        ll ans=0,sum=0,temp;
        cin>>n;
        for(i=1;i<=n;i++){
            cin>>arr[i];
        }


        for(i=0;i<=60;i++){
            a=0;
            for(j=1;j<=n;j++){
                if(check(arr[j],i)==1) a++;
            }

            if(a%2==0){
                  //  cout<<i<<" shajib "<<a<<endl;
                for(j=1;j<=n;j++){
                if(check(arr[j],i)==1) brr[j]=Set(brr[j],i);
                }
            }
            else{
                for(j=1;j<=n;j++){
                if(check(arr[j],i)==0) brr[j]=Set(brr[j],i);
                }
            }
        }

        for(i=1;i<=n;i++) cout<<brr[i]<<" ";
}




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

#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define ll long long
#define pb push_back
#define zoom                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define pl pair<ll, ll>
#define MOD 1000000007
#define endl "\n"
#define ff first
#define ss second
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl
#define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl
#define trace(v)                                   \
    for (auto it = v.begin(); it != v.end(); it++) \
        cerr << *it << " ";                        \
    cerr << endl;
#define tr(container, it) \
    for (typeof(container.begin()) it = container.begin(); it != container.end(); it++)

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vvi;

void solve()
{
    int n;
    ll w;
    cin >> n >> w;
    vector<pair<int, ll>> val(n);
    forn(i, n)
    {
        cin >> val[i].first >> val[i].second;
    }

    vector<vector<ll>> dp(n + 1, vector<ll>(w + 1, 0));
    
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= w; j++){
            if(val[i - 1].first <= j){
                dp[i][j] = val[i - 1].second + dp[i - 1][j - val[i - 1].first];
            }

            dp[i][j] = max(dp[i][j], dp[i - 1][j]);

            // cout << "dp[" << i << "][" << j <<"] = " << dp[i][j] <<endl;
        }
    }

    cout << dp[n][w] <<endl;
}

int main()
{
    zoom;
    ll t = 1;
    // cin >> t;

    while (t--)
    {
        solve();
    }
}

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

int main() {
	char a;
	vector<char>ans;
	for (int i = 0; i < 9; i++) {
		cin >> a; if (i == 0 || i == 4 || i == 8) {
			ans.push_back(a);
		}
	}
	for (int i = 0; i < 3; i++) {
		cout << ans[i];
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] array = new int[N];
		int c = 0;
      	int tmp = c;
		
		for(int i = 0; i<array.length; i++) {
			array[i] = sc.nextInt();
		}		
		for(int i = 0; i<array.length-1; i++) {
			if (array[i] >= array[i+1])
            {	
              c++;
            }
          	else
            {
              if (c > tmp)
              {
                tmp = c;
              }
              c=0;
            }
		}
      	if (c>tmp)	System.out.println(c);
      	else System.out.println(tmp);
	}
}

Measured memory_bytes: 49728
#include <iostream>
#include <string>
#include <vector>
#include <map>

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

using namespace std;

vector<string> split(string &str,char delim)
{
	vector<string> res;
	size_t current = 0,found;
	while((found = str.find_first_of(delim,current)) != string::npos)
	{
		res.push_back(string(str,current,found  - current));
		current = found + 1;
	}
	res.push_back(string(str,current,str.size() - current));
	return res;
}


int main()
{
	map<char,string> ID;
	ID[' '] = "101";
	ID['\''] = "000000";
	ID[','] = "000011";
	ID['-'] = "10010001";
	ID['.'] = "010001";
	ID['?'] = "000001";
	ID['A'] = "100101";
	ID['B'] = "10011010";
	ID['C'] = "0101";
	ID['D'] = "0001";
	ID['E'] = "110";
	ID['F'] = "01001";
	ID['G'] = "10011011";
	ID['H'] = "010000";
	ID['I'] = "0111";
	ID['J'] = "10011000";
	ID['K'] = "0110";
	ID['L'] = "00100";
	ID['M'] = "10011001";
	ID['N'] = "10011110";
	ID['O'] = "00101";
	ID['P'] = "111";
	ID['Q'] = "10011111";
	ID['R'] = "1000";
	ID['S'] = "00110";
	ID['T'] = "00111";
	ID['U'] = "10011100";
	ID['V'] = "10011101";
	ID['W'] = "000010";
	ID['X'] = "10010010";
	ID['Y'] = "10010011";
	ID['Z'] = "10010000";

	map<string,string> Id;
	Id["00000"] = "A";
	Id["00001"] = "B";
	Id["00010"] = "C";
	Id["00011"] = "D";
	Id["00100"] = "E";
	Id["00101"] = "F";
	Id["00110"] = "G";
	Id["00111"] = "H";
	Id["01000"] = "I";
	Id["01001"] = "J";
	Id["01010"] = "K";
	Id["01011"] = "L";
	Id["01100"] = "M";
	Id["01101"] = "N";
	Id["01110"] = "O";
	Id["01111"] = "P";
	Id["10000"] = "Q";
	Id["10001"] = "R";
	Id["10010"] = "S";
	Id["10011"] = "T";
	Id["10100"] = "U";
	Id["10101"] = "V";
	Id["10110"] = "W";
	Id["10111"] = "X";
	Id["11000"] = "Y";
	Id["11001"] = "Z";
	Id["11010"] = " ";
	Id["11011"] = ".";
	Id["11100"] = ",";
	Id["11101"] = "-";
	Id["11110"] = "'";
	Id["11111"] = "?";

	string s;
	string str = "";
	while(getline(cin,s))
	{
		if(cin.eof()) break;
		rep(i,s.size())
		{
			str += ID[s[i]];
		}

		while(str.length() % 5 != 0)
		{
			str += "0";
		}

		string ans = "";
		while(str.length())
		{
			string temp = str.substr(0,5);
			ans += Id[temp];
			str = str.substr(5);
		}

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

Measured memory_bytes: 1208
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mikit
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DInsertion solver = new DInsertion();
        solver.solve(1, in, out);
        out.close();
    }

    static class DInsertion {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            String s = in.next();
            int pre = 0;
            int open = 0;
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == '(') {
                    open++;
                } else {
                    if (open == 0) {
                        pre++;
                    } else {
                        open--;
                    }
                }
            }
            for (int i = 0; i < pre; i++) {
                out.print('(');
            }
            out.print(s);
            for (int i = 0; i < open; i++) {
                out.print(')');
            }
            out.println();
        }

    }
}



Measured memory_bytes: 23252
n = int(input())
arr = list(map(int, input().split()))
mod = 1000000007
pref = [arr[0]]

for i in arr[1:]:
	pref += [(i + pref[-1]) % mod]

res = 0
for i in range(n - 1):
	res = (res + (arr[i] * (pref[-1] - pref[i])) % mod) % mod
	
print(res)

Measured memory_bytes: 31384
#include<bits/stdc++.h>
#define For(i,l,r) for(int i = (l),i##end = (r);i <= i##end;i++)
#define Fordown(i,r,l) for(int i = (r),i##end = (l);i >= i##end;i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x,T y) { return y < x ? x = y,1 : 0; }
template <typename T> inline bool chkmax(T &x,T y) { return x < y ? x = y,1 : 0; }

const int INF = 0x3f3f3f3f;
const int N = 2e6 + 10;

int a[N],b[N];
int n;

inline int read() {
	int x = 0,flag = 1;
	char ch = getchar();
	while(!isdigit(ch) && ch != '-')ch = getchar();
	if(ch == '-')flag = -1,ch = getchar();
	while(isdigit(ch))x = (x << 3) + (x << 1) + (ch - '0'),ch = getchar();
	return x * flag;
}

inline bool check(int val) {
	For(i,1,n) b[i] = (a[i] >= val);
	int l = 1,r = n,mid = (l + r) / 2,Cl = b[1],Cr = b[n];
	For(i,2,n) if(b[i] == b[i - 1]) {
		if(i == mid || i == mid + 1) return b[i];
		if(i < mid && chkmax(l,i + 1)) Cl = b[i];
		if(i > mid + 1 && chkmin(r,i - 2)) Cr = b[i - 1];
	}
	if(Cl == Cr) return Cl;
	return mid - l < r - mid ? Cl : Cr;
}

int main() {
	n = (read() << 1) - 1;
	For(i,1,n) a[i] = read();
	int l = 1,r = n;
	while(l < r) {
		int mid = (l + r + 1) >> 1;
		if(check(mid)) l = mid;
		else r = mid - 1;
	}
	printf("%d\n",l);
	return 0;
}

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

typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,ll> PLL;
typedef pair<P,P> PP;
typedef vector<ll> llVEC;
typedef vector<P> PVEC;
typedef priority_queue<P,PVEC,greater<P> > PPQ;
typedef priority_queue<ll, llVEC, greater<ll> > llPQ;

#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n-1; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue;
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second

template<typename T>
ll sz(vector<T> &vec){ return (ll)vec.size(); }
ll sz(string &s){ return (ll)s.size(); }

ll gcd(ll a,ll b){ return ((!b) ?a :gcd(b, a%b)); }

ll dp[101][101][301] = {};
int main(){
    ll n,W;
    cin >> n >> W;
    ll w[n],v[n];
    
    ll wmemo = 0;
    rep(i,0,n){
        cin >> w[i] >> v[i];
        if(i == 0) wmemo = w[i];
        w[i] -= wmemo;
    }

    rep(i,1,n+1){
        rep(j,1,n+1){
            rep(k,0,301){
                dp[i][j][k] = max(dp[i-1][j][k], dp[i][j-1][k]);
                if(k-w[i-1] >= 0) dp[i][j][k] = max(dp[i][j][k],dp[i-1][j-1][k-w[i-1]]+v[i-1]);
            }
        }
    }

    ll ans = 0;
    rep(i,1,n+1){
        rep(j,1,n+1){
            rep(k,0,301){
                if(j*wmemo+k <= W) ans = max(ans,dp[i][j][k]);
            }
        }
    }
    cout << ans << endl;
    
    
    return 0;
}


Measured memory_bytes: 27340
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cstring>
#include<map>
 
 
using namespace std;
#define int long long int
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define pb push_back
#define mod 1000000007

char S[10];


signed main(){
    scanf("%s",&S);
    for(int i=1;i<3;i++){
        if(S[i]!=S[i-1]){
            printf("Yes\n");
            return 0;
        }
    }
    printf("No\n");
    return 0;
}


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

int main () {
  int H , W;
  cin >> H >> W;
  string S;
  vector<char> A(W + 2, '#');
  for (int i = 0; i < (W + 2); i++) {
    cout << A.at(i);
  }
  cout << endl;
  for (int i = 0; i < H; i++) {
    cin >> S;
    for (int j = 0; j < (W + 2); j++) {
      if (j == 0 || j == W + 1) {
      }
      else {
        A.at(j) = S.at(j - 1);
      }
    }
    for (int l = 0; l < (W + 2); l++) {
    cout << A.at(l);
    }
    cout << endl;
    for (int k = 0; k < (W + 2); k++){
      A.at(k) = '#';
    }
  }
  for (int i = 0; i < (W + 2); i++) {
    cout << A.at(i);
  }
}

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

typedef long long ll;
const ll MOD = 1e9+7;

int n,k,a[105];
ll dp[105][100005],s[100005];

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];

    dp[0][0]=1LL;

    for(int i=1;i<=n;i++){
        s[0]=dp[i-1][0];
        for(int j=1;j<=k;j++){
            s[j]=(dp[i-1][j]+s[j-1])%MOD;
        }
        for(int j=0;j<=k;j++){
            int mn=max(-1,j-a[i]-1);
            ll add;

            if(mn==-1)add=s[j];
            else add=(s[j]-s[mn]+MOD)%MOD;


            dp[i][j]=(dp[i][j]+add)%MOD;
        }
    }



    cout<<dp[n][k];
    return 0;
}


Measured memory_bytes: 79360
#include<stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
	int n,m,a,b,c,s,t,k;
	while(scanf("%d",&n)!=EOF)
	{
		s=0;
		t=n;
		while(t!=0)
		{
			k=t%10;
			s=s+k;
			t=t/10;
		}
		if(n%s==0)
		printf("Yes\n");
		else
		printf("No\n");
	}

}

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

int main() {
  int x,y;
  cin>>x>>y;
  for(int i=0;i<=100;i++){
    for(int j=0;j<=100;j++){
      if(2*i+4*j==y&&i+j==x){
        cout<<"Yes"<<endl;
        return 0;
      }
    }
  }
  cout<<"No"<<endl;
  return 0;
}

Measured memory_bytes: 3624
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <queue>
#include <deque>
#include <cmath>
#include <iomanip>
#include <cstdio>

using namespace std;

int main(){
    int n; cin >> n;
    int ans=false;
    for(int i=0; i*4<=n; ++i){
        if((n-i*4)%7==0){
            ans=true;
            break;
        }
    }
    cout << (ans ? "Yes" : "No") << endl;
    return 0;
}


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

typedef long long ll;

const int MAXV = 1e5 + 10;
const int MAXN = 1e3 + 10;
const ll INF = 1e13;

ll dp[MAXV];
int N,W,V,wi[MAXN],vi[MAXN];

int main(){

    cin >> N >> W;
    for(int i = 1;i<=N;i++){
        cin >> wi[i] >> vi[i];
        V += vi[i];
    }

    for(int i = 1;i<=V;i++) dp[i] = INF;
    dp[0] = 0;
    
    for(int item = 1;item<=N;item++){
        int w = wi[item], v = vi[item];
        for(int i = V;i>=v;i--){
            dp[i] = min(dp[i],dp[i - v] + w);
        }
    }

    for(int i = V;i>=0;i--){
        if(dp[i] <= W){
            cout << i << endl;
            break;
        }
    }

    return 0;

}

Measured memory_bytes: 4372
import sys
input = sys.stdin.readline
n, k = [ int(v) for v in input().split() ]
card_parent = [ i for i in range(n) ]
card_set = [ set([i]) for i in range(n) ]
card_list = set( tuple( int(v)-1 for v in input().split() ) for i in range(k) )

def parent(x,a):
    if card_parent[x] == x:
        return x, a
    else:
        return parent(card_parent[x],a+1)


for i in card_list:
    p, q = i[0], i[1]
    card_parent[p], rank_p = parent(p,0)
    card_parent[q], rank_q = parent(q,0)
    if card_parent[p] != card_parent[q]:
        if rank_p > rank_q:
            card_parent[card_parent[q]] = card_parent[p]
        else:
            card_parent[card_parent[p]] = card_parent[q]




card_parent = [ parent(i,0)[0] for i in range(n) ]

print(len(set(card_parent)))

Measured memory_bytes: 141216
#include <bits/stdc++.h>
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(v) (v).begin(),(v).end()
 
#define PII pair<int,int>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>
 
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } 
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } 
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}
 
#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} 
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue
 
#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);}
#define jeb fflush(stdout)

 
using namespace std;

#define max_n 505

int n;
char s[max_n][max_n];

bool check_bad() {
	FOR(i,0,n) FOR(j,0,n) if(s[i][j] == '#') return true;
	return false;
}

int main() {
	read(n);
	FOR(i,0,n) {
		scanf("%s", s[i]);
	}
	if (!check_bad()) {
		printf("-1\n");
		return 0;
	}
	int best = -1;
	VI dla;
	FOR(i,0,n) {
		int ja = 0;
		FOR(j,0,n) if (s[i][j] == '#') ja++;
		if (ja > best) {
			best = ja;
			dla.clear();
			dla.pb(i);
		} else if (ja == best) {
			dla.pb(i);
		}
	}
	int badcols = 0;
	FOR(j,0,n) {
		int ja = 0;
		FOR(i,0,n) if (s[i][j] == '#') ja++;
		if (ja != n) badcols++;
	}
	int ans = badcols + (n-best);
	bool ok = false;
	FOR(i,0,dla.size()) {
		int w = dla[i];
		int ile = 0;
		FOR(j,0,n) if (s[j][w]=='#') ile++;
		if (ile != 0) ok = true;
	}

	if (!ok) ans++;
	printf("%d\n", ans);
}


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

using namespace std;

struct edge{
	int u;	//?????????
	int v;	//??°??????
	int cost;	//??????
	//???????????????????????§?????§?°???¢????????????
	bool operator<(const edge& another) const {
			return cost < another.cost;
	}
};

const int MAX_N = 100000;
const int MAX_M = 100000;

edge es[MAX_M];
int par[MAX_N];
int rank[MAX_N];

void initial(int n){		//?????????
	for(int i=0;i<n;i++){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){		//??¨??????????±???????
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

void unite(int x,int y){	//x???????????¨??¨y???????????¨?????????
	x = find(x);	//x???????????¨??????????????????x??¨??????
	y = find(y);	//y???????????¨??????????????????y??¨??????
	if(x == y) return;
	if(rank[x] < rank[y]){
		par[x] = y;
	}else{
		par[y] = x;
		if(rank[x] == rank[y])
			rank[x]++;
	}
}

bool same(int x,int y){	//x,y???????????¨???????????§?????????
	return find(x) == find(y);
}

bool comp(const edge& e1,const edge& e2){
	return e1.cost < e2.cost;
}

int kruskal(int n,int m){
	initial(n);
	int res = 0;
	for(int i=0;i<m;i++){
		edge e = es[i];
		if(!same(e.u,e.v)){
			unite(e.u,e.v);
			res += e.cost;
		}
	}
	return res;
}

int main(){
	int n,m,j,k,l;
	cin >> n;	//????????°?????\???
	cin >> m;	//?????°?????\???
	for(int i=0;i<m;i++){
		cin >> j >> k >> l;
		es[i].u = j;	es[i].v = k;	es[i].cost = l;
	}
	sort(es,es+m);
	cout << kruskal(n,m) << "\n";
}

Measured memory_bytes: 4340
/*
 * main.c
 *
 *  Created on: 2019/12/29
 *      Author: family
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a > b ? b : a)
typedef long long int ll;

int main()
{
	ll A = 0, B = 0, K = 0;
	scanf("%lld %lld %lld", &A, &B, &K);

	if (A > 0) {
		if (A < K) {
			K = K - A;
			A = 0;
		} else {
			A = A - K;
			K = 0;
		}
	}
	if (B > 0) {
		if (B > K) {
			B -= K;
		} else {
			B = 0;
		}
	}
	printf("%lld %lld\n", A, B);
    return 0;
}


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

int main() {
  int h, w;
  cin >> h >> w;
  string c[h];
  for (int i = 0; i < h; i++) {
    cin >> c[i];
  }

  string s[2 * h];
  for (int i = 0; i < 2 * h; i++) {
    s[i] = c[i / 2];
  }

  for (int i = 0; i < 2 * h; i++) {
    cout << s[i] << endl;
  }

  return 0;
}


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

#define int long long
typedef pair<int, int> P;
//#define mod 1000000007
#define INF (1LL<<60)

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define YES puts("YES\n")
#define Yes puts("Yes\n")
#define NO  puts("NO\n")
#define No  puts("No\n")
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }


vector< int > z_algorithm(const string& s) {
	vector< int > prefix(s.size());
	for (int i = 1, j = 0; i < s.size(); i++) {
		if (i + prefix[i - j] < j + prefix[j]) {
			prefix[i] = prefix[i - j];
		}
		else {
			int k = max(0LL, j + prefix[j] - i);
			while (i + k < s.size() && s[k] == s[i + k]) ++k;
			prefix[i] = k;
			j = i;
		}
	}
	prefix[0] = (int)s.size();
	return prefix;
}


signed main() {

	int N;
	cin >> N;
	string S;
	cin >> S;

	int ans = 0;
	rep(i, N - 1) {
		vector<int> v = z_algorithm(S.substr(i,N-i));
		for (int j = i + 1; j < N; j++) {
			ans = max(ans, min(v[j-i],j-i));
		}
	}

	cout << ans << endl;


	return 0;
}


Measured memory_bytes: 256
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input())
    S = list(_ for _ in input())
    max_S = 0
    for i in range(N):
        S1 = set(S[0:i])
        S2 = set(S[i:N])
        max_S = max(max_S,len(S1.intersection(S2)))
    print(max_S)
    
if __name__ == '__main__':
    main()


Measured memory_bytes: 3572
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

mint calcNumOfCombination_mint(mint n, mint r){
  mint num = 1;
  for(mint i = 1; i.x <= r.x; i.x++){
    num = num * (n - i + 1) / i;
  }
  return num;
}

int main(){
  int n, k;
  cin >> n >> k;
  int b = k, r = n - k;
  int rr = r+1;
  rep2(i, 1, k+1){
    mint ans = calcNumOfCombination_mint(rr, i);
    mint fr = b - i;
    ans *= calcNumOfCombination_mint(fr+i-1, i-1);
    cout << ans.x << endl;
  }
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;

vector<pair<char,int>> v;

int main(){
    string s;
    cin >> s;
    s += "banpei";

    char cur = 'x';
    int cnt;
    rep(i,s.size()){
        if(cur != s[i]){
            v.emplace_back(cur, cnt);
            cur = s[i];
            cnt = 1;
        }else{
            cnt++;
        }
    }

    int ans = 0;
    rep(i,v.size() - 2){
        if(v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I'){
            int a = v[i].second ;
            int b = v[i + 1].second;
            int c = v[i + 2].second;
            if(a >= b && b <= c) ans = max(ans, b);
        }
    }

//    rep(i,v.size()){ cout << v[i].first << ' '; }cout << endl;
//    rep(i,v.size()){ cout << v[i].second << ' '; }cout << endl;

    cout << ans << endl;
}

Measured memory_bytes: 12632
#include<stdio.h>
#include<string.h>
#define LEN 100005

typedef struct pp{
	char name[100];
	int t;
}P;

P Q[LEN];
int head,tail,n;

void enqueue(P x){
	Q[tail] = x;
	tail = (tail + 1) % LEN;
}

P dequeue(){
	P x = Q[head];
	head = (head + 1) % LEN;
	return x;
}

int min(int a,int b) {return a < b ? a : b;}

int main(){
	int elaps = 0,c;
	int i,q;
	P u;
	scanf("%d %d",&n,&q);
	for(i = 1;i <= n;i++){
		scanf("%s",Q[i].name);
		scanf("%d",&Q[i].t);
	}
	head = 1; tail = n + 1;
	
	while(head != tail){
		u = dequeue();
		c = min(q, u.t);
		u.t -= c;
		elaps += c;
		if(u.t > 0) enqueue(u);
		else{
			printf("%s %d\n",u.name,elaps);
		}
	}
	
	return 0;
}

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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
#define dump(x) cout << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

int main() {
	int n, k; cin >> n >> k;
	puts(n >= k * 2 - 1 ? "YES" : "NO");
	return 0;
}

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

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define each(itr,v) for(auto &(itr):(v))
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define dbg(x) cout<<#x" = "<<(x)<<ln<<flush
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ')<<flush;}}

typedef complex<double> P;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

ll mop(ll a,ll b,ll m=mod) {ll r=1;a%=m;for(;b;b>>=1){if(b&1)r=r*a%m;a=a*a%m;}return r;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a*b/gcd(a,b);}
bool ool(int x,int y,int h,int w) {return((x<0)||(h<=x)||(y<0)||(w<=y));}
bool deq(double a,double b) {return abs(a-b)<eps;}

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;

// ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…
// .｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋
// ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・

// dp[i][j]: 黒をi番目、白をj番目まで左から並べるのに必要な最小操作回数
// b,w[i][j]: 黒をi番目、白をj番目まで並べた状態に黒/白を追加するのに必要な最小操作回数
// ∴ 初期状態で黒/白のi+1/j+1番目より左側にある、黒i+1以上のボールと白j+1以上のボールの個数
// これをO(N^2)で求められればいい
// まず各黒ボールの左側にある白j+1以上のボールの個数を累積和かなんかで求めてから、
// b[i+1][j] = b[i][j] + (p[0][i+1]<p[0][i]) で埋めていけばいい
// j以上の白ボールの個数 = j+1以上の白ボールの個数 - jの白が黒のi+1より左にあるかどうか
// dp[i][j] = min(dp[i-1][j]+b[i-1][j], dp[i][j-1]+w[i][j-1]) で更新すればok

int n, t, p[2][2020], b[2020][2020], w[2020][2020], dp[2020][2020];
char c;

int main() {
	cin >> n;
	rep(i,n*2) {
		cin >> c >> t;
		p[c=='W'][t-1] = i;
	}
	
	rep(i,n) {
		reps(j,i+1,n) if (p[0][j]<p[0][i]) b[i][n]++;
		for (int j=n-1; j>=0; --j) b[i][j] = b[i][j+1] + (p[1][j]<p[0][i]);
	}
	rep(i,n) {
		reps(j,i+1,n) if (p[1][j]<p[1][i]) w[n][i]++;
		for (int j=n-1; j>=0; --j) w[j][i] = w[j+1][i] + (p[0][j]<p[1][i]);
	}
	
	rep(i,2020) rep(j,2020) dp[i][j] = inf;
	dp[0][0] = 0;
	rep(i,n+1) rep(j,n+1) chmin(dp[i+1][j], dp[i][j]+b[i][j]), chmin(dp[i][j+1], dp[i][j]+w[i][j]);
	cout << dp[n][n];
}

Measured memory_bytes: 48000
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define all(a) a.begin(),a.end()
#define endl '\n'
#define ull unsigned long long
#define y1 ljhadglkjsadf
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define iter set< pair<int,ll> >::iterator
#define iter1 set<int>::iterator
#define int long long
using namespace std;
using namespace __gnu_pbds;

template<class T>
using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

template<class T>
using ordered_multiset=tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rnd1(chrono::steady_clock::now().time_since_epoch().count());

//find_by_order
//order_of_key

const int N=1e6+7;
const int inf=1e18+1e9;
const int mod=1e9+7;
const ld eps=1e-9;

//const int MAX_MEM=1e8;
//int mpos=0;
//char mem[MAX_MEM];
//
//inline void * operator new(size_t n){
//    char *res=mem+mpos;
//    mpos+=n;
//    if (mpos>=MAX_MEM){
//        cout<<"BAD"<<endl;
//        exit(0);
//    }
//    return (void*)res;
//}
//
//inline void operator delete(void *) {}

int n;
string s[N];
int cnt[N*27],kol[26];
int nxt[N][26];
int all=1;

int next(int s,int c)
{
    if (!nxt[s][c]){
        ++all;
        nxt[s][c]=all;
    }
    return nxt[s][c];
}

void add(string s)
{
    int cur=1;
    for (int i=0;i<s.size();++i){
        cur=next(cur,s[i]-'a');
    }
    cnt[cur]++;
    return;
}

main ()
{
    ios;
    cin>>n;
    for (int i=1;i<=n;++i){
        cin>>s[i];
        reverse(all(s[i]));
        add(s[i]);
    }
    int ans=0;
    for (int i=1;i<=n;++i){
        int cur=1;
        for (int j=(int)s[i].size()-1;j>=0;--j){
            kol[s[i][j]-'a']++;
        }
        for (int j=0;j<s[i].size();++j){
            for (int k=0;k<=25;++k){
                ans+=cnt[next(cur,k)]*(kol[k]>0);
            }
            kol[s[i][j]-'a']--;
            cur=next(cur,s[i][j]-'a');
        }
    }
    ans-=n;
    cout<<ans<<endl;
}
//8 4
//1 2 3 1 2 3 1 2
//1 2
//2 3
//4 5
//6 7


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

p = list(int(_) for _ in input().split()) 
q = p.copy()
p.sort(reverse=False)
#print(p)
#print(q)
cnt = 0

for i in range(n):
	if p[i] != q[i]:
		cnt += 1
#print(cnt)
if cnt >= 3:
	print('NO')
else:
	print('YES')

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
int A,B,C,D,rect1,rect2,ans;
main(){
  cin>>A>>B>>C>>D;
  rect1=A*B;
  rect2=C*D;
  if(rect1!=rect2){
    if(rect1>rect2){
      ans=rect1;
    }else{
      ans=rect2;
    }
  }else{
    ans=rect1;
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 256
/* 
  In the name of ALLAH
  Author : Raashid Anwar
*/
 
#include <bits/stdc++.h>
using namespace std;
 
#define int    int64_t
const int M = 1000000007;

int ans[2001];
int vis[2001];
vector <int> adj[2001];

void solve(int i) {
  memset(vis,0,sizeof(vis));
  priority_queue <pair<int,int>,vector<pair<int,int>>,greater<pair<int,int > > > q;
  int d,u;
  q.push({int(0),i});
  while(!q.empty()) {
    d=q.top().first;
    u=q.top().second;
    q.pop();
    if(vis[u])
      continue;
    vis[u]=1;
    ans[d]++;
    for(int v : adj[u])
      if(!vis[v])
        q.push({d+1,v});
  }
  return ;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
  //#endif
  int n,x,y;
  cin>>n>>x>>y;
  adj[x].push_back(y);
  adj[y].push_back(x);
  for(int i=1;i<=n-1;i++) {
    adj[i].push_back(i+1);
    adj[i+1].push_back(i);
  }
  for(int i=1;i<=n;i++)
    solve(i);
  for(int i=1;i<n;i++)
    cout<<(ans[i]/2)<<" ";
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, s, n) for (int i = (s); i < (n); i++)
#ifdef LOCAL
#define INPUT_FILE                    \
  ifstream in("abc143/abc143_d.txt"); \
  cin.rdbuf(in.rdbuf());
#else
#define INPUT_FILE
#endif
#define CIN_OPTIMIZE \
  cin.tie(0);        \
  ios::sync_with_stdio(false);
typedef pair<int, int> P;
typedef long long ll;
const int INF = 100100100;
const ll LINF = 1e18 + 100;
const int MOD = 1e9 + 7;

int main() {
  INPUT_FILE CIN_OPTIMIZE;

  int N;
  cin >> N;

  vector<int> c(N);
  rep(i, N) { cin >> c[i]; }
  sort(c.begin(), c.end(), greater<>());

  ll cnt = 0;

  rep(i, N) repi(j, i + 1, N - 1) {
    int diff = c[i] - c[j];
    auto comp = [&](int x) -> bool {
      // printf("x:%d\n", x);
      return x > diff;
    };
    auto startIt = c.begin() + j + 1;
    auto it = partition_point(startIt, c.end(), comp);
    auto l = it - startIt;
    cnt += l;
  }

  cout << cnt << endl;
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-

# AtCoder Grand Contest
# Problem A


if __name__ == '__main__':
    n, a, b = list(map(int, input().split()))
    min_value = a * (n - 1) + b
    max_value = a + b * (n - 1)

    print(max(max_value - min_value + 1, 0))


Measured memory_bytes: 2940
#include <iostream>
#include <iomanip>
#define REP(i, a, b) for (auto i = (a); i < (b); ++i)
using namespace std;

const int inf = 1e9;
const int N = 600;
bool adj[N][N];
int deg[N];
double E[N][2];

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

	int n, m; cin >> n >> m;

	while(m--) {
		int s, t; cin >> s >> t; --s, --t;
		adj[s][t] = 1;
		++deg[s];
	}

	auto solve = [&](bool b) {
		REP(k, 1, n) {
			int i = n - k - 1;
			E[i][b] = 0;
			REP(j, i + 1, n - 1)
				if (adj[i][j])
					E[i][b] += E[j][b];
			E[i][b] /= deg[i];
			++E[i][b];
		}
	};

	solve(0);
	double ans = E[0][0];

	REP(i, 0, n - 1) {
		if (deg[i] == 1) continue;

		int mxpos; double mx = -1;
		REP(j, i + 1, n) {
			if (adj[i][j] && mx < E[j][0]) {
				mxpos = j;
				mx = E[j][0];
			}
		}

		--deg[i]; adj[i][mxpos] = 0;
		solve(1);
		++deg[i]; adj[i][mxpos] = 1;

		ans = min(ans, E[0][1]);
	}

	cout << fixed << setprecision(10) << ans;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
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 (b<a) {a=b; return 1;} return 0;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    int x = n % 10;
    // if (x == 0 || x == 1 || x == 6 || x == 8) {
    //     cout << "pon" << endl;
    // } else if (x == 3) {
    //     cout << "bon" << endl;
    // } else {
    //     cout << "hon" << endl;
    // }

    switch (x)
    {
    case 0:
    case 1:
    case 6:
    case 8:
        cout << "pon" << endl;
        break;
    case 3:
        cout << "bon" << endl;
        break;
    default:
        cout << "hon" << endl;
        break;
    }
}

Measured memory_bytes: 3572
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
int solve(int n){
    if(n==0){
        return 1;
    }
    int num=0;
    while(n){
        n/=10;
        num++;
    }
    return num;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    int s,c;
    int mi=1e9;
    string ans="";
    for(int i=0;i<n;i++){
        ans+="#";
    }
    for(int i=0;i<m;i++){
        cin>>s>>c;
        mi=min(mi,s-1);
        if(ans[s-1]!='#'&&c!=(ans[s-1]-'0')){
            cout << -1<<endl;
            return 0;
        }
        if((s==1&&c==0)&&n!=1){
            cout << -1<<endl;
            return 0;
        }
        ans[s-1]=('0'+c);
    }
    if(ans=="#"&&n==1){
        cout <<0<<endl;
        return 0;
    }
    if(ans[0]=='#'){
        ans[0]='1';
    }
    for(int i=1;i<n;i++){
        if(ans[i]=='#'){
            ans[i]='0';
        }
    }
    cout << ans<<endl;
    return 0;
}


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

int main(){
  int a, b;
  cin >> a >> b;
  if(a*b%2) cout << "Odd";
  else cout << "Even";
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
using namespace std;
const int MOD=1000000007;
int n,m,t;
int h,g;
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=res*a%MOD;
		a=a*a%MOD,b>>=1;
	}
	return res;
}
int main()
{
	scanf("%d%d%d",&n,&m,&t);
	h=n/gcd(n,t),g=m/gcd(m,t);
	long long res=1;
	res=(res+ksm(2,h)-1)%MOD;
	res=(res+ksm(2,g)-1)%MOD;
	res=(res+ksm(2,gcd(h,g))-2)%MOD;
	res=ksm(res,1LL*(n/h)*(m/g))%MOD;
	printf("%lld",res);
	return 0;
}

Measured memory_bytes: 3784