Codeforces #530.div2
A
#include<bits/stdc++.h> using namespace std; int main() { int h,w,u1,d1,u2,d2,sum=0; cin>>w>>h>>u1>>d1>>u2>>d2; sum=w; while(h>0) { sum=sum+h; if(h==d1)sum=sum-u1; if(h==d2)sum=sum-u2; sum=max(sum,0); h--; } cout<<sum; }
B
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin>>n; n=sqrt((4*n)-3); n=n+1; cout<<n<<endl; return 0; }
C
#include <bits/stdc++.h> using namespace std; #define long long ll int main() { string s; cin>>s; int l = s.length(); int k; cin>>k; int count1 = 0; int count2 = 0; for(int i=0; i<l; i++) { if(s[i]=='?') count1++; else if(s[i] == '*') count2++; } int d = l-count1-count2; if(k<=d) { if(k<(d-(count1+count2))) cout<<"Impossible"; else { int Count = 0; for(int i=0; i<l-1; i++) { if((s[i+1]=='?' || s[i+1]=='*') && Count<(d-k)) { Count++; continue; } else if(s[i]=='?' || s[i] == '*') continue; else cout<<s[i]; } if(s[l-1]!='?' && s[l-1]!='*') cout<<s[l-1]; } } else { if(k>d && count2==0) cout<<"Impossible"; else { int tmp; for(int i=0; i<l-1; i++) { if(s[i+1]=='*') { tmp = i; break; } else if(s[i]=='?' || s[i] == '*') continue; else cout<<s[i]; } for(int i=0; i<(k-d+1); i++) cout<<s[tmp]; for(int i=tmp+1; i<l; i++) { if(s[i]=='?' || s[i] == '*') continue; else cout<<s[i]; } } } return 0; }
D
#include<bits/stdc++.h> #define LL long long using namespace std; const int inf = 1e9 + 10; vector<int> v[100010]; int a[100010]; int s[100010]; int flag; void dfs(int x,int pre,int sum) { if(flag == 0) return; if(s[x]!= -1) { if(s[x]< sum) { flag = 0; return; } a[x]=s[x]-sum; } else { int minn = inf; for(int i=0;i<v[x].size();i++) { int xx = v[x][i]; if(xx == pre) continue; if(s[xx] == -1) continue; minn = min(minn,s[xx]); } if(minn < sum) { flag = 0; return; } if(minn == inf) a[x] = 0; else a[x] = minn - sum; } for(int i=0;i<v[x].size();i++) { int xx = v[x][i]; if(xx == pre) continue; dfs(xx,x,sum + a[x]); } } int main(void) { int n,i,j; while(scanf("%d",&n)==1) { for(i=1;i<=n;i++) v[i].clear(); for(i=2;i<=n;i++) { int p; scanf("%d",&p); v[p].push_back(i); } for(i=1;i<=n;i++) scanf("%d",&s[i]); flag = 1; dfs(1,-1,0); if(flag == 0) printf("-1\n"); else { LL sum = 0; for(i=1;i<=n;i++) sum += a[i]; cout << sum << endl; } } return 0; }