Accessory Collection

  • + 0 comments

    can anyone find my mistake ? I think it is quite simple.

    string acessoryCollection(int L, int A, int N, int D) {
        long long ans=0;
        if(N<D) ans=-1;
        else if(D==1){
            ans=L*A;
        }
        else{ 
            int min=N-D+1;
            while(L>min){
                if(A==0) {
                    ans=-1;
                    break;
                }
                ans+=min*A;
                A--;
                L-=min;
            }
            if(L>0 & A==0) ans=-1;
            else if(L>0) ans+=L*A;
        }
        //cout << ans<<endl;
        string str;
        while(ans>0){
            str.push_back('0'+ans%10);
            ans=ans/10;
        }
        if(str.size()!=0){
            reverse(str.begin(),str.end());
        }
        
        if(ans==-1) return "SAD";
        else return str;
        
     
    }