## 12608 - Garbage Collection

All about problems in Volume 126. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

### 12608 - Garbage Collection

Hi guys i have a problem with 12608 - Garbage Collection

My solution works for given inputs but I got WA?
Could anybody give some suggestions or input?

Code: Select all

``````import java.io.*;
import java.util.Scanner;

class Main {

public static void main(String[] args) {

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

Scanner scanner = new Scanner(reader);

try {

int t = scanner.nextInt();
for (int i = 0; i < t; i++) {

int maxWeight = scanner.nextInt();
int n = scanner.nextInt();

Integer totalDistance = 0;
int lastDistance = 0;
int currentWeight = 0;

for (int j = 0; j < n; j++) {
int distance = scanner.nextInt();
int weight = scanner.nextInt();

if (currentWeight + weight > maxWeight) {
totalDistance += 2 * distance;
currentWeight = weight;
} else {
currentWeight += weight;
}
lastDistance = distance;

}

totalDistance += 2 * lastDistance;

writer.write(totalDistance.toString());
if (i < t - 1) {
writer.write("\n");
}

}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: WA 12608 - Garbage Collection

Input:

Code: Select all

``````10
564 12
1 380
3 551
8 25
15 520
21 36
24 321
30 20
37 517
44 399
49 506
59 319
64 8
388 11
1 259
5 182
15 243
25 249
34 2
35 9
40 110
42 99
49 181
50 77
55 131
179 20
1 13
8 77
13 118
19 14
25 124
30 14
37 57
39 125
40 61
49 59
59 104
63 134
71 36
81 47
83 175
86 175
96 125
106 131
107 140
108 37
625 6
1 518
3 522
10 322
19 177
27 183
35 581
715 16
1 104
10 88
13 424
14 267
19 707
25 545
35 553
37 286
41 199
49 15
55 299
61 274
63 668
66 39
72 612
77 548
14 10
1 14
8 6
14 13
19 7
26 4
36 2
45 9
53 14
62 11
65 14
964 8
1 594
3 203
9 571
17 7
21 715
30 686
33 770
35 241
370 13
1 231
10 341
15 38
24 213
27 241
35 80
42 127
52 270
54 367
62 271
66 51
75 104
77 203
875 10
1 273
5 592
15 44
19 334
21 144
29 139
36 40
41 88
44 696
48 176
582 8
1 374
5 171
14 392
15 209
17 404
25 494
28 55
29 13
``````
AC output:

Code: Select all

``````570
394
1886
220
948
630
326
828
214
200
``````
Check input and AC output for thousands of problems on uDebug!

Repon kumar Roy
Learning poster
Posts: 96
Joined: Tue Apr 23, 2013 12:54 pm

### Re: 12608 - Garbage Collection

Why WA ???

Code: Select all

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

/*------- Constants---- */
#define LMT				10005
#define ll				long long
#define ull				unsigned long long
#define mod				1000000007
#define MEMSET_INF		63
#define MEM_VAL			1061109567
#define FOR(i,n)			for( int i=0 ; i < n ; i++ )
#define mp(i,j)			make_pair(i,j)
#define lop(i,a,b)		for( int i = (a) ; i < (b) ; i++)
#define pb(a)			push_back((a))
#define gc				getchar_unlocked
#define PI				acos(-1.0)
#define inf				1<<30
#define lc				((n)<<1)
#define rc				((n)<<1 |1)
#define debug(x)              cout <<#x <<" -> " << x << endl;
#define EPS				1e-7
#define fred()                 freopen("in.txt","r",stdin);
#define fwrt()               freopen("in.txt","w",stdout);
/*---- short Cuts ------- */
#define ms(ara_name,value) memset(ara_name,value,sizeof(ara_name))
typedef pair<int, int> ii;
typedef vector<int > vi ;
/*------ template functions ------ */
inline void sc(int &x)
{
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 | c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}

template <class T> inline T bigmod(T p,T e,T M){
ll ret = 1;
for(; e > 0; e >>= 1){
if(e & 1) ret = (ret * p) % M;
p = (p * p) % M;
} return (T)ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

/*************************** END OF TEMPLATE ****************************/

int dis[LMT] , wei[LMT];

int main()
{

int tc, n , w ;
//fred();
sc(tc);
while( tc -- ) {
sc(w) ;sc(n);

for(int i =0 ; i < n; i ++ ){
sc(dis[i] );
sc(wei[i]);
}

int st = 0;
ll ans = 0;
while( st < n ) {
int sum  = 0;
int i , hat = 0;
for( i = st ; i < n  ; i ++) {
sum += wei[i];
hat = max( hat , dis[i]);
if( sum >= w ) break;
}

if( sum == w) st = i + 1;
else st = i ;
ans += 2 * hat;
}

printf("%lld\n"  , ans);

}
return 0;
}
/*
3
2 2
1 1
2 2
6 3
1 1
2 2
3 3
3 3
1 2
2 2
3 1
*/
``````