akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<string>
#include<algorithm>	
#include<cmath>
using namespace std;
int main() {
	long long n = 0, b = 0, ans = 0, sum = 0; string s; cin >> s;
	if (s[0] == '>') {
		for (int h = 0; h < s.size(); h++) {
			if (s[h] == '>') { sum++; }
			else { h = s.size(); }
		}
	}
	for (int h = sum; h < s.size(); h++) {
		if (s[h] == '<' && n != 0 && b != 0) {
			if (n >= b) { ans += n * (n + 1) / 2; ans += (b - 1) * b / 2; }
			else { ans += b * (b + 1) / 2; ans += (n - 1) * n / 2; }
			n = 1; b = 0;
		}
		else if (s[h] == '<') {
			n++;
		}
		if (s[h] == '>') {
			b++;
		}
	}
	if (n >= b) { ans += n * (n + 1) / 2; ans += (b - 1) * b / 2; }
	else { ans += b * (b + 1) / 2; ans += (n - 1) * n / 2; }
	cout << ans + sum * (sum + 1) / 2 << endl;
	cin >> n;
}

Measured memory_bytes: 900
#include<iostream>
#include<bits/stdc++.h>
#include<iomanip>
#include<math.h>
#include<vector>
#include<cctype>
#include<string>
#include<set>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
std::string line;
typedef long long ll;

int main(){

   string s,t;
   cin >> s >> t;

   int cnt = 0;
   rep(i,s.size()){
      if(s[i]==t[i]) cnt++;
   }

   cout << cnt << endl;



   return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define Sort(Array) sort(Array.begin(), Array.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define out(ans) cout << ans << endl;

int main(){
  int a,b,c,d;
  cin>>a>>b>>c>>d;

  int p1 = (a+(d-1))/d;
  int p2 = (c+(b-1))/b;
  // cout<<p1<<p2<<endl;

  if(p1<p2){ out("No");}
  else{out("Yes");}
}


Measured memory_bytes: 3636
import java.io.*;
import java.util.*;

public class Main {
    static boolean isLastChange = false;
    public static int countDiff(String s, boolean previousChanged) {
        int countChange = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == s.charAt(i - 1 < 0 ? s.length() - 1 : i - 1) && !previousChanged) {
                previousChanged = true;
                countChange++;
            } else {
                previousChanged = false;
            }
        }
        isLastChange = previousChanged;
        return countChange;
    }

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int times = sc.nextInt();
        int once = countDiff(s, true);
        if (times == 1) {
            System.out.println(once);
        } else {
            boolean firstTimeLastChange = isLastChange;
            int twice = countDiff(s, isLastChange);
            if (times == 2) {
                System.out.println(once + twice);
            } else {
                if (isLastChange == firstTimeLastChange) {
                    System.out.println(once + (long) twice * (times - 1));
                } else {
                    System.out.println((long) (times / 2) * (once + twice) + (times - times / 2 * 2) * once);
                }
            }
        }

    }


    static class Scanner {
        StringTokenizer st;
        BufferedReader br;
        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }
        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
    }
}

Measured memory_bytes: 21332
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define MAXN 200005
using namespace std;
using namespace __gnu_pbds;  

string s;

void solve()
{
	cin>>s;
	if(s[2]==s[3] && s[4]==s[5]) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}

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


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
int main(){
  string ans;
  REP(i,5){
  	int S;
  	cin>>S;
    if (S==0){
      cout<<i+1<<endl;
    }
  } 
}


Measured memory_bytes: 3620
#include<stdio.h>
#include<algorithm>
using namespace std;
int dx[10];
int dy[10];
int ex[10];
int ey[10];
int main(){
	int a;
	while(scanf("%d",&a),a){
		int b;
		scanf("%d",&b);
		int p,q;
		scanf("%d%d",&p,&q);
		for(int i=0;i<b-1;i++){
			int c,d;
			scanf("%d%d",&c,&d);
			dx[i]=c-p;
			dy[i]=d-q;
			p=c;
			q=d;
		}
		for(int i=0;i<a;i++){
			bool ok=false;
			int n;
			scanf("%d",&n);
			int s,t;
			scanf("%d%d",&s,&t);
			for(int j=0;j<n-1;j++){
				int u,v;
				scanf("%d%d",&u,&v);
				ex[j]=u-s;
				ey[j]=v-t;
				s=u;
				t=v;
			}
			if(n!=b)continue;
			for(int j=0;j<4;j++){
				bool OK=true;
				for(int k=0;k<n-1;k++)if(dx[k]!=ex[k]||dy[k]!=ey[k])OK=false;
				if(OK)ok=true;
				OK=true;
				for(int k=0;k<n-1;k++)if(dx[k]!=ex[n-2-k]||dy[k]!=ey[n-2-k])OK=false;
				if(OK)ok=true;
				for(int k=0;k<n-1;k++){
					int f=ey[k];
					int g=-ex[k];
					ex[k]=f;
					ey[k]=g;
				}
			}
			if(ok)printf("%d\n",i+1);
		}
		printf("+++++\n");
	}
}

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

public class Main{
	 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn=new Scanner(System.in);
		 
		  int n=scn.nextInt();
		  int m =scn.nextInt();
		  int x =scn.nextInt();
		  int [][] arr =new int[n][m+1];
		//  int [] cost =new int [n];
		  for(int i=0;i<n;i++){
			//  cost[i]=scn.nextInt();
			  
			  for(int  j=0;j<m+1;j++){
				  arr[i][j]=scn.nextInt();
			  }
		  }
		//  min=Integer.MAX_VALUE;
		  int []arr1 =new int[m];
		 int ans =res(arr,arr1,0,x,0);
		  if(ans==Integer.MAX_VALUE){
			  System.out.println(-1);
			  return;
		  }
		  System.out.println(ans);
 
	}
//	static int min=Integer.MAX_VALUE;
	
	
  public static int  res(int [][] arr ,int []arr1 ,int i, int x,int cost){
	  if(i==arr.length){
		  for(int j=0;j<arr1.length;j++){
			  //System.out.print(arr1[j]+"a");
			  if(arr1[j]<x){
				  return  Integer.MAX_VALUE;
			  }
		  }
		//  System.out.println();
		//   min=Math.min(min, cost);
		   return cost ;
	  }
	  
	  int val =  res(arr,arr1,i+1,x,cost);
	  for(int j=0;j<arr1.length;j++){
		  arr1[j]+=arr[i][j+1];
	  }
	 
		  
		 val=Math.min(val, res(arr,arr1,i+1,x,cost+arr[i][0]));
		  
	
	  for(int j=0;j<arr1.length;j++){
		  arr1[j]-=arr[i][j+1];
	  }
	    
	    return val;
	  
	  
  }
 
}

Measured memory_bytes: 38728
#include<bits/stdc++.h>
using namespace std;
#define all(a)  (a).begin(),(a).end()
typedef long long ll;
ll mod=1000000007;

int main() {
  int n;cin >>n;
  string s;cin >>s;
  map<string, ll> mp;
  for(int i=0;i<n;++i){
    string sub = s.substr(i,1);
    if(mp.find(sub) == mp.end()){
      mp[sub] =1;
    } else {
      mp[sub] += 1;
    }
  }
  ll ans = 1;
  for(auto itr=mp.begin();itr!=mp.end();++itr){
    ans *= (itr->second + 1);
    ans %= mod;
  }
  cout << (ans - 1 + mod)%mod << endl;
  return 0;
}


Measured memory_bytes: 512
def fac(a):
    result = 1
    for i in range(1, a+1):
        result = result * i % (10**9+7)
    return result

n, m = map(int, input().split())
if abs(n-m) >= 2:
    print(0)
else:
    dog = fac(n)
    monkey = fac(m)
    if abs(n-m) == 1:
        print(dog*monkey%(10**9+7))
    else:
        print(2*dog*monkey%(10**9+7))


Measured memory_bytes: 3060
#include<iostream>
#include<string>
int main() {
	std::string s;
	std::cin >> s;
	int cou=0;
	for (int i = 0; i < s.size(); i++){
		if (s[i + 0] == 'd'&&s[i + 1] == 'r'&&s[i + 2] == 'e'&&s[i + 3] == 'a'&&s[i + 4] == 'm') {
			if (s[i + 5] == 'e'&&s[i + 6] == 'r'&&!(s[i + 7] == 'a'&&s[i + 8] == 's'&&s[i + 9] == 'e')) {
				cou += 7;
				i += 6;
			}
			else {
				cou += 5;
				i += 4;
			}
		}
		 if (s[i + 0] == 'e'&&s[i + 1] == 'r'&&s[i + 2] == 'a'&&s[i + 3] == 's'&&s[i + 4] == 'e') {
			if (s[i + 5] == 'r') {
				cou += 6;
				i += 5;
			}
			else {
				cou += 5;
				i += 4;
			}
		}
}
	if (cou >= s.size()) {
		std::cout << "YES" << std::endl;
	}
	else {
		std::cout << "NO" << std::endl;
	}
	return 0;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
vector<int> adj[100000];
#define ull unsigned long long int
#define ll long long int
#define MAX 1e9+7
#define pll pair<ll,ll>
#define pii pair<int,int>
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ifstream fin("a.in");
    ofstream fout("a.out");
    int n,x,y;
  	cin>>n>>x>>y;
  	ll dis[n][n];
  	for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        dis[i][j]=MAX;
  	for(int i=0;i+1<n;i++) dis[i][i+1]=1;
  	dis[x-1][y-1]=1;
  	for(ll i=0;i<n;i++){
      for(ll j=0;j<n;j++){
        
        dis[i][j]=min(abs(j-i),min(abs(x-1-i)+abs(y-1-j)+1,abs(i-y+1)+abs(j-x+1)+1));
      }
    }
  	ll ans[n+1];
  	memset(ans,0,sizeof ans);
  	for(int i=0;i<n;i++){
      for(int j=0;j<n;j++) if(i<j) ans[dis[i][j]]++;
    
    }
  	for(int i=1;i<n;i++) cout<<ans[i]<<endl;
    
    return(0);
}

Measured memory_bytes: 33920
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int A,B;
  cin >> A >> B;
  
  if(A+B==3){
    cout << 3 << endl;
  }else if(A+B==4){
    cout << 2 << endl;
  }else{
    cout << 1 << endl;
  }
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <utility>
#include <tuple>
#include <cctype>
#include <bitset>
#include <complex>
#include <cmath>
#include <array>
#include <numeric>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
//#define MOD 998244353
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pint> vpint;
typedef vector<tint> vtint;
typedef vector<vint> vvint;
int dx[8]={0,0,-1,1,1,1,-1,-1};
int dy[8]={-1,1,0,0,1,-1,1,-1};
const int SIZE=100050;
//↑templete
//↓Union-Find Tree
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};
//↑Union-Find Tree
signed main(){

	int N, M, K;
	cin>>N>>M>>K;

	UnionFind UF(SIZE);
	vpint Block;
	int Bcnt[SIZE]={};
	int Fcnt[SIZE]={};

	for(int i=0;i<M;i++){
		int a, b;
		cin>>a>>b;
		a--, b--;
		UF.unionSet(a, b);
		Fcnt[a]++;
		Fcnt[b]++;
	}
	for(int i=0;i<K;i++){
		int c, d;
		cin>>c>>d;
		c--, d--;
		Block.eb(c, d);
		if(UF.findSet(c, d)){
			Bcnt[c]++;
			Bcnt[d]++;
		}
	}

	for(int i=0;i<N;i++){
		// cout<<"!"<<i+1<<endl;
		// cout<<Fcnt[i]<<" "<<Bcnt[UF.root(i)]<<endl;
		cout<<UF.size(i)-Fcnt[i]-Bcnt[i]-1<<" ";
		//cout<<endl;
	}
	cout<<endl;

	return 0;
}

Measured memory_bytes: 2932
if __name__ == "__main__":
    N = int(input())
    P = list(map(int, input().split()))
    ans = 0
    for i in range(N-1):
        if P[i] == i+1:
            tmp = P[i]
            P[i] = P[i+1]
            P[i+1] = tmp
            ans += 1
    if P[-1] == N:
        tmp = P[-1]
        P[-1] = P[-2]
        P[-2] = tmp
        ans += 1
    print(ans)


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

namespace ConsoleApp15
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
            bool frag = true;
            Dictionary<int, bool> Table = new Dictionary<int, bool>();
            foreach(int i in a)
            {
                if (Table.ContainsKey(i))
                {
                    frag = false;
                }
                else
                {
                    Table[i] = true;
                }
            }
            Console.WriteLine(frag ? "YES" : "NO");
        }
    }
}


Measured memory_bytes: 46280
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define el "\n"
const ll mod=1000000007;
const ll MAX=1e18;
#define all(v) v.begin(),v.end()




 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tc=1,i,j;
    //cin >> tc;
    while(tc--)
    {
      ll n;
      cin >> n;
      ll a[n];
      bool f=1;
      for(i=0;i<n;i++)
      {
        cin >> a[i];
        if(a[i]==0)
            f=0;
      }
      if(!f)
      {
          cout << "0\n";
          continue;
      }
      ll ans=1;
      for(i=0;i<n;i++)
      {
      	if(a[i]<=(MAX/ans))
        {
            ans*=a[i];
        }
        else
        {
            f=0;
            break;
        }
      }
      if(f)
        cout << ans << el;
     else
        cout << "-1\n";
    }
    return 0;
}


Measured memory_bytes: 4392
def main():
    N = int(input())
    A = input()
    B = input()
    C = input()
    ans = 0
    for i in range(N):
        s = set()
        s.add(A[i])
        s.add(B[i])
        s.add(C[i])
        if len(s) == 2:
            ans += 1
        elif len(s) == 3:
            ans += 2
    print(ans)


if __name__ == '__main__':
    main()


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

int main(){
  while(1){
    string number;
    int sum=0;
    cin>>number;
    for(int i=0;i<number.size();i++){
      sum+=number[i]-48;
    }
    if(sum==0) break;
    cout<<sum<<endl;
  }
  return 0;
}


Measured memory_bytes: 3212
#include <iostream>
#include <cstdio>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
 
using namespace std;
 
void solve()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int> > matrix(n, vector<int>(m));
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            cin >> matrix[i][j];
        }
    }
    vector<int> vec(m);
    for (int i = 0; i < m; ++i)
    {
        cin >> vec[i];
    }
    for (int i = 0; i < n; ++i)
    {
        int sum = 0;
        for (int j = 0; j < m; ++j)
        {
            sum += matrix[i][j] * vec[j];
        }
        cout << sum << endl;
    }
}
 
int main()
{
    solve();
    return(0);
}


Measured memory_bytes: 3188
#include <stdio.h>

int main(void) {
  int i, n, ans = 0, p;
  bool b = true;
  scanf("%d", &n);
  for(i = 0; i < n; ++i) {
    scanf("%d", &p);
    if(p == i + 1 && b) ans++, b = false;
    else b = true;
  }
  printf("%d", ans);
  return 0;
}

Measured memory_bytes: 128
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int result = (int) (a + Math.pow(a, 2) + Math.pow(a, 3));
    System.out.println(result);
    sc.close();
  }
}

Measured memory_bytes: 35808
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<cmath>
#include<string>

#define ls (t<<1)
#define rs ((t<<1)+1)
#define mid ((l+r)>>1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 100005
#define M 200005
#define seed 23333

using namespace std;
int i,j,m,n,p,k,ans=(int)2e9,A,B;
int work(int x)
{
	 int sa,sb;
	 if (x&1) sa=-A; else sa=A;
	 if (x&2) sb=-B; else sb=B;
	 if (sa>sb) return ans;
	 return sb-sa+(x&1)+((x&2)>0); 
}
int main()
{
	scanf("%d%d",&A,&B);
	for (i=0;i<4;++i) ans=min(ans,work(i)); 
	printf("%d\n",ans);
}


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

using namespace std;

int main(){
	int n;
	string ans = "";
	cin >> n;
	vector<string> s(n);
	for(int i = 0; i < n; i++){
		cin >> s[i];
	}
	vector<int> a(26);
	for(int i = 0; i < n; i++){
		vector<int> b(26, 0);
		for(int j = 0; j < s[i].size(); j++){
			int p = s[i][j] - 'a';
			b[p]++;
		}
		if(i == 0){
			a = b;
		}else{
			for(int j = 0; j < 26; j++){
				a[j] = min(a[j], b[j]);
			}
		}
	}
	for(int i = 0; i < 26; i++){
		for(int j = 0; j < a[i]; j++){
			ans += ('a' + i);
		}
	}
	cout << ans << endl;
	return 0;
}

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

int main()
{
    int N, sum;
    sum = 1;
    cin >> N;
    vector<int> mochi(N);
    for (int i = 0; i < N; i++)
    {
        cin >> mochi.at(i);
    }
    sort(mochi.begin(), mochi.end());
    for (int i = 0; i < N; i++)
    {
        if (i == N - 1)
        {
            break;
        }
        else if (mochi.at(i) < mochi.at(i + 1))
        {
            sum++;
        }
    }
    cout << sum << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;
int main(void){
    // Your code here!
    int cnt;
    double ans1, ans2, ans3, an3Mugen;
    cin >> cnt;
    int x[cnt];
    int y[cnt];
    int wkDis[cnt];
    
    for(int i=0; i<cnt; i++){
        cin >> x[i];
    }
    
    for(int i=0; i<cnt; i++){
        cin >> y[i];
    }
    
    for(int i=0; i<cnt; i++){
        wkDis[i] = abs(x[i] - y[i]);
    }
    
    for(int i=0; i<4; i++){
        double Distance, sum;
        sum = 0;
        switch(i){
            case 0:
                for(int j=0; j<cnt; j++){
                    sum += wkDis[j];
                }
            break;
            case 1:
                for(int j=0; j<cnt; j++){
                    sum += pow(wkDis[j],2);
                }
                sum = pow(sum, 1.0/2);
            break;
            case 2:
                for(int j=0; j<cnt; j++){
                    sum += pow(wkDis[j],3);
                }
                sum = pow(sum, 1.0/3);
            break;
            case 3:
                for(int j=0; j<cnt; j++){
                    if(wkDis[j] >= sum) sum = wkDis[j];    
                }
            break;
        }
        cout << fixed << setprecision(6) << sum << endl;
    }
}



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

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;
int n,a[100005];
void solve(){
    cin>>n;
    rep(i,1,n+1){
        int x;
        cin>>x;
        a[x]=i;
    }
    rep(i,1,n+1) cout<<a[i]<<" ";
    cout<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Measured memory_bytes: 1280
#include <iostream>
#include <unordered_map>
#include <vector>
//#define MOD 1000000007LL

typedef long long ll;

using namespace std;

int main() {
	ll n,m;
	cin >> n >> m;
	unordered_map<ll,ll> mp;
	vector<ll> ar;
	ll ts = 0;
	ll ans = 0;
	for(int i = 0; i < n; i++) {
		ll a;
		cin >> a;
		if(a%m==0) ans += 1;
		/*
		ll _index = (ts+(a%MOD))%MOD;
		if(mp[_index]>0) ans += mp[_index];
		*/
		ll _index = (m+m-(ts+a)%m)%m;
		if(mp[_index]>0) ans += mp[_index];

		for(int j = 0; j < ar.size(); j++) {
			ar[j] = (ar[j]+a)%m;
			if(ar[j]==0) ans += 1;
		}
		ar.push_back(a);
		ts+=a;

		if(i!=0&&i%987==0) {
			unordered_map<ll,ll> _mp;
			for(int j = 0; j < ar.size(); j++) {
				_mp[(ar[j]%m)] += 1;
			}
			for(pair<ll,ll> num : mp) {
				_mp[(num.first+ts)%m] += num.second;
				//cout << num.first << "," << num.second << endl;
			}
			mp.swap(_mp);
			//cout << "gegegege" << endl;
			vector<ll> _ar;
			ar.swap(_ar);
			ts = 0;
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 8576
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <vector>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long ll;

int main(void) {
  int a, b, k;
  cin >> a >> b >> k;
  for(int i = min(a, b); i > 0; --i) {
    if(a % i == 0 && b % i == 0) {
      --k;
      if(k == 0) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
	int a, b;
	scanf("%d %d", &a, &b);

	int d = a/b;
	int r = a%b;
	double f = (double)a/(double)b;

	printf("%d %d %f\n", d, r, f);

	return 0;
}

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

#define int long long
#define ll long long
#define rep(i,n) for (int i = 0; i < (n); i++)  
#define rrep(i,n) for (int i = (n)-1; i >= 0; i--)
#define rap(i,a,n) for (int i = a; i < (n); i++) 
#define rrap(i,n,a) for (int i = (n)-1; i >= a; i--) 
#define LL_MAX 9223372036854775807

typedef vector<int> vi;  
typedef pair<int,int> ii;  
typedef vector<ii> vii;  
typedef list<int> li;  
typedef unordered_map<int,int> mii;

vi bs,rs,gs;

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

    int n;
    string s;

    cin >> n;
    cin >> s;

    bs.assign(n,0);
    rs.assign(n,0);
    gs.assign(n,0);

    if (s[0] == 'B') bs[0] = 1;
    else if (s[0] == 'R') rs[0] = 1;
    else gs[0] = 1;

    rap(i,1,n) {
        if (s[i] == 'B') bs[i] = bs[i-1] + 1;
        else bs[i] = bs[i-1];
    }
    
    rap(i,1,n) {
        if (s[i] == 'R') rs[i] = rs[i-1] + 1;
        else rs[i] = rs[i-1];
    }

    rap(i,1,n) {
        if (s[i] == 'G') gs[i] = gs[i-1] + 1;
        else gs[i] = gs[i-1];
    }

    int sum = 0;

    rep(i,n) {
        rap(j,i+1,n) {
            if ((s[i] == 'R' && s[j] == 'G') || (s[i]=='G' && s[j]=='R')) {
                if (j + j - i >= n) sum += bs[n-1] - bs[j];
                else sum += bs[n-1] - bs[j] - (bs[j+j-i] - bs[j+j-i-1]);
            }
            else if ((s[i] == 'R' && s[j] == 'B') || (s[i]=='B' && s[j]=='R')) {
                if (j+j-i >= n) sum += gs[n-1] - gs[j];
                else sum += gs[n-1] - gs[j] - (gs[2*j-i] - gs[2*j-i-1]);
            }
            if ((s[i] == 'G' && s[j] == 'B') || (s[i]=='B' && s[j]=='G')) {
                if (j+j-i >= n) sum += rs[n-1] - rs[j];
                else sum += rs[n-1] - rs[j] - (rs[2*j-i] - rs[2*j-i-1]);
            }
        }
    }

    cout << sum << '\n';



    return 0;
    //compile: g++ -std=c++14 -O2 -Wall NAME.cpp -o NAME
}


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

N, X = map(int, input().split())

L, P = [1], [1]
for i in range(N - 1):
    L.append(L[-1] * 2 + 3)
    P.append(P[-1] * 2 + 1)


def countPatty(N, X):
    if N == 0:
        return 1 if X >= 1 else 0
    elif X <= 1:
        return 0
    elif 1 < X <= L[N - 1] + 1:
        return countPatty(N - 1, X - 1)
    elif L[N - 1] + 1 < X:
        return P[N - 1] + 1 + countPatty(N - 1, X - (L[N - 1] + 2))


print(countPatty(N, X))


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

int main(int argc, char const* argv[])
{
    int x, ans, tmp;
    cin >> x;
    ans = 0;
    for (int i = 2; i < 100; i++) {
        tmp = i * i;
        while (tmp <= x) {
            if (tmp > ans) {
                ans = tmp;
            }
            tmp *= i;
        }
    }
    if (x == 1) {
        ans = 1;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <functional>
#include <algorithm>
#include <vector>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iostream>
#include <queue>
#include <math.h>
#include <bits/stdc++.h>
#include <queue>
#include <map>

using namespace std;

#define INF (1ll<<60)
#define INFint (1<<30)
#define MOD 1000000007
#define BOUND 27182818284

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

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

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

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

// gcd
template<typename T>
T gcd(T a, T b){
    if(a==0)
        return b;
    return gcd(b%a,a);
}

// gcd array
int findGCD(int arr[], int n){
    int result = arr[0];
    for (int i=1; i < n; i++)
        result = gcd(arr[i], result);
    return result;
}

template<typename T>
T lcm( T m, T n ){
    // 引数に0がある場合は0を返す
    if ( ( 0 == m ) || ( 0 == n ) )
        return 0;
    return ((m/gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    fill( (T*)array, (T*)(array+N), val );
}


int dx[5]={1,0,-1,0};
int dy[5]={0,1,0,-1};
// v.front() = -BOUND;
// v.back() = BOUND;
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }
    // 素数である
    return true;
}

int N;
int d[100][100];
void warshall_floyd(){
    for(int k=0; k<N; k++) {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}
int A[202][202];
int main() {
    int H,W;
    int ans=0;
    cin >> H >> W;
    Fill(d,0);
    N=10;
    rep(i,10){
        rep(j,10){
            cin >> d[i][j];
        }
    }

    warshall_floyd();

    rep(i,H){
        rep(j,W){
            int s;
            cin >> s;
            if(s!=-1) {
                ans += d[s][1];
            }
        }
    }

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


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int main()
{
    int N;
    scanf("%d", &N);
    int grids[2][N];
    int c = 0;
    for (int i=0; i<2; i++)
    {
        int a = 0;
        for (int j=0; j<N; j++)
        {
            int b;
            scanf("%d", &b);
            a += b;
            grids[i][j] = a;
        }
    }
    for (int i=0; i<N; i++)
    {
        if (i==0)
            c = grids[0][0]+grids[1][N-1];
        else
            c = max(c, grids[0][i]+grids[1][N-1]-grids[1][i-1]);
        //cout << c << "\n";
    }
    printf ("%d", c);
    return 0;
}


Measured memory_bytes: 256
// 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <cmath>
#include <stack>

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

using namespace std;

long long a[100005] = {0};

int main(){
  int n;
  long long ans = 0;
  cin >> n;
  for(int i = 0; i < n; i++){
    long long work;
    cin >> work;
    if(work > n){
      ans++;
      continue;
    }
    a[work]++;
  }
  for(int i = 1; i <= n; i++){
    if(a[i] != i){
      if(a[i] >= i) ans += (a[i] - i);
      else ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
#define ll long long

int main(){
    string S;
    cin >> S;
    if((S[0]== '0' && S[1] != '0' ) || ( S[0]== '1' && (S[1]=='0' || S[1]=='1' || S[1]=='2') ) ){
        if((S[2]== '0'&& S[3] != '0' ) || ( S[2]== '1' && (S[3]=='0' || S[3]=='1' || S[3]=='2') ) ){
            cout << "AMBIGUOUS" << endl;
        }else{
            cout << "MMYY" << endl;
        }
    }else{
        if((S[2]== '0' && S[3] != '0' ) || ( S[2]== '1' && (S[3]=='0' || S[3]=='1' || S[3]=='2') ) ){
            cout << "YYMM" << endl;
        }else{
            cout << "NA" << endl;
        }
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define LL long long
#define PB push_back
#define X first
#define Y second
#define INF 1000 * 1000 * 1000
using namespace std;
const LL mod = 1000 * 1000 * 1000 + 7;
const LL ob = (mod + 1) >> 1;
vector<pair<LL , LL> > cols[100005];
LL p[100005];
LL rowMin[100005];
LL value[100005];
LL R , C , n;
vector<int> values[100005];
int find(int u)
{
	if(u == p[u])
		return u;
	return p[u] = find(p[u]);
}
void init()
{
	for(LL i = 1; i <= R; i++)
	{
		p[i] = i;
		values[i].PB(i);
	}
}
int main()
{
	ios_base::sync_with_stdio(0);
	
	cin >> R >> C >> n;
	for(LL i = 1; i <= R; i++)
		rowMin[i] = INF * (LL) INF;
	
	for(LL i = 0; i < n; i++)
	{
		LL r , c , a;
		cin >> r >> c >> a;
		rowMin[r] = min(rowMin[r] , a);
		cols[c].PB({r , a});
	}
	init();
	for(LL i = 1; i <= C; i++)
	{
		sort(cols[i].begin() , cols[i].end());

		for(LL j = 1; j < cols[i].size(); j++)	
		{
			LL diff = cols[i][j].Y - cols[i][j - 1].Y;
			int u = find(cols[i][j].X) , v = find(cols[i][j - 1].X);
			LL oldDiff = value[cols[i][j].X] - value[cols[i][j - 1].X];
			if(values[u].size() < values[v].size())
			{
				oldDiff = -oldDiff;
				swap(u , v);
				diff = -diff;
			}

			if(u == v && oldDiff != diff)
			{
				cout << "No" << endl;
				return 0;
			}

			LL newDiff = oldDiff - diff;
			if(u != v)
			{
				for(auto x : values[v])
				{
					values[u].PB(x);
					value[x] += newDiff;
				}
				p[v] = u;
			}
		}
	}
	for(int i = 1; i <= R; i++)
	{
		if(p[i] == i)
		{
			LL minima = INF * (LL)INF;
			for(auto x : values[i])
				minima = min(minima , value[x]);
			for(auto x : values[i])
			{
				if(value[x] - minima > rowMin[x])
				{
					cout << "No" << endl;
					return 0;
				}
			}
		}
	}


	cout << "Yes" << endl;
	return 0;
}




Measured memory_bytes: 14848
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>


using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())


//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

vector<string> tb;
vector<vector<bool>> used;
set<pair<pair<int,int>,pair<int,int>>> ans;

int h, w;

LL kuro, siro;

pair<int, int> start;
//1が.
//-1が#
//0が初回起動
void f(int y, int x, int now) {

	
	if (now == 1) {
		if (tb[y][x] == '#') {
			return;
		}
	}
	else if (now == -1) {
		if (tb[y][x] == '.') {
			return;
		}
	}

	used[y][x] = true;


	if (now == 1) {
		siro++;
	}
	else if (now == -1) {
		kuro++;
	}

	

	if (now == 0) {
		used[y][x] = true;
		now = -1;
	}

	if (y - 1 >= 0) {

		if (used[y - 1][x] == false) {
			f(y - 1, x, -now);
		}
		
	}

	if (y + 1 <= h - 1) {

		if (used[y+1][x] == false) {
			f(y + 1, x, -now);
		}
		
	}

	if (x - 1 >= 0) {
		if (used[y][x-1] == false) {
			f(y, x - 1, -now);
		}

		
	}

	if (x + 1 <=w-1) {
		if (used[y][x+1] == false) {
			f(y, x + 1, -now);
		}
	}

}

void init() {

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			
			used[i][j] = false;
		}
	}

	return;
}

int main() {

	cin >> h >> w;

	tb.resize(h);

	for (int i = 0; i < h; i++) {
		cin >> tb[i];
	}

	used.resize(h);
	for (int i = 0; i < h; i++) {
		used[i].resize(w);
	}

	LL ans = 0;

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (tb[i][j] == '#') {
				
				kuro = 1;
				siro = 0;
				f(i, j, 0);

				ans += kuro * siro;
			}
		}
	}

	cout << ans<< endl;

	return 0;
}

/*
2 2
#.
.#
*/

Measured memory_bytes: 20480
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <deque>
#include <cmath>
#include <map>
using ll = long long;
using namespace std;

int main() {
  string s;
  cin >> s;
  string t = "";
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') t += '0';
    else if (s[i] == '1') t += '1';
    else if (t.size() != 0) t.pop_back();
  }
  cout << t << endl;
}


Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int k,N;
bool vis[20];
bool Pan(int x)
{
	while(x>0) 
	{
		if(vis[x%10])
			return 0;
		x/=10;
	}
	return 1;
}
int main()
{
	scanf("%d%d",&N,&k);
	for(int i=1;i<=k;i++)
	{
		int x;
		scanf("%d",&x);
		vis[x]=1;
	}
	if(N==0&&vis[0])
	{
		printf("1");
		return 0;
	}
	for(int i=N;;i++)
		if(Pan(i))
		{
			printf("%d",i);
			return 0;
		}
}

Measured memory_bytes: 128
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<assert.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const ll mod=1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0;//角度转弧度 
ll powmod(ll a,ll b) {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;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N = 1e6+10, M = 1e6+10;
int n;
VI a, b; 
int main() {
	cin>>n;
	for(int i = 0;i < n;i++) {
		int x, y;
		cin>>x>>y;
		a.pb(x + y);
		b.pb(x - y);
	}
	sort(all(a));
	sort(all(b));
	cout<<max(abs(a[n - 1] - a[0]), abs(b[n - 1] - b[0]));
	return 0;
}

Measured memory_bytes: 5048
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;cin>>n>>k;int mn=INT_MAX,dp[k+1];
    vector<int>v(n);
    for(int i=0;i<n;i++){cin>>v[i];mn=min(mn,v[i]);}
    for(int i=0;i<mn;i++)dp[i]=0;
    for(int i=mn;i<=k;i++)
    {dp[i]=0;
    for(int j=0;j<n;j++)
    {
        if((i-v[j]>=0)&&(dp[i-v[j]]==0))
        dp[i]=1;
    }
    }
    if(dp[k])cout<<"First";
    else cout<<"Second";
    
}

Measured memory_bytes: 640
data = [
    [[0 for r in range(10)] for f in range(3)] for b in range(4)
]

# ??\??????????????????????????§????????????????????????????????????
count = int(input())
for c in range(count):
    b, f, r, v = [int(i) for i in input().split()]
    data[b-1][f-1][r-1] += v

for bi, b in enumerate(data):
    for f in b:
        for r in f:
            print(' {0}'.format(r), end='')

        print()

    if bi < 3:
        print('#' * 20)

Measured memory_bytes: 7656
# Si, Sj, Skは必ずR,G,Bのどれか
# Rから1つ、Gから1つ、Bから1つ選ぶ組合せ
# Rの数 * Gの数 * Bの数 = 組合せの総数
# j-i != k-jは全体からj-i == k-jを引く
# i,jが決まるとkは2j-iと決まる

n = int(input())
S = input()

countR = S.count('R')
countG = S.count('G')
countB = S.count('B')

# R,G,Bの組み合わせ
total = countR * countG * countB

count=0
for i in range(n):
    for j in range(i+1,n):
        k = int(2*j - i)
        if k >= n:
            break
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            count+=1

total -= count

print(total)


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

int main() {
  long long int n,k,i,ans ;
  long long int arr[300000] ;
  cin>>n>> k;
  for(i=0;i<n;i++){
    cin>>arr[i] ;
  }
  if(k<n){
  sort(arr,arr+n);

  ans=0;
  for(i=0;i<=n-k-1;i++){
    ans=ans+arr[i];
  }
  cout<< ans; 
}

else  {
  cout<< 0;
}
}

Measured memory_bytes: 2560
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID (l+r)/2
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;

#define EPS 1e-6

#define T double
typedef complex<T> pt;
#define x real()
#define y imag()
T dot(pt v, pt w) {return (conj(v)*w).x;}
T cross(pt v, pt w) {return (conj(v)*w).y;}
T orient(pt a, pt b, pt c) {return cross(b-a,c-a);}
T dist(pt a, pt b) {return abs(a-b);}

int main(){
	int n, k;
	cin>>n>>k;
	int v[n+1], w[n+1];
	for(int i=1;i<=n;i++) cin>>w[i]>>v[i];
	ll b[n+1][k+1] = {};
	for(int i=1;i<=n;i++) b[i][w[i]] = v[i];
	for(int i=1;i<=n;i++){
		for(int j=1;j<=k;j++){
			if(j-w[i]>=0) b[i][j] = max(b[i][j], b[i-1][j-w[i]] + v[i]);
			b[i][j] = max(b[i][j], b[i-1][j]);
			b[i][j] = max(b[i][j], b[i][j-1]);
		}
	}
	cout<<b[n][k]<<endl;
}


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

int A, B, N;

int find(int i)
{
	int r = 0;

	while (true)
	{
		if (i / 10 == 0)
		{
			r += i;
			goto exit;
		}

		int tmp = i % 10;
		r += tmp;
		i /= 10;
	}
 exit:
	return r;
}

int main(int argc, char **argv)
{
	cin >> N >> A >> B;
	vector<int> v;
	v.reserve(1000000);

	for (int i = 1; i <= N; ++i)
	{
		int sum_each = find(i);

		if (A <= sum_each && sum_each <= B)
			v.push_back(i);
	}

	std::cout << accumulate(v.begin(), v.end(), 0) << std::endl;
}


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

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto& e : a) cin >> e;
    sort(a.begin(), a.end());
    
    long long ans = 0;
    for (int _ = 0; _ < 2; _++) {
        int il = 1, ir = n-1;
        deque<long long> que;
        que.push_back(a[0]);
        while (il != ir) {
            vector<long long> d(4);
            d[0] = abs(que.front() - a[il]);
            d[1] = abs(que.back() - a[il]);
            d[2] = abs(que.front() - a[ir]);
            d[3] = abs(que.back() - a[ir]);
            long long mx = *max_element(d.begin(), d.end());
            if (mx == d[0]) que.push_front(a[il++]);
            else if (mx == d[1]) que.push_back(a[il++]);
            else if (mx == d[2]) que.push_front(a[ir--]);
            else que.push_back(a[ir--]);
        }
        if (abs(que.front()-a[il]) > abs(que.back()-a[il])) {
            que.push_front(a[il]);
        } else {
            que.push_back(a[il]);
        }
        long long res = 0;
        while (que.size() > 1) {
            long long t = que.front(); que.pop_front();
            res += abs(t - que.front());
            //cout << t << " ";
        }
        //cout << que.front() << endl;
        if (ans < res) ans = res;
        reverse(a.begin(), a.end());
    }
    
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1480
import java.io.*;
import java.util.*;
import java.util.function.*;

final class Solver {
	static final Solver INSTANCE = new Solver();

	void solve(Supplier<String> sc) {
		int N = Integer.parseInt(sc.get());
		int[] A = new int[N];
		Arrays.setAll(A, $ -> Integer.parseInt(sc.get()));
		int[] bucket = new int[100000 + 2];
		long sum = 0;
		for (int value : A) {
			bucket[value]++;
			sum += value;
		}

		int Q = Integer.parseInt(sc.get());
		for (int i = 0; i < Q; i++) {
			int B = Integer.parseInt(sc.get());
			int C = Integer.parseInt(sc.get());
			sum -= (long) B * bucket[B];
			sum += (long) C * bucket[B];
			bucket[C] += bucket[B];
			bucket[B] = 0;
			System.out.println(sum);
		}
	}
}

class Main {
	public static void main(String... args) {
		System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
		var reader = new BufferedReader(new InputStreamReader(System.in));
		Solver.INSTANCE.solve(new Supplier<>() {
			StringTokenizer line;

			public String get() {
				while (line == null || !line.hasMoreTokens()) try {
					line = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new UncheckedIOException(e);
				}
				return line.nextToken();
			}
		});
		System.out.flush();
	}
}

Measured memory_bytes: 55620
/*****/

#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <iomanip>	//cout << fixed << setprecision(桁数);
#include <stdexcept>

//#define int long long

using namespace std;
using ll = long long;
using ld = long double;
using vecll = vector<ll>;

#define nextline() (std::cerr << "\n")
#define debug(x) (#x " : " + (x))
#define debug_cout(x) (std::cerr << #x << " : " <<  (x) << "\n")
#define debug_tabcout(n, x) (std::cerr << strMulti("\t",n) << #x << " : " <<  (x) << "\n")
#define debug_headcout(h, x) (std::cerr << h << " : " <<  (x) << "\n")
#define debug_tabheadcout(n, h, x) (std::cerr << strMulti("\t",n) << h << " : " <<  (x) << "\n")

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep1(i, n) for(int i=1, i##_len=(n); i<=i##_len; i++)
#define rrep(i, n) for(int i=(n)-1; i >= 0; i--)
#define rrep1(i, n) for(int i=(n); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define pair(a, b) make_pair(a, b);
constexpr int INF = 2000000100;
constexpr ll DIV = 1000000007;	// 10e9+7
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
//template<class T> T divup(T a, T b) { if (a % b == 0) { return a / b; } return a / b + 1; }
#define divup(a, b) ((a) % (b) == 0 ? (a) / (b) : (a) / (b) + 1)
#define exist_in(list, value) (list.end() != std::find(all(list),value))
std::string strMulti(std::string t, int n) { std::string out = ""; for (int i = 0; i < n; i++) { out += t; } return out; }
//template<class T> T math_P(T m, T n) { T ret = 1; for (T i = m; i > m - n; i--) { ret *= i; } return ret; }
//template<class T> T math_C(T m, T n) { T ret = mathP(m, n); for (T i = 2; i <= n; i++) { ret /= i; } return ret; }
//template<class T> bool compare_by_b(pair<T, T> a, pair<T, T> b) { if (a.second != b.second) { return a.second < b.second; } return a.first < b.first; }
//template<class T> T math_gcd(T a, T b) { a = abs(a); b = abs(b); if (a < b) { std::swap(a, b); } T tmp; while (b != 0) { tmp = b; b = a % b; a = tmp; } return a; }
//template<class T> T math_lcm(T a, T b) { a = abs(a); b = abs(b); if (a > b) { std::swap(a, b); } if (a == 1) { return b; } T i = a; while (i % b != 0) { i += a; } return i; }
//template<class T1, class T2> bool exist_in(const T1& list, const T2& v) { return (list.end() != std::find(list.begin(), list.end(), v)); }
//template<class T> bool exist_in(const T& c, const typename T::value_type& v) {	return (c.end() != std::find(c.begin(), c.end(), v));}

/*****/

void Main()
{
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	if (a + b > c + d)
		cout << "Left";
	else if (a + b == c + d)
		cout << "Balanced";
	else
		cout << "Right";
}

/*****/

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

	std::cout << std::fixed << std::setprecision(10);


	/*
	while (true)
	{
		Main();
		cout << endl;
	}
	//*/


	Main();

	return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main(){
    int n;
    cin >> n;
    cout << (1000 - n%1000)%1000 << endl;
    return 0;
}


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

//https://qiita.com/tubo28/items/e6076e9040da57368845
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let m = 1000_000_000 + 7;
    let s: usize = read();
    let mut dp = vec![0; s + 1];
    //初期値設定
    dp[0] = 1;

    for i in 3..=s {
        //3個前までのdpを足し込む
        for j in 0..=i - 3 {
            dp[i] += dp[j];
            dp[i] %= m;
        }
    }
    println!("{}", dp[s]);
}


Measured memory_bytes: 2132
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <chrono>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include <random>
#include <utility>
#include <limits>
#include <list>
#include <type_traits>

/* template start */
 
#define rep(i, a, b) for (long long i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()

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

void debug_out(){std::cerr<<std::endl;}

template<typename Head,typename... Tail>
void debug_out(Head h,Tail... t){
  std::cerr<<" "<<h;
  if(sizeof...(t)>0)std::cout<<" :";
  debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}

template<typename Num>
constexpr Num mypow(Num a, long long b) {
  if(b==0)return 1;
  if (a==0)return 0;
  Num x = 1;
  while (b > 0) {
    if(b & 1)x*=a;
    a*=a;
    b >>= 1;
  }
  return x;
}

/* template end */

using ll = long long;

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

  std::string s;
  std::string t;
  std::cin>>s>>t;

  ll n=s.size();

  ll ans=0;
  rep(i,0,n)if(s[i]!=t[i])ans++;

  std::cout<<ans<<"\n";

  return 0;
}

Measured memory_bytes: 3772
h,w = map(int,(input().split()))
li = []
jud = 1
for i in range(h):
    li.append(list(input()))
dx = [0,1,-1,0]
dy = [1,0,0,-1]
num = 0
for i in range(h):
    for j in range(w):
        if li[i][j] == '#':
            num=0
            for d in range(len(dx)):
                ni = i+dy[d]
                nj = j+dx[d]
                if ni < 0 or h <= ni:
                    continue
                if nj < 0 or w <= nj:
                    continue
                if li[ni][nj] == '#':
                    num+=1
            if num == 0:
                jud = 0
                break

if jud == 0:
    print("No")
if jud == 1:
    print("Yes")

Measured memory_bytes: 3064