思路
移动石头
https://ac.nowcoder.com/acm/problem/21740
思路如果a[i]>b[i],就把多余的石头放到后面一堆,反之就从后面一堆拿,如果不能够完成的话n+1项肯定会被使用,即不为0,输出-1即可 #include"bits/stdc++.h" using namespace std; int n; int a[55],b[55]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } for(int i=1;i<=n;i++){ scanf("%d",&b[i]); } int ans=0; for(int i=1;i<=n;i++){ if(a[i]>b[i]){ a[i+1]+=a[i]-b[i]; ans+=a[i]-b[i]; } if(a[i]<b[i]){ a[i+1]-=b[i]-a[i]; ans+=b[i]-a[i]; } } if(a[n+1]!=0||b[n+1]!=0) cout<<"-1"; else cout<<ans; return 0; }