title: Easy online judge task, but struggling to find my implementation error
body: <p>I was solving a problem from an online judge. The problem goes like this:</p>
  
  <blockquote>
    <p>There are <code>n</code> people. They are going to participate in a race. They have to run <code>s</code> meters. </p>
    
    <p>Each player <code>i</code> has its original speed <code>v_i</code>(m/s). Each player runs at speed <code>v_i</code> until he runs 80% of the race (<code>0.8s</code> meters), and when only 20% (<code>0.2s</code> meters) left, the speed changes to <code>v_i + 10</code>(m/s). (Surprising!)</p>
    
    <p>The organizers want to know the sum of the distance that each player has run until the top 3 players reached the finish line. You should round down the answer to the nearest integer.</p>
  </blockquote>
  
  <p>My solution goes like this:</p>
  
  <ol>
  <li>Find the third biggest speed <code>v</code>.</li>
  <li>The time when the top 3 is decided is <code>0.8s / v + 0.2s / (v + 10)</code>. Let this be <code>t</code>.</li>
  <li>Make an <code>answer</code> variable and let it be 0.</li>
  <li>For each <code>v_i</code> smaller than <code>v</code>:
  
  <ol>
  <li>If <code>t</code>, this player would have run <code>t * v</code> kilometers until time <code>t</code>. So add <code>t * v</code> to <code>answer</code>.</li>
  <li>Otherwise, this player would have run <code>0.8s + (0.8s / v - t) * (v + 5)</code> kilometers until time <code>t</code>. So add <code>0.8s + (0.8s / v - t) * (v + 5)</code> to <code>answer</code>.</li>
  </ol></li>
  <li>For each <code>v_i</code> not smaller than <code>v</code> (<code>v_i &gt;= v</code>): Add <code>d</code> to <code>answer</code>, because they finished the race.</li>
  <li>Print <code>floor(answer)</code>.</li>
  </ol>
  
  <p>But it gets wrong on certain cases. At first glance I thought it was because of precision error, so I used <code>BigDecimal</code> of Java instead, but it still gets WA verdict. Is there any error in the logic above, or is it my implementation error?</p>
  
  <p>Here is my code in C++:</p>
  
  <pre><code>#include&lt;cstdio&gt;
  int main()
  {
      int s,n;scanf("%d%d",&amp;s,&amp;n);
      int v[1000],i,j;
      for(i=0;i&lt;n;i++)scanf("%d",&amp;v[i]);
      for(i=0;i&lt;n;i++)
      {
          for(j=i+1;j&lt;n;j++)
          {
              if(v[i]&gt;v[j])
              {
                  int temp=v[i];
                  v[i]=v[j];
                  v[j]=temp;
              }
          }
      }
      int vv=v[n-3];
      double t=0.8*s/vv+0.2*s/(vv+10);
      double answer=0;
      for(i=0;i&lt;n;i++)
      {
          if(v[i]&lt;vv)
          {
              if(t*v[i]&lt;=0.8*s)
              {
                  answer+=t*v[i];
              }
              else
              {
                  answer+=0.8*s+(t-0.8*s/v[i])*(v[i]+10);
              }
          }
          else
          {
              answer+=s;
          }
      }
      printf("%d\n",(int)answer);
  }
  </code></pre>
  
  <p>Thanks in advance :D</p>
  
  <p>Example:</p>
  
  <p>input:</p>
  
  <pre><code>60 8
  1 5 4 9 3 8 5 2
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>344
  </code></pre>