I'm trying to solve fractional knapsack problem, for now logic aside can anyone suggest me or explain me what is happening here? I never seen this runtime-error before, any help would be awesome, thank you.
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
bool myfunction(int a, int b){return(a>b);}
double get_optimal_value(int capacity, vector<int> weights, vector<int> values) {
double value = 0.0;
vector<double> prop(weights.size());
for(int i=0;i<=weights.size();i++){
double Pvalue = (weights.at(i))/(values.at(i));
prop.push_back(Pvalue);
}
std::sort(prop.begin(),prop.end(),myfunction);
for(int it =0;it<=values.size();it++){
while(capacity >=prop.at(it)){
value+=prop.at(it);
capacity-=prop.at(it);
}
}
return value;
}
int main() {
int n;
int capacity;
std::cin >> n >> capacity;
vector<int> values(n);
vector<int> weights(n);
for (int i = 0; i < n; i++) {
std::cin >> values[i] >> weights[i];
}
double optimal_value = get_optimal_value(capacity, weights, values);
std::cout.precision(10);
std::cout << optimal_value << std::endl;
return 0;
}