• + 0 comments

    Can someone explain why this code is not clearing all test cases:

    include

    define ll long long int

    using namespace std;

    ll fairCut(int k, vector arr) { int n = arr.size(); vector> dp(n+1, vector (k+1, LLONG_MAX));

    sort(arr.begin(), arr.end());
    
    dp[0][0]=0;
    
    for(int i=1; i<=n; i++){
        for(int j=0; j<=k; j++){
            if(i<j) continue;
    
            ll inc = dp[i-1][j-1];
            ll exc = dp[i-1][j];
    
            if(inc!=LLONG_MAX) inc+=arr[i-1]*(2*(i-j)-(n-k));
            if(exc!=LLONG_MAX) exc+=arr[i-1]*(2*j-k);
    
            dp[i][j]=min(inc, exc);
        }
    }
    
    return dp[n][k];
    

    }

    signed main() { int n, k;

    cin>>n>>k;
    
    vector<ll> arr(n);
    
    for (int i = 0; i < n; i++) {
        cin>>arr[i];
    }
    
    ll result = fairCut(k, arr);
    
    cout << result << "\n";
    
    return 0;
    

    }