Hi! I attempted to do the problem http://mirror.codeforces.com/problemset/problem/808/C but got a runtime error with my solution saying that the "Exit code is -1073741819".Please can someone tell me what did I do wrong?
Here is my code:
include<bits/stdc++.h>
define MAXN 100010
define pb push_back
define mp make_pair
using namespace std; int cmpfunc (const void * a, const void * b) { return ( (int)a — (int)b ); } int main(){ int cap,n,w,sum=0; cin>>n>>w; map <int,int> arr; map<int,int> :: iterator it; int p[n]; for(int i=0;i<n;i++){ cin>>cap; arr.insert(pair<int,int> (cap,i));
p[i]=ceil(cap/2.0); sum+=p[i];
} if(sum>w){ cout<<"-1"; } else { w=w-sum; it=arr.end(); for(;it!=arr.begin();it--){ if(w!=0){ if(w>=it->first/2){ p[it->second]=it->first; w-= (it->first-p[it->second]); } else { p[it->second]+=w; w=0; }
} else { break; } } for(int i=0;i<n;i++){ cout<<p[i]<<" "; }
} return 0; }
It looks like you tried to dereference arr.end() in your loop which is bad. You could try setting it to arr.end() and doing it-- before using it or use arr.rbegin().
Write it-- before the for loop