Min cost, traveling group.

Hello, I'm trying to find the minimum cost of a travelling group.
N students are put in a queue in front of a lift Each seat of the lift could be used by a single student and then they pay a sum equal of the person weight, or could be used by two neighbour students from the queue, if the sum of their wieghts is no more than W. The students pay the average of their weights.

The problem is i get some obscure high number, instead of what im supposed to get,

I will add an example output and input

Example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define MAXN 1000000
#define MAXW 120

int N, W;
int t[MAXN],M[MAXN];

int opt()
{
    M[1] = t[1];
    M[2] = t[1] + t[2];
   
    if(M[2] > W) M[2] /= 2;
    for(int i = 3;i <= N; i++)
    {
        M[i]=t[i]+M[i-1];
        if(M[i + 1]>t[i-1]+M[i-1])
           {
              M[i]=t[i - 1]+M[i - 2];
            }
    }
  
    return M[N];
}


int main() {

int i,j;
    scanf("%d %d", &N, &W);
    for(i = 0; i < N ;i++)
    {
      
        scanf("%d", &t[i]);
        
    }
    printf("%d",opt() );
    
    
    return 0;
}


Exmaple Input :
1
2
6 100
40 50 50 60 30 70


Example Output :
 
200


What i get with my code is :
 
260


I'm not sure what mistake im having here.
Your opt() seems to be using 1-based arrays, and your main starts (correctly) at 0.
Topic archived. No new replies allowed.