Advertisement
jeff69

Untitled

Aug 12th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. typedef long double ld;
  6.  
  7. typedef short sh;
  8. sh n;
  9. short a[107];
  10. map<pair<int,pair<int,int> >,bool > mp;
  11. priority_queue<pair<sh,pair<sh,sh> > > pq;
  12. int main(){
  13.  
  14. int t;
  15. cin>>t;
  16. while(t--){
  17.  
  18. memset(a,0,sizeof a);
  19. cin>>n;
  20. for(int i=1;i<1+n;i++)
  21. cin>>a[i];
  22. pq.push({0,{0,0}});
  23. sh fact=0;
  24. sh ans=300;
  25. mp.clear();
  26. while(!pq.empty())
  27. {
  28.  
  29. pair<sh,pair<sh,sh > >moth=pq.top();
  30. fact=-1*moth.first;
  31. pq.pop();
  32. if(mp[moth])continue;
  33. mp[moth]=1;
  34.  
  35. sh z=moth.second.second;
  36. sh y=moth.second.first+1;
  37. if(y==n+1){
  38.  
  39. ans=min(ans,fact);
  40.  
  41. continue;
  42. }
  43. if(fact>=ans)continue;
  44. // cout<<fact<<' '<<y<<endl;
  45. pq.push({-1*max(fact,(sh)abs(z+a[y])),{(sh)y,(sh)a[y]+z}});
  46. pq.push({-1*max(fact,(sh)abs(z-a[y])),{y,z-a[y]}});
  47.  
  48. }
  49. cout<<ans<<endl;;
  50. }
  51.  
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement