You are not logged in. Please login at to post your questions!


[closed] ZCO 14 SUPW

Link- SUPW

This is my code for SUPW in python-

n = int(input())
c = list(map(int,input().strip().split()))  
if n <= 3:
    cost = [c[0],c[1],c[2]]
    for i in range(3,n):
        cost.append(c[i] + min(cost[i-3:i]))

This is giving accepted for all test cases except for test case 2 in which it is giving Runtime Error. Can someone tell me the reason or provide a test case where it fails.

So what I have done is that-

Cost[i] = min time if we work that day.

So cost[0] = c[0],
   cost[1] = c[1],
   cost[2] = c[2]
And for the rest it is c[i] + min of the previous 3 elements.
And the answer is the min of the last 3 elements.

asked 02 Nov '16, 15:51

mathecodician's gravatar image

accept rate: 8%

closed 18 Mar, 19:55

The question has been closed for the following reason "The question is answered, right answer was accepted" by mathecodician 18 Mar, 19:55

Dont have any debugging experience in python , but I got AC with the same logic but I used C++ , here is my code,

#include <iostream>
#include <vector>
#include <algorithm>

int main(){
    int n;
    std::cin >> n;
    for(int i=0;i<n;i++){
        std::cin >> dutyTime[i];
    bestTime[0] = dutyTime[0];
    bestTime[1] = dutyTime[1];
    bestTime[2] = dutyTime[2];
    for(int i=3;i<n;i++){
        bestTime[i] = dutyTime[i]+std::min(bestTime[i-1],std::min(bestTime[i-2],bestTime[i-3]));

    std::cout << std::min(bestTime[n-1],std::min(bestTime[n-2],bestTime[n-3])) << std::endl;

    return 0;

Probably I would use c = raw_input(); timeList = c.split(" "); ##then a for loop for converting it to int list rather than list(map(int,input().strip().split())) but thats all upto you , i prefer the previous one for its readability and easy to debug'ness'.


answered 14 Nov '16, 11:31

mz54's gravatar image

accept rate: 30%

I tried changing list(map(.....)) but it didn't work out.

(14 Nov '16, 16:30) mathecodician5★

Maybe I should try it in c++ after learning c++ a bit more.

(14 Nov '16, 16:30) mathecodician5★

give it a try , I also started with python but now after 4 months with c++ , I am in love with it, because of its easiness to debug

(15 Nov '16, 08:37) mz542★

OK, so I finally found the bug. I submitted a solution which was the following-

Take the input. That's it.

It was still showing error on test-case 2.

Then I submitted a solution in which it said that if there's a character which is non numeric and it is not a space then it raises an error and it did on test case 2.

So finally I submitted a solution in which it took input in such a way that it did not include alphabetical characters and I got AC.

Thanks by the way mz54. I don't have enough karma to upvote else I would have upvoted your help. Here is my solution-

n = int(input().strip())
c = input().strip().split()
j = 0
while j < n:
        c[j] = int(c[j])
        j += 1
n = len(c)
if n <= 3:
    for i in range(3,n):
        c[i] += min(c[i-3:i])

My solution


answered 14 Nov '16, 19:47

mathecodician's gravatar image

accept rate: 8%

It took about 30-35 submissions.

(14 Nov '16, 20:11) mathecodician5★

The same thing happened in the problem Wormholes in which the problem was that it said that the next n lines will give the time of the contest but in 2 test cases there were not n lines but less then that so I was taking extra input and again I had to add a try-except block.

(16 Nov '16, 15:02) mathecodician5★

Thanks a lot @mathecodician. I had been trying to solve that SUPW problem for months and everytime it gave me error on the second test. Your suggestion to take input in such a way so that it does not include characters other than numbers helped a lot. It finally worked after 30-40 tries!! Thanks a lot! :)

(19 May, 20:07) akashbhalotia2★

For Java users, just do the conversion from String to integer inside the try block and also make a catch block to catch the exception. Like this:-

  N=Integer.parseInt(br.readLine()); //array length

  String s[]=br.readLine().trim().split(" "); //accepting the time for each SUPW work
  int a[]=new int[N];

  for(i=0;i<N;i++) //converting from String to integer
    int ch=0;
    catch(Exception e)

This will work :)


answered 19 May, 20:10

akashbhalotia's gravatar image

accept rate: 0%

edited 19 May, 20:12

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol



Asked: 02 Nov '16, 15:51

Seen: 667 times

Last updated: 19 May, 20:12