Skip to main content
1 of 5

"Food Ordering" program exceeds time limit in test 9

So, I've recently started working on CodeForces to practice from older contests (and maybe even start participating in current ones), but there is one problem that I can't seem to make an efficient enough program to pass all tests. Here is my code:

#include <bits/stdc++.h>
using namespace std;
struct food
{
    long long stock, cost;
} v[100001];
int main()
{
    struct order
    {
        long type;
        long long dishes;
    } cust; ///cust = customer
    struct minim
    {
        long pos=0;
        long value=2147483647;
    } mn;
    long long i,n,m,cost,d,j;
    cin >> n >> m;
    for (i=1; i<=n; i++)
    {
        cin >> v[i].stock;
    }
    for (i=1; i<=n; i++)
    {
        cin >> v[i].cost;
        if (v[i].cost < mn.value && v[i].stock)
        {
            mn.value = v[i].cost;
            mn.pos = i;
        }
    }
    for (i=1; i<=m; i++)
    {
        cost = 0;
        cin >> cust.type >> cust.dishes;
        d=v[cust.type].stock;
        v[cust.type].stock -= cust.dishes;
        if (v[cust.type].stock >= 0)
            cost = v[cust.type].cost * cust.dishes;
        else
        {
            cost = v[cust.type].cost * (cust.dishes + v[cust.type].stock);
            v[cust.type].stock = 0;
            cust.dishes -= d;
            while (cust.dishes)
            {
                if (cust.dishes > v[mn.pos].stock)
                {
                    cust.dishes = cust.dishes - v[mn.pos].stock;
                    cost += v[mn.pos].stock * v[mn.pos].cost;
                    v[mn.pos].stock = 0;
                    for (j=1; j<=n; j++)
                    {
                        if (v[j].cost <= mn.value && v[j].stock)
                        {
                            mn.value = v[j].cost;
                            mn.pos = j;
                            break;
                        }
                    }
                    if (v[mn.pos].stock == 0)
                    {
                        mn.pos=0;
                        mn.value = 2147483647;
                        for (j=1; j<=n; j++)
                        {
                            if (v[j].cost <= mn.value && v[j].stock)
                            {
                                mn.value = v[j].cost;
                                mn.pos = j;
                            }
                        }
                    }
                    if (mn.value == 2147483647 && cust.dishes)
                    {
                        cost = 0;
                        cust.dishes = 0;
                    }
                }
                else
                {
                    cost += v[mn.pos].cost * cust.dishes;
                    v[mn.pos].stock -= cust.dishes;
                    cust.dishes = 0;
                }
            }
        }
        cout << cost << "\n";
    }
}

I know I haven't written my code to look its best, but it'll do, I suppose. The requirement is here: https://codeforces.com/contest/1106/problem/B

From the time the evaluator is spending to evaluate the time-limit-exceeded test, I'm thinking my program gets stuck somewhere. Hoping to get some advice from you guys!