# Mining

### Problem Statement :

```There are n gold mines along a river, and each mine i produces wi tons of gold. In order to collect the mined gold, we want to redistribute and consolidate it amongst exactly k mines where it can be picked up by trucks. We do this according to the following rules:

You can move gold between any pair of mines (i.e., i and j, where 1 <= i <j <= n).
All the gold at some pickup mine i must either stay at mine i or be completely moved to some other mine, j.
Move w tons of gold between the mine at location xi and the mine at location xj at a cost of |xi-xj|*w.
Given n, k, and the amount of gold produced at each mine, find and print the minimum cost of consolidating the gold into k pickup locations according to the above conditions.

Input Format

The first line contains two space-separated integers describing the respective values of n (the number of mines) and k (the number of pickup locations).
Each line i of the n subsequent lines contains two space-separated integers describing the respective values of xi (the mine's distance from the mouth of the river) and wi (the amount of gold produced in tons) for mine i.

Note: It is guaranteed that the mines are will be given in order of ascending location.

Constraints
1 <= k < n <= 5000
1 <= wi,xi <= 10^6

Output Format

Print a single line with the minimum cost of consolidating the mined gold amongst k different pickup sites according to the rules stated above.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0);
for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int M=5010;
ll wt[M],dp[M][M],w[M][M],sw[M],scw[M];
int s[M][M],cord[M];
int n,p;

ll calc(int i, int j,int k) {
assert(i<=k&&k<=j);
return cord[k]*(sw[k]-sw[i-1])-(scw[k]-scw[i-1])+
scw[j]-scw[k-1]-cord[k]*(sw[j]-sw[k-1]);
}

int main() {
scanf("%d%d",&n,&p);
rep(i,1,n+1) {
scanf("%d%lld",cord+i,wt+i);
}
rep(i,1,n+1) sw[i]=sw[i-1]+wt[i],scw[i]=scw[i-1]+cord[i]*wt[i];
rep(i,1,n+1) {
int p=i;
rep(j,i+1,n+1) {
while (p<j&&calc(i,j,p)>calc(i,j,p+1)) ++p;
w[i][j]=calc(i,j,p);
}
}
rep(i,1,n+1) dp[1][i]=w[1][i],s[i][i]=i-1;
rep(i,2,p+1) {
int j=n;
dp[i][n]=1ll<<60;
for (int k = s[i - 1][n]; k <= n - 1; k++) {
ll temp = dp[i - 1][k] + w[k + 1][n];
if (temp < dp[i][n]) dp[i][n] = temp,s[i][n] = k;
}
for (int j = n - 1; j >= i + 1; j--) {
dp[i][j] = 1ll<<60;
for (int k = s[i - 1][j]; k <= s[i][j + 1]; k++) {
ll temp = dp[i - 1][k] + w[k + 1][j];
if (temp < dp[i][j]) dp[i][j] = temp,s[i][j] = k;
}
}
}
printf("%lld\n",dp[p][n]);
}

In Java :

import java.io.File;
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
//    	Scanner scn = null;
//    	try {
//    		scn = new Scanner(new File("input.txt"));
//    	} catch (Exception ex) {
//    		ex.printStackTrace();
//    	}
int n = scn.nextInt();
int m = scn.nextInt();
int[] x = new int[n];
long[] w = new long[n];
for (int i = 0; i < n; ++i) {
x[i] = scn.nextInt();
w[i] = scn.nextLong();
}
long f[][] = new long[n][m + 1];
int b[][] = new int[n][m + 1];
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= m; ++j) {
f[i][j] = Long.MAX_VALUE / 2;
}
}
f[0][1] = 0;
long s = w[0];
for (int i = 1; i < n; ++i) {
f[i][1] = f[i - 1][1] + s * (x[i] - x[i - 1]);
s += w[i];
}
for (int j = 2; j <= m; ++j) {
for (int i = j - 1; i < n; ++i) {
int l = i;
s = w[i];
long t = w[i];
long cost = 0;
for (int k = i - 1; k >= j - 2; --k) {
cost += (s - t) * (x[k + 1] - x[k]);
s += w[k];
while (l - 1 >= k && x[i] - x[l - 1] <= x[l - 1] - x[k]) {
l--;
t += w[l];
cost += w[l] * (x[i] - x[l]) - w[l] * (x[l] - x[k]);
}
if (f[i][j] > f[k][j - 1] + cost) {
f[i][j] = f[k][j - 1] + cost;
b[i][j] = k;
}
if (k < b[i - 1][j]) {
break;
}
//        		System.out.println("current cost = " + (f[k][j - 1] + cost));
}
//    			System.out.println("+++");
}
}
long ret = f[n - 1][m];
//    	System.out.println("ret = " + ret);
s = w[n - 1];
long cost = 0;
for (int i = n - 2; i >= m - 1; --i) {
//    		System.out.println("i = " + i);
//    		System.out.println("fim = " + f[i][m]);
cost += s * (x[i + 1] - x[i]);
//    		System.out.println("cost = " + cost);
ret = Math.min(ret, f[i][m] + cost);
s += w[i];
//        	System.out.println("ret = " + ret);
//        	System.out.println("+++");
}
System.out.println(ret);
}
}

In C :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int x[5000];
int w[5000];

int64_t val[5000][5000];
int64_t a[5000], b[5000];

int64_t mining()
{
int n, k, i;
scanf("%d %d", &n, &k);
for(i = 0; i < n; ++ i)
scanf("%d %d", &x[i], &w[i]);

for(i = 0; i < n; ++i)
{
int64_t left = 0, right = 0, acc = 0;
int s, j;

for(j = i + 1, s = i; j < n; ++ j)
{
acc += w[j] * (int64_t)(x[j] - x[s]);
right += w[j];

while(s < j && left + w[s] < right)
{
acc += (left + w[s] - right) * (int64_t)(x[s + 1] - x[s]);
left += w[s];
right -= w[s + 1];
++ s;
}
val[j][i] = acc;
}
}

/* memcpy(a, val[0], n * sizeof(int64_t)); */
for(i = 0; i < n; ++ i)
a[i] = val[i][0];

if(n * (int64_t) n * (int64_t) k < 1000000000)
{
for(; 1 < k; --k)
for(i = n - 1; -1 < i; --i)
{
int s;
a[i] = val[i][0];

for(s = 1; s < i + 1; ++ s)
{
const int64_t c = a[s - 1] + val[i][s];
if(c < a[i]) a[i] = c;
}
}

return a[n - 1];
}

for(; 1 < k; -- k)
{
int idx = 0;
memcpy(b, a, n * sizeof(int64_t));

for(i = 0; i < n; ++ i)
{
a[i] = (idx ? b[idx - 1] : 0) + val[i][idx];

for(; idx < i && b[idx] + val[i][idx + 1] < a[i]; ++ idx)
a[i] = b[idx] + val[i][idx + 1];

{
int s = i;
for(s = i; idx < s && i < s + 50; -- s)
{
const int64_t v = (s ? b[s - 1] : 0) + val[i][s];
if(v < a[i]) a[i] = v;
}

}

}
}

return a[n - 1];
}

int main()
{
printf("%lld", (long long) mining());
return EXIT_SUCCESS;
}```
```

## View More Similar Problems

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

## Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle