ankitsinha3005's blog

By ankitsinha3005, history, 7 years ago, In English

Hi codeforces community, I'm new to codeforces and also a beginner in competitive coding.In the previous contest, i tried to solved this question.Your text to link here... from Codeforces Round #449(Div2). It is failing in test case 50 .Help from community will be highly appreciated . My code is below. ~~~~~

Your code here...

~~~~~

include

include <bits/stdc++.h>

using namespace std;

int main() { int n,sum1=0,sum2=0; cin>>n; vector ar(n); for(int i=0;i<n;i++) { cin>>ar[i]; } sort(ar.begin(),ar.end()); reverse(ar.begin(),ar.end()); for(int j=0;j<n;j++) {

        if(sum2>=sum1)
        sum1=sum1+ar[j];
        else
        sum2=sum2+ar[j];
         }

  if(sum1>=sum2)
  cout<<sum1-sum2;
  else
  cout<<sum2-sum1;

return 0;

}

  • Vote: I like it
  • -11
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The problem statement says that you need to divide the sequence into two continuous segments. So you need to find the answer without changing the initial ordering of the array (you can't sort it ).