Cutting Boards

  • + 0 comments
    int boardCutting(vector<int> cost_y, vector<int> cost_x) {
        long h = 1, v = 1;
        sort(cost_x.rbegin(), cost_x.rend());
        sort(cost_y.rbegin(), cost_y.rend());
        
        int s1 = 0, s2 = 0;
        long res = 0;
        while (s1 < cost_x.size() && s2 < cost_y.size()) {
            if (cost_x[s1] > cost_y[s2]) {
                res += cost_x[s1] * v;
                h++;
                s1++;
            } else {
                res += cost_y[s2] * h;
                v++;
                s2++;
            }
            res %= 1000000007;
            
        }
        
        while (s1 < cost_x.size()) {
            res += cost_x[s1] * v;
            res %= 1000000007;
            
            s1++;
        }
        
        while (s2 < cost_y.size()) {
            res += cost_y[s2] * h;
            res %= 1000000007;
            s2++;
        }
        
        return (int)(res % 1000000007);
    }