Cod sursa(job #1453274)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 23 iunie 2015 10:13:26
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define FILL(x,y) memset(x,y,sizeof(x))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define x first
#define y second
#define st first
#define nd second
#define pb push_back
#define nleft (n<<1)
#define nright (nleft+1)
 
#include<vector>
 
using namespace std;
#include<fstream>
ifstream cin("transport.in");
ofstream cout("transport.out");
 
 
const int NMAX = 16010;
typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef pair<K, K> PKK;
typedef vector<int> VI;
 
const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};
 
int n,k,i,j,t,logN,suma,smax;
  
using namespace std;
int s[NMAX];

int check(int val)
{
	int t, a;
	t=1;
	a=val;
	for(i=1; i<=n; ++i)
	{
		val-=s[i];
		if(val<0)
		{
			t++;
			val=a-s[i];
		}
	}
	return t;
}
  
int lower_bound()
{
    int i, step;
    for(step=logN, i=suma; step; step>>=1)
        if(i-step>=smax && check(i-step) <= k)
            i-=step;
    return i;
}
  
int main() {
    cin>>n>>k;
    
    for(int i=1; i<=n; ++i)
        cin>>s[i], suma+=s[i], smax=MAX(smax, s[i]);
	
	//cout<<smax<<"\n";
	//cout<<suma<<"\n";
	
	
    for(logN=1;logN<=suma;logN<<=1);
    
    cout<<lower_bound();
    
    return 0;
}