# Codeforces Round #536 (Div. 2)解题报告（有待更新）

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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<ll,ll>
#define mk(x,y) make_pair(x,y)
const int  maxn =500+5;
const int mod=998244353;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

int n;
char s[maxn][maxn];
int dx[]={1,1,-1,-1};
int dy[]={-1,1,1,-1};
int main()
{
cin>>n;
int cnt=0;
rep(i,0,n) scanf("%s",s[i]);
rep(i,0,n) rep(j,0,n){
int flag=1;
rep(k,0,4){
int tx=i+dx[k];
int ty=j+dy[k];
if(tx<0||tx>=n||ty<0||ty>=n) {flag=0;break;}
if(s[tx][ty]!='X') {flag=0;break;}
}
if(s[i][j]=='X') if(flag) cnt++;
}
cout<<cnt<<endl;

return 0;
}


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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<ll,ll>
#define mk(x,y) make_pair(x,y)
const int  maxn =1e5+5;
const int mod=998244353;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

int n,m,a,b;
struct node{
int x,y,idx;
bool operator<(const node& q)  const{
if(q.y==y) return q.idx<idx;
return q.y<y;
}
} pq[maxn];
priority_queue<node> p;
int main(){
cin>>n>>m;
rep(i,0,n) cin>>pq[i].x;
rep(i,0,n) {cin>>pq[i].y;pq[i].idx=i;p.push(pq[i]);}
rep(i,0,m){
cin>>a>>b;a--;
ll cst=0;
if(pq[a].x>=b){
pq[a].x-=b;
cst+=1LL*b*pq[a].y;
cout<<cst<<endl;
}
else{
b-=pq[a].x;
cst+=1LL*pq[a].x*pq[a].y;
///cout<<cst<<endl;
pq[a].x=0;
while(!p.empty()&&b){
node tp=p.top();p.pop();
///cout<<tp.x<<" "<<tp.y<<b<<endl;
if(pq[tp.idx].x){
if(pq[tp.idx].x>=b) {
cst+=1LL*pq[tp.idx].y*b;
pq[tp.idx].x-=b;
b=0;
}else{
cst+=1LL*pq[tp.idx].y*pq[tp.idx].x;
b-=pq[tp.idx].x;
pq[tp.idx].x=0;
}
if(pq[tp.idx].x) p.push(pq[tp.idx]);
}
}
if(p.empty()&&b) puts("0");
else cout<<cst<<endl;
}
}
return 0;
}


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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<ll,ll>
#define mk(x,y) make_pair(x,y)
const int  maxn =3e5+5;
const int mod=998244353;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
int n;
int a[maxn];
int main()
{
cin>>n;
rep(i,0,n) cin>>a[i];
sort(a,a+n);
int l=0,r=n-1;
ll ans=0;
while(r-l+1>3){
ans=ans+1LL*(a[l]+a[r])*(a[l]+a[r]);
l++,r--;
}
int sum=0;
rep(i,l,r+1) sum+=a[i];
ans=ans+1LL*sum*sum;
cout<<ans<<endl;

return 0;
}


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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<ll,ll>
#define mk(x,y) make_pair(x,y)
const int  maxn =2e5+5;
const int mod=998244353;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

int n,m;
vector<int> ed[maxn];
priority_queue<int> pq;
int mark[maxn],has[maxn];
int main()
{
cin>>n>>m;
while(m--)
{
int x,y;
cin>>x>>y;
ed[y].push_back(x);
ed[x].push_back(y);
}

mst(mark,0),mst(has,0);
pq.push(-1);
while(n--){
int tp=-pq.top();pq.pop();
has[tp]=0;mark[tp]=1;
cout<<tp<<" ";
for(int i=0;i<ed[tp].size();i++){
int y=ed[tp][i];
if(mark[y]) continue;
if(has[y]) continue;
has[y]=1;///
pq.push(-y);
}
}
return 0;
}


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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<ll,ll>
#define mk(x,y) make_pair(x,y)
const int  maxn =1e5+2;
const int mod=998244353;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*

*/
int n,m,k,s,t,d,w;
int a[2][maxn];///更新数组
ll dp[202][maxn],INF;///决策数组
///数据结构部分
struct node{int x,y,tp;};
vector<node> v[maxn];///
multiset<pair<int,int>> st;
int main(){
cin>>n>>m>>k;
mst(dp,0xf),INF=dp[0][0];
rep(i,0,201) dp[i][n+1]=0LL;
while(k--){
cin>>s>>t>>d>>w;
v[s].push_back(node{w,d,1});
v[t+1].push_back(node{w,d,-1});
}
rep(i,1,n+1){
rep(j,0,v[i].size()){
int tw=v[i][j].x,td=v[i][j].y,tt=v[i][j].tp;
///cout<<tw<<" "<<td<<" "<<tt<<endl;
if(tt==1) st.insert(make_pair(-tw,-td));
else st.erase(st.find(make_pair(-tw,-td)));
}
if(!st.empty()) a[0][i]=-(*st.begin()).first,a[1][i]=-(*st.begin()).second;
}
for(int i=n;i>=1;i--) for(int j=0;j<=m;j++){
if(j) dp[j][i]=min(dp[j][i],dp[j-1][i+1]);
if(a[1][i])  dp[j][i]=min(dp[j][i],dp[j][a[1][i]+1]+a[0][i]);
else dp[j][i]=min(dp[j][i+1],dp[j][i]);
}
ll ans=INF;rep(i,0,m+1) ans=min(ans,dp[i][1]);
cout<<ans<<'\n';
return 0;
}