Problem:
Problem I:
Text Justification
You are hired by the ∀I¶אΞ℘, an extraterrestrial intelligence, as a programmer of their typesetting system. Your
task today is to design an algorithm for
text justification
.
Text justification is to equalize the line widths as much as possible by inserting line breaks at appropriate posi-
tions, given a word sequence called a
paragraph
and the width of the paper. Since you have not developed an
automatic hyphenation algorithm yet, you cannot break a line in the middle of a word. And since their language
does not put spaces between words, you do not need to consider about spacing.
To measure how well the text is justified in one configuration (i.e., a set of lines generated by inserting line breaks
to a paragraph), you have defined its
cost
as follows:
The total cost of a paragraph is the sum of the cost of each line.
The cost for the last line is defined as max(0,
s
-
w
).
The cost for other lines are given by |
s
-
w
|.
where
s
is the sum of the widths of the words in the line, and
w
is the width of the paper.
Please design the algorithm which takes a paragraph and calculates the configuration of the minimum cost.
Input
The input consists of multiple test cases.
The first line of each test case contains two positive integers
n
and
w
(0 ≤
n
≤ 1000 and 0 ≤
w
≤ 1,000,000).
n
is the length of paragraph and
w
is the width of the used paper. Each of the following n lines contains one positive
integer
a
i
which indicates the width of the
i
-th word in the paragraph. Here it is guaranteed that 0 ≤
a
i
≤
w
.
The input terminates with the line containing two zeros. This is not a part of any test case and should not be
processed.
Output
For each test case, print the case number and the minimum cost for the paragraph.
Sample Input
4 10
8
6
9
1
4 7
1
2
3
4
0 0
Output for the Sample Input
Case 1: 4
Case 2: 1

Submitted solutions:

code_snippet: #include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define endl "\n"

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, nuint_type,greater_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

vector<int> dx = {0, 0,  1, -1, -1, 1, 1, 1};
vector<int> dy = {1, -1, 0, 0, -1, 0, 1, -1};

#define int long long

int n, w;
int te = 1;
vector<int> a;
int memo[1000];

int get_mn(int pos){
    if(pos == n){
        return 0;
    }

    int &ans = memo[pos];
    if(~memo[pos]) return ans;


    ans = LLONG_MAX;
    int sum = 0;
    int new_cost;
    for(int i = pos; i < n; i++){
        sum += a[i];

        if(i == n-1){
            new_cost = max(0ll, sum - w);
        } else {
            new_cost = abs(sum - w);
        }
        ans = min(ans, new_cost + get_mn(i+1));
    }

    return ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> w;

    while(n && w){
        a.clear();
        a.resize(n);

        memset(memo, -1, sizeof(memo));

        for(int i = 0; i < n; i++) cin >> a[i];

        cout << "Case " << te << ": " << get_mn(0) << endl;
        cin >> n >> w;
        te++;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3420
final_rank: 17

code_snippet: #include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define endl "\n"

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, nuint_type,greater_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

vector<int> dx = {0, 0,  1, -1, -1, 1, 1, 1};
vector<int> dy = {1, -1, 0, 0, -1, 0, 1, -1};

#define int long long

int n, w;
int te = 1;
vector<int> a;
int memo[1000];
int vis[1000];

int get_mn(int pos){
    if(pos == n){
        return 0;
    }

    int &ans = memo[pos];
    if(vis[pos] >= te) return ans;
    vis[pos] = te;


    ans = LLONG_MAX;
    int sum = 0;
    int new_cost;
    for(int i = pos; i < n; i++){
        sum += a[i];

        if(i == n-1){
            new_cost = max(0ll, sum - w);
        } else {
            new_cost = abs(sum - w);
        }
        ans = min(ans, new_cost + get_mn(i+1));
    }

    return ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> w;
    memset(vis, 0, sizeof(vis));
    while(n && w){
        a.clear();
        a.resize(n);

        for(int i = 0; i < n; i++) cin >> a[i];

        cout << "Case " << te << ": " << get_mn(0) << endl;
        cin >> n >> w;
        te++;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 18

code_snippet: #include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;
long long int dp[1002][1002];
long long int as[1002];
void f(int n,int w,int no){
	memset(dp,-1,sizeof(dp));
	long long int s=0,t;
	as[0]=s;
	for(int i=1;i<=n;i++){
		cin>>t;
		s+=t;
		as[i]=s;
	}
	long long int ans=as[n];
	dp[0][0]=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(dp[i][j]==-1)continue;
			long long int t1,t2;
			t1=dp[i][j];
			for(int k=j+1;k<n;k++){
				t2=(as[k]-as[j])-w;
				if(t2<0)t2=-t2;
				t2+=t1;
				if(dp[i+1][k]==-1 || dp[i+1][k]>t2)dp[i+1][k]=t2;
			}
			t2=(as[n]-as[j])-w;
			if(t2<0)t2=0;
			t2+=t1;
			ans=min(t2,ans);
		}
	}
	cout<<"Case "<<no<<": "<<ans<<endl;
}

int main() {
int no=1;
while(true){
	int n,m;
	cin>>n>>m;
	if(n==0 && m==0)break;
	f(n,m,no);
	no++;
}
return 0;
}
accuracy: 1
time_ms: 2330
memory_kb: 10936
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const ll INF = LLONG_MAX/3;

ll solve(int n, int w){
    vector<ll> a(n);
    rep(i,n) scanf(" %lld", &a[i]);

    vector<ll> dp(n+1,INF);
    dp[0] = 0;
    rep(i,n){
        ll s = 0;
        for(int j=i; j<n; ++j){
            s += a[j];

            ll cost = abs(s-w);
            if(j==n-1) cost = max(0LL, s-w);

            dp[j+1] = min(dp[j+1], dp[i]+cost);
        }
    }
    return dp[n];
}

int main(){
    int C = 1;
    int n,w;
    while(scanf(" %d %d", &n, &w),n){
        printf("Case %lld: %d\n", C++, solve(n,w));
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3168
final_rank: 12

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N,width;
int ruisekiwa[1001],memo[1001];
int case_num = 1;

int recursive(int index){

	if(memo[index] != -1)return memo[index];


	int calc_right = N;

	int minimum = BIG_NUM;

	for(int i = index; i <= calc_right; i++){
		if(i < N){
			minimum = min(minimum,abs((ruisekiwa[i]-ruisekiwa[index-1])-width)+recursive(i+1));
		}else{
			minimum = min(minimum,max(0,(ruisekiwa[i]-ruisekiwa[index-1])-width));
		}
	}

	return memo[index] = minimum;
}

void func(){

	ruisekiwa[0] = 0;
	for(int i = 1; i <= N; i++){
		scanf("%d",&ruisekiwa[i]);
		ruisekiwa[i] += ruisekiwa[i-1];
	}

	for(int i = 1; i <= N; i++)memo[i] = -1;

	printf("Case %d: %d\n",case_num++,recursive(1));
}

int main(){

	while(true){
		scanf("%d %d",&N,&width);
		if(N == 0 && width == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3252
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,w,p=1;
  while(cin>>n>>w,n){
    int dp[n+1]={},a[n],s;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)dp[i]=1e9;
    for(int i=0;i<n;i++){
      s=0;
      for(int j=i;j<n;j++){
        s+=a[j];
        if(j!=n-1)dp[j+1]=min(dp[j+1],dp[i]+abs(s-w));
        else dp[j+1]=min(dp[j+1],dp[i]+max(0,s-w));
      }
    }
    cout<<"Case "<<p++<<": "<<dp[n]<<endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3080
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
int dp[1111];
signed main(){
  int n,w,cnt=0;
  while(cin>>n>>w,n||w){
    int a[n];
    for(int i=0;i<n;i++) cin>>a[i];
    int INF=1LL<<55LL;
    fill_n(dp,n+1,INF);
    dp[0]=0;
    int ans=INF;
    for(int i=0;i<n;i++){
      int tmp=0;
      for(int j=i;j<n;j++){
	tmp+=a[j];
	dp[j+1]=min(dp[j+1],dp[i]+abs(tmp-w));
	if(j+1==n) ans=min(ans,dp[i]+max(0LL,tmp-w));
      }
    }
    cout<<"Case "<<++cnt<<": "<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3096
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX-1;

int memo[1111];

int func(int pos, const int& n, const int& width, const vector<int>& a){
  if(memo[pos] != -1) return memo[pos];
  if(pos >= n) return 0;
  int w = 0;
  int res = INF;
  int p;
  int pp = -1;
  for(p = pos; p < n; p++){
    w += a[p];
    int tmp = func(p+1, n, width, a) + abs(width - w);
    if( res > tmp ){
      pp = p;
      res = tmp;
    }
  }
  if(pp+1 == n){
    memo[pos] = max(0, w-width);
    return memo[pos];
  }
  memo[pos] = res;
  return res;
}

int main(void){
  int n, w; 
  int Case = 1;
  while(cin >> n >> w, n | w){
    for(int i = 0; i < 1111; i++) memo[i] = -1;
    vector<int> a(n);
    for(int i = 0;  i< n; i++)
      cin >> a[i];
    cout << "Case " << Case++ << ": " << func(0, n, w, a) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3188
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#define INF (1e9)
#define N 1005
using namespace std;

int n,w,a[N],dp[N];

int main(){

  int T=1;
  
  while(1){
    
    cin>>n>>w;
    if(!n&&!w) break;

    for(int i=0;i<n;i++) cin>>a[i];
    
    for(int i=0;i<=n;i++) dp[i]=INF;
    
    dp[0]=0;
    
    for(int i=0;i<n;i++){

      int s=0;

      for(int j=i;j<n;j++){

	s+=a[j];
	
	if(j==n-1) dp[j+1]=min(dp[j+1],dp[i]+max(0,s-w));
	else dp[j+1]=min(dp[j+1],dp[i]+abs(s-w));
      }
      
    }

    cout<<"Case "<<T<<": "<<dp[n]<<endl;
    
    T++;
  }
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3020
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,w,total;
int a[1000];
int dp[1001];

int solve(){
  if(n==0)return 0;
  if(w==0)return total;
  
  fill(dp,dp+n+1, 1e9);
  dp[0]=0;
  for(int i=0;i<n;i++){
    int sum=0;
    for(int j=i+1;j<=n;j++){
      sum+=a[j-1];
      dp[j]=min( dp[j], dp[i]+ abs(sum-w) );
    }
  }

  int res=max(0,total-w);
  for(int i=0;i<n;i++){
    total-=a[i];
    res=min(res, dp[i+1] + max(0, total-w ) );
  }
  return res;
  
}

int main(){
  for(int tc=1;;tc++){
    cin>>n>>w;
    if(n==0&&w==0)break;
    total=0;
    for(int i=0;i<n;i++){
      cin>>a[i];
      total+=a[i];
    }
    cout<<"Case "<<tc<<": "<<solve()<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3020
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll n,m,tt=1;
  while(cin >> n >> m && n) {
    ll dp[n+1],a[n];
    fill(dp,dp+n+1,1LL<<60);
    dp[0]=0;
    for(int i=0; i<n; i++) cin >> a[i];
    for(int i=0; i<n; i++) {
      ll s=0;
      for(int j=i; j<n; j++) {
        s+=a[j];
        ll d=abs(s-m);
        if(j+1==n) d=max(0LL,s-m);
        dp[j+1]=min(dp[j+1],dp[i]+d);
      }
    }
    cout << "Case " << tt++ << ": " << dp[n] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3032
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;

int N,W;
int A[1005];

int dp[1005];
const int INF = 1000000009;
void update(int &a,int b){ a = min( a, b ); }
int solve(){
  fill( dp, dp+N+1, INF );
  dp[0] = 0;
  for(int i=0;i<N;i++){
    int sum = 0;
    for(int j=i;j<N;j++){
      sum += A[j];
      if( j+1 < N )
        update( dp[j+1], dp[i] + abs( sum-W ) );
      else
        update( dp[j+1], dp[i] + max( 0, sum-W ) );
    }
  }
  return dp[N];
}
int main(){
  int ttt=0;
  while( cin >> N >> W &&(N|W) ){
    for(int i=0;i<N;i++)
      cin >> A[i];
    cout << "Case " <<  ++ttt<< ": ";
    cout << solve() << endl;   
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3092
final_rank: 7

code_snippet: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int dp[1002], a[1002], n, w, r;
int main() {
	while (true) {
		cin >> n >> w; r++; if (n == 0)break;
		for (int i = 1; i <= n; i++)cin >> a[i];
		for (int i = 0; i <= n; i++) dp[i] = 999999999; dp[0] = 0;
		for (int i = 0; i < n; i++) {
			int s = 0;
			for (int j = i + 1; j <= n; j++) {
				s += a[j];
				if (j < n)dp[j] = min(dp[j], dp[i] + abs(s - w));
				if (j == n)dp[j] = min(dp[j], dp[i] + max(s - w, 0));
			}
		}
		cout << "Case " << r << ": " << dp[n] << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3096
final_rank: 8

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
int n, w, a[1009], sum[1009], dp[1009];
int main() {
	int cnt = 0;
	while (cin >> n >> w, n) {
		for (int i = 0; i < n; i++) cin >> a[i];
		for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + a[i];
		int ret = max(0, sum[n] - w);
		for (int i = 1; i < n; i++) {
			dp[i] = 1999999999;
			for (int j = 0; j < i; j++) {
				int cost = abs(w - sum[i] + sum[j]);
				dp[i] = min(dp[i], dp[j] + cost);
			}
			ret = min(ret, dp[i] + max(0, sum[n] - sum[i] - w));
		}
		cout << "Case " << ++cnt << ": " << ret << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2964
final_rank: 2

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

long long int memo[2000];
long long int aws[10000];
int N, W;

int getans(const int l) {
	if (memo[l] ==-1) {
		long long int sum = 0;
		long long int ans = 2e9 + 1;
		for (int r = l + 1; r <= N; ++r) {
			sum += aws[r - 1];
			if (r == N)ans = min(ans, max(0ll, sum - W));
			ans = min(ans, getans(r) + abs(sum - W));
		}
		memo[l] = ans;
	}
	return memo[l];
}
int main() {
	int num(0);
	while (1) {
		num++;
		cin >> N >> W;
		for (int i = 0; i < 2000; ++i) {
			memo[i] = -1;
		}
		if (!N)break;
		for (int i = 0; i < N; ++i) {
			cin >> aws[i];
		}
		long long int ans = getans(0);
		cout << "Case "<<num<<": "<<ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3092
final_rank: 11

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define MAX 1024
#define INF LLONG_MAX/4
 
ll N, W, memo[MAX];
vector<ll> a;
 
ll solve(int n)
{
    if (n == N) return 0;
    ll &res = memo[n], sum = 0;
    if (res != INF) return res;
    for (int i = n; i < N; i++) {
        sum += a[i];
        res = min(res, solve(i+1) + abs(sum - W));
    }
    res = min(res, solve(N) + max(0LL, sum - W));
    return res;
}
 
int main()
{
    int T = 1;
    while (cin >> N >> W, N) {
        a.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        fill(memo, memo + MAX, INF);
        cout << "Case " << T++ << ": " << solve(0) << endl;
    }    
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3088
final_rank: 10

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAX 1024
#define INF LLONG_MAX/4

ll N, W, memo[MAX];
vector<ll> a;

ll solve(int n)
{
    if (n == N) return 0;
    ll &res = memo[n], sum = 0;
    if (res != INF) return res;
    for (int i = n; i < N; i++) {
        sum += a[i];
        res = min(res, solve(i+1) + abs(sum - W));
    }
    res = min(res, solve(N) + max(0LL, sum - W));
    return res;
}

int main()
{
    int T = 1;
    while (cin >> N >> W, N) {
        a.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        fill(memo, memo + MAX, INF);
        cout << "Case " << T++ << ": " << solve(0) << endl;
    }    
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3188
final_rank: 15

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAX 1024
#define INF LLONG_MAX/4

ll N, W;
ll memo[MAX];
vector<ll> a;

ll solve(int n)
{
    if (n == N) return 0;
    ll &res = memo[n];
    if (res != -1) return res;
    res = INF;
    ll sum = 0;
    for (int i = n; i < N; i++) {
        sum += a[i];
        res = min(res, solve(i+1) + abs(sum - W));
    }
    res = min(res, solve(N) + max(0LL, sum - W));
    return res;
}

int main()
{
    int T = 1;
    while (cin >> N >> W, N) {
        a.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        memset(memo, -1, sizeof(memo));
        cout << "Case " << T++ << ": " << solve(0) << endl;
    }    
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3180
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAX 1024
#define INF LLONG_MAX/4

ll N, W;
ll memo[MAX];
vector<ll> a;

ll solve(int line, int n)
{
    if (n == N) return 0;
    ll &res = memo[n];
    if (res != -1) return res;
    res = INF;
    ll sum = 0;
    for (int i = n; i < N; i++) {
        sum += a[i];
        res = min(res, solve(line + 1, i+1) + abs(sum - W));
    }
    res = min(res, solve(line + 1, N) + max(0LL, sum - W));
    return res;
}

int main()
{
    int T = 1;
    while (cin >> N >> W, N) {
        a.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        memset(memo, -1, sizeof(memo));
        cout << "Case " << T++ << ": " << solve(0, 0) << endl;
    }    
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1308
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAX 1024
#define INF LLONG_MAX/4

ll N, W;
ll memo[MAX][MAX];
vector<ll> a;

ll solve(int line, int n)
{
    if (n == N) return 0;
    ll &res = memo[line][n];
    if (res != -1) return res;
    res = INF;
    ll sum = 0;
    for (int i = n; i < N; i++) {
        sum += a[i];
        res = min(res, solve(line + 1, i+1) + abs(sum - W));
    }
    res = min(res, solve(line + 1, N) + max(0LL, sum - W));
    return res;
}

int main()
{
    int T = 1;
    while (cin >> N >> W, N) {
        a.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        memset(memo, -1, sizeof(memo));
        cout << "Case " << T++ << ": " << solve(0, 0) << endl;
    }    
    return 0;
}
accuracy: 1
time_ms: 7290
memory_kb: 11428
final_rank: 20