akhauriyash/Code-Regression huggingface dataset:
from fractions import gcd
n,k = map(int,input().split())
a = list(map(int,input().split()))
m = max(a)
g = a[0]
for t in a:
  g = gcd(g,t)

if k%g == 0 and k <= m:
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")

Measured memory_bytes: 16208
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
listh = [list(map(int, input().split())) for i in range(Q)]

combi = list(range(M))

test = list(combinations_with_replacement(combi,N))

ans = 0

for i in test:
    sum = 0
    for j in range(Q):
        if i[listh[j][1]-1] - i[listh[j][0]-1] == listh[j][2]:
            sum += listh[j][3]
    if(sum > ans):
        ans = sum

print(ans)

Measured memory_bytes: 21616
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n,k;
  cin>>n>>k;
  int arr[n];
  for(int i=0;i<n;i++)
    cin>>arr[i];
  sort(arr,arr+n);
  int sum=0;
  for(int i=0;i<k;i++)
    sum=sum+arr[i];
  cout<<sum<<endl;

  
  
}

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

int main() {
    int N;  cin >> N;
    int c = N / 4;
    int d = N / 7;
    int res = 0;
    for (int i = 0; i <= c; ++i) {
        for (int j = 0; j <= d; ++j) {
            int total = 4 * i + 7 * j;
            if (total == N) ++res;
        }
    }
    cout << (res ? "Yes" : "No") << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main() {
    int X, Y;
    cin >> X >> Y;
    int ans = 0;
    if (X == 1) {
        ans = ans + 300000;
    }
    if (X == 2) {
        ans = ans + 200000;
    }
    if (X == 3) {
        ans = ans + 100000;
    }
    if (Y == 1) {
        ans = ans + 300000;
    }
    if (Y == 2) {
        ans = ans + 200000;
    }
    if (Y == 3) {
        ans = ans + 100000;
    }
    if (X == 1 && Y == 1) {
        ans = ans + 400000;
    }
    cout << ans << endl;
}

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

typedef unsigned long long ull;

int main(void) {
    array<ull, 3> d;
    cin >> d[0] >> d[1] >> d[2];
    sort(d.begin(), d.end());

    if(d[0] % 2 == 0 || d[1] % 2 == 0 || d[2] % 2 == 0) {
        cout << "0";
    }
    else {
        cout << min(d[0]*d[1], min(d[1]*d[2], d[2]*d[0]));
    }

    return 0;
}

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

int32_t main(){
  int n,k;
  cin>>n>>k;
  int a[n+1] = {0};
  for(int i=0;i<k;i++){
  	int d;
    cin>>d;
    for(int j=0;j<d;j++){
    	int x;cin>>x;a[x]++;
    }
  }
  int ans = 0;
  for(int i=1;i<=n;i++) if(a[i] == 0) ans++;
  cout << ans << endl;
}

Measured memory_bytes: 3624
import java.util.Scanner;
class Main{
    public static void main(String[] a){
		Scanner in = new Scanner(System.in);
		int top, second, third, input;
		top = -1;
		second = -2;
		third = -3;
		for(int i=0; i<10; i++){
			input = in.nextInt();
			if(input>top){
				third = second;
				second = top;
				top = input;
			}else if(input>second){
				third = second;
				second = input;
			}else if(input>third){
				third = input;
			}
		}
		System.out.println(top);
		System.out.println(second);
		System.out.println(third);
    }
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int n;
  cin >> n;
  cout << pow(n, 3);
}

Measured memory_bytes: 384
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	std::string S, ans = "Yes";
	std::cin >> S;

	int a[] = { 0, 0, 0, 0 };
	for (int i = 0; i < 4; i++){
		a[i] = std::count(S.cbegin(), S.cend(), S[i]);
	}
	for (int i = 0; i < 4; i++){
		if (a[i] != 2){
			ans = "No";
			break;
		}
	}

	std::cout << ans << std::endl;

	return 0;
}

Measured memory_bytes: 256
// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで
// -fsanitize=undefinedでオーバーフロー検出
#include <bits/stdc++.h>
#define mod 1000000007
#define INF LLONG_MAX
#define ll long long
#define ln cout<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};
bool comp(pair<ll, ll> a, pair<ll , ll> b) {
    // 基本はfirstで比較
    if(a.first != b.first){
         return a.first < b.first; // 昇順
        //return a.first > b.first; // 降順
    }

    // それ以外はsecondで比較
    if(a.second != b.second){
        return a.second > b.second;
    }else{
        // どちらも同じ
        return true;
    }
}

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

    ll a,b,c,d,m,n,maxi=0,f=0,mini=INF,sum=0;
    ll k,l;
    string str;
    ll q;
    cin>>n;
    vector<pair<ll,ll>> v(n);
    rep(i,n){
        cin>>a>>b;
        v[i]=make_pair(b,a);
    }
    sort(all(v),comp);
    sum+=v[0].second;
    sum+=v[0].first;
    cout<<sum<<endl;
    return 0;
}


Measured memory_bytes: 4792
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)

using namespace std;

int main() {
	int n, cnt, ans;
	cin >> n;
	int A[300000] = { 0 };
	rep(i, n) {
		char c;
		cin >> c;

		if (i == 0) {
			if (c == 'E')A[i] += 1; 
		}

		if (i != 0) {
			A[i] = A[i - 1];
			if (c == 'E')A[i] += 1;
		}
	}

	for (int i = 0;i < n;i++) {
		cnt = 0;

		if (i == 0) {
			ans = A[n - 1] - A[0];
		}

		if (i != 0&&i!=n-1) {
			cnt += i - A[i - 1];
			cnt += A[n - 1] - A[i];
			if (ans > cnt)ans = cnt;
		}
		if (i == n - 1) {
			cnt += i - A[i - 1];
			if (ans > cnt)ans = cnt;
		}
	}
	cout << ans << endl;
	return 0;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
#define pii pair <int, int>
#define X first
#define Y second

using namespace std;

const int OFF = (1 << 16);
vector <int> ve[OFF * 2];
int bio[2 * OFF], low[2 * OFF], scc[2 * OFF];
int cnt = 0, ind = 0, n;
stack <int> st;
int on_stack[2 * OFF];
int inde[2 * OFF];
vector <pii> tmp;

void dfs(int x) {
  bio[x] = low[x] = cnt++;
  st.push(x);
  on_stack[x] = 1;

  for (int y : ve[x]) {
    if (bio[y] == -1) {
      dfs(y);
      low[x] = min(low[x], low[y]); 
    } else if (on_stack[y]) {
      low[x] = min(low[x], bio[y]);
    }
  }

  if (low[x] == bio[x]) {
    while (st.size()) {
      int y = st.top();
      st.pop();
      on_stack[y] = 0;
      scc[y] = ind;
      if (y == x) break;
    }
    ind++;
  }

  return;
}

void tarjan(int N) {
  cnt = 1, ind = 0;
  memset(scc, -1, sizeof scc);
  memset(bio, -1, sizeof bio);

  REP(i, N) {
    if (bio[i] == -1) dfs(i);
  }

  return;
}

void connect(int a, int b, int k, int lo = 0, int hi = OFF, int x = 1) {
  if (b <= lo || hi <= a) return;
  if (a <= lo && hi <= b) {
    ve[k].push_back(x);
    return;
  }

  int mi = (lo + hi) / 2;
  connect(a, b, k, lo, mi, x * 2);
  connect(a, b, k, mi, hi, x * 2 + 1);

  return;
}

void generate(int D) {
  REP(i, OFF) ve[i + OFF].clear();

  int l = 0, r = 0;
  REP(i, 2 * n) {
    while (l < 2 * n && tmp[l].X < tmp[i].X - D) l++;
    while (r < 2 * n && tmp[r].X <= tmp[i].X + D) r++;

    connect(l + 2 * n, i + 2 * n, i + OFF);
    connect(i + 1 + 2 * n, r + 2 * n, i + OFF);
  }

  REP(i, n) {
    int a = inde[2 * i];
    int b = inde[2 * i + 1];

    ve[a + OFF].push_back(b + 2 * n + OFF);
    ve[b + OFF].push_back(a + 2 * n + OFF);
    ve[b + 2 * n + OFF].push_back(a + OFF);
    ve[a + 2 * n + OFF].push_back(b + OFF);
  }

  return;
}

bool check(int mi) {
  generate(mi);
  tarjan(2 * OFF);

  bool flag = 1;
  REP(i, 2 * n) {
    if (scc[i + OFF] == scc[i + 2 * n + OFF]) flag = 0;
  }
  return flag;
}

int main() {
  ios_base::sync_with_stdio(false);

  FOR(i, 1, OFF) {
    ve[i].push_back(i * 2);
    ve[i].push_back(i * 2 + 1);
  }

  cin >> n;
  REP(i, 2 * n) {
    int a;
    cin >> a;
    tmp.push_back({a, i});
  }

  sort(tmp.begin(), tmp.end());
  REP(i, 2 * n) inde[tmp[i].Y] = i;

  int lo = 0, hi = 1e9;
  while (lo < hi) {
    int mi = (lo + hi + 1) / 2;

    if (check(mi)) lo = mi;
    else           hi = mi - 1;
  }
  if (lo == 0 && !check(0)) cout << 0 << endl;
  else                      cout << lo + 1 << endl;

  return 0;
}

Measured memory_bytes: 14080
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n;
  cin >> n;
  int n1 = n / 1000;
  int n2 = n % 1000 / 100;
  int n3 = n % 100 / 10;
  int n4 = n % 10;
  if (n1 == n2 && n2 == n3)
    cout << "Yes" << endl;
  else if (n2 == n3 && n3 == n4)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

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

int main(void) {
	int n;
	cin >> n;
	if (n % 111 == 0)
		cout << n << "\n";
	else {
		switch (n / 111) {
			case 0:
				cout << 111 << "\n";
				break;
			case 1:
				cout << 222 << "\n";
				break;
			case 2:
				cout << 333 << "\n";
				break;
			case 3:
				cout << 444 << "\n";
				break;
			case 4:
				cout << 555 << "\n";
				break;
			case 5:
				cout << 666 << "\n";
				break;
			case 6:
				cout << 777 << "\n";
				break;
			case 7:
				cout << 888 << "\n";
				break;
			case 8:
				cout << 999 << "\n";
				break;
		}
	}

	return 0;
}


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

int main() {
    int A, B, C;
    cin >> A >> B >> C;
    if (A + B >= C - 1) {  // Cを全部食べられる時
        cout << B + C << endl;
    } else {  // Cを全部食べることができない時
        cout << (A + B + 1) + B << endl;
    }
}


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

int main() {
  int W, H, N;
  cin >> W >> H >> N;
  
  vector<int> vec(3*N);
  for(int i=0; i<3*N; i++){
    cin >> vec.at(i);
  }
  
  int a = 0;
  int b = W;
  int c = 0;
  int d = H;
  
  int j = 0;
  
  while(j<N){
    if(vec.at(j*3+2) == 1){
      if(vec.at(j*3) > a){
       a = vec.at(j*3); 
      }
    }
    if(vec.at(j*3+2) == 2){
      if(vec.at(j*3) < b){
       b = vec.at(j*3); 
      }
    }
    if(vec.at(j*3+2) == 3){
      if(vec.at(j*3+1) > c){
       c = vec.at(j*3+1);
      }
    }
    if(vec.at(j*3+2) == 4){
      if(vec.at(j*3+1) < d){
        d =vec.at(j*3+1);
      }
    }
  
    j++;
  }
  
  if((b - a) <0  || (d - c) < 0){
    cout << 0 << endl;
  }
  else{
  cout << (b - a) * (d - c) << endl;
  }

}

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

int main() {
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int dp[k+1];
	memset(dp,-1,sizeof(dp));
	dp[0]=0;
	/*for(int i=0;i<n;i++){
		dp[a[i]]=1;
	}*/
	for(int i=1;i<=k;i++){
		if(dp[i]==-1){
			for(int j=0;j<n;j++){
				if(i>=a[j]){
				if(dp[i-a[j]]==0){
					dp[i]=1;
					break;
				}
				}
			}
			dp[i]=(dp[i]==1)?1:0;
		}
		//cout<<dp[i]<<endl;
	}
	if(dp[k])cout<<"First";
	else cout<<"Second";
	return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	int t=0;
	for(int i=0;i<s.length();i+=2)
	{
		if(s[i]!='h'||s[i+1]!='i')
			t=1;
	}
	if(t)
		cout<<"No"<<endl;
	else
		cout<<"Yes"<<endl;

}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
    int h,w;
    
    while(1){
        cin >> h >> w;
        if(h == 0 && w == 0) break;
        
        for(int i = 1; i <= h; i++){
            
            if(i%2 != 0){    //iが奇数の時
                for(int j = 1; j <= w; j++){
                    if(j%2 != 0) cout <<"#";
                    else cout << ".";
                }
            
            }else{     //iが偶数の時
                for(int j = 1; j <= w; j++){
                    if(j%2 != 0) cout <<".";
                    else cout <<"#";
                }
                
            }
            cout << endl;
        }
        
        cout << endl;
    }
    
    
    return 0;
}


Measured memory_bytes: 3080
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
#include<string>
#include<stdio.h>
#include<math.h>
#include<unordered_map>

using namespace std;
using vec = vector<int>;
using mat = vector<vec>;
using lst = list<int>;


vector<char> stationList(52);
unordered_map<char, int> AtoI;


bool solve()
{
	int N;
	cin >> N;

	if (N == 0)return false;

	vec K(N);

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

	string s;
	cin >> s;

	for (int i = 0; i < s.length(); ++i)
	{
		int id = AtoI[s[i]];
		id = (id - K[i % N] + 52) % 52;
		cout << stationList[id];
	}

	cout << endl;

	return true;
}


int main()
{
	for (int i = 0; i < 26; ++i)
	{
		stationList[i] = (char)(i + 'a');
		AtoI[(char)(i + 'a')] = i;
	}
	for (int i = 0; i < 26; ++i)
	{
		stationList[i + 26] = (char)(i + 'A');
		AtoI[(char)(i + 'A')] = i + 26;
	}


	while(solve())
	{
	}

	return 0;
}


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

int main(){
  int n, w;
  cin >> n >> w;
  vector<ll> weight(n+1), value(n+1);
  rep(i,n) cin >> weight[i+1] >> value[i+1];
  vector<vector<ll>> dp(n+1, vector<ll>(100001, 10000000000000));
  int ans = 0;
  dp[0][0] = 0;
  for(int i=1; i<=n; i++){
    for(int v=0; v<100001; v++){
      if(v-value[i] >= 0) dp[i][v] = min(dp[i-1][v], dp[i-1][v-value[i]] + weight[i]);
      else dp[i][v] = dp[i-1][v];
      if(dp[i][v] <= w) ans = max(ans, v);
    }
  }
  //int ans = 100000;
  //while(dp[n][ans] > w) ans--;
  cout << ans << endl;
}


Measured memory_bytes: 83256
s = input()
p = input()

flag = False
for i in range(len(s)):
    if s[i] == p[0]:
        flag = True
        for j in range(len(p)):
            if s[(i+j)%len(s)] != p[j]:
                flag = False
                break
        if flag == True:
            break

if flag == True:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 5560
#include<bits/stdc++.h>
#define INF 2147483647
#define lli long long int
#define MOD 1000000007
#define pb push_back

using namespace std;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
int main() {
	int w,a,b; cin >> w >> a >> b;

	if (a + w < b){
		cout << b - a - w << endl;
	}else if (b + w < a){
		cout << a - b - w << endl;
	}else{
		cout << 0 << endl;
	}

	return 0;
}





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

int main(){
    string s;
    int a,b;
    cin >> a >> b;
    cin >> s;
    int frg = 1;
    if(s[a]!='-')frg=0;
    for(int i=0;i<a+b+1;i++){
        if(i!=a)if(s[i]<'0' || '9'<s[i])frg=0;
    }
    if(frg)cout << "Yes\n";
    else cout << "No\n";
}


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

using namespace std;
using LLONG = long long;

int main()
{
    int A, B, C;
    cin >> A >> B >> C;
    if (A + B >= C)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
int main(){
    int n,a,b,time=0,x=1;
    cin>>n;
    vector<pair<int,int>> A;
    rep(i,n) cin>>a>>b,A.push_back(make_pair(b,a));
    sort(A.begin(),A.end());
    rep(i,n){
        time+=A[i].second;
        if(time>A[i].first) x=0;
    }
    if(x) cout<<"Yes";
    else cout<<"No";
}

Measured memory_bytes: 2420
#include <bits/stdc++.h>
#define ll long long
#define intm INT_MAX
#define llm LLONG_MAX

using namespace std;
int MOD = 998244353;

char ar[4050];

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	int N;
	string s;
	cin >> N >> s;
	for(int i=1; i <= N; i++) ar[i] = s[i-1];
	map<int,int> m1;
	for(int i=1; i <= N; i++){
        if( ar[i] == 'R' ) m1[1] += 1;
        else if( ar[i] == 'B' ) m1[2] += 1;
        else if( ar[i] == 'G' ) m1[3] += 1;
	}
    ll ans = 1;
    ans = ans*m1[1]*m1[2]*m1[3];
    for(int i=1; i <= N-1; i++){
        for(int j=i+1; j <= N; j++){
            int k = 2*j - i;
            if( k <= N && ar[i] != ar[j] && ar[j] != ar[k] && ar[i] != ar[k] ) --ans;
        }
    }
    cout << ans;
}


Measured memory_bytes: 3656
//  Mayank Agarwal
//  IIIT Hyderabad



#include<bits/stdc++.h>
using namespace std;
const long long int M = 1000000007; 
#define ll long long int
#define ld double
#define pb push_back
#define mp make_pair
ll power(ll x, ll y,  ll p) 
{ 
    ll res = 1;      
    x = x % p;   
    while (y > 0) 
    { 
        if (y & 1) 
            res = (res*x) % p; 
        y = y>>1;x = (x*x) % p; 
    } return res%p; 
} 
ll modinver(ll a,ll b)
{
    return (power(a,1,M)*power(b,M-2,M))%M;
}
void SieveOfEratosthenes(int n) 
{   
    bool prime[10005]; 
    memset(prime, true, sizeof(prime)); 
  
    for (int p=2; p*p<=n; p++) 
    { 
        if (prime[p] == true) 
        { 
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
} 
ll n,a[102],k;
ll dp[100005][3];
ll kstonesans(ll stones,ll chance)
{
	if(dp[stones][chance]!=-1)
		return dp[stones][chance];
	if(chance==0)
	{	
		ll ans=0;
		for (int i = 0; i < n; ++i)
		{
			if(stones>=a[i])
			{
				ans=ans||kstonesans(stones-a[i],1-chance); 
				if(ans==1)
				{
					break;
				}
			}
			else{
				break;
			}
		}
		dp[stones][chance]=ans;
		return ans;
	}
	else
	{
		ll ans=1;
		for (int i = 0; i < n; ++i)
		{
			if(stones>=a[i])
			{
				ans=ans&&kstonesans(stones-a[i],1-chance);
				if(ans==0)
				{
					break; 
				}
			}
			else{
				break;
			}
		}
		dp[stones][chance]=ans;
		return ans;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>k;
    for (int i = 0; i <=100002; ++i)
    {
    	dp[i][0]=-1;
    	dp[i][1]=-1;
    }
    for (int i = 0; i < n; ++i)
    {
    	cin>>a[i];
    }
    sort(a,a+n);
    if(kstonesans(k,0))
    {
    	cout<<"First\n";
    }
    else
    {
    	cout<<"Second\n";
    }

}


Measured memory_bytes: 7296
N = int(input())
A = list(map(int, input().split()))

mod = 10**9 + 7

F = 1
for i in range(2, N + 1):
	F = F * i % mod

D = [0]
for i in range(2, N + 1):
	D.append(D[-1] + pow(i, mod - 2, mod))

L = [0] * N
for i in range(N // 2 + N % 2):
  t = round(F * (D[-1 - i] + D[i] + 1)) % mod
  L[i] = t
  L[-1 - i] = t

Ans = 0
for i in range(N):
  Ans += L[i] * A[i] % mod

print(Ans % mod)

Measured memory_bytes: 14484
#include <bits/stdc++.h>
using namespace std;
int main() {
    int N,K;
    cin >> N >> K;
    vector<int> count(N,0);
    int d;
    int A;
    int cnt = 0;
    for(int i=0;i<K;i++) {
        cin >> d;
        for(int j=0;j<d;j++) {
            cin >> A;
            count.at(A-1)++;
        }
    }
    for(int i=0;i<N;i++) {
        if(count.at(i) == 0) {
            cnt++;
        }
    }
    cout << cnt << endl;

}

Measured memory_bytes: 3636
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;

void main()
{
    auto N = readln.chomp.to!long;
    int[] aa;
    auto ii = new long[](N+1);
    foreach (i, a; readln.split.to!(int[])) {
        aa ~= a;
        ii[a] = i.to!int;
    }

    auto rbt = redBlackTree([ii[1]]);
    long res = (ii[1] + 1) * (N - ii[1]);
    foreach (long a; 2..N+1) {
        long l, r;
        auto ls = rbt.lowerBound(ii[a]);
        if (ls.empty) {
            l = ii[a] + 1;
        } else {
            l = ii[a] - ls.back;
        }
        auto rs = rbt.upperBound(ii[a]);
        if (rs.empty) {
            r = N - ii[a];
        } else {
            r = rs.front - ii[a];
        }
        res += (l * r) * a;
        rbt.insert(ii[a]);
    }
    writeln(res);
}

Measured memory_bytes: 21364
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <algorithm>

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

typedef long long ll;

using namespace std;
int main(){
	const int num = 2000001;
    int K ,X;
    cin >> K >> X;
    for (int i = X - K + 1; i <= X + K - 1; ++i)
    {
    	cout << i << " ";
    }
}


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

typedef long long ll;

void printArray(vector<int> arr) {
	for (int a: arr) cout << a << " ";
	cout << '\n';
}

string evaluate(ll n, ll i) {
	string ret = "";
	while (n > 0) {
		n--;
		ret = char(97 + n % 26) + ret;
		n /= 26;
	}
	return ret;
}

int main() {
	std::ios_base::sync_with_stdio(false);
	ll n;
	cin >> n;
	vector<ll> check;
	check.push_back(0ll);
	ll i = 0, p=1;
	while (check.back() < n) {
		i++;
		p *= 26;
		check.push_back(check.back() + p);
	}
	if (n == check.back()) {
		for (ll j = 0; j<i; j++) cout << "z";
		cout << '\n';
	}
	else cout << evaluate(n, i) << '\n';
	
}



Measured memory_bytes: 3592
# -*- coding: utf-8 -*-
from itertools import product
from bisect import bisect, bisect_left
def inpl(): return list(map(int, input().split()))
A, B, Q = inpl()
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]

for x in X:
    ans = 1e12
    ls, lt = S[max(0, bisect(S, x)-1)], T[max(0, bisect(T, x)-1)]
    rs, rt = S[min(A-1, bisect_left(S, x))], T[min(B-1, bisect_left(T, x))]
    for s in [ls, rs]:
        for t in [lt, rt]:
            if s < t:
                a, b = s, t
            else:
                a, b = t, s

            if x < a:
                if ans > b-x:
                    ans = b-x
            elif b < x:
                if ans > x-a:
                    ans = x-a
            else:
                if ans > 2*(x-a) + (b-x):
                    ans = 2*(x-a) + (b-x)
                if ans > (x-a) + 2*(b-x):
                    ans = (x-a) + 2*(b-x)
    print(ans)

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

#define MOD (1000000000+7)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(), x.end()
#define print(vec,l,r) for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl;

typedef long long int ll;

int limit = 1e5 + 1;

ll f(vector<vector<ll> > &dp, ll r, ll c, pair<ll,ll> p, ll W){
	ll v = p.second, w = p.first;
	if(v > r) return LLONG_MAX;
	else{
		if(dp[r-v][c-1] == LLONG_MAX) return LLONG_MAX;
		if(dp[r-v][c-1] + w <= W){
			return dp[r-v][c-1] + w;
		}
		else return LLONG_MAX;
	}
}

void solve(){
	ll N;
	cin >> N;
	ll W;
	cin >> W;
	vector<pair<ll,ll> > sack(N);
	for(ll i = 0; i < N; i++){
		ll t1, t2;
		cin >> t1 >> t2;
		sack[i] = mp(t1,t2);
		//limit += t1;
	}
	//limit++;
	sort(all(sack));
	
	vector<vector<ll> > dp(limit,vector<ll>(N,LLONG_MAX));
	for(ll i = 0; i < N; i++) dp[0][i] = 0;
	
	//dp[sack[0].second][0] = sack[0].first;
	
	for(int i = 0; i < N; i++){
		dp[sack[0].second][i] = sack[0].first;
	}
	
	for(ll i = 1; i < limit; i++){
		for(ll j = 1; j < N; j++){
			ll t= f(dp,i,j,sack[j], W);
			dp[i][j] = min(dp[i][j-1],t);
		}
	}

	for(ll i = limit-1; i >= 0; i--){
		if(dp[i][N-1] != LLONG_MAX){
			cout << i << endl;
			break;
		}
	}
//	for(int i = 0; i < limit; i++){
//		cout << i << ": ";
//		for(int j = 0; j < N; j++){
//			cout << dp[i][j] << " ";
//		}
//		cout << endl;
//	}
}

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

	int T;
	//cin >> T;
	T = 1;
	while(T--){
	//T = 1;
		solve();
	}
	return 0;
}




Measured memory_bytes: 82432
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
	//入力
	string S;
	int ans,count;
  	ans =0;
  	count =0;
	bool f = false;
	cin >> S;
	//処理
	for (int i = 0; i < S.length(); i++) {
		if (S.at(i) == 'A' || S.at(i) == 'C' || S.at(i) == 'G' || S.at(i) == 'T') {
			if (f = false) {
				f = true;
			}
			count++;
			ans = max(ans, count);
		}
		else {
          f = false;
          count = 0;
        }
	}
	//出力
	cout << ans << endl;
}

Measured memory_bytes: 256
s = [1] * 13
h = [1] * 13
c = [1] * 13
d = [1] * 13

n = int(input())
for _ in range(n):
    a, b = input().split()
    b = int(b)
    if a == "S":
        s[b - 1] = 0
    elif a == "H":
        h[b - 1] = 0
    elif a == "C":
        c[b - 1] = 0
    else:
        d[b - 1] = 0

for i in range(13):
    if s[i]:
        print(f"S {i+1}")
for i in range(13):
    if h[i]:
        print(f"H {i+1}")
for i in range(13):
    if c[i]:
        print(f"C {i+1}")
for i in range(13):
    if d[i]:
        print(f"D {i+1}")


Measured memory_bytes: 5616
#include <stdio.h>

int main(){
  char word;
  scanf("%c", &word);
  switch(word){
    case 'a':
    case 'i':
    case 'u':
    case 'e':
    case 'o':printf("vowel");
      break;
    default:printf("consonant");
      break;
  }
  return 0;
}

Measured memory_bytes: 128
n = int(input())
a = list(map(int,input().split()))
MOD = 10**9 + 7

rui = [0]*n
cur = 0
for i in range(n):
    cur = (cur+a[i]) % MOD
    rui[i] = cur
    
ans = 0
for i in range(n-1):
    ans = (ans + (a[i+1] * rui[i])) % MOD
    
print(ans)

Measured memory_bytes: 94112
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;

int main(){
  string str;
  string fstr;
  
  cin >> str;
  cin >> fstr;
  
  while(str.find(fstr)!=-1){
    cout << str.find(fstr) << endl;
    str[str.find(fstr)]='#';
  }
  
  return 0;

}

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

class SegmentTree {
private:
	long long Size;
	long long Unit;
	vector<long long> Node;
public:
	SegmentTree(long long N, long long X) : Unit(X) {
		Size = 1;
		while (Size < N) Size *= 2;
		Node.assign(Size * 2, Unit);
	}
	void update(long long L, long long R, long long X) {
		long long NOWS = Size;
		while (L < R) {
			if (L & 1) {
				Node[L + NOWS] += X;
				L++;
			}
			if (R & 1) {
				Node[R + NOWS - 1] += X;
				R--;
			}
			L >>= 1, R >>= 1, NOWS >>= 1;
		}
	}
	long long query(long long X) {
		long long ANS = 0;
		long long NOWS = Size;
		while (NOWS > 0) {
			ANS += Node[X + NOWS];
			if (X & 1) X--;
			X >>= 1, NOWS >>= 1;
		}
		return ANS;
	}
};

int main() {
	long long N, Q;
	cin >> N >> Q;
	SegmentTree Seg(N, 0);
	for (int i = 0; i < Q; i++) {
		long long COM;
		cin >> COM;
		if (COM == 0) {
			long long S, T, X;
			cin >> S >> T >> X;
			Seg.update(S - 1, T, X);
		}
		else {
			long long X;
			cin >> X;
			cout << Seg.query(X - 1) << endl;
		}
	}
}


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


int main(){

    string S;
    cin >> S;
    sort(S.begin(), S.end());
    if(S[0]==S[1] && S[1]!=S[2] && S[2]==S[3]){
        cout << "Yes" << endl;
    }else{
        cout << "No" << 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;
using ll = long long;
using P = pair<int,int>;

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> A(N);
  int maxi=0;
  cin >> A.at(0);
  for(int i=1; i<N; i++){
    cin >> A.at(i);
    maxi = max(A.at(i) - A.at(i-1), maxi);
  }
  if(A.at(0) == 0){
   maxi = max(maxi, K-A.at(N-1)); 
  }else{
   maxi = max(maxi, K-A.at(N-1)+A.at(0)); 
  }
  cout << K -  maxi << endl;
  return 0;
}




Measured memory_bytes: 4036
#include<bits/stdc++.h>
using namespace std;
//size_t f = string::npos
//make pair, tuple

int main(){
      string s;cin >> s;
      size_t start = s.find('A');
      size_t f = s.find('Z');
      while(f < start || s.find('Z', f + 1) != string::npos)f = s.find('Z', f + 1);
      cout << f - start + 1;
}

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

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;
using namespace std;

int main() {
    int n, r;
    cin >> n >> r;
    cout << (n >= 10 ? r : r + 100 * (10 - n)) << endl;

    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
 
using namespace std;
 
void outPut(int Array[],char mark){
    for(int i = 1; i <= 13; i++){
        if(Array[i] == 0) printf("%c %d\n",mark,i);
    }
}
 
int main(){
    char mark[2];
    int n,S[14],H[14],C[14],D[14],tmp;
    for(int i = 0; i < 14; i++) S[i] = H[i] = C[i] = D[i] = 0;
    scanf("%d",&n);
 
    for(int i = 0; i < n; i++){
        scanf("%s %d",mark,&tmp);
        switch(mark[0]){
        case 'S':
            S[tmp] = 1;
            break;
        case 'H':
            H[tmp] = 1;
            break;
        case 'C':
            C[tmp] = 1;
            break;
        case 'D':
            D[tmp] = 1;
            break;
        }
    }
    outPut(S,'S');
    outPut(H,'H');
    outPut(C,'C');
    outPut(D,'D');
}


Measured memory_bytes: 2692
#include<stdio.h>
int main(void){
	long int x,k,d,j;
	scanf("%ld %ld %ld",&x,&k,&d);
  if(x<0) x = x*(-1);
        	if(x/d > k){
            	printf("%ld",x-k*d);
             	return 0;
            }
          	else{
            	j = x/d;
              	x -= j*d;
              	if((k-j)%2 == 1){
                	printf("%ld",(x-d)*(-1));
                  	return 0;
                }
              	else{
                	printf("%ld",x);
                  	return 0;
                }
            }

  	return 0;
}

Measured memory_bytes: 1736
s=gets
[*?a..?z].each{|c|
  if i=s.index(c+c)
    puts "#{i+1} #{i+2}"
    exit
  elsif i=s.index(%r{#{c}.?#{c}})
    puts "#{i+1} #{i+3}"
    exit
  end
}
puts "-1 -1"

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

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

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define INF 1000000009
#define INFL 1000000000000000018
#define mod 10007
#define pb push_back
#define MAXN 505
#define ff first
#define sf second.first
#define ss second.second
using namespace std;
typedef pair<int, int> PA;
typedef priority_queue<int> PQ;
typedef vector<int> VE;
typedef map<int, int> MAP;

int n, k, x[200000], ans = INF, l, r;

signed main()
{
    cin >> n >> k;
    for(int i = 0; i < n; i++){
        cin >> x[i];
    }
    
    for(int i = 0; i < n - k + 1; i++){
        int l = i, r = i + k - 1;
        ans = min(ans, abs(x[r] - x[l])+min(abs(x[l]), abs(x[r])));
    }
    cout << ans << endl;


    return 0;
}



Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+9;
ll arr[maxn];
ll pos[maxn];
ll xu[maxn];
int main(){
    ios::sync_with_stdio(0);
    ll a,b,c,d,e,f,g,h,n;
    cin>>n>>d;
    pos[0]=d;
    for(int i=1;i<=n;i++){
        cin>>arr[i];
        if(arr[i]<=pos[i-1]){
            pos[i]=pos[i-1]-arr[i];
        }
        else{
            pos[i]=min(pos[i-1],arr[i]-pos[i-1]);
        }
    }
    e=0;
    for(int i=n;i>0;i--){
        if(!e){
            if(arr[i]!=1){
                xu[i]=0;
            }
            else{
                xu[i]=1;
                e=1;
            }
        }
        else{
            f=e+1;
            if(arr[i]<f*2ll){
                e+=arr[i];
            }
            xu[i]=e;
        }
    }
    /*for(int i=1;i<=n;i++){
        cout<<pos[i]<<' '<<xu[i]<<endl;
    }*/
    cin>>b;
    while(b--){
        cin>>e;
        if(pos[e-1]<=xu[e+1]){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }
    return 0;
}


Measured memory_bytes: 13696
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int x,y;
  cin>>x>>y;
  if(y%2==1){
    cout<<"No"<<endl;
  }
  else if(y<2*x){
    cout<<"No"<<endl;
  }
  else if(y>4*x){
    cout<<"No"<<endl;
  }
  else{
    cout<<"Yes"<<endl;
  }

}

Measured memory_bytes: 3632
// The moment you think of giving up you have already lost the battle
#include <iostream>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI  3.14159265358979323846
#define Mod 1000000007
ll fast(ll n,ll a,ll m)
{
    if(n==0)
        return 1;
    else if(n==1)
        return a;
    else
    {
        ll r=fast(n/2,a,m);
        if(n%2==0)
        {
            return (((r%m)*(r%m))%m);
        }
        else
        {
            return (((((r%m)*a)%m)*r)%m);
        }

    }
}
const ll N=1e7+5;
ll  dp[N+1]={0};
int main()
{
    ll a;
    cin>>a;
    ll b=a+a*a+a*a*a;
    cout<<b;






}


Measured memory_bytes: 3592
#include<bits/stdc++.h>
using namespace std;
int n;
main()
{
    int ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int l,r;scanf("%d%d",&l,&r);
        ans+=(r-l+1);
    }
    printf("%d\n",ans);
    return 0;
}



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

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  
  vector<long long> res(n,0);
  for(int i=1; i<n; i++) {
      for(int j=i+1; j<=n; j++) {
          if((i <= x && j <= x) || (i >= y && j >= y))
              ++res[j-i];
          else if(i <= x && j >= y) 
              ++res[(x-i) + (j-y) + 1];
          else {
                if(i <= x && j <= y) {
                  int temp = min(j-x, y-j+1);
                  ++res[(x-i) + temp];
                }
                else if(i >= x && j <= y) {
                  int temp = min(j-i, (i-x)+(y-j)+1);
                  ++res[temp];
                }
                else {
                  int temp = min(y-i, i-x+1);
                  ++res[(j-y) + temp];
                }
          }
      }
  }
    
    for(int i=1; i<n; i++)
        cout << res[i] << endl;
  
  return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <map>
#include <string>

using namespace std;

pair<int, int> par[256];

void init(int n)
{
	for (int i = 0; i < n; i++){
		par[i] = make_pair(i, 0);
	}
}

pair<int, int> find(int x)
{
	if (par[x].first == x) return (par[x]);
	pair<int, int> ret = find(par[x].first);
	return (par[x] = make_pair(ret.first, par[x].second + ret.second));
}

bool same(int u, int v)
{
	return (find(u).first == find(v).first);
}

void merge(int u, int v, int val)
{
	pair<int, int> uu = find(u);
	pair<int, int> vv = find(v);
	
	if (uu.first == vv.first) return;
	
	// 1 u = 10^val v
	par[uu.first] = make_pair(vv.first, vv.second + val - uu.second);
}

int main()
{
	int n;
	
	while (scanf("%d", &n) && n){
		getchar();
		
		map<string, int> mp;
		init(256);
		bool ok = true;
		
		for (int i = 0; i < n; i++){
			char s[1024], t[1024];
			int val;
			
			scanf("1 %s = 10^%d %s", s, &val, t);
			getchar();
			
			string ss = s, st = t;
			if (!mp.count(ss)){
				int ct = mp.size();
				mp[ss] = ct;
			}
			if (!mp.count(st)){
				int ct = mp.size();
				mp[st] = ct;
			}
			
			int u = mp[ss], v = mp[st];
			if (same(u, v)){
				if (par[u].second - par[v].second != val) ok = false;
			}
			else {
				merge(u, v, val);
			}
		}
		printf("%s\n", ok ? "Yes" : "No");
	}
	
	return (0);
}

Measured memory_bytes: 1092
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <math.h>

using namespace std;

typedef long long int ll;
typedef long double ld;

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define vsort(a) sort(a.begin(), a.end())
#define rev(a) reverse(a.begin(), a.end())

const int MOD = 1000000007;
const int INF = INT_MAX;



int main(){

    
    int n;
    cin >> n;
    vector <pair<ll, ll>> p(n);
    rep(i,n) cin >> p[i].first >> p[i].second;

    rep(i,n) swap(p[i].first, p[i].second);

    vsort(p);

    int flag = 1;
    ll time = 0;
    rep(i,n){
        time += p[i].second;
        if (p[i].first < time){
            flag = 0;
        }

    }

    if (flag) cout << "Yes" << endl;
    else cout << "No" << endl;
    

}





//              g++ -std=c++1z


Measured memory_bytes: 3328
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#define fi "darts.inp"
#define fo "darts.out"
using namespace std;
long long n,m,a[100011],ans;
vector<long long > vt;
int main()
{
//    freopen (fi , "r" ,stdin);
//    freopen (fo , "w" ,stdout);
    while (1)
    {
    scanf("%lld%lld",&n,&m);
    if (n==0&&m==0) return 0;
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    ans=0;
    n++;
    a[n]=0;
    vt.clear();
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            vt.push_back(a[i]+a[j]);
        }
    sort(vt.begin(),vt.end());
    int c;
    c=vt.size()-1;
    for (int i=0;i<vt.size();i++)
    {
        while (c>=0&&vt[c]+vt[i]>m)
            c--;
        if (vt[c]+vt[i]<=m) ans=max(ans,vt[c]+vt[i]);
    }
    cout<<ans<<endl;
    }
    return 0;
}

Measured memory_bytes: 10116
N = int(input())
p = [int(i) for i in input().split()]

ret = 0
for i in range(0, N - 2) :
    p_ = p[i:i+3]
    if p_[0] < p_[1] < p_[2] or p_[2] < p_[1] < p_[0] :
        ret += 1
        
print(ret)


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

 

int main()
{
   int a, b;     
   cin >> a >> b;
   
   if (a == b)
      cout << "a == b" << endl;
   else if(a > b)
      cout << "a > b" << endl;
   else
      cout << "a < b" << endl;
   
   return 0;
}


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

 int main() {
	 int N;
	 scanf("%d", &N);
	 vector<int>v(N);
	 for (int i = 0;i < N;++i) {
		 cin >> v[i];

	 }
	 sort(v.begin(), v.end());

	 int answer = v[N / 2] - v[N / 2 - 1];

	 printf("%d", answer);
	 return 0;
 }

Measured memory_bytes: 640
n = input()
s = str(n)
a = list(map(int, s))
if a[0] == a[1]:
    result = "Bad"
elif a[1] == a[2]:
    result = "Bad"
elif a[2] == a[3]:
    result = "Bad"
else:
    result = "Good"
print(result)


Measured memory_bytes: 2940
import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val N = sc.nextInt
  var A = Array.fill(N)(sc.nextInt)
  val Q = sc.nextInt
  val BC = Array.fill(Q)((sc.nextInt, sc.nextInt))

  val count = Array.fill(100001)(0L)
  val sum = Array.fill(Q+1)(0L)
  A.foreach(x => {
    count(x) += 1
    sum(0) += x
  })

  for (i <- 0 until Q) {
    val (b, c) = BC(i)
    sum(i+1) = sum(i) + count(b) * (c - b)
    count(c) += count(b)
    count(b) = 0
    println(sum(i+1))
  }
}


Measured memory_bytes: 65360
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

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






int main() {
    ll n, x;
    cin >> n >> x;
    vector<ll> s(n);
    REP(i, n){
        cin >> s[i];
    }
    sort(s.begin(), s.end(), greater<ll>());
    vector<vector<ll>> dp(n + 1, vector<ll>(x + 1));
    dp[0][x] = 1;
    REP(i, n){
        for(ll j = 0; j <= x; j++){
            dp[i + 1][j % s[i]] += dp[i][j];
            dp[i + 1][j % s[i]] %= MOD;
            dp[i + 1][j] += dp[i][j] * (n - 1 - i);
            dp[i + 1][j] %= MOD;
        }
    }
    ll ans = 0;
    for(ll i = 0; i <= x; i++){
        ans += dp[n][i] * i;
        ans %= MOD;
    }
    cout << ans << endl;
}

Measured memory_bytes: 158592
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
#include<math.h>
#include<map>
#include<set>
#include <climits>
using namespace std;
#define INF 11000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> P;

int C[10];

int main(){
    int n;
    for(int i=0;i<4;i++){
        cin>>n;
        C[n]++;
    }
    if(C[1] && C[9] && C[7] && C[4]){
        cout<<"YES"<<endl;
    }else cout<<"NO"<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug" << x << endl;
using namespace std;

int main(){
    int w,h,x,y,r;
    while(cin >> w >> h >> x >> y >> r){
        bool c = true;
        if(x - r < 0 || x + r > w) c = false;
        if(y - r < 0 || y + r > h) c = false;
        if(c) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}

Measured memory_bytes: 1156
n = int(input())
h = list(map(int, input().split()))
l = [0, abs(h[0] - h[1])] + [-1] * (n-2)

for i in range(2, n):
    l[i] = min(l[i-2]+abs(h[i-2]-h[i]), l[i-1]+abs(h[i-1]-h[i]))

print(l[-1])

Measured memory_bytes: 20648
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<cmath>
#include<stack>
#include<iomanip>
#include<cstdio>
using namespace std;
int main()
{
    int n,x,y; //n个点
    //int ditu[2005][2005]={0};
    int ans[2005]={0};
    cin>>n>>x>>y;
    /*for(int i = 1;i<=n;i++)
    {
        ditu[i][i+1]=1;
        ditu[i+1][i]=1;
    }
    ditu[x][y]=1;
    ditu[y][x]=1;
     */
    int t1,t2,min;
    for(int i = 1;i<=n-1;i++)
        for(int j = i + 1;j<=n;j++)
        {
                if(i<=x&&j>=y)//可以省的情况
                {
                    ans[1+x-i+j-y]+=1;
                }
                else if(i<=x&&j>x&&j<y)
                {
                    t1=j-i;//3 6
                    t2=(y-j)+1+x-i;
                    min= t1>t2?t2:t1;
                    ans[min]+=1;
                }
                else if(i>=x&&j<=y)
                {
                    t1=j-i;//3 6
                    t2=(y-j)+1+i-x;
                    min= t1>t2?t2:t1;
                    ans[min]+=1;
                }
                else if(i>=x&&j>y)
                {
                    t1=j-i;//3 6
                    t2=(j-y)+1+i-x;
                    min= t1>t2?t2:t1;
                    ans[min]+=1;
            }
                else
                    ans[j-i]+=1;//j-1代表路镜头长度
            
        }
    for(int i = 1;i<=n-1;i++)
    {
        cout<<ans[i]<<endl;
    }
    
    return 0;
}


Measured memory_bytes: 256
N,T = map(int,input().split())
t = list(map(int,input().split()))
X = 0
for i in range(N):
  if i==0: #1st 
    end = T #お湯が止まる時間
  
  elif t[i]>end: 
    X+=T
  elif t[i]<=end:
    X+=t[i]-t[i-1]
    
  end = t[i]+T

X+=T #last
print(X)

Measured memory_bytes: 30812
#include <bits/stdc++.h>
using namespace std;
int isprime(int n)
{
  for(int i=2;i*i<=n;i++)
  {
    if(n%i==0)return 0;
  }
  return 1;
}
int main()
{
  int n;
  cin>>n;
  if(n<4)
  {
    cout<<n<<endl;
    return 0;
  }
  for(int i=n;i<1000000000;i++)
  {
    if(isprime(i))
    {
      cout<<i<<endl;
      break;
    }
  }
  return 0;
}

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



int main() {
    int n;
    cin >> n;
    
    vector<int> a(n+1);
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    
    vector<int> b, all(n+1);
    for(int i = n; i >= 1; i--) {
        int cnt = 0;
        for(int j = i; j <= n; j+=i) {
            if(all[j] == 1)
                cnt++;
        }
        
        if((cnt % 2 == 0 && a[i] == 1) ||
           (cnt % 2 == 1 && a[i] == 0)) {
            all[i] = 1;
            b.push_back(i);
        }
    }
    
    int bs = (int)b.size();
    
    cout << bs << endl;
    rep(i,bs) cout << b[i] << endl;
    
    
    return 0;
}




Measured memory_bytes: 5436
#include <bits/stdc++.h>
#define MAX 100000

int A[MAX], n;

using namespace std;

int partition(int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

int main() {
  int i, q;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    /* code */
  }
  q = partition(0, n - 1);

  for (i = 0; i < n; i++) {
    if (i) printf(" ");
    if (i == q) printf("[");
    printf("%d", A[i]);
    if (i == q) printf("]");
    /* code */
  }
  printf("\n");

  return 0;
}


Measured memory_bytes: 3536
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;

int main() {        
    int a, b, c, x, y;
    cin >> a >> b >> c >> x >> y;
    if (a + b >= 2 * c) {
        int z = min(x, y);
        x -= z;
        y -= z;
        cout << min(z * 2 * c + x * a + b * y, max(x, y) * c * 2 + z * 2 * c) << endl;
    } else {
        cout << a * x + b * y << endl;
    }
}


Measured memory_bytes: 3628
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <functional>
#include <numeric>
#include <iomanip>
#include <climits>
#include <cstring>

using namespace std;

#define int long long
#define rep(i, n) for (int i=0;i<(int)(n);++i)
#define rep1(i, n) for (int i=1;i<=(int)(n);++i)
#define rrep(i, n) for (int i=(int)(n)-1;i>=0;--i)
#define rrep1(i, n) for (int i=(int)(n);i>=1;--i)
#define range(i, l, r) for (int i=l;i<(int)(r);++i)
#define rrange(i, l, r) for (int i=(int)(r)-1;i>=l;--i)
#define unless(a) if(!(a))
#define all(a) begin(a),end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back

using vi=vector<int>;
using pii=pair<int, int>;
using vpii=vector<pii>;
using ll=long long;

bool chmin(int&a,int b){return a>b?(a=b,true):false;}
bool chmax(int&a,int b){return a<b?(a=b,true):false;}
int read(){int a;scanf("%lld",&a);return a;}

signed main()
{
	int a,b,c,k;cin>>a>>b>>c>>k;
	int m=min(a,k);
	int ans = m;
	if (a == m) {
		//puts("de");
		k-=a;
		k-=b;
		chmax(k,0ll);
		ans+=-min(k,c);
		cout<<ans<<endl;
	} else {
		cout << ans << endl;
	}

}




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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, m, n) for(int i = m; i >=n; i--)

#define INF 999999999

#define llong long long
int main()
{
    int a, b, c;
    cin >> a;
    cin >> b;
    cin >> c;
    if (a == b && b == c)
    {
        cout << 0 << endl;
        return 0;
    }
    int numlist[] = {a, b, c};
    stable_sort(numlist, numlist + 3);
    int maxn = numlist[2];
    int ot1 = maxn - numlist[0];
    int ot2 = maxn - numlist[1];
    //cout << maxn << " " << ot1 << " " << ot2 << endl;
    if ((ot1 % 2 == 0) && (ot2 % 2 == 0))
    {
        int res = (ot1 / 2) + (ot2 / 2);
        cout << res << endl;
    }
    else if ((ot1 % 2 == 1) && (ot2 % 2 == 1))
    {
        int res = 1 + ((ot1 - 1) / 2) + ((ot2 - 1) / 2);
        cout << res << endl;
    }
    else if ((ot1 % 2 == 1) && (ot2 % 2 == 0))
    {
        int res = 1 + ((ot1 + 1) / 2) + ot2 / 2;
        cout << res << endl;
    }
    else
    {
        int res = 1 + ot1 / 2 + (ot2 + 1) / 2;
        cout << res << endl;
    }
    return 0;
}

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

int main() {
  int n;
  string s;
  cin >> n;
  bool a[4] = {false};
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s[0] == 'P') {
      a[0] = true;
    }
    else if (s[0] == 'W') {
      a[1] = true;
    }
    else if (s[0] == 'G') {
      a[2] = true;
    }
    else {
      a[3] = true;
    }
  }
  int cnt = 0;
  for (int i = 0; i < 4; i++) {
    if (a[i]) {
      cnt++;
    }
  }
  if (cnt == 3) {
    cout << "Three" << endl;
  }
  else {
    cout << "Four" << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
	string s;
	cin >> s;
	for (int i = 0; i < s.length(); i++) {
		if ( (s[i] == 'R' && i % 2 == 1) || (s[i] == 'L' && i % 2 == 0) ) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;
}

Measured memory_bytes: 256