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

int main() {
    ll N;
    cin >> N;
    vector<ll> A(N),B(N);
    ll sumA =0;
    vector<ll> point(N);
    for(int i=0;i<N;i++){
        cin >> A[i] >> B[i];
        sumA += B[i];
        point[i] = A[i] + B[i];
    }
    ll ans  = -sumA;
    sort(point.rbegin(),point.rend());
    for(int i=0;i<N;i++){
        if(i%2==1){
            continue;
        }
        ans += point[i];
    }
    cout << ans << endl;


    return 0;
}

Measured memory_bytes: 2688
#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 <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <unordered_set>
using namespace std;
inline long  toInt(string s) {long  v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define long long long
long  pw(long  x, long  y){long  ans = 1;for(long  i = 0; i < y; i++){ans = ans * x;}return ans;}
long  factorial(long  x){long  ans = 1; for(int i = 1 ;i < x+1; i++){ans = ans *i;}return ans;}
long  combination(long  x,long  y){return factorial(x)/factorial(y)/factorial(x-y);}
long  permutation(long  x, long  y){return factorial(x)/factorial(x-y);}
#define TimeCount1    auto chrono_start = std::chrono::system_clock::now();
#define TimeCount2   auto chrono_end = std::chrono::system_clock::now();std::cout << "Elapsed time(C++):" << std::chrono::duration_cast<std::chrono::milliseconds>(chrono_end - chrono_start).count() << "[ms]" << std::endl;

#define MOD 1000000007


int main(){
	long n,k,x,y;
	cin >> n>> k >> x >> y;
	if(n <= k){
		cout << x * n << endl;
		return 0;
	}else{
		cout << x * k + y * (n - k) << endl;
		return 0;
	}

}



Measured memory_bytes: 256
#include<iostream>
#include <cstdio>
#include<cstring>
#include <cstdlib>  
#include<cmath>   
#include <cctype>
#include<string>
#include<set>
#include <iomanip>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include <deque>
#include <climits>
#include <typeinfo>
#include <utility>
#define LEN 100005
using namespace std;
using ll = long long;
double pi() { return 3.14; }
int sum(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
ll LGCD(ll x, ll y) {if (x % y == 0) return y;else return LGCD(y, x % y);}
ll LLCM(ll x, ll y) {return x * y / LGCD(x, y);}

int GCD(int x, int y) {if (x % y == 0) return y;else return GCD(y, x % y);}
int LCM(int x, int y) {return x * y / GCD(x, y);}
ll dp[10000][10000];
ll combination(ll n, ll r) {
         if (n == r){dp[n][n] = 1;}
	else if (r == 0) {return dp[n][0] = 1;}
	else if (r == 1) {return dp[n][1]=n;}
	else {dp[n][r]=dp[n - 1][r - 1] % 1000000007 + dp[n - 1][r] % 1000000007;
	return dp[n][r]=combination(n - 1, r - 1) % 1000000007 + combination(n - 1, r) % 1000000007;}
}

int main() {
	string A;
	int JOI = 0;
	int IOI = 0;
	while(cin>>A){
		int B = A.length();
		for (int i = 0;i < B-2;i++) {
			if (A.at(i) == 'J'&&A.at(i + 1) == 'O'&&A.at(i + 2) == 'I')JOI = JOI + 1;
			if (A.at(i) == 'I'&&A.at(i + 1) == 'O'&&A.at(i + 2) == 'I')IOI = IOI + 1;

		}
		cout<<JOI<<endl;
		cout << IOI << endl;
		JOI = 0;
		IOI = 0;
	}
	return 0;
}



Measured memory_bytes: 3164
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<map>
#include<algorithm>
#include<set>
#include <stdexcept>
#include<queue>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
int ceil(int a,int b){// a以上bの倍数の最小値//
     return (a+b-1)/b*b;
 }
int stoii(string s){//stoi関数の代用 文字列⇒int型//
    return atoi(s.c_str());
}

int R,C;
int sy,sx;
int gy,gx;
string c[51];
void solve(){
	queue<P> qp;
	qp.push({sy,sx});
}
int dp[1010][1010]={0};
int main(){
	string s;
	cin>>s;
	int sum=0;
	rep(i,s.size()){
		if(s[i]=='+') sum++;
		else sum--;

	}
	cout<<sum<<endl;
	return 0;
}

Measured memory_bytes: 256
def main():
  import sys,bisect
  input=sys.stdin.readline
  n,m=map(int,input().split())
  nums=list(map(int,input().split()))
  change=[list(map(int,input().split())) for i in  range(m)]
  
  nums.sort()
  change.sort(key=lambda i:i[1],reverse=True)
  
  limit=0
  i,j=0,0
  b,c=change[j][0],change[j][1]
  while i<n and nums[i]<c:
    nums[i]=c
    i+=1
    b-=1
    if b==0:
      j+=1
      if j<m:
        b,c=change[j][0],change[j][1]
      else:
        break
  print(sum(nums))
  
if __name__=='__main__':
  main()

Measured memory_bytes: 32900
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
template <class T> void chmin(T &a, const T &b) noexcept { if (b < a) a = b; }
template <class T> void chmax(T &a, const T &b) noexcept { if (a < b) a = b; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); }
#ifdef _DEBUG
  #define debug(...) debug_out(__VA_ARGS__)
#else
  #define debug(...) 
#endif

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  double r;
  cin >> r;
  double ans = 2*acos(-1)*r;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3800
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int X,tmp;
    while(scanf("%d",&X)==1)
    {
        if(X==0)
        {
            printf("0\n");
            continue;
        }
        if(X==1)
        {
            printf("1\n");
            continue;
        }
        tmp=(int)sqrt(X*2);
        int i;
        for(i=tmp-3;i*(i+1)<X*2;i++);
        printf("%d\n",i);
    }
    return 0;
}


Measured memory_bytes: 128
#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>;
//pow(x,n)
//cout << fixed << setprecision(桁数);


int main() {
    int k,s; cin >> k >> s;
    int cnt=0;
    for(int x=0;x<=k;++x){
        for(int y=0;y<=s-x && y<=k;++y){
            int z=s-x-y;
            if(0<=z && z<=k) ++cnt;
        }
    }
    cout << cnt << endl;
}





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

using namespace std;

int p[20];

int main() {

	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

	int n;
	cin >> n;
	int cnt = 0;
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
	}
	for (int i = 1; i < n - 1; ++i) {
		if ((p[i] < p[i - 1] && p[i] > p[i + 1]) ||
			(p[i] > p[i - 1] && p[i] < p[i + 1]))
			cnt++;
	}
	cout << cnt << '\n';
	return 0;
}

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() {
    //A
    int a,b,c;
    cin >> a >> b >> c;
    swap(a,b);
    swap(a,c);
    cout << a << " " << b << " " << c << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <set>
#include <tuple>
#include <algorithm>
#include <memory>
#include <numeric>
#include <queue>
#include <functional>
#include <cmath>
#include <cstring>
#include <map>
#include <unordered_map>
#include <sstream>

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

const ll INF = 1LL << 60;

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

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

	vector<vector<ll>> dp(s.size() + 1, vector<ll>(t.size() + 1, -1));

	for (ll i = 0; i < (ll)s.size(); ++i)
	{
		for (ll j = 0; j < (ll)t.size(); ++j)
		{
			if (s[i] == t[j])
			{
				chmax(dp[i + 1][j + 1], dp[i][j] + 1);
			}

			chmax(dp[i + 1][j + 1], dp[i + 1][j]);
			chmax(dp[i + 1][j + 1], dp[i][j + 1]);
		}
	}

	ll s_i = s.size();
	ll t_i = t.size();

	stringstream ss;
	while (true)
	{
		ll tmp = dp[s_i][t_i];
		if (s_i > 0) if (tmp == dp[s_i - 1][t_i])
		{
			s_i--;
			continue;
		}

		if (t_i > 0) if (tmp == dp[s_i][t_i - 1])
		{
			t_i--;
			continue;
		}

		s_i--;
		t_i--;
		if (s_i == -1 || t_i == -1) break;

		ss << s[s_i];
	}

	string sss = ss.str();
	std::reverse(sss.begin(), sss.end());
	cout << sss << endl;

	//system("pause");
}


Measured memory_bytes: 70784
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
maxi=0
for i in range(n):
    total=0
    x=sum(a[:i+1])
    y=sum(b[i:])
    total=x+y
    maxi=max(total,maxi)
print(maxi)

Measured memory_bytes: 3188
#include<bits/stdc++.h>
using namespace std;
int n,a[1000100],k,kk,b[100000];
int f[20][250010],lg[250010];
int gt()
{
	
    for(int i=2;i<=n;i++)
        lg[i]=lg[i>>1]+1;
    for(int k=0;k<19;k++)
        for(int i=1;i<=n;i++)
            f[k+1][i]=min(f[k][i],f[k][min(n,i+(1<<k))]);
}
int rmq(int l,int r)
{
	if(l>r)
    swap(l,r);
    k=lg[r-l+1];return min(f[k][l],f[k][r-(1<<k)+1]);
}
int ff[20][250010],lgf[250010];
int gtf()
{
	
    for(int i=2;i<=n;i++)
        lgf[i]=lgf[i>>1]+1;
    for(int k=0;k<19;k++)
        for(int i=1;i<=n;i++)
            ff[k+1][i]=max(ff[k][i],ff[k][min(n,i+(1<<k))]);
}
int rmqf(int l,int r)
{
	if(l>r)
    swap(l,r);
    k=lgf[r-l+1];return max(ff[k][l],ff[k][r-(1<<k)+1]);
}
int main(){
    scanf("%d%d",&n,&kk);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),f[0][i]=a[i],ff[0][i]=a[i];gtf();gt();
    int ans=1,la=n+1,bl=0;
    for(int i=1;i<=kk;i++)
    {
    	if(a[i]>=a[i-1])la=min(la,i);else la=max(la,i);
	}
    for(int i=kk+1;i<=n;i++)
    {ans++;//cout<<rmq(i-kk,i-1)<<" "<<rmqf(i-kk+1,i)<<endl;
    if(a[i]>=a[i-1])la=min(la,i);else la=max(la,i);//cout<<la<<" "<<bl<<endl;
		if(la<=i-kk+1)if(bl){ans--;continue;}else bl=1;
    	if(rmq(i-1-kk+1,i+kk-2-kk+1)!=a[i-1-kk+1])continue;
    	if(rmqf(i-kk+1,i+kk-1-kk+1)!=a[i+kk-1-kk+1])continue;//cout<<i<<endl;
		ans--;//cout<<i<<endl;
	}cout<<ans;
}

Measured memory_bytes: 42880
#include <bits/stdc++.h>
#define reoz(i, z, n) for(decltype(n) i = (z); i < (n); i++)
#define rep(i, n) for(decltype(n) i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define SIZE(v) ((ll)(v).size())
#define MAX(v) (*max_element(all(v)))
#define MIN(v) (*min_element(all(v)))
#define INF 1000000000000 //1E+12
#define MOD 1000000007    //合同式の法

// 略
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define READ cin >>
#define PRINT cout <<

using namespace std;
typedef long long ll;
typedef unsigned long long ul;
// 1s間で可能なループ回数2E+8回
// intの最大値2147483647~2E+9
// long longの最大値9223372036854775807〜9E+18
// 総和accumulate
// 単純リストvector<> push_back
// 連想配列map<key,value> insert,
// pair キーとバリューを格納 firstとsecondでアクセス
// 小数点指定 setprecision()
// to_string<->stoi
// stoll(longlong)
// stod(double)
// 大文字65-90(-32)
// 小文字97-122(+32)
// 1E+9 +7
const ll mod = 1000000000+7;
int main(){
  // faster
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int a,b,h,m; cin >> a >> b >> h >> m;
  double angle=30*h+0.5*m-6*m;
  //cout << angle << endl;
  //cout << cos(double(angle)/180.0/3.141592653589793238) << endl;
  
  double res = sqrt(double(a)*double(a)+double(b)*double(b)-2.0*double(a)*double(b)*cos(double(angle)/180.0*3.141592653589793238));
  
  cout << fixed << setprecision(20) << res << endl;
  return 0;
}


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

typedef long long ll;

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

    string s; cin >> s;
    int q; cin >> q;
    deque<char> dq;
    for(int i=0;i<s.size();i++) dq.push_back(s[i]);
    int cnt = 0;
    bool flag = false;
    for(int i=0;i<q;i++){
        int t; cin >> t;
        if(t==1){
            cnt++;
            if(cnt%2) flag = true;
            else flag = false;
        }
        else if(t==2){
            int f; char c;
            cin >> f >> c;
            if(f==1){
                if(flag) dq.push_back(c);
                else dq.push_front(c);
            }
            if(f==2){
                if(flag) dq.push_front(c);
                else dq.push_back(c);
            }
        }
    }
    if(cnt%2) reverse(dq.begin(),dq.end());
    for(auto c : dq) cout << c;
    cout << endl;
    return 0;
}

Measured memory_bytes: 976
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   //setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>  //sin, cos, tan

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T>
void fin(T a)
{
  cout << a << endl;
  exit(0);
}

void Main()
{
  str s; cin >> s;
  ll Max = 0;
  ll tmp = 0;
  rep(i, s.size())
  {
    if(s[i] == 'R') tmp ++;
    else
    {
      Max = max(Max, tmp);
      tmp = 0;
    }
  }
  Max = max(Max, tmp);
  fin(Max);
}

//前処理
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(4); //高精度少数表示
  Main();
  return 0;
}


Measured memory_bytes: 3564
#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define pb push_back
#define eb emplace_back

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;

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

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

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

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

template<class T>
void dcomb(T n,T m, string s){
    if(n==0) cout << s << endl;
    else {
        for(T i=0; i<m; ++i){
            dcomb(n-1,m,s+to_string(i));
        }
    }
}

int  INF = 1e9+7;

int main(){
    int n;
    cin >> n;
    vector<int> a(n),b(n),c(n);
    rep(i,n) cin >> a[i] >> b[i] >> c[i];
    vector<vector<int>> dp(n+1,vector<int>(3,0));
    rep(i,n){
        dp[i+1][0] = max(dp[i][1],dp[i][2])+a[i];
        dp[i+1][1] = max(dp[i][2],dp[i][0])+b[i];
        dp[i+1][2] = max(dp[i][0],dp[i][1])+c[i];
    }
    cout << *max_element(dp[n].begin(),dp[n].end()) << endl;
}

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

int main() {
    std::string W;
    std::cin >> W;

    int count = 0;
    while (true) {
        std::string word;
        std::cin >> word;
        if (word == "END_OF_TEXT") break;

        std::transform(word.begin(), word.end(), word.begin(), ::tolower);
        if (word == W) {
            count += 1;
        }
    }
    std::cout << count << std::endl;
}



Measured memory_bytes: 3184
#from collections import deque
import sys
input = sys.stdin.readline
def MI():return map(int,input().split())
def main():
  n,m=MI()
  G=[[] for _ in range(n)]
  for _ in range(m):
    u,v=MI()
    u-=1
    v-=1
    G[u].append(v)
  s,t=MI()
  s-=1
  t-=1
  
  fi=[True]*n
  se=[True]*n
  th=[True]*n
  th[s]=False
  
  #dq=deque([])
  #dq.append(s)
  dq=[s]
  depth=0
  
  while dq:
    depth+=1
    tank1=[]
    tank2=[]
    tank3=[]
    
    for p in dq:
      for c in G[p]:
        if fi[c]:
          fi[c]=False
          tank2.append(c)
    for p in tank2:
      for c in G[p]:
        if se[c]:
          se[c]=False
          tank3.append(c)
    for p in tank3:
      for c in G[p]:
        if th[c]:
          th[c]=False
          tank1.append(c)
        if c==t:
          print(depth)
          exit()
    dq=tank1
  print(-1)  
if __name__=='__main__':
  main()


Measured memory_bytes: 56316
//Solution by Tima
#include <bits/stdc++.h>
 
#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define vi vector <int>
#define ld long double
#define pii pair<int, int>
#define y1 sda
 
using namespace std;    
const int N = int(3e5) + 12, mod = 998244353; 

int n;
string s,t,p,q;

ll cnt[N];
vector <int> dv;

int main () {
	cin >> n >> s;
	int d = 1;
	int m = n;
	while(m % 2 == 0){
		d = d * 2;
		m /= 2;
	}
	ll ans = 0;
	for(int j = d; j <= n; j += d) if(n % j == 0){
		ll res = 0;
		p = "", q = "";;
		t = "";
		for(int i = 0; i < j; i++){
			res = (res * 2 + s[i] - '0') % mod;
			p += s[i];
			q += char(1 - (s[i] - '0') + '0');
		}
		int cur = 1;
		while(t.size() < s.size()) {
			if(cur) t += p;
			else t += q;
			cur ^= 1;
		}
		if(t <= s) res++;
		cnt[j] = res;
		for(int v : dv) if(j % v == 0) {
			cnt[j] -= cnt[v];
			if(cnt[j] < 0) cnt[j] += mod;
		}
		dv.pb(j);
		ans = (ans + 1ll * cnt[j] * j * 2) % mod;
	}
	cout << (ans + mod) % mod;
 
return 0;
}

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

using namespace std;

int N;
vector<int> L(100);

int main(){
    scanf("%d",&N);
    for(int i =0;i<N;i++){
        scanf("%d",&L[i]);
    }
    int max = *max_element(L.begin(),L.end());
    int sum = accumulate(L.begin(), L.end(), 0);
    int sub = sum - max;
    if(max < sub){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
}

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())

E = [[] for _ in range(N + 1)]

for _ in range(N-1):
    u, v, w = map(int, readline().split())
    E[u].append((v, w))
    E[v].append((u, w))

COLOR = [None] * (N + 1)


def setColor(v, c):
    COLOR[v] = c
    for w, l in E[v]:
        if COLOR[w] == None:
            nc = c
            if l % 2 != 0:
                nc = int(not nc)
            setColor(w, nc)


setColor(1, 0)

print("\n".join(map(str, COLOR[1:])))


Measured memory_bytes: 79380
using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            var h = consoleReader.Split.Int;

            var current = h[0] - 1;

            for (int i = 1; i < h.Length; i++)
            {
                if (current == h[i]) { }
                else if (current < h[i]) current = h[i] - 1;
                else return "No";
            }
            return "Yes";
        }

        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader()).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }

#pragma warning disable
    public class ConsoleReader { public class SplitLine { string[] splited; public SplitLine(string line) { splited = line.Split(); } public string[] String => splited.ToArray(); public int[] Int => splited.Select(int.Parse).ToArray(); public long[] Long => splited.Select(long.Parse).ToArray(); public ulong[] ULong => splited.Select(ulong.Parse).ToArray(); public double[] Double => splited.Select(double.Parse).ToArray(); } public class RepeatLine { ConsoleReader reader; int num; public RepeatLine(ConsoleReader reader, int num) { this.reader = reader; this.num = num; } public IEnumerable<string> String { get { for (int i = 0; i < num; i++) yield return reader.String; } } public IEnumerable<int> Int { get { for (int i = 0; i < num; i++) yield return reader.Int; } } public IEnumerable<long> Long { get { for (int i = 0; i < num; i++) yield return reader.Long; } } public IEnumerable<string[]> SplitString { get { for (int i = 0; i < num; i++) yield return reader.Split.String; } } public IEnumerable<int[]> SplitInt { get { for (int i = 0; i < num; i++) yield return reader.Split.Int; } } } public virtual string String => Console.ReadLine(); public int Int => int.Parse(String); public long Long => long.Parse(String); public ulong ULong => ulong.Parse(String); public double Double => double.Parse(String); public SplitLine Split => new SplitLine(String); public RepeatLine Repeat(int count) => new RepeatLine(this, count); }
}

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

int INF = 1ll << 30;

void chmin(int &a, int b){
  if(a > b)
    a = b;
}

int main(){
  ll n;
  cin >> n;
  vl a(n);
  vl sum(n);
  sum[0] = 0;
  ll mod = 1000000007;

  rep(i, 0, n){
    cin >> a[i];
  }
  sum[0] = a[0];
  rep(i, 1, n){
    sum[i] = (sum[i-1] + a[i]) % mod;
    sum[i] %= mod;
  }
  
  ll ans = 0;
  
  rep(i, 1, n){
    ans += (a[i] * sum[i-1]) % mod;
    ans %= mod;
  }
  
  if(ans < 0)
    ans += mod;
  
  cout << ans << endl;
  
  return 0;
  
}

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

int main(){
  int N,M;
  cin >> N >> M;
  vector<int> L(M);
  vector<int> R(M);
  for(int i=0;i<M;i++){
    cin >> L[i] >> R[i];
  }

  int ans=(*min_element(R.begin(),R.end()))
    -(*max_element(L.begin(),L.end()))+1;
  if(ans>0){
    cout << ans << endl;
  }else{
    cout << 0 << endl;
  }
  return 0;
}

Measured memory_bytes: 1024
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;


int main(){
    ll N, M;

    cin >> N >> M;

    ll H[N];
    ll p[N];
    ll ans = N;

    for(ll i = 0 ; i < N; i++){
        cin >> H[i];
        p[i] = 1;
    }

    ll A, B;

    for(ll i = 0; i < M; i++){
        cin >> A >> B;
        if(H[A - 1] >= H[B - 1]){
            if(p[B - 1] == 1){
                p[B - 1] = 0;
                ans--;
            }
        }
        if(H[A - 1] <= H[B - 1]){
            if(p[A - 1] == 1){
                p[A - 1] = 0;
                ans--;
            }
        }
    }

    cout << ans << endl;

    return 0;
}


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

using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()

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

  int H,W,K;
  cin>>H>>W>>K;
  vector<string>S(H);
  for(int i=0;i<H;i++)cin>>S[i];

  vector<vector<int>>ans(H,vector<int>(W,0));
  int c=0;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(S[i][j]=='#'){
        c++;
        for(;j<W;j++){
          ans[i][j]=c;
          if(j+1<W&&S[i][j+1]=='#')break;
        }
      }
    }
  }

  for(int i=0;i<H;i++){
    for(int j=W-2;j>=0;j--){
      if(ans[i][j]==0){
        ans[i][j]=ans[i][j+1];
      }
    }
  }

  for(int i=1;i<H;i++){
    bool b=true;
    for(int j=0;j<W;j++){
      if(ans[i][j]!=0)b=false;
    }
    if(b){
      for(int j=0;j<W;j++){
        ans[i][j]=ans[i-1][j];
      }
    }
  }

  for(int i=H-2;i>=0;i--){
    bool b=true;
    for(int j=0;j<W;j++){
      if(ans[i][j]!=0)b=false;
    }
    if(b){
      for(int j=0;j<W;j++){
        ans[i][j]=ans[i+1][j];
      }
    }
  }
  
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cout<<ans[i][j]<<" ";
    }
    cout<<endl;
  }
  return 0;
}


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

int main() {

    // input from txt （提出時にこの箇所は削除すること）
    //std::ifstream in("input.txt");
    //std::cin.rdbuf(in.rdbuf());
    
    int N, M, Q;
    cin >> N >> M >> Q;
    queue<string> S;
    for(int i=0; i<M; i++){
        string T = "";
        char x = '0' + i;
        T += x;
        S.push(T);
    }
    
    int q = S.size();
    
    for(int i=1; i<N; i++){
        for(int k=0; k<q; k++){
        string T = S.front();
        S.pop();
        int x = T.at(T.size()-1) - '0';
        for(int j=0; j<M; j++){
            if(x > j){
                continue;
            }
            else{
                string U = T;
                char y = '0' + j;
                U += y;
                S.push(U);
            }
        }
        }
        q = S.size();
    }
    
    vector<vector<int64_t>> vec(Q,vector<int64_t>(4));
    for(int i=0; i<Q; i++){
        for(int j=0; j<4; j++){
            cin >> vec[i][j];
        }
    }
    
    int64_t ans = 0;
    while(S.size()){
        int64_t ans_x = 0;
        string A = S.front();
        S.pop();
        for(int i=0; i<Q; i++){
            int a = vec[i][0] - 1;
            int b = vec[i][1] - 1;
            int SA = A[a] - '0';
            int SB = A[b] - '0';
            if(SB - SA == vec[i][2]){
                ans_x += vec[i][3];
            }
        }
        ans = max(ans,ans_x);
    }
    
    cout << ans << endl;
    
}




Measured memory_bytes: 6368
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = [int(i) * -1 for i in input().split()]
# print(a)
heapq.heapify(a)
for i in range(m):
    maximum = heapq.heappop(a) * -1
    heapq.heappush(a, (maximum // 2) * -1)
print(sum(a) * -1)


Measured memory_bytes: 97512
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

before = sum(a)
for i in range(n):
    if a[i] - b[i] >= 0:
        a[i] = a[i] - b[i] 
    else:
        b[i] = b[i] - a[i]
        a[i] = 0
        if a[i+1] - b[i] >= 0:
            a[i+1] = a[i+1] - b[i]
        else:
            a[i+1] = 0

after = sum(a)

print(before - after)






                                        




    




        


    







        



Measured memory_bytes: 18476
//****************************//
/**********  main *************/
//****************************//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
 
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,a,b) for(i=a;i<b;i++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
 
bool is_prime(const unsigned n){
    switch(n) {
        case 0: // fall-through
        case 1: return false;
        case 2: return true;
    } // n > 2 が保証された

    if(n % 2 == 0) return false;

    // 上で i=2 相当は調べたので、i=3から奇数のみ調べる
    for(unsigned i = 3; i * i <= n; i += 2) {
        if(n % i == 0) return false;
    }

    return true;
} 
 
 

int main(){
    int x;    
    cin >> x;
    int i;
    bool tmp;
    
    for(i=x;i<200000;i++){
        tmp = is_prime(i);
        if(tmp == true) {
            cout << i << endl;
            break;
        }
    }
    
} 
 
 
 

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

bool f(string s) {
    string rs = s;
    reverse(all(rs));
    return s == rs;
}

int main() {
    string S;
    cin >> S;
    int N = S.size();
    string S2 = S.substr(0,(N-1)/2);
    string S3 = S.substr((N+3)/2-1);
    
    cout << (f(S)&&f(S2)&&f(S3)?"Yes":"No") << endl;
}

Measured memory_bytes: 256
function Main(s){
  s = s.split("\n");
  var n = +s[0]-1;
  var a = s[1].split(" ").map(a=>+a);
  var b = [];
  b[n] = a.reduce((a,z,i)=>{b[i-1]=a;return a+z;});
  var ans = 9e15;
  for(var i = 1; i < n - 1; i++){
    var sl = b[i] / 2, sr = (b[n] + b[i]) / 2;
    var l = lower_bound(b,0,i-1,sl);
    var r = lower_bound(b,i+1,n-1,sr);
    var m = [b[l],b[i]-b[l],b[r]-b[i],b[n]-b[r]].sort((a,b)=>a-b);
    ans = Math.min(ans, m[3] - m[0]);
    if(l){
      m = [b[l-1],b[i]-b[l-1],b[r]-b[i],b[n]-b[r]].sort((a,b)=>a-b);
      ans = Math.min(ans, m[3] - m[0]);
      if(r - i > 1){
        m = [b[l-1],b[i]-b[l-1],b[r-1]-b[i],b[n]-b[r-1]].sort((a,b)=>a-b);
        ans = Math.min(ans, m[3] - m[0]);
      }
    }
    if(r - i > 1){
      m = [b[l],b[i]-b[l],b[r-1]-b[i],b[n]-b[r-1]].sort((a,b)=>a-b);
      ans = Math.min(ans, m[3] - m[0]);
    }
  }
  console.log(ans);
}
function lower_bound(d,a,b,e){for(var c;1<b-a;)d[c=0|(a+b)/2]<e?a=c:b=c;return d[a]<e?b:a};
Main(require("fs").readFileSync("/dev/stdin","utf8"));

Measured memory_bytes: 38712
import scala.io.StdIn

object Main extends App {
  val Array(a, b, c)  = StdIn.readLine().split(" ").map(_.toInt)

  def solve(a: Int, b: Int, c: Int): Int = {
    val d = a - b
    val ans = c - d

    if (ans < 0) 0 else ans
  }

  println(solve(a, b, c))
}
  



Measured memory_bytes: 27308
//start of jonathanirvings' template v3.0.3 (BETA)

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG 
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s); 
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s); 
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int k;
vector<LL> coba;
vector<pair<long double, LL>> risan;
LL batas = 1000000000000000LL;

LL S(LL x)
{
  if (x == 0) return 0;
  return x % 10 + S(x / 10);
}

int main()
{
  scanf("%d",&k);
  FORN(i,1,10000)
  {
    LL x = i;
    while (x < batas)
    {
      coba.pb(x);
      x = x * 10 + 9;
    }
  }
  REP(i,SIZE(coba))
    risan.pb(mp((long double)coba[i] / S(coba[i]), coba[i]));
  SORT(risan);
  LL now = 0;
  REP(i,SIZE(risan))
  {
    if (risan[i].se > now)
    {
      now = risan[i].se;
      printf("%lld\n",now);
      --k;
      if (k == 0) return 0;
    }
  }
  return 0;
}











Measured memory_bytes: 6640
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    int arr[n] = {};
    vector<int> dp(n,INT_MAX);

    for(int i=0;i<n;i++) cin >> arr[i];
    dp[0] = 0;
	

	for(int i=0;i<n;i++){
		for(int j: {i+1,i+2}){
			if(j<n){
				dp[j] = min(dp[j],dp[i] +abs(arr[j] - arr[i]));
			}
		}
	}
   	

   	cout << dp[n-1] << endl;


    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
using namespace std;
int i,j,k,n,m;
string s;

long gcd(long a, long b) {
  long r = b%a;
  long d;
  while(r!=0)
  {
    b = a;
    a = r;
    r = b%a;
  }
  if(r==0) d = a;
  return d;
}

int main() {
  long long x;cin >>n >> x;
  vector<long long> v(n);
  for(i=0;i<n; ++i) {
    long long t;
    cin >> t;
    v[i] = abs(t-x);
  }
  long ans = v[0];
  for (i=0;i<n-1;++i) {
    ans = gcd(ans,v[i+1]);
  }
  cout << ans << endl;
  return 0;
}


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

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int MOD = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;

int h[maxn], dp[maxn];

int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &h[i]), dp[i] = inf;

    dp[1] = 0;
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= min(i - 1, k); j++) dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }

    printf("%d\n", dp[n]);
	return 0;
}


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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

int main() {
source:
    ll s;
    cin >> s;
    ll a1, a2, b1, b2;
    a1 = mod - 7;
    a2 = 1;
    b2 = (s + a1 - 1) / a1;
    b1 = a1 * b2 - s;

    cout << 0 << " " << 0 << " " << a1 << " " << a2 << " " << b1 << " " << b2
         << endl;

    if (a1 * b2 - a2 * b1 != s) {
        cout << "No" << endl;
    }
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
     int N;
     cin >> N;
     vector<int> w(N);
     int sum = 0;
     for(int i = 0;i < N;i++){
          cin >> w[i];
          sum += w[i];
     }

     int ans = sum;
     int tsum = 0;
     for(int i = 0;i < N;i++){
          tsum += w[i];
          ans = min(ans,abs(tsum-(sum-tsum)));
     }

     cout << ans << endl;
}

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

count = 0

for i in range(n-2):
  list = [p[i],p[i+1],p[i+2]]
  sort_list = [p[i],p[i+1],p[i+2]]
  sort_list.sort()
  if sort_list[1] == list[1]:
    count = count +1
print(count)


Measured memory_bytes: 3060
#include<bits/stdc++.h>
#define mod 1000000007
#define lim 1000000000000000007
#define lim1 18446744073709551615  //Unsigned
#define sq(a) ((a)*(a))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define mms(v,i) memset(v,i,sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPN(i,a,b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> PL;
typedef pair<ll,int> PLI;
typedef pair<int,ll> PIL;
typedef pair<int,pair<int,int> > pii;
typedef pair<double,double> pdd;
ll power(ll a,ll b) {
    if (b==0) return 1;
    ll c = power(a,b>>1);
    c = (c*c)%mod;
    if (b&1) return (c*a)%mod;
    return c;
}
ll gcdll(ll a,ll b) {
    if (b==0) return a;
    return gcdll(b,a%b);
}
int gcd(int a,int b) {
    if (b==0) return a;
    return gcd(b,a%b);
}
const int N = (int)2e5+5;
const int Q = 301;

int a[N];
void solve() {
    int n;
    cin>>n;
    int prev = 0;
    REP(i,1,n) {
        cin>>a[i];
        if (a[i] == prev+1) {
            prev++;
        }
    }
    if (prev==0) prev = n+1;
    cout<<(n-prev);
}
int main() {
    //freopen("class_treasurer.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    //cin>>T;
    REP(TC,1,T) {
        //cout<<"Case #"<<TC<<": ";
        solve();
        cout<<"\n";
    }
}


Measured memory_bytes: 1024
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;

class Program
{
    static void Main(string[] args)
    {
    	var KX = ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
        var (K, X) = (KX[0], KX[1]);
        
        for(int i = X - K + 1; i < X + K; i++)
            Write($"{i} ");
    }
}


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

using namespace std;
typedef long long ll;
const ll MAX = 200010;
ll mod = 1000000007,inv[MAX],fact[MAX],fi[MAX];

void inverse(){
	int i;
	inv[0] = 1; inv[1] = 1;
	for(i=2;i<MAX;i++){
		inv[i] = mod - (mod/i)*inv[mod%i]%mod;
	}
	fact[0] = 1; fi[0] = 1;
	for(i=1;i<MAX;i++){
		fact[i] = fact[i-1]*i%mod;
		fi[i] = (fi[i-1]*inv[i])%mod;
	}
}

int main(){
	ll i,n,a[100010],b[100010]={0},ans = 0;
	cin >> n;
	for(i=0;i<n;i++){
		cin >> a[i];
	}
	inverse();
	b[1] = 1;
	for(i=2;i<=n;i++){
		b[i] += (b[i-1] + inv[i])%mod;
	}
	for(i=0;i<n;i++){
		(ans += fact[n]*(b[i+1]+b[n-i]-1)%mod*a[i]) %= mod;
	}
	cout << ans << endl;
}

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

#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define forn(i,N) for(int i=0;i<N;i++)

using namespace std;
using ll=long long int;
using ld=long double;
using vi=vector<int>;
using vll=vector<ll>;

//https://codeforces.com/problemset/problem/1343/C
//https://codeforces.com/problemset/problem/1154/B

int main() {
//freopen("input.txt","rt",stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);

int N;
cin>>N;
vll a(N);
ll xr=0;
for(ll& x: a) {
  cin>>x;
  xr^=x;
}
for(ll& x: a) {
  xr^=x;
  cout<<xr<<" ";
  xr^=x;
}
cout<<endl;
return 0;
}


Measured memory_bytes: 4780
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
int main(){ 
    int n;
    cin>>n;
    if(n<2){
        cout<<n;
    }else if(n<4){
        cout<<2;
    }else if(n<8){
        cout<<4;
    }else if(n<16){
        cout<<8;
    }else if(n<32){
        cout<<16;
    }else if(n<64){
        cout<<32;
    }else if(n<=101){
        cout<<64;
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#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 rep2(i,n) for(int i=2;i<(int)(n);i++)
#define repk(i,k,n) for(int i=k;i<(int)(n);i++)
#define rev(v) reverse(v.begin(), v.end())
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
//const int64 INF = 1LL << 58;
#define dame { puts("-1"); return 0;}
#define YES { cout << "YES" << endl; return 0; }
#define NO { cout << "NO" << endl; return 0; }
#define Yes { cout << "Yes" << endl; return 0; }
#define No { cout << "No" << endl; return 0; }
#define uniq(a) sort(all(a));(a).resize(unique(all(a)) - a.begin());
#define dab(v) for(auto i:v) cout << i << ' ' << endl;
#define vin(v) for (ll i = 0; i < v.size(); ++i) cin >> v[i];
#define vin2(v, w) for (ll i = 0; i < v.size(); ++i) cin >> v[i] >> w[i];
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } return 0; }
int dx[] = { 0, 0, -1, 1 };
int dy[] = { 1, -1, 0, 0 };
int gcd(int a, int b) { if (a % b == 0) { return(b); } else { return(gcd(b, a % b)); } }


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//cout << fixed << setprecision(18);

	int A, V, B, W, T;
	cin >> A >> V >> B >> W >> T;
	
	int distance = abs(B - A);
	int relative_speed = V - W;

	if(relative_speed >= (long double)distance / (long double)T){
        YES;
    }
    NO;
	return 0;
}


Measured memory_bytes: 3648
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size() 
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--) 
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++) 
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi; 
int main(){
	int a;
	cin>>a;
	cout<<a+a*a+a*a*a; 
	re 0;
}

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

int p, q, a, n, ans;

void dfs( int nume, int deno , int num, int pos ){
	if( p * deno == q * nume ){
		ans++;
		return;
	}
	if( num == n ) return;
	if( p * deno < q * nume ) return;
	for( int i = pos; i * deno <= a; i++ ){
		dfs( nume*i+deno, deno*i, num+1, i );
	}
}

int main(){
	while( cin >> p >> q >> a >> n && p ){
		ans = 0;
		dfs(0,1,0,1);
		cout << ans << endl;
	}
}

Measured memory_bytes: 1160
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define PI 3.141592
#define MOD7 1000000007
#define LL_INF 9000000000000000000
#define LL_MINF -9000000000000000000
#define INT_INF 2000000000
#define INT_MINF -2000000000
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main(){
    int N, M;
    cin >> N >> M;
    vector<pair<ll,ll>> AB(N);
    for(int i = 0; i < N; i++) cin >> AB[i].fi >> AB[i].se;
    sort(all(AB));
    ll sum = 0;
    ll sum_n = 0;
    for(int i = 0; i < N; i++){
        if(sum_n + AB[i].se >= M){
            sum += AB[i].fi * (M - sum_n);
            break;
        }else{
           sum_n += AB[i].se;
           sum += AB[i].fi * AB[i].se; 
        }
    }
    cout << sum << endl;
}

Measured memory_bytes: 4836
// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd(){static uint seed=416;return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args){read(t);read(args...);}
template <typename _Tp,typename... Args>Finline _Tp min(const _Tp &a,const _Tp &b,const Args &...args){return a<b?min(a,args...):min(b,args...);}
template <typename _Tp,typename... Args>Finline _Tp max(const _Tp &a,const _Tp &b,const Args &...args){return a<b?max(b,args...):max(a,args...);}
Finline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=1005;
char s[N],t[N];
int main()
{
	int n=read_str(s+1),m=read_str(t+1);
	int ans=inf;
	for(int i=1;i<=n-m+1;++i)
	{
		int tmp=0;
		for(int j=1;j<=m;++j) tmp+=s[i+j-1]!=t[j];
		chmin(ans,tmp);
	}
	printf("%d\n",ans);
	return 0;
}


Measured memory_bytes: 3764